<commit_before>using System.Collections.Generic;
using System.Linq;
using Arkivverket.Arkade.Core.Addml.Definitions;
using Arkivverket.Arkade.Core.Addml.Processes;
using Arkivverket.Arkade.ExternalModels.Cpf;
using Arkivverket.Arkade.Test.Core.Addml.Builders;
using FluentAssertions;
using Xunit;
using Xunit.Abstractions;

namespace Arkivverket.Arkade.Test.Core.Addml.Definitions
{
    public class AddmlDefinitionTest
    {
        private readonly ITestOutputHelper _testOutputHelper;

        public AddmlDefinitionTest(ITestOutputHelper testOutputHelper)
        {
            _testOutputHelper = testOutputHelper;
        }

        [Fact]
        public void ShouldAddControlForeignKeyToFlatFileContainingReferencedPrimaryKey()
        {
            AddmlFlatFileDefinition personFile = new AddmlFlatFileDefinitionBuilder().WithName("personFile").Build();
            AddmlRecordDefinition personFileRecordDef = new AddmlRecordDefinitionBuilder().WithAddmlFlatFileDefinition(personFile).Build();
            AddmlFieldDefinition personPrimaryKey = new AddmlFieldDefinitionBuilder().WithRecordDefinition(personFileRecordDef)
                .WithName("personPrimaryKey")
                .IsPartOfPrimaryKey(true).Build();


            AddmlFlatFileDefinition addressFile = new AddmlFlatFileDefinitionBuilder().WithName("addressFile").Build();
            AddmlRecordDefinition addressFileRecordDef = new AddmlRecordDefinitionBuilder().WithAddmlFlatFileDefinition(addressFile).Build();
            new AddmlFieldDefinitionBuilder().WithRecordDefinition(addressFileRecordDef).WithName("addressPrimaryKey").IsPartOfPrimaryKey(true).Build();
            AddmlFieldDefinition addressForeignKey = new AddmlFieldDefinitionBuilder().WithRecordDefinition(addressFileRecordDef)
                .WithName("addressForeignKey")
                .WithForeignKey(personPrimaryKey)
                .WithProcess(ControlForeignKey.Name)
                .Build();

            AddmlFlatFileDefinition unrelatedFile = new AddmlFlatFileDefinitionBuilder().WithName("unrelatedFile").Build();
            AddmlRecordDefinition unrelatedFileRecordDef = new AddmlRecordDefinitionBuilder().WithAddmlFlatFileDefinition(unrelatedFile).Build();
            AddmlFieldDefinition unrelatedFilePrimaryKey = new AddmlFieldDefinitionBuilder().WithRecordDefinition(unrelatedFileRecordDef)
                .WithName("unrelatedFilePrimaryKey")
                .IsPartOfPrimaryKey(true).Build();


            AddmlDefinition addmlDefinition = new AddmlDefinition(new List<AddmlFlatFileDefinition>() {personFile, addressFile, unrelatedFile});

            Dictionary<IAddmlIndex, List<string>> fieldProcesses = addmlDefinition.GetFieldProcessesGroupedByField();

            _testOutputHelper.WriteLine("field processes:");
            foreach (var item in fieldProcesses)
            {
                _testOutputHelper.WriteLine(item.Key.ToString());
                if (item.Value == null || !item.Value.Any())
                {
                    _testOutputHelper.WriteLine("* [empty]");
                }
                else
                {
                    foreach (var process in item.Value)
                    {
                        _testOutputHelper.WriteLine($"* {process}");
                    }
                }
            }

            fieldProcesses[personPrimaryKey.GetIndex()].Contains(ControlForeignKey.Name).Should().BeTrue();
            fieldProcesses[addressForeignKey.GetIndex()].Contains(ControlForeignKey.Name).Should().BeTrue();
            fieldProcesses[unrelatedFilePrimaryKey.GetIndex()].Contains(ControlForeignKey.Name).Should().BeFalse();
        }

    }<commit_msg>Add extra test for ControlForeignKey handling<commit_after>using System.Collections.Generic;
using System.Linq;
using Arkivverket.Arkade.Core.Addml.Definitions;
using Arkivverket.Arkade.Core.Addml.Processes;
using Arkivverket.Arkade.ExternalModels.Cpf;
using Arkivverket.Arkade.Test.Core.Addml.Builders;
using FluentAssertions;
using Xunit;
using Xunit.Abstractions;

namespace Arkivverket.Arkade.Test.Core.Addml.Definitions
{
    public class AddmlDefinitionTest
    {
        private readonly ITestOutputHelper _testOutputHelper;

        public AddmlDefinitionTest(ITestOutputHelper testOutputHelper)
        {
            _testOutputHelper = testOutputHelper;
        }

