diff --git a/.github/workflows/dotnet.yml b/.github/workflows/dotnet.yml index 30f3d64..4939ca9 100644 --- a/.github/workflows/dotnet.yml +++ b/.github/workflows/dotnet.yml @@ -26,9 +26,14 @@ jobs: - name: Build run: dotnet build -c Release --no-restore - name: Test - run: dotnet test --verbosity normal -p:CollectCoverage=true - - name: Upload a Build Artifact + run: dotnet run --framework net9.0 --report-xunit-html --report-xunit-html-filename TestReport.html --project src/Tests/Tests.csproj + - name: Upload Build Artifact uses: actions/upload-artifact@v4 with: name: DwC-A_dotnet.nupkg - path: src/DWC_A/bin/Release/DwC-A_dotnet.*.nupkg + path: src/DWC_A/bin/Release/DwC-A_dotnet.*.nupkg + - name: Upload Test Report + uses: actions/upload-artifact@v4 + with: + name: TestReport.html + path: src/Tests/bin/**/TestReport.html diff --git a/src/Tests/ArchiveReaderTests.cs b/src/Tests/ArchiveReaderTests.cs index 6c737f7..3aaa69a 100644 --- a/src/Tests/ArchiveReaderTests.cs +++ b/src/Tests/ArchiveReaderTests.cs @@ -16,46 +16,42 @@ public class ArchiveReaderTests [Fact] public void ShouldOpenCoreFile() { - using (var archive = new ArchiveReader(archiveFileName)) + using var archive = new ArchiveReader(archiveFileName); + foreach (var row in archive.CoreFile.Rows) { - foreach(var row in archive.CoreFile.Rows) - { - Assert.NotNull(row[0]); - Assert.NotNull(row["id"]); - } + Assert.NotNull(row[0]); + Assert.NotNull(row["id"]); } } [Fact] public async Task ShouldOpenCoreFileAsync() { - using(var archive = new ArchiveReader(archiveFileName)) + using var archive = new ArchiveReader(archiveFileName); + await foreach (var row in archive + .GetAsyncCoreFile() + .GetDataRowsAsync(TestContext.Current.CancellationToken)) { - await foreach(var row in archive.GetAsyncCoreFile().GetDataRowsAsync()) - { - Assert.NotNull(row[0]); - } + Assert.NotNull(row[0]); } } [Fact] public void ShouldReturnDescriptionExtensionFile() { - using (var archive = new ArchiveReader(archiveFileName)) - { - var descriptionFile = archive.Extensions.GetFileReaderByFileName("description.txt"); - Assert.NotEmpty(descriptionFile.Rows); - } + using var archive = new ArchiveReader(archiveFileName); + var descriptionFile = archive.Extensions.GetFileReaderByFileName("description.txt"); + Assert.NotEmpty(descriptionFile.Rows); } [Fact] public async Task ShouldReturnDescriptionExtensionFileAsync() { - using (var archive = new ArchiveReader(archiveFileName)) - { - var descriptionFile = archive.Extensions.GetAsyncFileReaderByFileName("description.txt"); - Assert.NotEmpty(await descriptionFile.GetDataRowsAsync().ToArrayAsync()); - } + using var archive = new ArchiveReader(archiveFileName); + var descriptionFile = archive.Extensions.GetAsyncFileReaderByFileName("description.txt"); + Assert.NotEmpty(await descriptionFile + .GetDataRowsAsync(TestContext.Current.CancellationToken) + .ToArrayAsync(TestContext.Current.CancellationToken)); } [Fact] @@ -75,12 +71,10 @@ public void ShouldNotThrowOnDispose() [Fact] public void ShouldOpenWhalesArchive() { - using (var whales = new ArchiveReader(whalesArchiveFileName)) - { - Assert.NotEmpty(whales.CoreFile.Rows); - Assert.Empty(whales.CoreFile.FileMetaData.Fields); - Assert.Equal(4, whales.CoreFile.DataRows.Count()); - } + using var whales = new ArchiveReader(whalesArchiveFileName); + Assert.NotEmpty(whales.CoreFile.Rows); + Assert.Empty(whales.CoreFile.FileMetaData.Fields); + Assert.Equal(4, whales.CoreFile.DataRows.Count()); } [Fact] diff --git a/src/Tests/ArchiveWriterTests.cs b/src/Tests/ArchiveWriterTests.cs index 58c6f43..2011e68 100644 --- a/src/Tests/ArchiveWriterTests.cs +++ b/src/Tests/ArchiveWriterTests.cs @@ -8,19 +8,13 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -using Xunit; namespace Tests { [Collection("ArchiveWriterCollection")] - public class ArchiveWriterTests + public class ArchiveWriterTests(ArchiveWriterFixture fixture) { - private readonly ArchiveWriterFixture fixture; - - public ArchiveWriterTests(ArchiveWriterFixture fixture) - { - this.fixture = fixture; - } + private readonly ArchiveWriterFixture fixture = fixture; [Fact] public void ShouldBuildArchiveFromExistingFile() @@ -46,19 +40,17 @@ public void ShouldBuildArchiveFromExistingFile() Assert.True(File.Exists("whales.zip")); - using(var archive = new ArchiveReader("whales.zip")) - { - var whales = archive.CoreFile - .DataRows - .Select(n => n[Terms.vernacularName]); - Assert.Equal(new[] { "sperm whale", "cachalot", "gray whale" }, whales); - } + using var archive = new ArchiveReader("whales.zip"); + var whales = archive.CoreFile + .DataRows + .Select(n => n[Terms.vernacularName]); + Assert.Equal(["sperm whale", "cachalot", "gray whale"], whales); } [Fact] public async Task ShouldBuildArchive() { - var context = BuilderContext.Default; + var context = new BuilderContext(Guid.NewGuid().ToString(), true); var occurrences = await fixture.GetOccurrencesAsync(); var occurrenceMetaDataBuilder = fixture.OccurrenceFieldsMetaDataBuilder; @@ -98,7 +90,7 @@ public async Task ShouldBuildArchive() [Fact] public async Task ShouldBuildCoreFile() { - var context = BuilderContext.Default; + var context = new BuilderContext(Guid.NewGuid().ToString(), true); var occurrences = await fixture.GetOccurrencesAsync(); var occurrenceMetaDataBuilder = fixture.OccurrenceFieldsMetaDataBuilder; @@ -121,7 +113,7 @@ public async Task ShouldBuildCoreFile() [Fact] public async Task ShouldBuildCoreFileWithCustomHeader() { - var context = BuilderContext.Default; + var context = new BuilderContext(Guid.NewGuid().ToString(), true); var occurrences = await fixture.GetOccurrencesAsync(); var occurrenceMetaDataBuilder = fixture.OccurrenceFieldsMetaDataBuilder; @@ -146,7 +138,7 @@ public async Task ShouldBuildCoreFileWithCustomHeader() [Fact] public async Task FileBuilderShouldThrowOnWrongNumberOfHeaderLines() { - var context = BuilderContext.Default; + var context = new BuilderContext(Guid.NewGuid().ToString(), true); var occurrences = await fixture.GetOccurrencesAsync(); var occurrenceMetaDataBuilder = fixture.OccurrenceFieldsMetaDataBuilder; diff --git a/src/Tests/FieldMetadataTests.cs b/src/Tests/FieldMetadataTests.cs index 9029a4f..1f63c91 100644 --- a/src/Tests/FieldMetadataTests.cs +++ b/src/Tests/FieldMetadataTests.cs @@ -9,8 +9,8 @@ namespace Tests { public class FieldMetadataTests { - readonly ICollection fieldTypes = new List() - { + readonly ICollection fieldTypes = + [ new FieldType() { Index = 1, @@ -29,9 +29,9 @@ public class FieldMetadataTests Term = Terms.accessRights, Default = "Default Value" } - }; + ]; - IFieldMetaData fieldMetaData; + FieldMetaData fieldMetaData; [Fact] public void ShouldReturnNegativeOneOnNotFound() @@ -57,7 +57,7 @@ public void ShouldReturnCount3WithId() IndexSpecified = true }; fieldMetaData = new FieldMetaData( idField, fieldTypes); - Assert.Equal(4, fieldMetaData.Count()); + Assert.Equal(4, fieldMetaData.Length); } [Fact] @@ -70,7 +70,7 @@ public void ShouldNotCreateDuplicateIdFields() IndexSpecified = true }; fieldMetaData = new FieldMetaData(idField, fieldTypes); - Assert.Equal(3, fieldMetaData.Count()); + Assert.Equal(3, fieldMetaData.Length); } [Fact] @@ -96,7 +96,7 @@ public void ShouldReturnListOfDefaultFields() IndexSpecified = true }; fieldMetaData = new FieldMetaData(idField, fieldTypes); - Assert.NotEmpty(fieldMetaData.Where(n => !n.IndexSpecified)); + Assert.Contains(Terms.accessRights, fieldMetaData.SingleOrDefault(n => !n.IndexSpecified).Term); } [Fact] @@ -109,7 +109,7 @@ public void ShouldEnumerateAllFields() IndexSpecified = true }; fieldMetaData = new FieldMetaData(idField, fieldTypes); - Assert.Equal(4, fieldMetaData.Count()); + Assert.Equal(4, fieldMetaData.Length); } } } diff --git a/src/Tests/FileReaderTests.cs b/src/Tests/FileReaderTests.cs index 15dc4c7..2031267 100644 --- a/src/Tests/FileReaderTests.cs +++ b/src/Tests/FileReaderTests.cs @@ -16,10 +16,9 @@ namespace Tests public class FileReaderTests { private const string fileName = "./resources/dwca-vascan-v37.5/taxon.txt"; - private const string resourceRelationShipFileName = "./resources/dwca-vascan-v37.5/resourcerelationship.txt"; private readonly IRowFactory rowFactory; private readonly ITokenizer tokenizer; - private readonly Mock fileMetaDataMock = new Mock(); + private readonly Mock fileMetaDataMock = new(); public FileReaderTests() { @@ -51,7 +50,9 @@ public async Task ShouldEnumerateFileAsync() var fileReader = new FileReader(fileName, rowFactory, tokenizer, fileMetaDataMock.Object, new FileReaderConfiguration()); - Assert.NotEmpty(await fileReader.GetDataRowsAsync().ToArrayAsync()); + Assert.NotEmpty(await fileReader + .GetDataRowsAsync(TestContext.Current.CancellationToken) + .ToArrayAsync(TestContext.Current.CancellationToken)); } [Fact] @@ -71,7 +72,9 @@ public async Task ShouldReturnHeaderRowAsync() var fileReader = new FileReader(fileName, rowFactory, tokenizer, fileMetaDataMock.Object, new FileReaderConfiguration()); - Assert.Single(await fileReader.GetHeaderRowsAsync().ToArrayAsync()); + Assert.Single(await fileReader + .GetHeaderRowsAsync(TestContext.Current.CancellationToken) + .ToArrayAsync(TestContext.Current.CancellationToken)); } [Fact] @@ -91,7 +94,9 @@ public async Task ShouldReturnDataRowsAsync() var fileReader = new FileReader(fileName, rowFactory, tokenizer, fileMetaDataMock.Object, new FileReaderConfiguration()); - Assert.NotEmpty(await fileReader.GetDataRowsAsync().ToArrayAsync()); + Assert.NotEmpty(await fileReader + .GetDataRowsAsync(TestContext.Current.CancellationToken) + .ToArrayAsync(TestContext.Current.CancellationToken)); } [Fact] @@ -104,7 +109,9 @@ public async Task ShouldThrowOnCancellation() var ct = new CancellationToken(true); await Assert.ThrowsAsync(async () => { - await fileReader.GetDataRowsAsync(ct).ToArrayAsync(); + await fileReader + .GetDataRowsAsync(ct) + .ToArrayAsync(TestContext.Current.CancellationToken); }); } @@ -126,8 +133,12 @@ public async Task ShouldSeekToBeginningForHeaderRowsAsync() var fileReader = new FileReader(fileName, rowFactory, tokenizer, fileMetaDataMock.Object, new FileReaderConfiguration()); - Assert.NotEmpty(await fileReader.GetDataRowsAsync().ToArrayAsync()); - Assert.NotEmpty(await fileReader.GetHeaderRowsAsync().ToArrayAsync()); + Assert.NotEmpty(await fileReader + .GetDataRowsAsync(TestContext.Current.CancellationToken) + .ToArrayAsync(TestContext.Current.CancellationToken)); + Assert.NotEmpty(await fileReader + .GetHeaderRowsAsync(TestContext.Current.CancellationToken) + .ToArrayAsync(TestContext.Current.CancellationToken)); } } } diff --git a/src/Tests/StreamEnumeratorTests.cs b/src/Tests/StreamEnumeratorTests.cs index 938f30f..0dcaee3 100644 --- a/src/Tests/StreamEnumeratorTests.cs +++ b/src/Tests/StreamEnumeratorTests.cs @@ -18,8 +18,8 @@ public class StreamEnumeratorTests private readonly IRowFactory rowFactory; private readonly ITokenizer tokenizer; - Mock fileMetaDataMock = new Mock(); - Mock fileAttributesMock = new Mock(); + readonly Mock fileMetaDataMock = new(); + readonly Mock fileAttributesMock = new(); public StreamEnumeratorTests() { @@ -38,36 +38,28 @@ public StreamEnumeratorTests() tokenizer = new Tokenizer(fileMetaDataMock.Object); } - ICollection fieldTypes = new FieldType[] - { - - - }; - [Fact] public void ShouldEnumerateFile() { - using (Stream stream = new FileStream(fileName, FileMode.Open)) - { - var streamEnumerator = new DwC_A.StreamReader( - rowFactory, - tokenizer, - fileMetaDataMock.Object); - Assert.NotEmpty(streamEnumerator.ReadRows(stream).ToArray()); - } + using Stream stream = new FileStream(fileName, FileMode.Open); + var streamEnumerator = new DwC_A.StreamReader( + rowFactory, + tokenizer, + fileMetaDataMock.Object); + Assert.NotEmpty(streamEnumerator.ReadRows(stream).ToArray()); } [Fact] public async Task ShouldEnumerateFileAsync() { - using (Stream stream = new FileStream(fileName, FileMode.Open)) - { - var streamEnumerator = new DwC_A.StreamReader( - rowFactory, - tokenizer, - fileMetaDataMock.Object); - Assert.NotEmpty(await streamEnumerator.ReadRowsAsync(stream).ToArrayAsync()); - } + using Stream stream = new FileStream(fileName, FileMode.Open); + var streamEnumerator = new DwC_A.StreamReader( + rowFactory, + tokenizer, + fileMetaDataMock.Object); + Assert.NotEmpty(await streamEnumerator + .ReadRowsAsync(stream, TestContext.Current.CancellationToken) + .ToArrayAsync(TestContext.Current.CancellationToken)); } } diff --git a/src/Tests/StreamReaderTests.cs b/src/Tests/StreamReaderTests.cs index f33f250..d44d5dc 100644 --- a/src/Tests/StreamReaderTests.cs +++ b/src/Tests/StreamReaderTests.cs @@ -3,6 +3,7 @@ using Moq; using System.IO; using System.Text; +using System.Threading.Tasks; using Xunit; namespace Tests @@ -36,7 +37,7 @@ public void ShouldReadStreamWithQuotes() } [Fact] - public async void ShouldReadStreamWithQuotesAsync() + public async Task ShouldReadStreamWithQuotesAsync() { using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(data))) { diff --git a/src/Tests/Tests.csproj b/src/Tests/Tests.csproj index 8ab6d05..821699b 100644 --- a/src/Tests/Tests.csproj +++ b/src/Tests/Tests.csproj @@ -3,6 +3,8 @@ net9.0 Debug;Release;LocalRelease + true + true @@ -17,7 +19,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive @@ -73,4 +75,8 @@ + + + +