Skip to content

Improve test coverage for SqlServer project#5335

Merged
rbans96 merged 18 commits intomainfrom
personal/ribans/test-coverage-sqlServer
Feb 3, 2026
Merged

Improve test coverage for SqlServer project#5335
rbans96 merged 18 commits intomainfrom
personal/ribans/test-coverage-sqlServer

Conversation

@rbans96
Copy link
Contributor

@rbans96 rbans96 commented Jan 14, 2026

Description

Improve test coverage for SqlServer project.

Related issues

Addresses [174927].

Testing

Describe how this change was tested.

FHIR Team Checklist

  • Update the title of the PR to be succinct and less than 65 characters
  • Add a milestone to the PR for the sprint that it is merged (i.e. add S47)
  • Tag the PR with the type of update: Bug, Build, Dependencies, Enhancement, New-Feature or Documentation
  • Tag the PR with Open source, Azure API for FHIR (CosmosDB or common code) or Azure Healthcare APIs (SQL or common code) to specify where this change is intended to be released.
  • Tag the PR with Schema Version backward compatible or Schema Version backward incompatible or Schema Version unchanged if this adds or updates Sql script which is/is not backward compatible with the code.
  • When changing or adding behavior, if your code modifies the system design or changes design assumptions, please create and include an ADR.
  • CI is green before merge Build Status
  • Review squash-merge requirements

Semver Change (docs)

Patch|Skip|Feature|Breaking (reason)

@rbans96 rbans96 requested a review from a team as a code owner January 14, 2026 19:47
Copy link
Contributor

@github-advanced-security github-advanced-security bot left a comment

Choose a reason for hiding this comment

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

CodeQL found more than 20 potential problems in the proposed changes. Check the Files changed tab for more details.

@rbans96 rbans96 changed the title [Draft] Improve test coverage for SqlServer project Improve test coverage for SqlServer project Jan 23, 2026
@rbans96 rbans96 added Enhancement-Test Enhancement on tests. No-ADR ADR not needed labels Jan 23, 2026
@rbans96 rbans96 added this to the CY25Q3/2Wk07 milestone Jan 23, 2026
@rbans96 rbans96 added Azure Healthcare APIs Label denotes that the issue or PR is relevant to the FHIR service in the Azure Healthcare APIs No-PaaS-breaking-change labels Jan 23, 2026
Copy link
Contributor

@mikaelweave mikaelweave left a comment

Choose a reason for hiding this comment

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

Added a suggestion to strengthen the SQL assertion for reference token composite generator.

Copy link
Contributor

@mikaelweave mikaelweave left a comment

Choose a reason for hiding this comment

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

Added suggestions to strengthen SQL assertions for remaining query generator tests.

Copy link
Contributor

@mikaelweave mikaelweave left a comment

Choose a reason for hiding this comment

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

Added comments for remaining generator/unit/integration test assertion improvements per pr_review_5335 analyses.

- Combine assertions in ReferenceQueryGeneratorTests to verify specific values (ReferenceResourceTypeId = {value})
- Update BaseUri null check to assert specific column name (BaseUri IS NULL)
- Strengthen ReferenceTokenCompositeQueryGeneratorTests assertions:
  * Use regex patterns to validate component-indexed columns (ReferenceResourceId1, Code2)
  * Verify specific NULL checks for component columns (BaseUri1 IS NULL, SystemId2 IS NULL)
- Add exact count assertion for GetSurrogateIdRanges test (Assert.Equal(5, ranges.Count()))
- Remove redundant range count check

All tests passing (36/36 tests)
@rbans96
Copy link
Contributor Author

rbans96 commented Jan 29, 2026

Addressed review comments from @mikaelweave in commit b80537e:

✅ ReferenceQueryGeneratorTests.cs:

  • Combined assertions to verify ReferenceResourceTypeId = {resourceTypeId} (specific value check)
  • Updated to assert BaseUri IS NULL specifically (not just generic IS NULL)

