# tests/testthat/test_deltaGWD.R In gwdegree: A Shiny App to Aid Interpretation of Geometrically-Weighted Degree Estimates in Exponential Random Graph Models

```library(ergm)
context("Testing analytical delta-gwdegree solution")

test_that("Deleting an edge: deltaGWD() matches simulation-based calculation for a random dyad in random network with a random decay-parameter value in [0, 3].",
{
decay = runif(1, 0, 3)
repeat ({  # Make sure we have an edge that is present
n = network(sample(5:50, 1), density = runif(1, .01, .5), directed = FALSE)
nodesToAdd = sample(network.size(n), 2, replace = FALSE)
break
})
nodesDegrees = degree(n, nodes = nodesToAdd, gmode = "graph")
analyticalResult = -sum(gwdegree:::deltaGWD(nodesDegrees - 1, decay))

originalGWD = summary(n ~ gwdegree(decay, fixed = TRUE))
newGWD = summary(n ~ gwdegree(decay, fixed = TRUE))
simResult = unname(newGWD - originalGWD)

expect_equal(analyticalResult, simResult)
})

test_that("Adding an edge: deltaGWD() matches simulation-based calculation for a random dyad in random network with a random decay-parameter value in [0, 3].",
{
decay = runif(1, 0, 3)
repeat ({  # Make sure we do not have an edge that is present
n = network(sample(5:50, 1), density = runif(1, .01, .5), directed = FALSE)
nodesToAdd = sample(network.size(n), 2, replace = FALSE)
break
})
nodesDegrees = degree(n, nodes = nodesToAdd, gmode = "graph")
analyticalResult = sum(gwdegree:::deltaGWD(nodesDegrees, decay))

originalGWD = summary(n ~ gwdegree(decay, fixed = TRUE))