Skip to content

Conversation

@rbans96
Copy link
Contributor

@rbans96 rbans96 commented Dec 4, 2025

Description

Add tests to improve R4.Core tests coverage

Related issues

Addresses 174931

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 December 4, 2025 22:13
@rbans96 rbans96 added Azure Healthcare APIs Label denotes that the issue or PR is relevant to the FHIR service in the Azure Healthcare APIs Azure API for FHIR Label denotes that the issue or PR is relevant to the Azure API for FHIR labels Dec 4, 2025
@rbans96 rbans96 added this to the CY25Q3/2Wk07 milestone Dec 4, 2025
@rbans96 rbans96 added the Enhancement-Test Enhancement on tests. label Dec 4, 2025
{
// Arrange
const string operationName = "export";
var cancellationTokenSource = new CancellationTokenSource();

Check warning

Code scanning / CodeQL

Missing Dispose call on local IDisposable Warning

Disposable 'CancellationTokenSource' is created but not disposed.

// Assert
Assert.NotNull(result);
var resultPatient = result.ToPoco<Patient>();

Check warning

Code scanning / CodeQL

Useless assignment to local variable Warning

This assignment to
resultPatient
is useless, since its value is never read.

Copilot Autofix

AI 2 days ago

To fix the useless assignment, remove the declaration and assignment to the resultPatient variable on line 254. Since the value is never used, this can be deleted without affecting program logic or test coverage. You should update only the portion with the assignment so that the code reads smoothly without the variable. No imports or additional code are required.

Suggested changeset 1
src/Microsoft.Health.Fhir.R4.Core.UnitTests/Features/Operations/MemberMatch/MemberMatchServiceTests.cs

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/src/Microsoft.Health.Fhir.R4.Core.UnitTests/Features/Operations/MemberMatch/MemberMatchServiceTests.cs b/src/Microsoft.Health.Fhir.R4.Core.UnitTests/Features/Operations/MemberMatch/MemberMatchServiceTests.cs
--- a/src/Microsoft.Health.Fhir.R4.Core.UnitTests/Features/Operations/MemberMatch/MemberMatchServiceTests.cs
+++ b/src/Microsoft.Health.Fhir.R4.Core.UnitTests/Features/Operations/MemberMatch/MemberMatchServiceTests.cs
@@ -251,7 +251,7 @@
 
             // Assert
             Assert.NotNull(result);
-            var resultPatient = result.ToPoco<Patient>();
+            // Removed useless assignment to resultPatient
             Assert.Equal("patient1", matchingPatient.Id);
         }
 
EOF
@@ -251,7 +251,7 @@

// Assert
Assert.NotNull(result);
var resultPatient = result.ToPoco<Patient>();
// Removed useless assignment to resultPatient
Assert.Equal("patient1", matchingPatient.Id);
}

Copilot is powered by AI and may make mistakes. Always verify output.
Comment on lines +346 to +356
foreach (var type in new[] { "ValueSet", "CodeSystem", "StructureDefinition" })
{
if (type != resourceType)
{
_searchService.SearchAsync(
type,
Arg.Any<List<Tuple<string, string>>>(),
Arg.Any<CancellationToken>())
.Returns(new SearchResult(new List<SearchResultEntry>(), null, null, new List<Tuple<string, string>>()));
}
}

Check notice

Code scanning / CodeQL

Missed opportunity to use Where Note

This foreach loop
implicitly filters its target sequence
- consider filtering the sequence explicitly using '.Where(...)'.

Copilot Autofix

AI 2 days ago

To address the issue, the foreach loop in SetupSearchServiceWithResults should iterate only over elements where type != resourceType, utilizing LINQ's .Where(...) method for clarity and intent. The if (type != resourceType) guard inside the loop becomes superfluous and should be removed. There is no change to existing functionality—just a refactor to express filtering more idiomatically. Only the loop over the array on line 322–332 in SetupSearchServiceWithResults needs to be updated. Since System.Linq is already imported (line 8), no imports are required.