✅ ReferenceTokenCompositeQueryGeneratorTests.cs:

  • Strengthened assertions with regex patterns to verify component-indexed columns
    • ReferenceResourceId1\s*=\s*@\w+ (validates component 1 column)
    • Code2\s*=\s*@\w+ (validates component 2 column instead of generic "Code")
  • Added specific NULL checks for component columns:
    • BaseUri1 IS NULL (component 1)
    • SystemId2 IS NULL (component 2)

✅ SqlServerSearchServiceIntegrationTests.cs:

  • Added Assert.Equal(5, ranges.Count()) to verify exact count of ranges returned
  • Removed redundant assertion

All tests passing (36/36 tests).

- Remove unused patient variable in GetStatsFromCache_AfterSearch_ReturnsPopulatedCollection test
- The variable was assigned but never read, causing a code scanning alert

Addresses: #5335 (comment)
- ResourceIdParameterQueryGeneratorTests: Added regex assertions for ResourceId = and LIKE patterns
- ResourceSurrogateIdParameterQueryGeneratorTests: Added regex assertions for ResourceSurrogateId with =, >, < operators
- TokenQueryGeneratorTests: Strengthened SystemId, Code assertions with regex patterns
- TokenTextQueryGeneratorTests: Strengthened Text column assertions with = and LIKE patterns
- TokenTokenCompositeQueryGeneratorTests: Added Code1, Code2 column verifications
- TokenNumberNumberQueryGeneratorTests: Added SingleValue2 null-guard and component assertions
- TokenDateTimeCompositeQueryGeneratorTests: Added Code1, StartDateTime2 column verifications
- CompartmentQueryGeneratorTests: Added regex assertions for CompartmentTypeId and ReferenceResourceId
- DateTimeQueryGeneratorTests: Added regex assertions for StartDateTime, EndDateTime, IsLongerThanADay
- NumberQueryGeneratorTests: Added regex assertions for SingleValue, LowValue, HighValue columns
- StringQueryGeneratorTests: Strengthened Text column assertions for =, LIKE patterns
- UriQueryGeneratorTests: Added regex assertion for Uri column
- QuantityQueryGeneratorTests: Strengthened SingleValue, LowValue, HighValue, QuantityCodeId, SystemId assertions
- PrimaryKeyRangeParameterQueryGeneratorTests: Added regex assertions for ResourceTypeId (literal) and ResourceSurrogateId (param)
- DateTimeBoundedRangeRewriterTests: Added System.Linq using and refactored to use Select()
- SqlServerSearchParameterStatusDataStoreTests: Replaced Assert.True(true) with Assert.Null(Record.Exception())
- CompartmentQueryGeneratorTests: Verify special characters are parameterized (not raw in SQL)
- PrimaryKeyRangeParameterQueryGeneratorTests: Verify full predicate shape and IN clause contains expected ids
- TokenTokenCompositeQueryGeneratorTests: Strengthen assertion to verify Code1 column with component suffix
Copy link
Contributor

@mikaelweave mikaelweave left a comment

Choose a reason for hiding this comment

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

Added targeted recommendations for each reported finding (core/complex/integration) with concrete assertion alternatives tied to the implementation.

var sql = context.StringBuilder.ToString();

Assert.NotEmpty(sql);
Assert.True(context.Parameters.HasParametersToHash);
Copy link
Contributor

Choose a reason for hiding this comment

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

Finding: UTC conversion not validated. Recommendation: assert the parameter equals localDateTime.UtcDateTime (or UtcDateTime), and that SQL targets StartDateTime with the correct operator.


var sql = context.StringBuilder.ToString();

Assert.Matches($@"{VLatest.StringSearchParam.Text.Metadata.Name}\s+LIKE\s+@\w+", sql);
Copy link
Contributor

Choose a reason for hiding this comment

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

Finding: LIKE check is too generic. Recommendation: assert the parameter value is %value% for Contains (or value% for StartsWith), and include collation if applicable.

@rbans96
Copy link
Contributor Author

rbans96 commented Feb 2, 2026

Response to Unresolved Review Comments

