Exemplo n.º 1
0
        public void ShouldReturnErrorWhenForeignKeyReferencesNonExistingPrimaryKeyValue()
        {
            var addmlFlatFileDefinition = new AddmlFlatFileDefinitionBuilder().Build();
            var recordDef = new AddmlRecordDefinitionBuilder()
                            .WithAddmlFlatFileDefinition(addmlFlatFileDefinition)
                            .Build();

            AddmlFieldDefinition primaryKeyFieldDef = recordDef.AddAddmlFieldDefinition("id", null, null, new IntegerDataType(), true, false,
                                                                                        null, null, null, null, null, true);
            var primaryKeyField = new Field(primaryKeyFieldDef, "1001");

            AddmlFieldDefinition foreignKeyFieldDef = new AddmlFieldDefinitionBuilder()
                                                      .WithForeignKey(primaryKeyFieldDef)
                                                      .Build();

            var foreignKeyField = new Field(foreignKeyFieldDef, "25");

            var controlForeignKey = new ControlForeignKey();

            controlForeignKey.Run(primaryKeyField);
            controlForeignKey.Run(foreignKeyField);

            controlForeignKey.EndOfFile();
            TestRun testRun = controlForeignKey.GetTestRun();

            testRun.IsSuccess().Should().BeFalse();
            testRun.Results.Count.Should().Be(1);
        }
        public void ShouldReportIfRecordDefinitionIsNotInUse()
        {
            AddmlFlatFileDefinition fileDefinition        = new AddmlFlatFileDefinitionBuilder().Build();
            AddmlRecordDefinition   recordDefinitionInUse = new AddmlRecordDefinitionBuilder()
                                                            .WithAddmlFlatFileDefinition(fileDefinition)
                                                            .Build();
            AddmlRecordDefinition recordDefinitionNotInUse = new AddmlRecordDefinitionBuilder()
                                                             .WithAddmlFlatFileDefinition(fileDefinition)
                                                             .Build();
            AddmlFieldDefinition fieldDefinitionInUse = new AddmlFieldDefinitionBuilder()
                                                        .WithRecordDefinition(recordDefinitionInUse)
                                                        .Build();
            AddmlFieldDefinition fieldDefinitionNotInUse = new AddmlFieldDefinitionBuilder()
                                                           .WithRecordDefinition(recordDefinitionNotInUse)
                                                           .Build();
            FlatFile flatFile = new FlatFile(fileDefinition);

            A_14_ControlNotUsedRecordDef test = new A_14_ControlNotUsedRecordDef();

            test.Run(flatFile);
            test.Run(new Arkade.Core.Base.Addml.Record(recordDefinitionInUse, new List <Field> {
                new Field(fieldDefinitionInUse, "A"),
                new Field(fieldDefinitionInUse, "A"),
                new Field(fieldDefinitionInUse, "B")
            }));
            test.EndOfFile();

            TestRun testRun = test.GetTestRun();

            testRun.IsSuccess().Should().BeFalse();
            testRun.Results.Count.Should().Be(1);
            testRun.Results[0].Location.ToString().Should().Be(recordDefinitionNotInUse.GetIndex().ToString());
            testRun.Results[0].Message.Should().Be("Posttypen er ikke i bruk");
        }
Exemplo n.º 3
0
        public void ShouldReturnNoErrorsWhenAllForeignKeysExists()
        {
            var addmlFlatFileDefinition = new AddmlFlatFileDefinitionBuilder().Build();
            var recordDef = new AddmlRecordDefinitionBuilder()
                            .WithAddmlFlatFileDefinition(addmlFlatFileDefinition)
                            .Build();

            AddmlFieldDefinition primaryKeyFieldDef = recordDef.AddAddmlFieldDefinition("id", null, null, new IntegerDataType(), true, false,
                                                                                        null, null, null, null, null, true);
            var primaryKeyField = new Field(primaryKeyFieldDef, "1001");

            AddmlFieldDefinition foreignKeyFieldDef = recordDef.AddAddmlFieldDefinition("foreignKeyId", null, null, new IntegerDataType(), true, false,
                                                                                        null, null, null, null, null, false);
            var foreignKeyField = new Field(foreignKeyFieldDef, "1001");

            var controlForeignKey = new ControlForeignKey();

            controlForeignKey.Run(primaryKeyField);
            controlForeignKey.Run(foreignKeyField);

            controlForeignKey.EndOfFile();
            TestRun testRun = controlForeignKey.GetTestRun();

            testRun.IsSuccess().Should().BeTrue();
        }
Exemplo n.º 4
0
        public void ShouldReportIfRecordLengthIsDifferentFromSpecified()
        {
            AddmlRecordDefinition recordDefiniton = new AddmlRecordDefinitionBuilder()
                                                    .WithRecordLength(10)
                                                    .Build();

            AddmlFieldDefinition fieldDefinition = new AddmlFieldDefinitionBuilder()
                                                   .WithRecordDefinition(recordDefiniton)
                                                   .Build();

            FlatFile flatFile = new FlatFile(recordDefiniton.AddmlFlatFileDefinition);

            ControlFixedLength test = new ControlFixedLength();

            test.Run(flatFile);
            test.Run(new Arkade.Core.Base.Addml.Record(recordDefiniton, new List <Field> {
                new Field(fieldDefinition, "1"),
                new Field(fieldDefinition, "12"),
                new Field(fieldDefinition, "123"),
            }));
            test.EndOfFile();

            TestRun testRun = test.GetTestRun();

            testRun.IsSuccess().Should().BeFalse();
            testRun.Results.Count.Should().Be(1);
            testRun.Results[0].Location.ToString().Should().Be(recordDefiniton.GetIndex().ToString());
            testRun.Results[0].Message.Should().Be("Oppgitt postlengde (10) er ulik faktisk (6)");
        }
Exemplo n.º 5
0
        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();
        }
Exemplo n.º 6
0
        public void ShouldAddCollectPrimaryKeyToRecordDefinitionsContainingReferencedPrimaryKey()
        {
            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();
            var addmlForeignKey = new AddmlForeignKey("testkey");

            AddmlRecordDefinition addressFileRecordDef = new AddmlRecordDefinitionBuilder()
                                                         .WithAddmlFlatFileDefinition(addressFile)
                                                         .WithRecordProcess(A_16_ControlForeignKey.Name)
                                                         .WithForeignKey(addmlForeignKey)
                                                         .Build();

            new AddmlFieldDefinitionBuilder().WithRecordDefinition(addressFileRecordDef).WithName("addressPrimaryKey").IsPartOfPrimaryKey(true).Build();
            AddmlFieldDefinition addressForeignKey = new AddmlFieldDefinitionBuilder().WithRecordDefinition(addressFileRecordDef)
                                                     .WithName("addressForeignKey")
//                .WithForeignKey(personPrimaryKey)
                                                     .Build();

            addmlForeignKey.ForeignKeys.Add(addressForeignKey);
            addmlForeignKey.ForeignKeyReferenceIndexes.Add(personPrimaryKey.GetIndex());
            addmlForeignKey.ForeignKeyReferenceFields.Add(personPrimaryKey);

            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
            },
                new List <AddmlFlatFileDefinition>()
            {
                personFile, addressFile
            });

            Dictionary <IAddmlIndex, List <string> > recordProcesses = addmlDefinition.GetRecordProcessesGroupedByRecord();

            DebugPrintListOfProcesses(recordProcesses);

            recordProcesses[personFileRecordDef.GetIndex()].Contains(AI_01_CollectPrimaryKey.Name).Should().BeTrue();
            recordProcesses[addressFileRecordDef.GetIndex()].Contains(A_16_ControlForeignKey.Name).Should().BeTrue();
            recordProcesses[unrelatedFileRecordDef.GetIndex()].Should().BeEmpty();
        }
Exemplo n.º 7
0
        public void ShouldReportIfKeyIsNotUnique()
        {
            AddmlRecordDefinition recordDefinition = new AddmlRecordDefinitionBuilder()
                                                     .Build();
            AddmlFieldDefinition fieldDefinition = new AddmlFieldDefinitionBuilder()
                                                   .WithRecordDefinition(recordDefinition)
                                                   .IsPartOfPrimaryKey(true)
                                                   .Build();
            AddmlFieldDefinition fieldDefinition2 = new AddmlFieldDefinitionBuilder()
                                                    .WithRecordDefinition(recordDefinition)
                                                    .IsPartOfPrimaryKey(false)
                                                    .Build();
            FlatFile flatFile = new FlatFile(recordDefinition.AddmlFlatFileDefinition);

            ControlKey test = new ControlKey();

            test.Run(flatFile);
            test.Run(new Arkade.Core.Addml.Record(recordDefinition, new List <Field> {
                new Field(fieldDefinition, "A"),
                new Field(fieldDefinition, "A"),
                new Field(fieldDefinition2, "B")
            }));
            test.Run(new Arkade.Core.Addml.Record(recordDefinition, new List <Field> {
                new Field(fieldDefinition, "A"),
                new Field(fieldDefinition, "B"),
                new Field(fieldDefinition2, "B")
            }));
            test.Run(new Arkade.Core.Addml.Record(recordDefinition, new List <Field> {
                new Field(fieldDefinition, "A"),
                new Field(fieldDefinition, "C"),
                new Field(fieldDefinition2, "B")
            }));
            test.Run(new Arkade.Core.Addml.Record(recordDefinition, new List <Field> {
                new Field(fieldDefinition, "A"),
                new Field(fieldDefinition, "B"),
                new Field(fieldDefinition2, "B")
            }));
            test.Run(new Arkade.Core.Addml.Record(recordDefinition, new List <Field> {
                new Field(fieldDefinition, "A"),
                new Field(fieldDefinition, "C"),
                new Field(fieldDefinition2, "C")
            }));
            test.EndOfFile();

            TestRun testRun = test.GetTestRun();

            testRun.IsSuccess().Should().BeFalse();
            testRun.Results.Count.Should().Be(1);
            testRun.Results[0].Location.ToString().Should().Be(recordDefinition.GetIndex().ToString());
            testRun.Results[0].Message.Should().Be("Følgende primærnøkkelverdier er ikke unike: A,B A,C");
        }
Exemplo n.º 8
0
        public void ShouldUseRecordDefinitionFixedLengthAndSkipRecordSeparator()
        {
            AddmlFlatFileDefinition fileDefinition = new AddmlFlatFileDefinitionBuilder()
                                                     .WithRecordSeparator("CRLF")
                                                     .Build();
            AddmlRecordDefinition recordDefinition = new AddmlRecordDefinitionBuilder()
                                                     .WithAddmlFlatFileDefinition(fileDefinition)
                                                     .WithRecordLength(7)
                                                     .Build();
            AddmlFieldDefinition fieldDefinition1 = new AddmlFieldDefinitionBuilder()
                                                    .WithRecordDefinition(recordDefinition)
                                                    .WithFixedLength(1)
                                                    .Build();
            AddmlFieldDefinition fieldDefinition2 = new AddmlFieldDefinitionBuilder()
                                                    .WithRecordDefinition(recordDefinition)
                                                    .WithFixedLength(2)
                                                    .Build();
            AddmlFieldDefinition fieldDefinition3 = new AddmlFieldDefinitionBuilder()
                                                    .WithRecordDefinition(recordDefinition)
                                                    .WithFixedLength(4)
                                                    .Build();

            FlatFile file = new FlatFile(fileDefinition);

            var sb = new StringBuilder();

            sb.Append("1121234\r\n");
            sb.Append("AABABCD\r\n");
            sb.Append("       \r\n");
            MemoryStream stream = CreateStream(sb.ToString());

            FixedFileFormatReader reader = new FixedFileFormatReader(file, stream);

            reader.MoveNext().Should().BeTrue();
            reader.Current.Value.Should().Be("1121234");
            reader.Current.Fields[0].Value.Should().Be("1");
            reader.Current.Fields[1].Value.Should().Be("12");
            reader.Current.Fields[2].Value.Should().Be("1234");
            reader.MoveNext().Should().BeTrue();
            reader.Current.Value.Should().Be("AABABCD");
            reader.Current.Fields[0].Value.Should().Be("A");
            reader.Current.Fields[1].Value.Should().Be("AB");
            reader.Current.Fields[2].Value.Should().Be("ABCD");
            reader.MoveNext().Should().BeTrue();
            reader.Current.Value.Should().Be("       ");
            reader.Current.Fields[0].Value.Should().Be(" ");
            reader.Current.Fields[1].Value.Should().Be("  ");
            reader.Current.Fields[2].Value.Should().Be("    ");
            reader.MoveNext().Should().BeFalse();
        }
Exemplo n.º 9
0
        public void ShouldFindMaxAndMinLengthRecord()
        {
            AddmlFlatFileDefinition flatFileDefinition = new AddmlFlatFileDefinitionBuilder()
                                                         .WithFieldSeparator("BB")
                                                         .WithRecordSeparator("A")
                                                         .Build();
            FlatFile flatFile = new FlatFile(flatFileDefinition);
            AddmlRecordDefinition recordDefinition = new AddmlRecordDefinitionBuilder()
                                                     .WithAddmlFlatFileDefinition(flatFileDefinition)
                                                     .Build();
            AddmlFieldDefinition fieldDefinition = new AddmlFieldDefinitionBuilder()
                                                   .WithRecordDefinition(recordDefinition)
                                                   .Build();

            List <Field> fields1 = new List <Field>
            {
                new Field(fieldDefinition, "1234567890"),
                new Field(fieldDefinition, "12345"),
                new Field(fieldDefinition, "1")
            };
            Record record1 = new Record(recordDefinition, fields1);

            List <Field> fields2 = new List <Field>
            {
                new Field(fieldDefinition, "1"),
                new Field(fieldDefinition, ""),
                new Field(fieldDefinition, "3")
            };
            Record record2 = new Record(recordDefinition, fields2);


            AnalyseFindExtremeRecords test = new AnalyseFindExtremeRecords();

            test.Run(flatFile);
            test.Run(record1);
            test.Run(record2);
            test.EndOfFile();

            TestRun testRun = test.GetTestRun();

            testRun.IsSuccess().Should().BeTrue();
            testRun.Results.Count.Should().Be(1);
            testRun.Results[0].Location.ToString().Should().Be(recordDefinition.GetIndex().ToString());
            testRun.Results[0].Message.Should().Be("Lengste/korteste post: 20/6");
        }
Exemplo n.º 10
0
        public void RecordValueShouldContainFieldSeparator()
        {
            AddmlRecordDefinition recordDefinition = new AddmlRecordDefinitionBuilder()
                                                     .WithAddmlFlatFileDefinition(new AddmlFlatFileDefinitionBuilder()
                                                                                  .WithFieldSeparator("AA")
                                                                                  .Build())
                                                     .Build();
            List <Field> fields = new List <Field>
            {
                new Field(null, "1"),
                new Field(null, "2"),
                new Field(null, "3")
            };

            Record record = new Record(recordDefinition, fields);

            record.Value.Should().Be("1AA2AA3");
        }
Exemplo n.º 11
0
        public void ShouldNotAddCollectPrimaryKeyProcessWhenControlForeignKeyIsNotDefinedInAnyRecordDefinition()
        {
            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();
            var addmlForeignKey = new AddmlForeignKey("testkey");
            AddmlRecordDefinition addressFileRecordDef = new AddmlRecordDefinitionBuilder()
                                                         .WithAddmlFlatFileDefinition(addressFile)
                                                         .WithForeignKey(addmlForeignKey)
                                                         .Build();

            new AddmlFieldDefinitionBuilder().WithRecordDefinition(addressFileRecordDef).WithName("addressPrimaryKey").IsPartOfPrimaryKey(true).Build();
            var addressField = new AddmlFieldDefinitionBuilder().WithRecordDefinition(addressFileRecordDef)
                               .WithName("addressForeignKey")
                               .Build();

            addmlForeignKey.ForeignKeys.Add(addressField);
            addmlForeignKey.ForeignKeyReferenceIndexes.Add(personPrimaryKey.GetIndex());
            addmlForeignKey.ForeignKeyReferenceFields.Add(personPrimaryKey);


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

            Dictionary <IAddmlIndex, List <string> > recordProcesses = addmlDefinition.GetRecordProcessesGroupedByRecord();

            DebugPrintListOfProcesses(recordProcesses);

            recordProcesses[personFileRecordDef.GetIndex()].Should().BeEmpty();
            recordProcesses[addressFileRecordDef.GetIndex()].Should().BeEmpty();
        }
        public void SemicolonsWithinQuotesAreNotInterpretedAsFieldDelimiters()
        {
            AddmlFlatFileDefinition addmlFlatFileDefinition = new AddmlFlatFileDefinitionBuilder()
                                                              .WithRecordSeparator("CRLF").WithFieldSeparator(";").Build();

            AddmlRecordDefinition recordDefinition = new AddmlRecordDefinitionBuilder()
                                                     .WithAddmlFlatFileDefinition(addmlFlatFileDefinition).Build();

            new AddmlFieldDefinitionBuilder().WithRecordDefinition(recordDefinition).Build();
            new AddmlFieldDefinitionBuilder().WithRecordDefinition(recordDefinition).Build();
            new AddmlFieldDefinitionBuilder().WithRecordDefinition(recordDefinition).Build();

            const string csvData = "AA;\"B;B\";CC";

            var streamReader           = new StreamReader(new MemoryStream(Encoding.UTF8.GetBytes(csvData)));
            var recordReader           = new DelimiterFileFormatReader(new FlatFile(addmlFlatFileDefinition), streamReader);
            var actionOfGettingCurrent = (Action)(() => ((Func <object>)(() => recordReader.Current))());

            recordReader.MoveNext(); // AA;"B;B";CC

            actionOfGettingCurrent.Should().NotThrow <Exception>();
            recordReader.Current?.Fields?.Count.Should().Be(3);
        }
        public void ShouldCountRecordDefinitionOccurences()
        {
            var process = new AnalyseCountRecordDefinitionOccurences();
            AddmlFlatFileDefinition addmlFlatFileDefinition = new AddmlFlatFileDefinitionBuilder().Build();
            FlatFile flatFile = new FlatFile(addmlFlatFileDefinition);

            var addmlRecordDefinition1 = new AddmlRecordDefinitionBuilder()
                                         .WithAddmlFlatFileDefinition(addmlFlatFileDefinition)
                                         .WithName("recordDef1")
                                         .Build();
            var addmlRecordDefinition2 = new AddmlRecordDefinitionBuilder()
                                         .WithAddmlFlatFileDefinition(addmlFlatFileDefinition)
                                         .WithName("recordDef2")
                                         .Build();

            Record record1 = new Record(addmlRecordDefinition1, new List <Field>());
            Record record2 = new Record(addmlRecordDefinition1, new List <Field>());
            Record record3 = new Record(addmlRecordDefinition2, new List <Field>());

            process.Run(flatFile);
            process.Run(record1);
            process.Run(record2);
            process.Run(record3);
            process.EndOfFile();
            TestRun testRun = process.GetTestRun();

            testRun.Results.Count.Should().Be(2);

            TestResult resultDef1 = testRun.Results.FirstOrDefault(r => r.Location.ToString().Contains("recordDef1"));
            TestResult resultDef2 = testRun.Results.FirstOrDefault(r => r.Location.ToString().Contains("recordDef2"));

            resultDef1.Should().NotBeNull();
            resultDef2.Should().NotBeNull();

            resultDef1?.Message.Should().Contain("2");
            resultDef2?.Message.Should().Contain("1");
        }
        public ControlForeignKeySingleTest()
        {
            AddmlFlatFileDefinition attachmentFileDefinition = new AddmlFlatFileDefinitionBuilder().Build();

            _attachmentRecordDef = new AddmlRecordDefinitionBuilder()
                                   .WithAddmlFlatFileDefinition(attachmentFileDefinition)
                                   .Build();
            _messageIdPrimaryKeyFieldDef = new AddmlFieldDefinitionBuilder()
                                           .WithRecordDefinition(_attachmentRecordDef)
                                           .WithName("MessageId")
                                           .IsPartOfPrimaryKey(true)
                                           .Build();

            AddmlForeignKey foreignKey = new AddmlForeignKey("fk1");

            AddmlFlatFileDefinition addmlFlatFileDefinition = new AddmlFlatFileDefinitionBuilder().Build();

            _recordDef = new AddmlRecordDefinitionBuilder()
                         .WithAddmlFlatFileDefinition(addmlFlatFileDefinition)
                         .WithRecordProcess(ControlForeignKey.Name)
                         .WithForeignKey(foreignKey)
                         .Build();

            _primaryKeyFieldDef = new AddmlFieldDefinitionBuilder()
                                  .WithRecordDefinition(_recordDef)
                                  .WithName("Id")
                                  .IsPartOfPrimaryKey(true)
                                  .Build();
            _foreignKeyMessageIdFieldDef = new AddmlFieldDefinitionBuilder()
                                           .WithRecordDefinition(_recordDef)
                                           .WithName("MessageId")
                                           .Build();

            foreignKey.ForeignKeys.Add(_foreignKeyMessageIdFieldDef);
            foreignKey.ForeignKeyReferenceIndexes.Add(_messageIdPrimaryKeyFieldDef.GetIndex());
            foreignKey.ForeignKeyReferenceFields.Add(_messageIdPrimaryKeyFieldDef);
        }
Exemplo n.º 15
0
        public void ShouldSupportReadingOfMultipleRecordTypesWithinSameFlatFile()
        {
            var fieldPosttype   = "posttype";
            var fieldPostnummer = "postnummer";
            var fieldFirmanavn  = "firmanavn";
            var fieldPersonnavn = "personnavn";

            AddmlFlatFileDefinition fileDefinition = new AddmlFlatFileDefinitionBuilder()
                                                     .WithRecordSeparator("CRLF")
                                                     .WithFieldSeparator(";")
                                                     .WithRecordDefinitionFieldIdentifier(fieldPosttype)
                                                     .Build();

            var recdef1 = "recdef1";
            AddmlRecordDefinition recordDefinition1 = new AddmlRecordDefinitionBuilder()
                                                      .WithAddmlFlatFileDefinition(fileDefinition)
                                                      .WithName(recdef1)
                                                      .WithRecordDefinitionFieldValue("1")
                                                      .Build();

            new AddmlFieldDefinitionBuilder()
            .WithRecordDefinition(recordDefinition1)
            .WithName(fieldPosttype)
            .Build();
            new AddmlFieldDefinitionBuilder()
            .WithRecordDefinition(recordDefinition1)
            .WithName(fieldPostnummer)
            .Build();
            new AddmlFieldDefinitionBuilder()
            .WithRecordDefinition(recordDefinition1)
            .WithName(fieldFirmanavn)
            .Build();

            var recdef2 = "recdef2";
            AddmlRecordDefinition recordDefinition2 = new AddmlRecordDefinitionBuilder()
                                                      .WithAddmlFlatFileDefinition(fileDefinition)
                                                      .WithName(recdef2)
                                                      .WithRecordDefinitionFieldValue("2")
                                                      .Build();

            new AddmlFieldDefinitionBuilder()
            .WithRecordDefinition(recordDefinition2)
            .WithName(fieldPosttype)
            .Build();
            new AddmlFieldDefinitionBuilder()
            .WithRecordDefinition(recordDefinition2)
            .WithName(fieldPersonnavn)
            .Build();
            new AddmlFieldDefinitionBuilder()
            .WithRecordDefinition(recordDefinition2)
            .WithName(fieldPostnummer)
            .Build();

            var sb = new StringBuilder();

            sb.Append("1;2500;Skattedirektoratet\r\n");
            sb.Append("1;2501;Skattedirektoratet\r\n");
            sb.Append("2;Hans Hansen;3374\r\n");
            sb.Append("2;Ole Olsen;2235\r\n");

            var streamReader = new StreamReader(new MemoryStream(Encoding.UTF8.GetBytes(sb.ToString())));

            var reader = new DelimiterFileFormatReader(new FlatFile(fileDefinition), streamReader);

            reader.MoveNext().Should().BeTrue();
            reader.Current.Definition.Name.Should().Be(recdef1);
            RecordShouldMatch(reader.Current, 0, fieldPosttype, "1");
            RecordShouldMatch(reader.Current, 1, fieldPostnummer, "2500");
            RecordShouldMatch(reader.Current, 2, fieldFirmanavn, "Skattedirektoratet");

            reader.MoveNext().Should().BeTrue();
            reader.Current.Definition.Name.Should().Be(recdef1);
            RecordShouldMatch(reader.Current, 0, fieldPosttype, "1");
            RecordShouldMatch(reader.Current, 1, fieldPostnummer, "2501");
            RecordShouldMatch(reader.Current, 2, fieldFirmanavn, "Skattedirektoratet");

            reader.MoveNext().Should().BeTrue();
            reader.Current.Definition.Name.Should().Be(recdef2);
            RecordShouldMatch(reader.Current, 0, fieldPosttype, "2");
            RecordShouldMatch(reader.Current, 1, fieldPersonnavn, "Hans Hansen");
            RecordShouldMatch(reader.Current, 2, fieldPostnummer, "3374");

            reader.MoveNext().Should().BeTrue();
            reader.Current.Definition.Name.Should().Be(recdef2);
            RecordShouldMatch(reader.Current, 0, fieldPosttype, "2");
            RecordShouldMatch(reader.Current, 1, fieldPersonnavn, "Ole Olsen");
            RecordShouldMatch(reader.Current, 2, fieldPostnummer, "2235");

            reader.MoveNext().Should().BeFalse();
        }