Skip to content
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

Replace static DICOM test data with minimal dynamic content #1918

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/SmiServices/Common/Messages/AccessionDirectoryMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Newtonsoft.Json;
using System;
using System.IO;
using System.IO.Abstractions;

namespace SmiServices.Common.Messages
{
Expand All @@ -19,7 +20,7 @@ public sealed class AccessionDirectoryMessage : MemberwiseEquatable<AccessionDir

public AccessionDirectoryMessage() { }

public AccessionDirectoryMessage(string root, DirectoryInfo directory)
public AccessionDirectoryMessage(string root, IDirectoryInfo directory)
{
if (!directory.FullName.StartsWith(root, StringComparison.CurrentCultureIgnoreCase))
throw new Exception("Directory '" + directory + "' did not share a common root with the root '" + root + "'");
Expand Down
3 changes: 2 additions & 1 deletion src/SmiServices/Common/Messages/DicomFileMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Newtonsoft.Json;
using System;
using System.IO;
using System.IO.Abstractions;
using System.Text;

namespace SmiServices.Common.Messages
Expand Down Expand Up @@ -49,7 +50,7 @@ public sealed class DicomFileMessage : MemberwiseEquatable<DicomFileMessage>, IF

public DicomFileMessage() { }

public DicomFileMessage(string root, FileInfo file)
public DicomFileMessage(string root, IFileInfo file)
: this(root, file.FullName) { }

public DicomFileMessage(string root, string file)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using SmiServices.Microservices.DicomTagReader.Execution;
using System;
using System.Collections.Generic;
using System.IO.Abstractions;

namespace SmiServices.Microservices.DicomTagReader
{
Expand All @@ -21,12 +22,14 @@ public static int Main(IEnumerable<string> args)

private static int OnParse(GlobalOptions globals, DicomTagReaderCliOptions opts)
{
var fileSystem = new FileSystem();

if (opts.File != null)
{
try
{
var host = new DicomTagReaderHost(globals);
host.AccessionDirectoryMessageConsumer.RunSingleFile(opts.File);
host.AccessionDirectoryMessageConsumer.RunSingleFile(fileSystem.FileInfo.New(opts.File));
return 0;
}
catch (Exception ex)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using CommandLine;
using SmiServices.Common.Options;
using System.IO;
using System.Diagnostics.CodeAnalysis;

namespace SmiServices.Microservices.DicomTagReader
{
[ExcludeFromCodeCoverage]
public class DicomTagReaderCliOptions : CliOptions
{
/// <summary>
Expand All @@ -15,6 +16,6 @@ public class DicomTagReaderCliOptions : CliOptions
Required = false,
HelpText = "[Optional] Name of a specific dicom or zip file to process instead of subscribing to rabbit"
)]
public FileInfo? File { get; set; }
public string? File { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using SmiServices.Common.Options;
using SmiServices.Microservices.DicomTagReader.Execution;
using System;
using System.IO;
using System.IO.Abstractions;

namespace SmiServices.Microservices.DicomTagReader.Messaging
{
Expand All @@ -15,17 +15,19 @@ public class DicomTagReaderConsumer : Consumer<AccessionDirectoryMessage>
{
private readonly TagReaderBase _reader;
private readonly GlobalOptions _opts;

private readonly IFileSystem _fileSystem;

/// <summary>
/// Default constructor
/// </summary>
/// <param name="reader"></param>
/// <param name="dicomTagReaderOptions"></param>>
public DicomTagReaderConsumer(TagReaderBase reader, GlobalOptions dicomTagReaderOptions)
/// <param name="dicomTagReaderOptions"></param>
/// <param name="fileSystem"></param>>
public DicomTagReaderConsumer(TagReaderBase reader, GlobalOptions dicomTagReaderOptions, IFileSystem? fileSystem = null)
{
_reader = reader;
_opts = dicomTagReaderOptions;
_fileSystem = fileSystem ?? new FileSystem();
}

/// <summary>
Expand Down Expand Up @@ -61,10 +63,10 @@ protected override void ProcessMessageImpl(IMessageHeader header, AccessionDirec
/// Runs a single file (dicom or zip) through tag reading process
/// </summary>
/// <param name="file"></param>
public void RunSingleFile(FileInfo file)
public void RunSingleFile(IFileInfo file)
{
// tell reader only to consider our specific file
_reader.IncludeFile = f => new FileInfo(f).FullName.Equals(file.FullName, StringComparison.CurrentCultureIgnoreCase);
_reader.IncludeFile = f => _fileSystem.FileInfo.New(f).FullName.Equals(file.FullName, StringComparison.CurrentCultureIgnoreCase);
_reader.ReadTags(null, new AccessionDirectoryMessage(_opts.FileSystemOptions!.FileSystemRoot!, file.Directory!));

// good practice to clear this afterwards
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using SmiServices.UnitTests.Common;
using System;
using System.IO;
using System.IO.Abstractions;
using System.Linq;
using System.Threading;
using Tests.Common;
Expand Down Expand Up @@ -47,9 +48,10 @@ public void Test_DodgyTagNames()
DirectoryInfo d = new(Path.Combine(TestContext.CurrentContext.TestDirectory, nameof(Test_DodgyTagNames)));
d.Create();

var td = new TestData();
var fi = td.Create(new FileInfo(Path.Combine(d.FullName, "MyTestFile.dcm")));
var fi2 = td.Create(new FileInfo(Path.Combine(d.FullName, "MyTestFile2.dcm")));
var fileSystem = new FileSystem();
var fi = fileSystem.FileInfo.New(fileSystem.Path.Combine(d.FullName, "MyTestFile.dcm"));
DicomFileTestHelpers.WriteSampleDicomFile(fi);
var fi2 = fileSystem.FileInfo.New(fileSystem.Path.Combine(d.FullName, "MyTestFile2.dcm"));

DicomFile dcm;

Expand Down Expand Up @@ -113,7 +115,9 @@ public void TestLoadingOneImage_SingleFileMessage(int numberOfMessagesToSend, bo
DirectoryInfo d = new(Path.Combine(TestContext.CurrentContext.TestDirectory, nameof(TestLoadingOneImage_SingleFileMessage)));
d.Create();

var fi = new TestData().Create(new FileInfo(Path.Combine(d.FullName, "MyTestFile.dcm")));
var fileSystem = new FileSystem();
var fi = fileSystem.FileInfo.New(fileSystem.Path.Combine(d.FullName, "MyTestFile.dcm"));
DicomFileTestHelpers.WriteSampleDicomFile(fi);

if (mixInATextFile)
{
Expand Down Expand Up @@ -167,7 +171,7 @@ public void TestLoadingOneImage_MileWideTest()
d.Create();

var r = new Random(5000);
FileInfo[] files;
IFileInfo[] files;

using (var g = new DicomDataGenerator(r, d.FullName, "CT"))
files = g.GenerateImageFiles(1, r).ToArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
using System;
using System.Data;
using System.IO;
using System.IO.Abstractions;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
Expand Down Expand Up @@ -122,7 +123,9 @@ public void IntegrationTest_HappyPath(DatabaseType databaseType, Type namerType)
foreach (var f in dir.GetFiles())
f.Delete();

new TestData().Create(new FileInfo(Path.Combine(dir.FullName, "MyTestFile.dcm")));
var fileSystem = new FileSystem();
var fi = fileSystem.FileInfo.New(fileSystem.Path.Combine(dir.FullName, "MyTestFile.dcm"));
DicomFileTestHelpers.WriteSampleDicomFile(fi);

RunTest(dir, 1);
}
Expand Down Expand Up @@ -159,7 +162,9 @@ public void IntegrationTest_NoFileExtensions(DatabaseType databaseType, Type nam
foreach (var f in dir.GetFiles())
f.Delete();

new TestData().Create(new FileInfo(Path.Combine(dir.FullName, "Mr.010101"))); //this is legit a dicom file
var fileSystem = new FileSystem();
var fi = fileSystem.FileInfo.New(fileSystem.Path.Combine(dir.FullName, "Mr.010101"));
DicomFileTestHelpers.WriteSampleDicomFile(fi);

try
{
Expand Down Expand Up @@ -206,7 +211,9 @@ public void IntegrationTest_Rejector(DatabaseType databaseType, Type? rejector)
foreach (var f in dir.GetFiles())
f.Delete();

new TestData().Create(new FileInfo(Path.Combine(dir.FullName, "Mr.010101"))); //this is legit a dicom file
var fileSystem = new FileSystem();
var fi = fileSystem.FileInfo.New(fileSystem.Path.Combine(dir.FullName, "Mr.010101"));
DicomFileTestHelpers.WriteSampleDicomFile(fi);

try
{
Expand Down Expand Up @@ -285,33 +292,36 @@ public void IntegrationTest_HappyPath_WithIsolation(DatabaseType databaseType, T
foreach (var f in dir.GetFiles())
f.Delete();

new TestData().Create(new FileInfo(Path.Combine(dir.FullName, "MyTestFile.dcm")));
var fileSystem = new FileSystem();
DicomFileTestHelpers.WriteSampleDicomFile(fileSystem.FileInfo.New(fileSystem.Path.Combine(dir.FullName, "MyTestFile.dcm")));

var ds1 = new DicomDataset
{
{ DicomTag.StudyInstanceUID, "1.2.3" },
{ DicomTag.SeriesInstanceUID, "1.2.2" },
{ DicomTag.SOPInstanceUID, "1.2.3" },
{ DicomTag.PatientAge, "030Y" },
{ DicomTag.PatientID, "123" },
{ DicomTag.SOPClassUID, "1" },
{ DicomTag.Modality, "MR" }
};

new DicomFile(ds1).Save(Path.Combine(dir.FullName, "abc.dcm"));

var ds2 = new DicomDataset
{
{ DicomTag.StudyInstanceUID, "1.2.3" },
{ DicomTag.SeriesInstanceUID, "1.2.4" },
{ DicomTag.SOPInstanceUID, "1.2.7" },
{ DicomTag.PatientAge, "040Y" }, //age is replicated but should be unique at study level so gets isolated
{ DicomTag.PatientID, "123" },
{ DicomTag.SOPClassUID, "1" },
{ DicomTag.Modality, "MR" }
};
DicomFileTestHelpers.WriteSampleDicomFile(
fileSystem.FileInfo.New(fileSystem.Path.Combine(dir.FullName, "abc.dcm")),
new DicomDataset
{
{ DicomTag.StudyInstanceUID, "1.2.3" },
{ DicomTag.SeriesInstanceUID, "1.2.2" },
{ DicomTag.SOPInstanceUID, "1.2.3" },
{ DicomTag.PatientAge, "030Y" },
{ DicomTag.PatientID, "123" },
{ DicomTag.SOPClassUID, "1" },
{ DicomTag.Modality, "MR" }
}
);

new DicomFile(ds2).Save(Path.Combine(dir.FullName, "def.dcm"));
DicomFileTestHelpers.WriteSampleDicomFile(
fileSystem.FileInfo.New(fileSystem.Path.Combine(dir.FullName, "def.dcm")),
new DicomDataset
{
{ DicomTag.StudyInstanceUID, "1.2.3" },
{ DicomTag.SeriesInstanceUID, "1.2.4" },
{ DicomTag.SOPInstanceUID, "1.2.7" },
{ DicomTag.PatientAge, "040Y" }, //age is replicated but should be unique at study level so gets isolated
{ DicomTag.PatientID, "123" },
{ DicomTag.SOPClassUID, "1" },
{ DicomTag.Modality, "MR" }
}
);

var checks = new ProcessTaskChecks(_helper.LoadMetadata);
checks.Check(new AcceptAllCheckNotifier());
Expand Down Expand Up @@ -379,13 +389,24 @@ public void IntegrationTest_HappyPath_WithElevation(DatabaseType databaseType, b
foreach (var f in dir.GetFiles())
f.Delete();

new TestData().Create(new FileInfo(Path.Combine(dir.FullName, "MyTestFile.dcm")));
var ds = DicomFileTestHelpers.DefaultDicomDataset();
var sequenceDs = new DicomDataset()
{
{DicomTag.CodeValue, "CodeValue" },
{DicomTag.CodingSchemeDesignator, "CodingSchemeDesignator" },
{DicomTag.CodeMeaning, "CodeMeaning" },
};
ds.Add(new DicomSequence(DicomTag.DerivationCodeSequence, sequenceDs));

var fileSystem = new FileSystem();
var fi = fileSystem.FileInfo.New(fileSystem.Path.Combine(dir.FullName, "MyTestFile.dcm"));
DicomFileTestHelpers.WriteSampleDicomFile(fi, ds);

RunTest(dir, 1);

var tbl = _helper.ImageTable.GetDataTable();
Assert.That(tbl.Rows, Has.Count.EqualTo(1));
Assert.That(tbl.Rows[0]["d_DerivationCodeMeaning"], Is.EqualTo("Full fidelity image, uncompressed or lossless compressed"));
Assert.That(tbl.Rows[0]["d_DerivationCodeMeaning"], Is.EqualTo("CodeMeaning"));

_helper.ImageTable.DropColumn(_helper.ImageTable.DiscoverColumn("d_DerivationCodeMeaning"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
using SmiServices.Microservices.DicomTagReader.Execution;
using SmiServices.UnitTests.Common;
using System;
using System.IO;
using System.IO.Abstractions;
using System.IO.Abstractions.TestingHelpers;
using System.IO.Compression;
using System.Linq;
using System.Threading;
Expand Down Expand Up @@ -79,7 +80,9 @@ public void TestBasicOperation()
[Test]
public void TestTagReader_SingleFileMode()
{
var dirRoot = new DirectoryInfo(Path.Combine(TestContext.CurrentContext.WorkDirectory, "TestTagReader_SingleFileMode"));
var fileSystem = new FileSystem();

var dirRoot = fileSystem.DirectoryInfo.New(fileSystem.Path.Combine(TestContext.CurrentContext.WorkDirectory, "TestTagReader_SingleFileMode"));

if (dirRoot.Exists)
dirRoot.Delete(true);
Expand All @@ -103,11 +106,11 @@ public void TestTagReader_SingleFileMode()
Assert.That(_helper.SeriesCount, Is.EqualTo(1));
});

var julyZip = Path.Combine(dirRoot.FullName, "july.zip");
var julyZip = fileSystem.Path.Combine(dirRoot.FullName, "july.zip");

ZipFile.CreateFromDirectory(julyFolder.FullName, julyZip);

host.AccessionDirectoryMessageConsumer.RunSingleFile(new FileInfo(julyZip));
host.AccessionDirectoryMessageConsumer.RunSingleFile(fileSystem.FileInfo.New(julyZip));

Assert.Multiple(() =>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using NUnit.Framework;
using SmiServices.Common.Options;

namespace SmiServices.IntegrationTests.Microservices.DicomTagReader
{
[RequiresRabbit]
public class DicomTagReaderTests
{
[Test]
public void Main_RunSingleFile_Exception_ReturnsOne()
{
// Arrange

SmiCliInit.InitSmiLogging = false;

// Act

var rc = SmiServices.Microservices.DicomTagReader.DicomTagReader.Main(["-f", "some.dcm"]);

// Assert

Assert.That(rc, Is.EqualTo(1));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using SmiServices.UnitTests.Common;
using System;
using System.IO;
using System.IO.Abstractions;

namespace SmiServices.UnitTests.Microservices.IsIdentifiable
{
Expand Down Expand Up @@ -49,8 +50,10 @@ public void TestClassifierName_ValidClassifier()
{
var options = new GlobalOptionsFactory().Load(nameof(TestClassifierName_ValidClassifier));

var testDcm = new FileInfo(Path.Combine(TestContext.CurrentContext.TestDirectory, nameof(TestClassifierName_ValidClassifier), "f1.dcm")); Path.Combine(TestContext.CurrentContext.TestDirectory, nameof(TestClassifierName_ValidClassifier), "f1.dcm");
new TestData().Create(testDcm);
var fileSystem = new FileSystem();
fileSystem.Directory.CreateDirectory(fileSystem.Path.Combine(TestContext.CurrentContext.TestDirectory, nameof(TestClassifierName_ValidClassifier)));
var testDcm = fileSystem.FileInfo.New(fileSystem.Path.Combine(TestContext.CurrentContext.TestDirectory, nameof(TestClassifierName_ValidClassifier), "f1.dcm"));
DicomFileTestHelpers.WriteSampleDicomFile(testDcm);

using var tester = new MicroserviceTester(options.RabbitOptions!, options.IsIdentifiableServiceOptions!);
tester.CreateExchange(options.IsIdentifiableServiceOptions!.IsIdentifiableProducerOptions!.ExchangeName!, null);
Expand Down Expand Up @@ -96,10 +99,9 @@ public void TestIsIdentifiable_TesseractStanfordDicomFileClassifier()
if (!File.Exists(dest))
File.Copy(Path.Combine(DataDirectory, "tessdata", "eng.traineddata"), dest);

var testDcm = new FileInfo(Path.Combine(TestContext.CurrentContext.TestDirectory, nameof(TestIsIdentifiable_TesseractStanfordDicomFileClassifier), "f1.dcm"));

Path.Combine(TestContext.CurrentContext.TestDirectory, nameof(TestClassifierName_ValidClassifier), "f1.dcm");
new TestData().Create(testDcm);
var fileSystem = new FileSystem();
var testDcm = fileSystem.FileInfo.New(fileSystem.Path.Combine(TestContext.CurrentContext.TestDirectory, nameof(TestIsIdentifiable_TesseractStanfordDicomFileClassifier), "f1.dcm"));
DicomFileTestHelpers.WriteSampleDicomFile(testDcm);

using var tester = new MicroserviceTester(options.RabbitOptions!, options.IsIdentifiableServiceOptions);
options.IsIdentifiableServiceOptions.ClassifierType = typeof(TesseractStanfordDicomFileClassifier).FullName;
Expand Down
Loading
Loading