Suggested changeset 1
src/Microsoft.Health.Fhir.R4.Core.UnitTests/Features/Validation/ServerProvideProfileValidationTests.cs

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/src/Microsoft.Health.Fhir.R4.Core.UnitTests/Features/Validation/ServerProvideProfileValidationTests.cs b/src/Microsoft.Health.Fhir.R4.Core.UnitTests/Features/Validation/ServerProvideProfileValidationTests.cs
--- a/src/Microsoft.Health.Fhir.R4.Core.UnitTests/Features/Validation/ServerProvideProfileValidationTests.cs
+++ b/src/Microsoft.Health.Fhir.R4.Core.UnitTests/Features/Validation/ServerProvideProfileValidationTests.cs
@@ -319,16 +319,13 @@
                 .Returns(searchResult);
 
             // Setup for other resource types to return empty
-            foreach (var type in new[] { "ValueSet", "CodeSystem", "StructureDefinition" })
+            foreach (var type in new[] { "ValueSet", "CodeSystem", "StructureDefinition" }.Where(type => type != resourceType))
             {
-                if (type != resourceType)
-                {
-                    _searchService.SearchAsync(
-                        type,
-                        Arg.Any<List<Tuple<string, string>>>(),
-                        Arg.Any<CancellationToken>())
-                        .Returns(new SearchResult(new List<SearchResultEntry>(), null, null, new List<Tuple<string, string>>()));
-                }
+                _searchService.SearchAsync(
+                    type,
+                    Arg.Any<List<Tuple<string, string>>>(),
+                    Arg.Any<CancellationToken>())
+                    .Returns(new SearchResult(new List<SearchResultEntry>(), null, null, new List<Tuple<string, string>>()));
             }
         }
 
EOF
@@ -319,16 +319,13 @@
.Returns(searchResult);

// Setup for other resource types to return empty
foreach (var type in new[] { "ValueSet", "CodeSystem", "StructureDefinition" })
foreach (var type in new[] { "ValueSet", "CodeSystem", "StructureDefinition" }.Where(type => type != resourceType))
{
if (type != resourceType)
{
_searchService.SearchAsync(
type,
Arg.Any<List<Tuple<string, string>>>(),
Arg.Any<CancellationToken>())
.Returns(new SearchResult(new List<SearchResultEntry>(), null, null, new List<Tuple<string, string>>()));
}
_searchService.SearchAsync(
type,
Arg.Any<List<Tuple<string, string>>>(),
Arg.Any<CancellationToken>())
.Returns(new SearchResult(new List<SearchResultEntry>(), null, null, new List<Tuple<string, string>>()));
}
}

Copilot is powered by AI and may make mistakes. Always verify output.
var emptyResult = new SearchResult(Enumerable.Empty<SearchResultEntry>(), null, null, new Tuple<string, string>[0]);
_searchService.SearchAsync(Arg.Any<SearchOptions>(), CancellationToken.None).Returns(emptyResult);

SearchResult actualResult = await _patientEverythingService.SearchAsync("123", null, null, null, null, null, CancellationToken.None);

Check warning

Code scanning / CodeQL

Useless assignment to local variable Warning

This assignment to
actualResult
is useless, since its value is never read.

Copilot Autofix

AI 1 day ago

To fix the problem, simply remove the assignment to the actualResult variable. In the context of an await on an asynchronous function, if the returned value is not needed, just perform the call with await and omit assigning its result. This removes the useless variable and its assignment, while still triggering any required behavior of the awaited function.
Specifically, edit src/Microsoft.Health.Fhir.Shared.Core.UnitTests/Features/Operations/Everything/PatientEverythingServiceTests.cs, and in the method for the test GivenPhase0WithNoResults_WhenSearch_ThenProceedsToPhase1, remove the entire assignment statement on line 195 and replace it with just the awaited call:

await _patientEverythingService.SearchAsync("123", null, null, null, null, null, CancellationToken.None);

No additional imports, method definitions, or variable declarations are necessary.


