Ejemplo n.º 1
0
        public void TestIgnoredColumn_HandlePreAndPostProcessing()
        {
            var ignored = new IgnoredColumn()
            {
                ColumnName    = "Ignored",
                NullFormatter = NullFormatter.ForValue("NULL"),
                OnParsing     = (ctx, value) =>
                {
                    Assert.AreEqual("NULL", value);
                    return(value);
                },
                OnParsed = (ctx, value) =>
                {
                    Assert.IsNull(value);
                    return(value);
                },
                OnFormatting = (ctx, value) =>
                {
                    Assert.IsNull(value);
                    return(value);
                },
                OnFormatted = (ctx, value) =>
                {
                    Assert.AreEqual("NULL", value);
                    return(value);
                }
            };
            object value = ignored.Parse(null, "NULL");

            Assert.IsNull(value);
            string formatted = ignored.Format(null, value);

            Assert.AreEqual("NULL", formatted);
        }
Ejemplo n.º 2
0
        public void TestIgnoredMapping_HandlePreAndPostProcessing()
        {
            ISeparatedValueTypeMapper <IgnoredOnly> mapper = SeparatedValueTypeMapper.Define(() => new IgnoredOnly());

            mapper.Ignored()
            .ColumnName("Ignored")
            .NullFormatter(NullFormatter.ForValue("NULL"))
            .OnParsing((ctx, value) =>
            {
                Assert.AreEqual("NULL", value);
                return(value);
            })
            .OnParsed((ctx, value) =>
            {
                Assert.IsNull(value);
                return(value);
            })
            .OnFormatting((ctx, value) =>
            {
                Assert.IsNull(value);
                return(value);
            })
            .OnFormatted((ctx, value) =>
            {
                Assert.AreEqual("NULL", value);
                return(value);
            });
            var    ignored = mapper.GetSchema().ColumnDefinitions["Ignored"];
            object value   = ignored.Parse(null, "NULL");

            Assert.IsNull(value);
            string formatted = ignored.Format(null, value);

            Assert.AreEqual("NULL", formatted);
        }
        public void ShouldTreatConstantAsNull_TypeMapper()
        {
            var nullHandler = NullFormatter.ForValue("----");
            var mapper      = SeparatedValueTypeMapper.Define <Product>();

            mapper.Property(p => p.Name).ColumnName("name").NullFormatter(nullHandler);
            mapper.Property(p => p.Cost).ColumnName("cost").NullFormatter(nullHandler).FormatProvider(CultureInfo.InvariantCulture);
            mapper.Property(p => p.Available).ColumnName("available").NullFormatter(nullHandler);
            mapper.Property(p => p.Vendor).ColumnName("vendor").NullFormatter(nullHandler);

            string       content      = "----,5.12,----,apple" + Environment.NewLine;
            StringReader stringReader = new StringReader(content);
            var          products     = mapper.Read(stringReader).ToArray();

            Assert.AreEqual(1, products.Length);

            Product product = products.Single();

            Assert.IsNull(product.Name);
            Assert.AreEqual(5.12m, product.Cost);
            Assert.IsNull(product.Available);
            Assert.AreEqual("apple", product.Vendor);

            StringWriter stringWriter = new StringWriter();

            mapper.Write(stringWriter, products);
            string output = stringWriter.ToString();

            Assert.AreEqual(content, output);
        }
        private static SeparatedValueSchema getSchema()
        {
            var nullHandler = NullFormatter.ForValue("----");

            SeparatedValueSchema schema = new SeparatedValueSchema();

            schema.AddColumn(new StringColumn("Name")
            {
                NullFormatter = nullHandler
            });
            schema.AddColumn(new DecimalColumn("Cost")
            {
                NullFormatter = nullHandler, FormatProvider = CultureInfo.InvariantCulture
            });
            schema.AddColumn(new SingleColumn("Available")
            {
                NullFormatter = nullHandler
            });
            schema.AddColumn(new StringColumn("Vendor")
            {
                NullFormatter = nullHandler
            });

            return(schema);
        }
Ejemplo n.º 5
0
        public static NullFormatter Instance()
        {
            if (_staticInstance == null)
            {
                _staticInstance = new NullFormatter();
            }

            return(_staticInstance);
        }
Ejemplo n.º 6
0
        public void ShouldPreserveWhiteSpaceIfConfigured_AllWhitespace()
        {
            string               source       = " \t\n\r ";
            StringReader         stringReader = new StringReader(source);
            SeparatedValueSchema schema       = new SeparatedValueSchema();

            schema.AddColumn(new StringColumn("a")
            {
                Trim = false, NullFormatter = NullFormatter.ForValue(null)
            });
            SeparatedValueOptions options = new SeparatedValueOptions()
            {
                IsFirstRecordSchema = false, RecordSeparator = "\r\n", PreserveWhiteSpace = true
            };
            SeparatedValueReader reader = new SeparatedValueReader(stringReader, schema, options);

            object[][] expected = new object[][]
            {
                new object[] { " \t\n\r " }
            };
            assertRecords(expected, reader);
        }