I've analyzed all unresolved, non-outdated review comments and here's the status:


✅ Already Addressed

SearchParameterStatusCollectionTests.cs (line 255) - "Useless upcast"

This is a false positive from the code scanner. The cast (IEnumerable<SqlDataRecord>)collection is required because SearchParameterStatusCollection implements two interfaces:

  • IEnumerable<ResourceSearchParameterStatus> (default iterator)
  • IEnumerable<SqlDataRecord> (explicit implementation)

Without the explicit cast, C# uses the default iterator which returns ResourceSearchParameterStatus objects, not SqlDataRecord. Removing the cast causes compilation error CS0266.

TokenTokenCompositeQueryGeneratorTests.cs (line 170) - Code1/Code2 assertions

✅ Fixed - Now uses Assert.Matches(@"Code1\s*=\s*@\w+", sqlAfterBoth) and Assert.Matches(@"Code2\s*=\s*@\w+", sqlAfterBoth) to verify both component columns appear distinctly.

PrimaryKeyRangeParameterQueryGeneratorTests.cs (line 196) - IN clause assertions

✅ Fixed - Replaced comma counting with explicit verification:

for (int i = 0; i < 10; i++)
{
    Assert.Matches($@"\b{i}\b", sql);
}

PrimaryKeyRangeParameterQueryGeneratorTests.cs (line 89) - Predicate shape

✅ Already correct - Verifies full predicate structure with:

  • Assert.Matches(@"ResourceTypeId\s*=\s*1", sql)
  • Assert.Matches(@"ResourceSurrogateId\s*>\s*@\w+", sql)
  • Assert.Contains("OR", sql)
  • Assert.Matches(@"ResourceTypeId\s+IN\s*\(", sql)

CompartmentQueryGeneratorTests.cs (line 76) - Predicate shape

✅ Already correct - Uses proper regex assertions:

Assert.Matches($@"{VLatest.CompartmentAssignment.CompartmentTypeId.Metadata.Name}\s*=\s*@\w+", sql);
Assert.Matches($@"{VLatest.CompartmentAssignment.ReferenceResourceId.Metadata.Name}\s*=\s*@\w+", sql);

SqlServerSearchParameterStatusDataStoreTests.cs (line 170) - Assert.True(true) bypass

✅ This pattern no longer exists in the code. The test now uses Assert.Null(exception) to verify the method completes without throwing.

ExceptionExtensionTests.cs (line 20) - Missing test patterns

✅ Fixed - Added 15+ new test cases covering all retriable patterns including:

  • Network errors (socket, connection closed, severe error, etc.)
  • Database availability (login failed, emergency mode, size quota, etc.)
  • Incorrect async call pattern
  • Remote procedure call errors

📋 Requires Infrastructure Changes (Not Addressed)

The following comments request parameter VALUE validation. This would require significant infrastructure refactoring because the SqlCommand is disposed when CreateContext() returns, making parameter values inaccessible for assertion.

  • DateTimeQueryGeneratorTests (lines 76, 98, 116, 187, 188, 216)
  • NumberQueryGeneratorTests (lines 173, 192, 208, 223)
  • QuantityQueryGeneratorTests (lines 147, 192, 305)
  • StringQueryGeneratorTests (lines 74, 102)

These would need a test infrastructure change to preserve parameter values, which is beyond the scope of this PR focused on test coverage improvements.


Test Results

All modified tests pass:

Passed! - Failed: 0, Passed: 106, Skipped: 0, Total: 106 (net8.0)
Passed! - Failed: 0, Passed: 106, Skipped: 0, Total: 106 (net9.0)

@rbans96 rbans96 merged commit 898560b into main Feb 3, 2026
61 of 64 checks passed
@rbans96 rbans96 deleted the personal/ribans/test-coverage-sqlServer branch February 3, 2026 18:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Azure Healthcare APIs Label denotes that the issue or PR is relevant to the FHIR service in the Azure Healthcare APIs Enhancement-Test Enhancement on tests. No-ADR ADR not needed No-PaaS-breaking-change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants