From 2dd3570430c8596dc35df4313fa7008dabf325fb Mon Sep 17 00:00:00 2001 From: Justin Simon Date: Sun, 28 Sep 2025 20:55:14 -0400 Subject: [PATCH] Implement tests for period file parsing --- EconDataLens.Tests/Fixtures/cu.period.empty | 1 + EconDataLens.Tests/Fixtures/cu.period.sample | 3 + .../Services/CpiPeriodParserTests.cs | 70 +++++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 EconDataLens.Tests/Fixtures/cu.period.empty create mode 100644 EconDataLens.Tests/Fixtures/cu.period.sample create mode 100644 EconDataLens.Tests/Services/CpiPeriodParserTests.cs diff --git a/EconDataLens.Tests/Fixtures/cu.period.empty b/EconDataLens.Tests/Fixtures/cu.period.empty new file mode 100644 index 0000000..24204c7 --- /dev/null +++ b/EconDataLens.Tests/Fixtures/cu.period.empty @@ -0,0 +1 @@ +period period_abbr period_name \ No newline at end of file diff --git a/EconDataLens.Tests/Fixtures/cu.period.sample b/EconDataLens.Tests/Fixtures/cu.period.sample new file mode 100644 index 0000000..ab2b491 --- /dev/null +++ b/EconDataLens.Tests/Fixtures/cu.period.sample @@ -0,0 +1,3 @@ +period period_abbr period_name +M01 JAN January +S03 AN AV Annual Average diff --git a/EconDataLens.Tests/Services/CpiPeriodParserTests.cs b/EconDataLens.Tests/Services/CpiPeriodParserTests.cs new file mode 100644 index 0000000..026e67d --- /dev/null +++ b/EconDataLens.Tests/Services/CpiPeriodParserTests.cs @@ -0,0 +1,70 @@ +using EconDataLens.Core.Configuration; +using EconDataLens.Core.Interfaces; +using EconDataLens.Core.Entities.Cpi; +using EconDataLens.Services; +using Microsoft.Extensions.Options; + +namespace EconDataLens.Tests.Services; + +public class CpiPeriodParserTests +{ + private ICpiDataFileParser _parser; + + [SetUp] + public void SetUp() + { + // This isn't actually used in the test, but the parser requires it. + var blsOptions = Options.Create(new BlsOptions + { + Cpi = new CpiOptions + { + FootnoteFile = "cu.footnote" + } + }); + + // This isn't actually used in the test, but the parser requires it. + var downloadOptions = Options.Create(new DownloadOptions + { + DownloadDirectory = "downloads", + DeleteDownloadedFiles = false + }); + + _parser = new CpiDataFileParser(blsOptions, downloadOptions); + } + + [Test] + public async Task ParseCpiPeriodAsync_HeaderOnly_YieldsNoResults() + { + var path = Path.Combine(TestContext.CurrentContext.TestDirectory, "Fixtures", "cu.period.empty"); + var rows = new List(); + + await foreach (var row in _parser.ParseCpiPeriodsAsync(path)) + rows.Add(row); + + Assert.That(rows, Is.Empty); + } + + [Test] + public async Task ParseCpiPeriodAsync_FileWithRecords_YieldsResults() + { + var path = Path.Combine(TestContext.CurrentContext.TestDirectory, "Fixtures", "cu.period.sample"); + var rows = new List(); + + await foreach (var row in _parser.ParseCpiPeriodsAsync(path)) + rows.Add(row); + + Assert.That(rows, Has.Count.EqualTo(2)); + + Assert.Multiple(() => + { + Assert.That(rows[0].Period, Is.EqualTo("M01")); + Assert.That(rows[0].PeriodAbbreviation, Is.EqualTo("JAN")); + Assert.That(rows[0].PeriodName, Is.EqualTo("January")); + + Assert.That(rows[1].Period, Is.EqualTo("S03")); + Assert.That(rows[1].PeriodAbbreviation, Is.EqualTo("AN AV")); + Assert.That(rows[1].PeriodName, Is.EqualTo("Annual Average")); + }); + } + +} \ No newline at end of file