Suggested changeset 1
src/Microsoft.Health.Fhir.Shared.Core.UnitTests/Features/Operations/Everything/PatientEverythingServiceTests.cs

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/src/Microsoft.Health.Fhir.Shared.Core.UnitTests/Features/Operations/Everything/PatientEverythingServiceTests.cs b/src/Microsoft.Health.Fhir.Shared.Core.UnitTests/Features/Operations/Everything/PatientEverythingServiceTests.cs
--- a/src/Microsoft.Health.Fhir.Shared.Core.UnitTests/Features/Operations/Everything/PatientEverythingServiceTests.cs
+++ b/src/Microsoft.Health.Fhir.Shared.Core.UnitTests/Features/Operations/Everything/PatientEverythingServiceTests.cs
@@ -192,7 +192,7 @@
             var emptyResult = new SearchResult(Enumerable.Empty<SearchResultEntry>(), null, null, new Tuple<string, string>[0]);
             _searchService.SearchAsync(Arg.Any<SearchOptions>(), CancellationToken.None).Returns(emptyResult);
 
-            SearchResult actualResult = await _patientEverythingService.SearchAsync("123", null, null, null, null, null, CancellationToken.None);
+            await _patientEverythingService.SearchAsync("123", null, null, null, null, null, CancellationToken.None);
 
             // Should have called search multiple times (phase 0, then phase 1/2)
             await _searchService.Received().SearchAsync(Arg.Any<SearchOptions>(), Arg.Any<CancellationToken>());
EOF
@@ -192,7 +192,7 @@
var emptyResult = new SearchResult(Enumerable.Empty<SearchResultEntry>(), null, null, new Tuple<string, string>[0]);
_searchService.SearchAsync(Arg.Any<SearchOptions>(), CancellationToken.None).Returns(emptyResult);

SearchResult actualResult = await _patientEverythingService.SearchAsync("123", null, null, null, null, null, CancellationToken.None);
await _patientEverythingService.SearchAsync("123", null, null, null, null, null, CancellationToken.None);

// Should have called search multiple times (phase 0, then phase 1/2)
await _searchService.Received().SearchAsync(Arg.Any<SearchOptions>(), Arg.Any<CancellationToken>());
Copilot is powered by AI and may make mistakes. Always verify output.
Unable to commit as this autofix suggestion is now outdated
var start = PartialDateTime.Parse("2020-01-01");
var end = PartialDateTime.Parse("2020-12-31");

SearchResult actualResult = await _patientEverythingService.SearchAsync("123", start, end, null, null, null, CancellationToken.None);

Check warning

Code scanning / CodeQL

Useless assignment to local variable Warning

This assignment to
actualResult
is useless, since its value is never read.

Copilot Autofix

AI 1 day ago

To fix this problem, remove the assignment of the result of _patientEverythingService.SearchAsync(...) to the unused local variable actualResult, and simply await the call as a statement. This change should be made only within the GivenPhase1WithDateRange_WhenSearch_ThenSearchCompartmentWithDateIsCalled test method, at line 216. No other changes are required, as there is no use of actualResult later in the method. This will improve code clarity and eliminate the useless assignment flagged by CodeQL.


Suggested changeset 1
src/Microsoft.Health.Fhir.Shared.Core.UnitTests/Features/Operations/Everything/PatientEverythingServiceTests.cs

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/src/Microsoft.Health.Fhir.Shared.Core.UnitTests/Features/Operations/Everything/PatientEverythingServiceTests.cs b/src/Microsoft.Health.Fhir.Shared.Core.UnitTests/Features/Operations/Everything/PatientEverythingServiceTests.cs
--- a/src/Microsoft.Health.Fhir.Shared.Core.UnitTests/Features/Operations/Everything/PatientEverythingServiceTests.cs
+++ b/src/Microsoft.Health.Fhir.Shared.Core.UnitTests/Features/Operations/Everything/PatientEverythingServiceTests.cs
@@ -213,7 +213,7 @@
             var start = PartialDateTime.Parse("2020-01-01");
             var end = PartialDateTime.Parse("2020-12-31");
 
-            SearchResult actualResult = await _patientEverythingService.SearchAsync("123", start, end, null, null, null, CancellationToken.None);
+            await _patientEverythingService.SearchAsync("123", start, end, null, null, null, CancellationToken.None);
 
             // Should have searched with date parameters
             await _searchService.Received().SearchAsync(Arg.Any<SearchOptions>(), CancellationToken.None);
EOF
@@ -213,7 +213,7 @@
var start = PartialDateTime.Parse("2020-01-01");
var end = PartialDateTime.Parse("2020-12-31");