Ejemplo n.º 7
0
        public async Task RetrieveMeets_GivenDateRange_ShouldOnlyReturnMeetsInRange()
        {
            // Arrange.
            var settings           = Substitute.For <ISettings>();
            var requestMaker       = Substitute.For <IRestRequestMaker>();
            var googleSheetFactory = Substitute.For <IMeetsGoogleSheetFactory>();
            var googleSheet        = Substitute.For <IMeetsGoogleSheet>();
            var dateTimeService    = Substitute.For <IDateTimeService>();
            var logger             = Substitute.For <ILogger>();
            var formatter          = NullFormatter.Instance();

            settings
            .GetValidString(Arg.Any <string>())
            .Returns("SomeSheetId");

            settings
            .GetValidString(Arg.Any <string>())
            .Returns("SomeAppKey");

            googleSheetFactory
            .CreateSheet(
                Arg.Any <Uri>(),
                Arg.Any <IRestRequestMaker>(),
                Arg.Any <ILogger>())
            .Returns(googleSheet);

            var dateField = new MeetField(
                false,
                false,
                "Date",
                "Date",
                0,
                false,
                formatter);

            googleSheet
            .Retrieve()
            .Returns(true);

            googleSheet
            .Fields
            .Returns(new[]
            {
                dateField
            });

            googleSheet
            .ValuesByRow
            .Returns(
                new[]
            {
                new[] { new MeetFieldValue(dateField, "2019-7-15", new ValidatorChain()) },
                new[] { new MeetFieldValue(dateField, "2019-7-31", new ValidatorChain()) },
                new[] { new MeetFieldValue(dateField, "2019-7-1", new ValidatorChain()) }
            });

            var testObject = new MeetsService(
                settings,
                requestMaker,
                googleSheetFactory,
                dateTimeService,
                logger);

            // Act.
            var result = await testObject.RetrieveMeets(
                new DateTime(2019, 7, 15),
                new DateTime(2019, 7, 31));

            // Assert.
            Assert.IsNotNull(result);
            Assert.AreEqual(2, result.Count());
        }
        public void Process_GivenGoogleSheet_ShouldOutputMeetDetails()
        {
            // Arrange.
            var sheet          = Substitute.For <IMeetsGoogleSheet>();
            var validatorChain = new ValidatorChain();
            var formatter      = NullFormatter.Instance();
            var field          = new MeetField(false, false, string.Empty, string.Empty, 0, false, formatter);

            sheet
            .Fields
            .Returns(new[]
            {
                new MeetField(false, false, string.Empty, "Column 1", 0, false, formatter),
                new MeetField(false, false, string.Empty, "Column 2", 0, false, formatter),
                new MeetField(false, false, string.Empty, "Column 3", 0, true, formatter),
                new MeetField(true, false, string.Empty, "Column 4", 0, false, formatter),
                new MeetField(false, true, string.Empty, "Column 5", 0, false, formatter),
            });

            sheet
            .ValuesByRow
            .Returns(new[]
            {
                new[]
                {
                    new MeetFieldValue(field, "R1C1", validatorChain),
                    new MeetFieldValue(field, "R1C2", validatorChain),
                    new MeetFieldValue(field, "R1C3", validatorChain),
                    new MeetFieldValue(field, "R1C4", validatorChain),
                    new MeetFieldValue(field, "R1C5", validatorChain)
                },
                new[]
                {
                    new MeetFieldValue(field, "R2C1", validatorChain),
                    new MeetFieldValue(field, "R2C2", validatorChain),
                    new MeetFieldValue(field, "R2C3", validatorChain),
                    new MeetFieldValue(field, "R2C4", validatorChain),
                    new MeetFieldValue(field, "R2C5", validatorChain)
                },
            });

            sheet
            .FindHeaderIndex("Column 3", Arg.Any <bool>())
            .Returns(2);

            sheet
            .FindHeaderIndex("Column 5", Arg.Any <bool>())
            .Returns(4);

            // Act.
            GoogleSheetToMeetDetailsTransformer.Process(
                sheet,
                out IEnumerable <MeetDetailsModel> models);

            // Assert.
            Assert.IsNotNull(models);

            Assert.AreEqual(2, models.Count());

            Assert.AreEqual("R1C1", models.ElementAt(0).FieldValues.ElementAt(0).Value);
            Assert.AreEqual("R2C5", models.ElementAt(1).FieldValues.ElementAt(4).Value);

            Assert.AreEqual(5, models.ElementAt(0).AllFields.Count());

            Assert.AreEqual("Column 1", models.ElementAt(0).AllFields.ElementAt(0).FriendlyText);
            Assert.AreEqual("Column 5", models.ElementAt(0).AllFields.ElementAt(4).FriendlyText);

            Assert.IsTrue(models.ElementAt(0).AllFields.ElementAt(3).DisplayInHeader);
            Assert.IsTrue(models.ElementAt(0).AllFields.ElementAt(4).IsRequired);
            Assert.IsTrue(models.ElementAt(0).AllFields.ElementAt(2).IsMeetTitle);
        }