diff --git a/EconDataLens.Tests/EtlRepositoryTests/AreaEtlRepositoryTests.cs b/EconDataLens.Tests/EtlRepositoryTests/AreaEtlRepositoryTests.cs index 8264ff4..5622318 100644 --- a/EconDataLens.Tests/EtlRepositoryTests/AreaEtlRepositoryTests.cs +++ b/EconDataLens.Tests/EtlRepositoryTests/AreaEtlRepositoryTests.cs @@ -1,4 +1,3 @@ -using EconDataLens.Core.Entities.Cpi; using EconDataLens.Core.Interfaces; using EconDataLens.Core.Configuration; using EconDataLens.Services; @@ -14,7 +13,6 @@ public class AreaEtlRepositoryTests private EconDataLensDbContext _dbContext = null!; private ICpiDataFileParser _parser = null!; private ICpiIngestionRepository _repository = null!; - private ICpiIngestionService _service = null!; private string _connectionString = string.Empty; [SetUp] @@ -61,13 +59,13 @@ public async Task UpsertCpiAreaAsync_NewAreas_InsertsSuccessfully() var sample = await _dbContext.CpiArea.FirstOrDefaultAsync(a => a.AreaCode == "0000"); - Assert.That(sample, Is.Not.Null); - Assert.That(sample.AreaName, Is.EqualTo("U.S. city average")); + Assert.That(sample!, Is.Not.Null); + Assert.That(sample!.AreaName, Is.EqualTo("U.S. city average")); sample = await _dbContext.CpiArea.FirstOrDefaultAsync(a => a.AreaCode == "S49G"); Assert.That(sample, Is.Not.Null); - Assert.That(sample.AreaName, Is.EqualTo("Urban Alaska")); + Assert.That(sample!.AreaName, Is.EqualTo("Urban Alaska")); } @@ -85,13 +83,13 @@ public async Task UpsertCpiAreaAsync_ExistingAreas_UpdatesSuccessfully() var sample = await _dbContext.CpiArea.FirstOrDefaultAsync(a => a.AreaCode == "0000"); - Assert.That(sample, Is.Not.Null); - Assert.That(sample.AreaName, Is.EqualTo("U.S. city average")); + Assert.That(sample!, Is.Not.Null); + Assert.That(sample!.AreaName, Is.EqualTo("U.S. city average")); sample = await _dbContext.CpiArea.FirstOrDefaultAsync(a => a.AreaCode == "S49G"); Assert.That(sample, Is.Not.Null); - Assert.That(sample.AreaName, Is.EqualTo("Urban Alaska")); + Assert.That(sample!.AreaName, Is.EqualTo("Urban Alaska")); // Ingest modified data @@ -105,8 +103,8 @@ public async Task UpsertCpiAreaAsync_ExistingAreas_UpdatesSuccessfully() sample = await _dbContext.CpiArea.FirstOrDefaultAsync(a => a.AreaCode == "S49G"); - Assert.That(sample, Is.Not.Null); - Assert.That(sample.AreaName, Is.EqualTo("Urban Alaska UPDATED")); + Assert.That(sample!, Is.Not.Null); + Assert.That(sample!.AreaName, Is.EqualTo("Urban Alaska UPDATED")); } [TearDown] diff --git a/EconDataLens.Tests/EtlRepositoryTests/ItemEtlRepositoryTests.cs b/EconDataLens.Tests/EtlRepositoryTests/ItemEtlRepositoryTests.cs index aefaa99..fb6f093 100644 --- a/EconDataLens.Tests/EtlRepositoryTests/ItemEtlRepositoryTests.cs +++ b/EconDataLens.Tests/EtlRepositoryTests/ItemEtlRepositoryTests.cs @@ -1,4 +1,3 @@ -using EconDataLens.Core.Entities.Cpi; using EconDataLens.Core.Interfaces; using EconDataLens.Core.Configuration; using EconDataLens.Services; @@ -14,7 +13,6 @@ public class ItemEtlRepositoryTests private EconDataLensDbContext _dbContext = null!; private ICpiDataFileParser _parser = null!; private ICpiIngestionRepository _repository = null!; - private ICpiIngestionService _service = null!; private string _connectionString = string.Empty; [SetUp] @@ -62,12 +60,12 @@ public async Task UpsertCpiItemsAsync_NewItems_InsertsSuccessfully() var sample = await _dbContext.CpiItem.FirstOrDefaultAsync(i => i.ItemCode == "AA0"); Assert.That(sample, Is.Not.Null); - Assert.That(sample.ItemName, Is.EqualTo("All items - old base")); + Assert.That(sample!.ItemName, Is.EqualTo("All items - old base")); sample = await _dbContext.CpiItem.FirstOrDefaultAsync(i => i.ItemCode == "SSHJ031"); Assert.That(sample, Is.Not.Null); - Assert.That(sample.ItemName, Is.EqualTo("Infants' furniture")); + Assert.That(sample!.ItemName, Is.EqualTo("Infants' furniture")); } @@ -86,12 +84,12 @@ public async Task UpsertCpiItemsAsync_ExistingItems_UpdatesSuccessfully() var sample = await _dbContext.CpiItem.FirstOrDefaultAsync(i => i.ItemCode == "AA0"); Assert.That(sample, Is.Not.Null); - Assert.That(sample.ItemName, Is.EqualTo("All items - old base")); + Assert.That(sample!.ItemName, Is.EqualTo("All items - old base")); sample = await _dbContext.CpiItem.FirstOrDefaultAsync(i => i.ItemCode == "SSHJ031"); Assert.That(sample, Is.Not.Null); - Assert.That(sample.ItemName, Is.EqualTo("Infants' furniture")); + Assert.That(sample!.ItemName, Is.EqualTo("Infants' furniture")); // Ingest modified data @@ -106,7 +104,7 @@ public async Task UpsertCpiItemsAsync_ExistingItems_UpdatesSuccessfully() sample = await _dbContext.CpiItem.FirstOrDefaultAsync(i => i.ItemCode == "SSHJ031"); Assert.That(sample, Is.Not.Null); - Assert.That(sample.ItemName, Is.EqualTo("Infants' furniture UPDATED")); + Assert.That(sample!.ItemName, Is.EqualTo("Infants' furniture UPDATED")); } [TearDown] diff --git a/EconDataLens.Tests/EtlRepositoryTests/PeriodEtlRepositoryTests.cs b/EconDataLens.Tests/EtlRepositoryTests/PeriodEtlRepositoryTests.cs new file mode 100644 index 0000000..dc60e27 --- /dev/null +++ b/EconDataLens.Tests/EtlRepositoryTests/PeriodEtlRepositoryTests.cs @@ -0,0 +1,114 @@ +using EconDataLens.Core.Interfaces; +using EconDataLens.Core.Configuration; +using EconDataLens.Services; +using EconDataLens.Data; +using EconDataLens.Repositories; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Options; + +namespace EconDataLens.Tests.EtlRepositoryTests; + +public class PeriodEtlRepositoryTests +{ + private EconDataLensDbContext _dbContext = null!; + private ICpiDataFileParser _parser = null!; + private ICpiIngestionRepository _repository = null!; + private string _connectionString = string.Empty; + + [SetUp] + public async Task SetUp() + { + _connectionString = PostgresFixture.ConnectionString; + var opts = new DbContextOptionsBuilder() + .UseNpgsql(_connectionString) + .UseSnakeCaseNamingConvention() + .Options; + + await DbReset.RecreateDatabaseAsync(_connectionString); + + _dbContext = new EconDataLensDbContext(opts); + + var blsOptions = Options.Create(new BlsOptions + { + Cpi = new CpiOptions + { + PeriodFile = "cu.period.sample" + } + }); + + var downloadOptions = Options.Create(new DownloadOptions + { + DownloadDirectory = "TestData/EtlData", + DeleteDownloadedFiles = false + }); + + _parser = new CpiDataFileParser(blsOptions, downloadOptions); + _repository = new CpiIngestionRepository(_dbContext); + } + + [Test] + public async Task UpsertCpiPeriodsAsync_NewPeriods_InsertsSuccessfully() + { + var path = Path.Combine(TestContext.CurrentContext.TestDirectory, "TestData", "EtlData", "cu.period.sample"); + + await _repository.UpsertCpiPeriodAsync(_parser.ParseCpiPeriodsAsync(path)); + + var count = await _dbContext.CpiPeriod.CountAsync(); + + Assert.That(count, Is.EqualTo(16)); + + var sample = await _dbContext.CpiPeriod.FirstOrDefaultAsync(p => p.Period == "M01"); + + Assert.Multiple(() => + { + Assert.That(sample, Is.Not.Null); + Assert.That(sample!.PeriodName, Is.EqualTo("January")); + Assert.That(sample!.PeriodAbbreviation, Is.EqualTo("JAN")); + }); + } + + [Test] + public async Task UpsertCpiPeriodsAsync_ExistingPeriods_UpdatesSuccessfully() + { + var path = Path.Combine(TestContext.CurrentContext.TestDirectory, "TestData", "EtlData", "cu.period.sample"); + + await _repository.UpsertCpiPeriodAsync(_parser.ParseCpiPeriodsAsync(path)); + + var count = await _dbContext.CpiPeriod.CountAsync(); + + Assert.That(count, Is.EqualTo(16)); + + var sample = await _dbContext.CpiPeriod.FirstOrDefaultAsync(p => p.Period == "M01"); + + Assert.Multiple(() => + { + Assert.That(sample, Is.Not.Null); + Assert.That(sample!.PeriodName, Is.EqualTo("January")); + Assert.That(sample!.PeriodAbbreviation, Is.EqualTo("JAN")); + }); + + path = Path.Combine(TestContext.CurrentContext.TestDirectory, "TestData", "EtlData", "cu.period.modified.sample"); + await _repository.UpsertCpiPeriodAsync(_parser.ParseCpiPeriodsAsync(path)); + + count = await _dbContext.CpiPeriod.CountAsync(); + + Assert.That(count, Is.EqualTo(16)); + + sample = await _dbContext.CpiPeriod.FirstOrDefaultAsync(p => p.Period == "M01"); + + Assert.Multiple(() => + { + Assert.That(sample, Is.Not.Null); + Assert.That(sample!.PeriodName, Is.EqualTo("JanuaryM")); + Assert.That(sample!.PeriodAbbreviation, Is.EqualTo("MOD")); + }); + + } + + [TearDown] + public async Task TearDown() + { + await _dbContext.DisposeAsync(); + } + +} diff --git a/EconDataLens.Tests/TestData/EtlData/cu.period.modified.sample b/EconDataLens.Tests/TestData/EtlData/cu.period.modified.sample new file mode 100644 index 0000000..7fa235f --- /dev/null +++ b/EconDataLens.Tests/TestData/EtlData/cu.period.modified.sample @@ -0,0 +1,17 @@ +period period_abbr period_name +M01 MOD JanuaryM +M02 FEB February +M03 MAR March +M04 APR April +M05 MAY May +M06 JUN June +M07 JUL July +M08 AUG August +M09 SEP September +M10 OCT October +M11 NOV November +M12 DEC December +M13 AN AV Annual Average +S01 HALF1 First Half +S02 HALF2 Second Half +S03 AN AV Annual Average