Skip to content

tests(stat-model): Add unit tests to stat model server#118

Merged
tonywu1999 merged 12 commits intodevelfrom
refactor-stat-model
Nov 2, 2025
Merged

tests(stat-model): Add unit tests to stat model server#118
tonywu1999 merged 12 commits intodevelfrom
refactor-stat-model

Conversation

@tonywu1999
Copy link
Copy Markdown
Contributor

@tonywu1999 tonywu1999 commented Nov 2, 2025

Note: When looking at diff on the Github PR, hide the whitespace.

Summary by CodeRabbit

  • Tests

    • Added comprehensive test coverage for the statistical model module, exercising initialization, group choices, contrast-matrix construction, validation and edge cases.
  • Chores

    • Reworked server-side module to use the Shiny module server pattern and updated server declaration style to improve scoping and testability.

Note: No user-facing behavior changes.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Nov 2, 2025

Note

Currently processing new changes in this PR. This may take a few minutes, please wait...

📥 Commits

Reviewing files that changed from the base of the PR and between 6bbff67 and ea0e235.

📒 Files selected for processing (1)
  • R/server.R (2 hunks)
 _______________________________
< Grow a pair... of test cases. >
 -------------------------------
  \
   \   (\__/)
       (•ㅅ•)
       /   づ

Tip

CodeRabbit can generate a title for your PR based on the changes with custom instructions.

Add the reviews.auto_title_instructions setting in your project's settings in CodeRabbit to generate a title for your PR based on the changes in the PR with custom instructions.

Walkthrough

The changes migrate the statmodelServer Shiny module from a direct function pattern to the moduleServer architecture with an updated function signature (id-based scoping), standardize assignment operators in the server file, and introduce comprehensive test coverage for the statmodelServer module with mock data and multiple configuration scenarios.

Changes

Cohort / File(s) Summary
Shiny Module Refactoring
R/module-statmodel-server.R
Migrated from direct Shiny module pattern to moduleServer-based architecture; updated function signature from statmodelServer(input, output, session, ...) to statmodelServer(id, parent_session, loadpage_input, qc_input, get_data, preprocess_data); encapsulated all UI/output wiring, event handlers, and observers within moduleServer wrapper for explicit namespace scoping.
Server Syntax Standardization
R/server.R
Changed assignment operators: server function declaration from <- to =; standardized variable assignments (isWebServer, maxRequestSize, etc.) to =; updated inline comments reflecting partial refactoring status of module invocations.
Module Test Suite Addition
tests/testthat/test-module-statmodel-server.R
Added comprehensive test suite with mock data generators (create_mock_data, create_mock_raw_data) and testServer cases covering initialization, group choices, matrix construction (all_pair, all_one, custom_np, custom), validation checks, matrix reset behavior, and edge cases across DDA, TMT, and PTM configurations.

Sequence Diagram

sequenceDiagram
    participant Caller as Caller/Parent Module
    participant Module as statmodelServer Module
    participant Session as Shiny Session

    rect rgb(240, 248, 255)
    note over Caller,Module: Old Pattern: Direct Function Call
    Caller->>Module: statmodelServer(input, output, session, ...)
    Module->>Module: Execute module logic<br/>(direct closure)
    Module-->>Caller: Return reactive outputs
    end

    rect rgb(255, 240, 245)
    note over Caller,Module: New Pattern: moduleServer Architecture
    Caller->>Module: statmodelServer(id, parent_session, ...)
    Module->>Session: moduleServer(id, function(input,<br/>output, session) { ... })
    Session->>Session: Create namespaced session<br/>(ns <- session$ns)
    Module->>Module: Execute module logic<br/>(within module scope)
    Module-->>Session: Reactive outputs<br/>(namespaced)
    Session-->>Caller: Return reactive outputs<br/>(properly scoped)
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Areas requiring extra attention:

  • R/module-statmodel-server.R: Verify all UI element references correctly use ns() for namespacing; confirm event handlers (observeEvent, reactive expressions) properly access inputs/outputs within module scope; validate closure references (parent_session, loadpage_input, qc_input, get_data, preprocess_data) work as intended in the new architecture.
  • R/server.R: Cross-check that module invocation patterns align with the refactored statmodelServer signature, particularly how id and parent_session are passed; review comment annotations for incomplete refactorings.
  • tests/testthat/test-module-statmodel-server.R: Verify test assumptions about reactive input/output behavior align with actual moduleServer implementation; check mock data generators produce valid, representative scenarios for all three data types (DDA, TMT, PTM).

