From 26e1a3614a1cf5480b34a07b6ac6925f5289357b Mon Sep 17 00:00:00 2001 From: SondreJDigdir Date: Fri, 15 Nov 2024 13:43:24 +0100 Subject: [PATCH] fix: ensure nace is filtered correctly (#15) --- .../BrEntityRegisterEntryExtensions.cs | 19 +++- .../Dan.Plugin.Tilda.Test.csproj | 7 +- .../BrEntityRegisterEntryExtensionsTests.cs | 92 +++++++++++++++++++ test/Dan.Plugin.Tilda.Test/Globals.cs | 2 + test/Dan.Plugin.Tilda.Test/UnitTest1.cs | 12 --- 5 files changed, 116 insertions(+), 16 deletions(-) create mode 100644 test/Dan.Plugin.Tilda.Test/Extensions/BrEntityRegisterEntryExtensionsTests.cs create mode 100644 test/Dan.Plugin.Tilda.Test/Globals.cs delete mode 100644 test/Dan.Plugin.Tilda.Test/UnitTest1.cs diff --git a/src/Dan.Plugin.Tilda/Extensions/BrEntityRegisterEntryExtensions.cs b/src/Dan.Plugin.Tilda/Extensions/BrEntityRegisterEntryExtensions.cs index 8e83680..5cc6537 100644 --- a/src/Dan.Plugin.Tilda/Extensions/BrEntityRegisterEntryExtensions.cs +++ b/src/Dan.Plugin.Tilda/Extensions/BrEntityRegisterEntryExtensions.cs @@ -23,10 +23,23 @@ public static bool MatchesFilterParameters(this BREntityRegisterEntry brEntity, return false; } + // Need to check if any of the potential up to 3 nace codes match + // 1. If nace-parameter is null, condition breaks and we don't check it. + // 2. Since nace is not null, we need to find at least one match out of three codes: + // a. If code is null, we don't check for match and check the next code + // b. If code does not start with nace, we check the next code + // 3. If having gone through all codes and not found match, will return false, otherwise + // go on to next checks (currently no further checks at the time of writing, but keeping it this + // way for future extensibility) + // See https://www.ssb.no/klass/klassifikasjoner/6 for explanation of nace, and why we check for starts with + // instead of equals. + var code1 = brEntity?.Naeringskode1?.Kode; + var code2 = brEntity?.Naeringskode2?.Kode; + var code3 = brEntity?.Naeringskode3?.Kode; if (tildaParameters.nace != null && - (brEntity.Naeringskode1?.Kode != tildaParameters.nace || - brEntity.Naeringskode2?.Kode != tildaParameters.nace || - brEntity.Naeringskode3?.Kode != tildaParameters.nace)) + (code1 == null || !code1.StartsWith(tildaParameters.nace)) && + (code2 == null || !code2.StartsWith(tildaParameters.nace)) && + (code3 == null || !code3.StartsWith(tildaParameters.nace))) { return false; } diff --git a/test/Dan.Plugin.Tilda.Test/Dan.Plugin.Tilda.Test.csproj b/test/Dan.Plugin.Tilda.Test/Dan.Plugin.Tilda.Test.csproj index 2eb5620..fe327ab 100644 --- a/test/Dan.Plugin.Tilda.Test/Dan.Plugin.Tilda.Test.csproj +++ b/test/Dan.Plugin.Tilda.Test/Dan.Plugin.Tilda.Test.csproj @@ -1,13 +1,14 @@ - net6.0 + net8.0 enable false + @@ -21,4 +22,8 @@ + + + + diff --git a/test/Dan.Plugin.Tilda.Test/Extensions/BrEntityRegisterEntryExtensionsTests.cs b/test/Dan.Plugin.Tilda.Test/Extensions/BrEntityRegisterEntryExtensionsTests.cs new file mode 100644 index 0000000..b3abd06 --- /dev/null +++ b/test/Dan.Plugin.Tilda.Test/Extensions/BrEntityRegisterEntryExtensionsTests.cs @@ -0,0 +1,92 @@ +using Dan.Plugin.Tilda.Extensions; +using Dan.Plugin.Tilda.Models; + +namespace Dan.Plugin.Tilda.Test.Extensions; + +public class BrEntityRegisterEntryExtensionsTests +{ + [Fact] + public void MatchesFilterParameters_ParametersIsNull_ReturnTrue() + { + // Arrange + TildaParameters? tildaParameters = null; + + var brEntity = new BREntityRegisterEntry(); + + // Act + var actual = brEntity.MatchesFilterParameters(tildaParameters); + + // Assert + actual.Should().BeTrue(); + } + + [Fact] + public void MatchesFilterParameters_ParametersAreNull_ReturnTrue() + { + // Arrange + var tildaParameters = new TildaParameters( + fromDate: null, + toDate: null, + npdid: null, + includeSubunits: null, + sourceFilter: null, + identifier: null, + filter: null, + year: null, + month: null, + postcode: null, + municipalityNumber: null, + nace: null + ); + + var brEntity = new BREntityRegisterEntry(); + + // Act + var actual = brEntity.MatchesFilterParameters(tildaParameters); + + // Assert + actual.Should().BeTrue(); + } + + [Theory] + [InlineData("nace",null,null,null,false)] + [InlineData("nace","nope",null,null,false)] + [InlineData("nace","nope","nope","nope",false)] + [InlineData("nace","nace",null,null,true)] + [InlineData("nace","nace","nope","nope",true)] + [InlineData("nace","nace","nace","nace",true)] + [InlineData("nace","nacee","nacee","nacee",true)] + [InlineData("nacee","nace","nace","nace",false)] + public void MatchesFilterParameters_Theory( + string nace, string code1, string code2, string code3, bool expected) + { + // Arrange + var tildaParameters = new TildaParameters( + fromDate: null, + toDate: null, + npdid: null, + includeSubunits: null, + sourceFilter: null, + identifier: null, + filter: null, + year: null, + month: null, + postcode: null, + municipalityNumber: null, + nace: nace + ); + + var brEntity = new BREntityRegisterEntry + { + Naeringskode1 = new InstitusjonellSektorkode { Kode = code1 }, + Naeringskode2 = new InstitusjonellSektorkode { Kode = code2 }, + Naeringskode3 = new InstitusjonellSektorkode { Kode = code3 }, + }; + + // Act + var actual = brEntity.MatchesFilterParameters(tildaParameters); + + // Assert + actual.Should().Be(expected); + } +} diff --git a/test/Dan.Plugin.Tilda.Test/Globals.cs b/test/Dan.Plugin.Tilda.Test/Globals.cs new file mode 100644 index 0000000..30b9735 --- /dev/null +++ b/test/Dan.Plugin.Tilda.Test/Globals.cs @@ -0,0 +1,2 @@ +global using Xunit; +global using FluentAssertions; diff --git a/test/Dan.Plugin.Tilda.Test/UnitTest1.cs b/test/Dan.Plugin.Tilda.Test/UnitTest1.cs deleted file mode 100644 index 52c615e..0000000 --- a/test/Dan.Plugin.Tilda.Test/UnitTest1.cs +++ /dev/null @@ -1,12 +0,0 @@ -using Xunit; - -namespace Dan.Plugin.DATASOURCENAME.Test -{ - public class UnitTest1 - { - [Fact] - public void TestMethod1() - { - } - } -}