Skip to content

Commit

Permalink
fix: ensure nace is filtered correctly (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
SondreJDigdir authored Nov 15, 2024
1 parent 2f90466 commit 26e1a36
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 16 deletions.
19 changes: 16 additions & 3 deletions src/Dan.Plugin.Tilda/Extensions/BrEntityRegisterEntryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
7 changes: 6 additions & 1 deletion test/Dan.Plugin.Tilda.Test/Dan.Plugin.Tilda.Test.csproj
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.12.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="Moq" Version="4.17.2" />
<PackageReference Include="xunit" Version="2.4.1" />
Expand All @@ -21,4 +22,8 @@
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Dan.Plugin.Tilda\Dan.Plugin.Tilda.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -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);
}
}
2 changes: 2 additions & 0 deletions test/Dan.Plugin.Tilda.Test/Globals.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
global using Xunit;
global using FluentAssertions;
12 changes: 0 additions & 12 deletions test/Dan.Plugin.Tilda.Test/UnitTest1.cs

This file was deleted.

0 comments on commit 26e1a36

Please sign in to comment.