Suggested labels

Review effort 3/5

Poem

🐰 Modules now dance in proper scope,
With moduleServer and ID-based hope,
Each namespace clean, each test complete—
From direct calls to architecturally neat!
✨ The refactor hops on forward!

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The PR title "tests(stat-model): Add unit tests to stat model server" accurately describes the addition of a comprehensive test suite for the statmodelServer module, which is a substantial deliverable in this changeset. While the PR also includes significant refactoring of the module structure from a direct function pattern to a moduleServer-based pattern (evident in the changed method signature and updated scoping), the title focuses specifically on the testing aspect. The title is clear, concise, and related to a real and important aspect of the changes, even if it doesn't capture the entire scope of the refactoring work also present in this PR.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Nov 2, 2025

Failed to generate code suggestions for PR

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
R/server.R (1)

13-15: Consider R community conventions for assignment operators.

The changes replace <- with = for assignments. While both are valid in R, the <- operator is strongly preferred by the R community (tidyverse style guide, Google R style guide) for variable assignments. Using = is typically reserved for function arguments.

Consider maintaining consistency with R community conventions:

-server = function(input, output, session) {
-  isWebServer = Sys.getenv("SHINY_ENV", "development") == "production"
-  maxRequestSize = if (isWebServer) 250*1024^2 else 1000000*1024^2
+server <- function(input, output, session) {
+  isWebServer <- Sys.getenv("SHINY_ENV", "development") == "production"
+  maxRequestSize <- if (isWebServer) 250*1024^2 else 1000000*1024^2
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f68b896 and 6bbff67.

📒 Files selected for processing (3)
  • R/module-statmodel-server.R (1 hunks)
  • R/server.R (2 hunks)
  • tests/testthat/test-module-statmodel-server.R (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: build
🔇 Additional comments (6)
tests/testthat/test-module-statmodel-server.R (2)

39-632: Excellent test coverage for the statmodelServer module.

The test suite provides comprehensive coverage of the core module functionality:

  • Initialization and reactive setup
  • Group choices extraction across DDA/TMT/PTM configurations
  • All matrix building scenarios (custom, all_pair, all_one, custom_np)
  • Validation logic (identical group checks, zero-sum weight constraints)
  • State management and reset behavior
  • Edge cases (duplicates, empty comparisons)

The tests properly use testServer with MockShinySession and are well-isolated.


30-36: Remove the unused create_mock_raw_data() helper function.

This function is defined at lines 30-36 but is never called anywhere in the test suite or codebase. It should be removed to keep the test file clean.

R/server.R (1)

45-53: Correct migration to direct moduleServer invocation.

The change from callModule(statmodelServer, ...) to direct invocation statmodelServer(id = "statmodel", ...) is correct for modules that now use moduleServer() internally. The id-based scoping aligns with the refactored module implementation.

R/module-statmodel-server.R (3)

21-24: Correct migration to the moduleServer architecture.

The function signature change and moduleServer() wrapper follow the proper Shiny module pattern. The id parameter enables namespace scoping, and the inner function receives input, output, and session from the module context.


57-113: Proper namespace usage for module UI elements.

The consistent pattern of ns = session$ns followed by wrapping IDs with ns() ensures proper namespace scoping for the module. This prevents ID conflicts and enables multiple instantiations of the module.


912-920: Return value structure correctly preserved.

The module returns a list with input and dataComparison as expected by the calling code in server.R. The structure is preserved from the original implementation.

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