-
Notifications
You must be signed in to change notification settings - Fork 67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixed issue where bundle extension could return null when resource exists #19
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
test/Microsoft.Health.Extensions.Fhir.R4.UnitTests/BundleExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
using System; | ||
namalu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
using System.Threading.Tasks; | ||
using Xunit; | ||
using Hl7.Fhir.Model; | ||
using Hl7.Fhir.Rest; | ||
using System.Collections.Generic; | ||
using NSubstitute; | ||
|
||
namespace Microsoft.Health.Extensions.Fhir.R4.UnitTests | ||
{ | ||
public class BundleExtensionsTests | ||
{ | ||
[Fact] | ||
public async void GivenNoEntriesAndNoContinuationToken_ReadOneFromBundleWithContinuationAsync_ThenNullIsReturned_Test() | ||
{ | ||
var bundle = new Bundle(); | ||
bundle.Entry = new List<Bundle.EntryComponent>(); | ||
bundle.Link = new List<Bundle.LinkComponent>(); | ||
|
||
var client = Substitute.For<IFhirClient>(); | ||
client.ContinueAsync(Arg.Any<Bundle>()).Returns(System.Threading.Tasks.Task.FromResult<Bundle>(null)); | ||
|
||
Assert.Null(await bundle.ReadOneFromBundleWithContinuationAsync<Observation>(client)); | ||
} | ||
|
||
[Fact] | ||
public async void GivenOneEntryAndNoContinuationToken_ReadOneFromBundleWithContinuationAsync_ThenResourceIsReturned_Test() | ||
{ | ||
var bundle = new Bundle(); | ||
bundle.Entry = new List<Bundle.EntryComponent>(); | ||
bundle.Link = new List<Bundle.LinkComponent>(); | ||
|
||
var observation = new Observation(); | ||
var entry = new Bundle.EntryComponent(); | ||
entry.Resource = observation; | ||
bundle.Entry.Add(entry); | ||
|
||
var client = Substitute.For<IFhirClient>(); | ||
client.ContinueAsync(Arg.Any<Bundle>()).Returns(System.Threading.Tasks.Task.FromResult<Bundle>(null)); | ||
|
||
Assert.Equal(observation, await bundle.ReadOneFromBundleWithContinuationAsync<Observation>(client)); | ||
} | ||
|
||
[Fact] | ||
public async void GivenOneEntryAfterContinuationToken_ReadOneFromBundleWithContinuationAsync_ThenResourceIsReturned_Test() | ||
{ | ||
var bundle = new Bundle(); | ||
bundle.Entry = new List<Bundle.EntryComponent>(); | ||
bundle.Link = new List<Bundle.LinkComponent>(); | ||
|
||
var continuationBundle = new Bundle(); | ||
continuationBundle.Entry = new List<Bundle.EntryComponent>(); | ||
continuationBundle.Link = new List<Bundle.LinkComponent>(); | ||
|
||
var observation = new Observation(); | ||
var entry = new Bundle.EntryComponent(); | ||
entry.Resource = observation; | ||
continuationBundle.Entry.Add(entry); | ||
|
||
var client = Substitute.For<IFhirClient>(); | ||
client.ContinueAsync(Arg.Any<Bundle>()).Returns(ret => System.Threading.Tasks.Task.FromResult(continuationBundle), ret => System.Threading.Tasks.Task.FromResult<Bundle>(null)); | ||
|
||
Assert.Equal(observation, await bundle.ReadOneFromBundleWithContinuationAsync<Observation>(client)); | ||
} | ||
|
||
[Fact] | ||
public async void GivenTwoEntriesAndNoContinuationToken_ReadOneFromBundleWithContinuationAsync_ThenThrows_Test() | ||
{ | ||
var bundle = new Bundle(); | ||
bundle.Entry = new List<Bundle.EntryComponent>(); | ||
bundle.Link = new List<Bundle.LinkComponent>(); | ||
|
||
var observation1 = new Observation(); | ||
var entry1 = new Bundle.EntryComponent(); | ||
entry1.Resource = observation1; | ||
bundle.Entry.Add(entry1); | ||
|
||
var observation2 = new Observation(); | ||
var entry2 = new Bundle.EntryComponent(); | ||
entry2.Resource = observation2; | ||
bundle.Entry.Add(entry2); | ||
|
||
var client = Substitute.For<IFhirClient>(); | ||
client.ContinueAsync(Arg.Any<Bundle>()).Returns(System.Threading.Tasks.Task.FromResult<Bundle>(null)); | ||
|
||
await Assert.ThrowsAsync<MultipleResourceFoundException<Observation>>(() => bundle.ReadOneFromBundleWithContinuationAsync<Observation>(client)); | ||
} | ||
|
||
[Fact] | ||
public async void GivenOneEntryBeforeAndAfterContinuationToken_ReadOneFromBundleWithContinuationAsync_ThenThrows_Test() | ||
{ | ||
var bundle = new Bundle(); | ||
bundle.Entry = new List<Bundle.EntryComponent>(); | ||
bundle.Link = new List<Bundle.LinkComponent>(); | ||
|
||
var observation1 = new Observation(); | ||
var entry1 = new Bundle.EntryComponent(); | ||
entry1.Resource = observation1; | ||
bundle.Entry.Add(entry1); | ||
|
||
var continuationBundle = new Bundle(); | ||
continuationBundle.Entry = new List<Bundle.EntryComponent>(); | ||
continuationBundle.Link = new List<Bundle.LinkComponent>(); | ||
|
||
var observation2 = new Observation(); | ||
var entry2 = new Bundle.EntryComponent(); | ||
entry2.Resource = observation2; | ||
continuationBundle.Entry.Add(entry2); | ||
|
||
var client = Substitute.For<IFhirClient>(); | ||
client.ContinueAsync(Arg.Any<Bundle>()).Returns(ret => System.Threading.Tasks.Task.FromResult(continuationBundle), ret => System.Threading.Tasks.Task.FromResult<Bundle>(null)); | ||
|
||
await Assert.ThrowsAsync<MultipleResourceFoundException<Observation>>(() => bundle.ReadOneFromBundleWithContinuationAsync<Observation>(client)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor, but best to use .ToArray to avoid multiple enumerations over the same collection (happens with Count & FirstOrDefault). Impact is minimal since ReadFromBundleWithContinuation is using a materialized collection behind the scenes (list in this case) but if that ever changes we should re-evaluate.
We also may want to look at using async enumerables going forward as well https://docs.microsoft.com/en-us/archive/msdn-magazine/2019/november/csharp-iterating-with-async-enumerables-in-csharp-8