Skip to content

Commit

Permalink
fix: #2802 - let SnapshotSource reuse self-generated snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
cknaap committed Jun 25, 2024
1 parent 776e52d commit ef57a26
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private async Tasks.Task<Resource> ensureSnapshot(Resource res)
{
if (res is StructureDefinition sd)
{
if (!sd.HasSnapshot || Generator.Settings.ForceRegenerateSnapshots || !sd.Snapshot.IsCreatedBySnapshotGenerator())
if (!sd.HasSnapshot || (Generator.Settings.ForceRegenerateSnapshots && !sd.Snapshot.IsCreatedBySnapshotGenerator()))
{
await Generator.UpdateAsync(sd).ConfigureAwait(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ public class SnapshotSourceTest
[TestMethod]
public async Tasks.Task TestElementSnapshot()
{
var zipSource = ZipSource.CreateValidationSource();
var cachedSource = new CachedResolver(zipSource);
var snapSource = new SnapshotSource(cachedSource, regenerate:true);
// Request core Element snapshot; verify recursion handling

var orgSource = ZipSource.CreateValidationSource();
var cachedSource = new CachedResolver(orgSource);

// Assumption: source provides Element structure
var sdCached = await cachedSource.FindStructureDefinitionForCoreTypeAsync(FHIRAllTypes.Element);
Assert.IsNotNull(sdCached);
Expand All @@ -28,11 +28,9 @@ public async Tasks.Task TestElementSnapshot()

// Generate snapshot by calling SnapshotSource
// Important! Specify flag to force re-generation (don't trust existing core snapshots...)
var snapSource = new SnapshotSource(cachedSource, true);

var sd = await snapSource.FindStructureDefinitionForCoreTypeAsync(FHIRAllTypes.Element);
Assert.IsNotNull(sd);
Assert.AreEqual(sdCached, sd); // Expecting same (cached) object reference, with updated Snapshot component
Assert.AreEqual(sdCached, sd); // Expecting same (cached) object reference, with updated Snapshot component
Assert.IsTrue(sd.HasSnapshot);
Assert.IsTrue(sd.Snapshot.IsCreatedBySnapshotGenerator());

Expand Down Expand Up @@ -75,7 +73,8 @@ void assert_ele1(ElementDefinition eld)
// STU3: Element.id has type code "string"
// R4: Element.id has no type code, only special "compiler magic" extensions
// => Element.id no longer inherits constraints from "Element", e.g. "ele-1"
if (elem.Type?.FirstOrDefault()?.Code is string typeName && !typeName.StartsWith("http://hl7.org/fhirpath/System."))
if (elem.Type?.FirstOrDefault()?.Code is string typeName &&
!typeName.StartsWith("http://hl7.org/fhirpath/System."))
{
assert_ele1(elem);
}
Expand All @@ -85,24 +84,54 @@ void assert_ele1(ElementDefinition eld)
[TestMethod]
public void CannotCreateSnapshotGeneratorFromSnapshotSource()
{
var orgSource = ZipSource.CreateValidationSource();
var cachedSource = new CachedResolver(orgSource);
var src = new SnapshotSource(cachedSource);
var zipSource = ZipSource.CreateValidationSource();
var cachedSource = new CachedResolver(zipSource);
var snapSource = new SnapshotSource(cachedSource, regenerate:true);

// Verify that SnapshotGenerator ctor rejects SnapshotSource arguments
Assert.ThrowsException<ArgumentException>(() => new SnapshotGenerator(src));
Assert.ThrowsException<ArgumentException>(() => new SnapshotGenerator(snapSource));
}

[TestMethod]
public void CannotCreateNestedSnapshotSource()
{
var orgSource = ZipSource.CreateValidationSource();
var cachedSource = new CachedResolver(orgSource);
var src = new SnapshotSource(cachedSource);
var zipSource = ZipSource.CreateValidationSource();
var cachedSource = new CachedResolver(zipSource);
var snapSource = new SnapshotSource(cachedSource, regenerate:true);

// Verify that SnapshotSource ctor rejects SnapshotSource arguments
Assert.ThrowsException<ArgumentException>(() => new SnapshotSource(src));
Assert.ThrowsException<ArgumentException>(() => new SnapshotSource(snapSource));
}

[TestMethod]
public async Tasks.Task Generate_ForceRegenerate_DoesNotReuse_SnapshotCreatedByOthers()
{
var zipSource = ZipSource.CreateValidationSource();
var cachedSource = new CachedResolver(zipSource);
var snapSource = new SnapshotSource(cachedSource, regenerate:true);

var original = await cachedSource.FindStructureDefinitionForCoreTypeAsync(FHIRAllTypes.Element);
Assert.IsTrue(original.HasSnapshot);
var originalSnapshot = original.Snapshot;
var regenerated =
await snapSource.FindStructureDefinitionForCoreTypeAsync(FHIRAllTypes.Element);
Assert.IsTrue(regenerated.HasSnapshot);
Assert.AreNotSame(originalSnapshot, regenerated.Snapshot);
}

[TestMethod]
public async Tasks.Task Generate_ForceRegenerate_Reuses_SelfCreatedSnapshot()
{
var zipSource = ZipSource.CreateValidationSource();
var cachedSource = new CachedResolver(zipSource);
var snapSource = new SnapshotSource(cachedSource, regenerate:true);

var first =
await snapSource.FindStructureDefinitionForCoreTypeAsync(FHIRAllTypes.Element);
var firstSnapshot = first.Snapshot;
var second =
await snapSource.FindStructureDefinitionForCoreTypeAsync(FHIRAllTypes.Element);
Assert.AreSame(firstSnapshot, second.Snapshot);
}
}
}

0 comments on commit ef57a26

Please sign in to comment.