        [Fact]
        public void ShouldAddControlForeignKeyToFlatFileContainingReferencedPrimaryKey()
        {
            AddmlFlatFileDefinition personFile = new AddmlFlatFileDefinitionBuilder().WithName("personFile").Build();
            AddmlRecordDefinition personFileRecordDef = new AddmlRecordDefinitionBuilder().WithAddmlFlatFileDefinition(personFile).Build();
            AddmlFieldDefinition personPrimaryKey = new AddmlFieldDefinitionBuilder().WithRecordDefinition(personFileRecordDef)
                .WithName("personPrimaryKey")
                .IsPartOfPrimaryKey(true).Build();


            AddmlFlatFileDefinition addressFile = new AddmlFlatFileDefinitionBuilder().WithName("addressFile").Build();
            AddmlRecordDefinition addressFileRecordDef = new AddmlRecordDefinitionBuilder().WithAddmlFlatFileDefinition(addressFile).Build();
            new AddmlFieldDefinitionBuilder().WithRecordDefinition(addressFileRecordDef).WithName("addressPrimaryKey").IsPartOfPrimaryKey(true).Build();
            AddmlFieldDefinition addressForeignKey = new AddmlFieldDefinitionBuilder().WithRecordDefinition(addressFileRecordDef)
                .WithName("addressForeignKey")
                .WithForeignKey(personPrimaryKey)
                .WithProcess(ControlForeignKey.Name)
                .Build();

            AddmlFlatFileDefinition unrelatedFile = new AddmlFlatFileDefinitionBuilder().WithName("unrelatedFile").Build();
            AddmlRecordDefinition unrelatedFileRecordDef = new AddmlRecordDefinitionBuilder().WithAddmlFlatFileDefinition(unrelatedFile).Build();
            AddmlFieldDefinition unrelatedFilePrimaryKey = new AddmlFieldDefinitionBuilder().WithRecordDefinition(unrelatedFileRecordDef)
                .WithName("unrelatedFilePrimaryKey")
                .IsPartOfPrimaryKey(true).Build();


            AddmlDefinition addmlDefinition = new AddmlDefinition(new List<AddmlFlatFileDefinition>() {personFile, addressFile, unrelatedFile});

            Dictionary<IAddmlIndex, List<string>> fieldProcesses = addmlDefinition.GetFieldProcessesGroupedByField();

            _testOutputHelper.WriteLine("field processes:");
            foreach (var item in fieldProcesses)
            {
                _testOutputHelper.WriteLine(item.Key.ToString());
                if (item.Value == null || !item.Value.Any())
                {
                    _testOutputHelper.WriteLine("* [empty]");
                }
                else
                {
                    foreach (var process in item.Value)
                    {
                        _testOutputHelper.WriteLine($"* {process}");
                    }
                }
            }

            fieldProcesses[personPrimaryKey.GetIndex()].Contains(ControlForeignKey.Name).Should().BeTrue();
            fieldProcesses[addressForeignKey.GetIndex()].Contains(ControlForeignKey.Name).Should().BeTrue();
            fieldProcesses[unrelatedFilePrimaryKey.GetIndex()].Contains(ControlForeignKey.Name).Should().BeFalse();
        }

        [Fact]
        public void ShouldNotAddControlForeignKeyProcessWhenNotDefinedInAnyFieldDefinition()
        {
            AddmlFlatFileDefinition personFile = new AddmlFlatFileDefinitionBuilder().WithName("personFile").Build();
            AddmlRecordDefinition personFileRecordDef = new AddmlRecordDefinitionBuilder().WithAddmlFlatFileDefinition(personFile).Build();
            AddmlFieldDefinition personPrimaryKey = new AddmlFieldDefinitionBuilder().WithRecordDefinition(personFileRecordDef)
                .WithName("personPrimaryKey")
                .IsPartOfPrimaryKey(true).Build();


            AddmlFlatFileDefinition addressFile = new AddmlFlatFileDefinitionBuilder().WithName("addressFile").Build();
            AddmlRecordDefinition addressFileRecordDef = new AddmlRecordDefinitionBuilder().WithAddmlFlatFileDefinition(addressFile).Build();
            new AddmlFieldDefinitionBuilder().WithRecordDefinition(addressFileRecordDef).WithName("addressPrimaryKey").IsPartOfPrimaryKey(true).Build();
            AddmlFieldDefinition addressForeignKey = new AddmlFieldDefinitionBuilder().WithRecordDefinition(addressFileRecordDef)
                .WithName("addressForeignKey")
                .WithForeignKey(personPrimaryKey)
                .Build();

            AddmlDefinition addmlDefinition = new AddmlDefinition(new List<AddmlFlatFileDefinition>() { personFile, addressFile });

            Dictionary<IAddmlIndex, List<string>> fieldProcesses = addmlDefinition.GetFieldProcessesGroupedByField();

            _testOutputHelper.WriteLine("field processes:");
            foreach (var item in fieldProcesses)
            {
                _testOutputHelper.WriteLine(item.Key.ToString());
                if (item.Value == null || !item.Value.Any())
                {
                    _testOutputHelper.WriteLine("* [empty]");
                }
                else
                {
                    foreach (var process in item.Value)
                    {
                        _testOutputHelper.WriteLine($"* {process}");
                    }
                }
            }

            fieldProcesses[personPrimaryKey.GetIndex()].Contains(ControlForeignKey.Name).Should().BeFalse();
            fieldProcesses[addressForeignKey.GetIndex()].Contains(ControlForeignKey.Name).Should().BeFalse();
        }

    }
{'fraction_non_alphanumeric': 0.08839835728952772, 'fraction_numerical': 0.0, 'mean_word_length': 2.969437652811736, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '18696', 'n_tokens_mistral': 2391, 'n_tokens_neox': 2291, 'n_words': 313}