SearchResult actualResult = await _patientEverythingService.SearchAsync("123", start, end, null, null, null, CancellationToken.None);
await _patientEverythingService.SearchAsync("123", start, end, null, null, null, CancellationToken.None);

// Should have searched with date parameters
await _searchService.Received().SearchAsync(Arg.Any<SearchOptions>(), CancellationToken.None);
Copilot is powered by AI and may make mistakes. Always verify output.
Unable to commit as this autofix suggestion is now outdated
var emptyResult = new SearchResult(Enumerable.Empty<SearchResultEntry>(), null, null, new Tuple<string, string>[0]);
_searchService.SearchAsync(Arg.Any<SearchOptions>(), CancellationToken.None).Returns(emptyResult);

SearchResult actualResult = await _patientEverythingService.SearchAsync("123", null, null, null, null, null, CancellationToken.None);

Check warning

Code scanning / CodeQL

Useless assignment to local variable Warning

This assignment to
actualResult
is useless, since its value is never read.

Copilot Autofix

AI 1 day ago

To address this issue, simply remove the assignment to the unused variable actualResult, but keep the await call to _patientEverythingService.SearchAsync(...) to ensure the side effects and possible exceptions are still observed (as may be needed for the test). Replace

SearchResult actualResult = await _patientEverythingService.SearchAsync(...);

with

await _patientEverythingService.SearchAsync(...);

This preserves all current functionality but eliminates the useless assignment. No additional imports, definitions, or method changes are needed.

Suggested changeset 1
src/Microsoft.Health.Fhir.Shared.Core.UnitTests/Features/Operations/Everything/PatientEverythingServiceTests.cs

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/src/Microsoft.Health.Fhir.Shared.Core.UnitTests/Features/Operations/Everything/PatientEverythingServiceTests.cs b/src/Microsoft.Health.Fhir.Shared.Core.UnitTests/Features/Operations/Everything/PatientEverythingServiceTests.cs
--- a/src/Microsoft.Health.Fhir.Shared.Core.UnitTests/Features/Operations/Everything/PatientEverythingServiceTests.cs
+++ b/src/Microsoft.Health.Fhir.Shared.Core.UnitTests/Features/Operations/Everything/PatientEverythingServiceTests.cs
@@ -231,7 +231,7 @@
             var emptyResult = new SearchResult(Enumerable.Empty<SearchResultEntry>(), null, null, new Tuple<string, string>[0]);
             _searchService.SearchAsync(Arg.Any<SearchOptions>(), CancellationToken.None).Returns(emptyResult);
 
-            SearchResult actualResult = await _patientEverythingService.SearchAsync("123", null, null, null, null, null, CancellationToken.None);
+            await _patientEverythingService.SearchAsync("123", null, null, null, null, null, CancellationToken.None);
 
             // Should proceed through phases
             await _searchService.Received().SearchAsync(Arg.Any<SearchOptions>(), Arg.Any<CancellationToken>());
EOF
@@ -231,7 +231,7 @@
var emptyResult = new SearchResult(Enumerable.Empty<SearchResultEntry>(), null, null, new Tuple<string, string>[0]);
_searchService.SearchAsync(Arg.Any<SearchOptions>(), CancellationToken.None).Returns(emptyResult);

SearchResult actualResult = await _patientEverythingService.SearchAsync("123", null, null, null, null, null, CancellationToken.None);
await _patientEverythingService.SearchAsync("123", null, null, null, null, null, CancellationToken.None);

// Should proceed through phases
await _searchService.Received().SearchAsync(Arg.Any<SearchOptions>(), Arg.Any<CancellationToken>());
Copilot is powered by AI and may make mistakes. Always verify output.
Unable to commit as this autofix suggestion is now outdated
var patient = Samples.GetDefaultPatient();
var coverage = Samples.GetDefaultCoverage();
var request = new MemberMatchRequest(coverage, patient);
var cts = new CancellationTokenSource();

Check warning

Code scanning / CodeQL

Missing Dispose call on local IDisposable Warning

Disposable 'CancellationTokenSource' is created but not disposed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Azure API for FHIR Label denotes that the issue or PR is relevant to the Azure API for FHIR 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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants