Skip to content

Add tests for untested code paths and model invariants - #60

Draft
alexpghayes wants to merge 7 commits into
mainfrom
test/coverage-gaps
Draft

alexpghayes wants to merge 7 commits into
mainfrom
test/coverage-gaps

Conversation

@alexpghayes

Copy link
Copy Markdown
Collaborator

Adds tests only. No changes to R/, NEWS.md or vignettes/.

Coverage

Overall line coverage (covr::package_coverage()) goes from 74.76% to 92.58%.

File Before After
R/plotting.R 7.1% 100.0%
R/undirected_chung_lu.R 35.1% 97.3%
R/undirected_sbm.R 45.2% 85.7%
R/undirected_mmsbm.R 50.0% 89.0%
R/undirected_factor_model.R 51.1% 86.4%
R/undirected_dcsbm.R 54.0% 86.9%
R/directed_factor_model.R 55.9% 98.9%
R/undirected_overlapping_sbm.R 78.0% 97.0%
R/undirected_planted_partition.R 81.7% 91.7%
R/directed_dcsbm.R 82.9% 90.1%
R/directed_planted_partition.R 88.8% 89.5%
R/directed_erdos_renyi.R 91.7% 91.7%
R/undirected_erdos_renyi.R 92.9% 96.4%
R/utils.R 97.1% 97.1%
R/expected-*.R, R/sample_*.R 100% 100%

Most of the lines still uncovered are validator branches that the public constructors can't reach, because an earlier check always fails first.

What is now covered

  • Model consistency (test-model-consistency.R): after construction, z, X, theta, pi and S agree with each other and with the arguments. This is checked for dcsbm(), sbm(), planted_partition(), chung_lu(), mmsbm(), overlapping_sbm() and directed_dcsbm(), across these combinations:

    • sort_nodes, force_identifiability, poisson_edges and allow_self_loops;
    • shared_blocks and shared_theta;
    • k = 1 and k > 1, with n not divisible by k;
    • block sizes with zeros and pi with zeros;
    • square and rectangular B.

    The file also covers argument validation and the random theta / B messages.

  • Print methods (test-print-methods.R): every print method runs. The expected edges, degrees, density, dimensions and sorted / unsorted status it prints match the model.

  • Sampler agreement (test-sampler-agreement.R):

    • For a common seed, sample_edgelist(), sample_sparse(), sample_igraph() and sample_tidygraph() agree on edge counts, degrees, vertex counts and multigraph = FALSE weights. This covers undirected, directed square and directed rectangular models.
    • Undirected edgelists have from <= to.
    • With allow_self_loops = FALSE there are no self-loops, and with poisson_edges = FALSE there are no multi-edges.
    • Samplers and constructors are reproducible for a fixed seed.
  • E[A] = expectation(model) (test-expectation-monte-carlo.R):

    • Entrywise Pearson chi-squared tests compare the totals of sampled adjacency matrices to their exact Poisson or Binomial distributions: Poisson(reps P) for Poisson edges, Binomial(reps, 1 - exp(-P)) for Bernoulli edges. These cover undirected and directed models, with and without self-loops, and several constructors.
    • expected_degrees() and directed in- and out-degrees are checked node by node, with exact Poisson z-scores and a Bonferroni bound.
    • Bernoulli graphs near density 1 get an exact Binomial check.
  • Spectra and Matrix classes (test-spectra-and-matrix-classes.R):

    • eigs_sym() and svds() agree with dense eigen() and svd() of expectation(), including an indefinite mixing matrix and rectangular directed models.
    • dgeMatrix, dgCMatrix, dsyMatrix and ddiMatrix inputs give the same model and the same samples as base matrices.
    • A non-symmetric S is symmetrized.
  • Scaling (test-scaling-targets.R):

    • expected_degree and expected_density hit their targets for every undirected constructor.
    • expected_in_degree, expected_out_degree and expected_density hit their targets for every directed constructor, square and rectangular, including shared blocks.
    • Bernoulli blockmodels hit the target on the probability scale.
    • The scaling arguments are validated.
  • Plot helpers (test-plotting.R): plot_dense_matrix(), plot_expectation() and plot_sparse_matrix() were previously almost untested.

  • Boundary cases (test-edge-cases.R):

    • tiny graphs (n = 1, n = 2) and graphs with no edges, across every sampler;
    • empty and zero-probability blocks;
    • balanced default block sizes;
    • rank one models of every kind;
    • planted_partition() validation;
    • sample_edgelist() called directly on matrices.

Shared helpers are in tests/testthat/helper-invariants.R. Statistical checks use exact distributional bounds, following test-expected-edges-density.R. The Monte Carlo tests and the two largest combination loops are marked skip_on_cran(). testthat::test_local() passes in about 60s with NOT_CRAN=true and about 30s on CRAN.

🤖 Generated with Claude Code

alexpghayes and others added 7 commits November 17, 2026 15:04
Check that z, X, theta, pi and S agree after construction for dcsbm(),
sbm(), planted_partition(), chung_lu(), mmsbm(), overlapping_sbm() and
directed_dcsbm(), across sort_nodes, force_identifiability, poisson_edges,
allow_self_loops, shared_blocks / shared_theta, k = 1 and k > 1, block
sizes with zeros and pi with zeros. Also cover argument validation and
the random theta / B messages.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Every print method runs and reports expected edges, degrees and density
that match the model. All four samplers describe the same graph for a
common seed, across undirected, directed square and directed rectangular
models, with and without self-loops and Poisson edges. Also check seed
reproducibility of samplers and constructors.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Pearson chi-squared tests of the entrywise totals of sampled adjacency
matrices against exact Poisson / Binomial distributions, for undirected,
directed square and directed rectangular models, Poisson and Bernoulli
edges, with and without self-loops. Also check expected_degrees() and
directed in/out degrees node by node with exact Poisson z-scores, and the
density of very dense Bernoulli graphs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
eigs_sym() and svds() agree with eigen() and svd() of expectation() for
undirected models (including an indefinite mixing matrix) and for square
and rectangular directed models. Models built from dgeMatrix, dgCMatrix,
dsyMatrix and ddiMatrix inputs match those built from base matrices, and
non-symmetric S is symmetrized.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
expected_degree / expected_density for every undirected constructor, with
and without self-loops; expected_in_degree / expected_out_degree /
expected_density for every directed constructor, square and rectangular,
including shared blocks. Bernoulli blockmodels hit the target on the
probability scale. Also cover the scaling argument validation in
undirected_factor_model() and directed_factor_model().

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Cover plot_dense_matrix(), plot_expectation() and plot_sparse_matrix(),
which were almost entirely untested. Add tests for tiny graphs (n = 1,
2), models with no edges, empty and zero-probability blocks, balanced
default block sizes, rank one models of every kind, planted_partition()
validation and calling sample_edgelist() directly on matrices.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Skip the two largest combination loops on CRAN, pair allow_self_loops
with poisson_edges in the dcsbm() loop since it does not affect
construction, and cover the 'not arranged by block' print branches and
the k-and-B error in dcsbm().

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant