Exemplo n.º 1
0
        public void TestInputRecordDefinesAllFields()
        {
            // Tests that the input record has a field definition for every property on the
            // output record that has been marked with the FixedWidthFieldAttribute for the input
            // record defintion's record name.
            var inputRecordDefinition = new Mock <IFixedWidthInputRecordDefinition>();

            inputRecordDefinition.Setup(x => x.RecordName).Returns("TestRecord");
            inputRecordDefinition.Setup(x => x.FieldDefinitions).Returns(
                new[] {
                FixedWidthInputFieldDefinition.Create("NameField", 20, x => x.Trim()),
                // No family size field
                FixedWidthInputFieldDefinition.Create("BirthdayField", 10, FixedWidthParser.ParseIso8601Date)
            });

            Assert.That(() => {
                // ReSharper disable once ObjectCreationAsStatement
                new FixedWidthParser <TestOutputRecord>(inputRecordDefinition.Object);
            }, Throws.ArgumentException.With.Message.Contains(
                            "The field(s) 'FamilySizeField' on the output record TestOutputRecord were not mapped from the input record TestRecord"));
        }
Exemplo n.º 2
0
        public void TestDuplicateFieldDefinitionsOnInputRecord()
        {
            // Tests that duplicate field definitions are not allowed on the input record type,
            // regardless of whether that field is mapped onto the output record or not.

            // Case #1 - Duplicated field (Field1) is not mapped onto the output record
            var faultyInputRecordDefinition1 = new Mock <IFixedWidthInputRecordDefinition>();

            faultyInputRecordDefinition1.Setup(x => x.RecordName).Returns("TestRecord");
            faultyInputRecordDefinition1.Setup(x => x.FieldDefinitions).Returns(
                new[] {
                FixedWidthInputFieldDefinition.Create("Field1", 10, FixedWidthParser.ParseString),
                FixedWidthInputFieldDefinition.Create("Field1", 20, FixedWidthParser.ParseIso8601Date),
                FixedWidthInputFieldDefinition.Create("NameField", 20, FixedWidthParser.ParseString),
                FixedWidthInputFieldDefinition.Create("FamilySizeField", 2, FixedWidthParser.ParseInt),
                FixedWidthInputFieldDefinition.Create("BirthdayField", 10, FixedWidthParser.ParseIso8601Date)
            });

            Assert.That(() => {
                // ReSharper disable once ObjectCreationAsStatement
                new FixedWidthParser <TestOutputRecord>(faultyInputRecordDefinition1.Object);
            }, Throws.ArgumentException.With.Message.Contains("Duplicate field definition at index 1 (Field1)"));

            // Case #2 - Duplicated field (NameField) *is* mapped onto the output record (more serious)
            var faultyInputRecordDefinition2 = new Mock <IFixedWidthInputRecordDefinition>();

            faultyInputRecordDefinition2.Setup(x => x.RecordName).Returns("TestRecord");
            faultyInputRecordDefinition2.Setup(x => x.FieldDefinitions).Returns(
                new[] {
                FixedWidthInputFieldDefinition.Create("NameField", 20, FixedWidthParser.ParseString),
                FixedWidthInputFieldDefinition.Create("FamilySizeField", 2, FixedWidthParser.ParseInt),
                FixedWidthInputFieldDefinition.Create("BirthdayField", 10, FixedWidthParser.ParseIso8601Date),
                FixedWidthInputFieldDefinition.Create("NameField", 20, FixedWidthParser.ParseIso8601Date)
            });
            Assert.That(() => {
                // ReSharper disable once ObjectCreationAsStatement
                new FixedWidthParser <TestOutputRecord>(faultyInputRecordDefinition2.Object);
            }, Throws.ArgumentException.With.Message.Contains("Duplicate field definition at index 3 (NameField)"));
        }