Example #1
0
        public void CreateConverterTest8(bool throwsOnParseErrors)
        {
            ICsvTypeConverter conv = CsvConverterFactory.CreateConverter(CsvTypeCode.ByteArray, false, false, throwOnParseErrors: throwsOnParseErrors);

            Assert.IsInstanceOfType(conv, typeof(Base64Converter));
            Assert.AreEqual(typeof(byte[]), conv.Type);
            Assert.AreEqual(throwsOnParseErrors, conv.ThrowsOnParseErrors);

            if (conv.FallbackValue is byte[] bts)
            {
                Assert.AreEqual(0, bts.Length);
            }
            else
            {
                Assert.Fail();
            }

            var test1 = new byte[] { 17, 24, 42 };

            string?s = conv.ConvertToString(test1);

            byte[]? test2 = (byte[]?)conv.Parse(s);

            CollectionAssert.AreEqual(test1, test2);
        }
        public void StringConverterTest()
        {
            ICsvTypeConverter conv = CsvConverterFactory.CreateConverter(CsvTypeCode.String, false);

            Assert.IsNotNull(conv.FallbackValue);
            Assert.AreEqual(typeof(string), conv.Type);
            Assert.IsFalse(conv.ThrowsOnParseErrors);
        }
        public void ParseTest(string s)
        {
            ICsvTypeConverter conv = CsvConverterFactory.CreateConverter(CsvTypeCode.DateTime);

            var dt = conv.Parse(s);

            Assert.AreEqual(new DateTime(1972, 01, 31), dt);
        }
        public void ParseTest1()
        {
            ICsvTypeConverter conv = CsvConverterFactory.CreateConverter(CsvTypeCode.ByteArray, true);

            Assert.IsInstanceOfType(conv, typeof(Base64Converter));

            Assert.IsNull(conv.FallbackValue);
            Assert.IsNull(conv.Parse(null));
        }
        public void ParseTest3()
        {
            ICsvTypeConverter conv = CsvConverterFactory.CreateConverter(CsvTypeCode.ByteArray, true, throwOnParseErrors: false);

            Assert.IsInstanceOfType(conv, typeof(Base64Converter));

            Assert.IsNull(conv.FallbackValue);
            Assert.IsNull(conv.Parse("blabla"));
        }
        public void ConvertToStringTest2()
        {
            var dt = new DateTime(1972, 01, 31);
            ICsvTypeConverter conv = CsvConverterFactory.CreateConverter(CsvTypeCode.DateTime);

            //dt = dt.ToLocalTime();

            string?s = conv.ConvertToString(dt);

            Assert.IsNotNull(s);
        }
Example #7
0
        public void TryGetValueTest()
        {
            KeyedCollection <string, CsvProperty> kColl = new TestCollection();

            var prop1 = new CsvProperty("Test", new string[0], CsvConverterFactory.CreateConverter(CsvTypeCode.Boolean));

            kColl.Add(prop1);

            Assert.IsTrue(kColl.TryGetValue("Test", out CsvProperty? prop2));

            Assert.AreEqual(prop1, prop2);
        }
        public void ConvertToStringTest()
        {
            ICsvTypeConverter conv = CsvConverterFactory.CreateConverter(CsvTypeCode.String, true, true);

            Assert.IsNull(conv.ConvertToString(DBNull.Value));

            Assert.IsNull(conv.ConvertToString(null));

            const string test = "Test";

            Assert.AreEqual(test, conv.ConvertToString(test));
        }
Example #9
0
        public void CreateConverterTest3(
            CsvTypeCode typeCode,
            bool nullable,
            bool throwOnParseErrors,
            Type converterType)
        {
            ICsvTypeConverter conv = CsvConverterFactory.CreateConverter(typeCode, nullable, true, throwOnParseErrors: throwOnParseErrors);

            Assert.AreEqual(converterType, conv.Type);
            Assert.IsTrue(Convert.IsDBNull(conv.FallbackValue));
            Assert.AreEqual(throwOnParseErrors, conv.ThrowsOnParseErrors);
        }
Example #10
0
        public void CreateConverterTest2(
            CsvTypeCode typeCode,
            bool nullable,
            bool throwOnParseErrors,
            Type converterType,
            object?fallBackValue)
        {
            ICsvTypeConverter conv = CsvConverterFactory.CreateConverter(typeCode, nullable, false, throwOnParseErrors: throwOnParseErrors);

            Assert.AreEqual(converterType, conv.Type);
            Assert.AreEqual(fallBackValue, conv.FallbackValue);
            Assert.AreEqual(throwOnParseErrors, conv.ThrowsOnParseErrors);
        }
        public void RoundtripTest1()
        {
            double d = 72.81;

            ICsvTypeConverter conv = CsvConverterFactory.CreateConverter(CsvTypeCode.Double);

            string?s = conv.ConvertToString(d);

            Assert.IsNotNull(s);

            double?d2 = (double?)conv.Parse(s);

            Assert.AreEqual(d, d2);
        }
        public void ParseTest()
        {
            ICsvTypeConverter conv = CsvConverterFactory.CreateConverter(CsvTypeCode.String, true);

            Assert.IsNull(conv.Parse(null));

            conv = CsvConverterFactory.CreateConverter(CsvTypeCode.String, false);

            Assert.IsNotNull(conv.Parse(null));

            const string test = "Test";

            Assert.AreEqual(test, conv.Parse(test));
        }
        public void Roundtrip1()
        {
            TimeSpan now = DateTime.UtcNow.TimeOfDay;

            ICsvTypeConverter conv = CsvConverterFactory.CreateConverter(CsvTypeCode.TimeSpan);

            string?tmp = conv.ConvertToString(now);

            Assert.IsNotNull(tmp);

            var now2 = (TimeSpan?)conv.Parse(tmp);

            Assert.AreEqual(now, now2);
        }
Example #14
0
        public void Roundtrip1()
        {
            var guid = Guid.NewGuid();

            ICsvTypeConverter conv = CsvConverterFactory.CreateConverter(CsvTypeCode.Guid);

            string?tmp = conv.ConvertToString(guid);

            Assert.IsNotNull(tmp);

            var now2 = (Guid?)conv.Parse(tmp);

            Assert.AreEqual(guid, now2);
        }
Example #15
0
        public AccessBenchmark()
        {
            _csv = Properties.Resources.Test1;
            ICsvTypeConverter conv = CsvConverterFactory.CreateConverter(CsvTypeCode.String);

            _indexWrapper = new CsvRecordWrapper();
            _indexWrapper.AddProperty(new CsvIndexProperty("Column0", 0, conv));
            _indexWrapper.AddProperty(new CsvIndexProperty("Column1", 1, conv));
            _indexWrapper.AddProperty(new CsvIndexProperty("Column2", 2, conv));

            _nameWrapper = new CsvRecordWrapper();
            _nameWrapper.AddProperty(new CsvProperty("Column0", new string[] { "Column0" }, conv));
            _nameWrapper.AddProperty(new CsvProperty("Column1", new string[] { "Column1" }, conv));
            _nameWrapper.AddProperty(new CsvProperty("Column2", new string[] { "Column2" }, conv));
        }
Example #16
0
        public void Roundtrip1()
        {
            DateTimeOffset now = new DateTime(2021, 3, 1, 17, 25, 38, DateTimeKind.Local);

            ICsvTypeConverter conv = CsvConverterFactory.CreateConverter(CsvTypeCode.DateTimeOffset);

            string?tmp = conv.ConvertToString(now);

            Assert.IsNotNull(tmp);

            var now2 = (DateTimeOffset?)conv.Parse(tmp);


            Assert.AreEqual(now, now2);
        }
Example #17
0
        public void DateTimeOffsetConverterTest0()
        {
            ICsvTypeConverter conv = CsvConverterFactory.CreateConverter(CsvTypeCode.DateTimeOffset);

            Assert.IsInstanceOfType(conv, typeof(DateTimeOffsetConverter));

            var dt = new DateTime(1975, 07, 14);

            string?s = conv.ConvertToString(new DateTimeOffset(dt));

            Assert.IsNotNull(s);

            var dto = (DateTimeOffset)conv.Parse(s) !;

            Assert.AreEqual(dt, dto.DateTime);
        }
Example #18
0
        public void CreateConverterTest9(bool throwsOnParseErrors)
        {
            ICsvTypeConverter conv = CsvConverterFactory.CreateConverter(CsvTypeCode.Guid, false, false, throwOnParseErrors: throwsOnParseErrors);

            Assert.IsInstanceOfType(conv, typeof(GuidConverter));
            Assert.AreEqual(typeof(Guid), conv.Type);
            Assert.AreEqual(throwsOnParseErrors, conv.ThrowsOnParseErrors);
            Assert.AreEqual(conv.FallbackValue, default(Guid));

            var test1 = Guid.NewGuid();

            string?s = conv.ConvertToString(test1);

            var test2 = (Guid?)conv.Parse(s);

            Assert.AreEqual(test1, test2);
        }
        public void RoundtripTest1()
        {
            var bytes = new byte[7];

            using var rnd = RandomNumberGenerator.Create();
            rnd.GetBytes(bytes);

            ICsvTypeConverter conv = CsvConverterFactory.CreateConverter(CsvTypeCode.ByteArray);

            string?s = conv.ConvertToString(bytes);

            Assert.IsNotNull(s);

            var bytes2 = (byte[]?)conv.Parse(s);

            CollectionAssert.AreEqual(bytes, bytes2);
        }
Example #20
0
        public void CreateConverterTest7(bool throwsOnParseErrors)
        {
            ICsvTypeConverter conv = CsvConverterFactory.CreateConverter(CsvTypeCode.Decimal, false, false, CultureInfo.CreateSpecificCulture("de-DE"), throwOnParseErrors: throwsOnParseErrors);

            Assert.IsInstanceOfType(conv, typeof(NumberConverter <decimal>));
            Assert.AreEqual(typeof(decimal), conv.Type);
            Assert.AreEqual(default(decimal), conv.FallbackValue);
            Assert.AreEqual(throwsOnParseErrors, conv.ThrowsOnParseErrors);

            decimal test = 57.839m;
            string? s    = conv.ConvertToString(test);

            Assert.IsNotNull(s);

            decimal?test2 = (decimal?)conv.Parse(s);

            Assert.AreEqual(test, test2);
        }
Example #21
0
        public void CreateConverterTest12(bool throwsOnParseErrors)
        {
            ICsvTypeConverter conv = CsvConverterFactory.CreateConverter(CsvTypeCode.DateTime, false, false, CultureInfo.InvariantCulture, throwOnParseErrors: throwsOnParseErrors);

            Assert.IsInstanceOfType(conv, typeof(DateTimeConverter));
            Assert.AreEqual(typeof(DateTime), conv.Type);
            Assert.AreEqual(default(DateTime), conv.FallbackValue);
            Assert.AreEqual(throwsOnParseErrors, conv.ThrowsOnParseErrors);

            var    test = new DateTime(1956, 12, 24, 14, 27, 32);
            string?s    = conv.ConvertToString(test);

            Assert.IsNotNull(s);

            var test2 = (DateTime?)conv.Parse(s);

            Assert.AreEqual(test, test2);
        }
Example #22
0
        public void CreateConverterTest15(bool throwsOnParseErrors)
        {
            ICsvTypeConverter conv = CsvConverterFactory.CreateConverter(CsvTypeCode.DateTimeOffset, false, false, CultureInfo.CreateSpecificCulture("de-DE"), throwOnParseErrors: throwsOnParseErrors);

            Assert.IsInstanceOfType(conv, typeof(DateTimeOffsetConverter));
            Assert.AreEqual(typeof(DateTimeOffset), conv.Type);
            Assert.AreEqual(default(DateTimeOffset), conv.FallbackValue);
            Assert.AreEqual(throwsOnParseErrors, conv.ThrowsOnParseErrors);

            var    test = new DateTimeOffset(1956, 12, 24, 14, 27, 32, TimeSpan.FromHours(3));
            string?s    = conv.ConvertToString(test);

            Assert.IsNotNull(s);

            var test2 = (DateTimeOffset?)conv.Parse(s);

            Assert.AreEqual(test, test2);
        }
Example #23
0
        public void CreateConverterTest17(bool throwsOnParseErrors)
        {
            ICsvTypeConverter conv = CsvConverterFactory.CreateConverter(CsvTypeCode.TimeSpan, false, false, CultureInfo.CreateSpecificCulture("de-DE"), throwOnParseErrors: throwsOnParseErrors);

            Assert.IsInstanceOfType(conv, typeof(TimeSpanConverter));
            Assert.AreEqual(typeof(TimeSpan), conv.Type);
            Assert.AreEqual(default(TimeSpan), conv.FallbackValue);
            Assert.AreEqual(throwsOnParseErrors, conv.ThrowsOnParseErrors);

            var    test = new TimeSpan(16, 24, 53);
            string?s    = conv.ConvertToString(test);

            Assert.IsNotNull(s);

            var test2 = (TimeSpan?)conv.Parse(s);

            Assert.AreEqual(test, test2);
        }
Example #24
0
    protected override void InitCsvRecordWrapperUndefinedValues(Tuple <string, ContactProp?, IList <string> > tpl, CsvRecordWrapper wrapper)
    {
        Debug.Assert(tpl.Item2.HasValue);


        if (tpl.Item2 == (ContactProp)AdditionalProp.Swap)
        {
            wrapper.AddProperty(
                new CsvProperty(
                    tpl.Item1,
                    tpl.Item3,
                    CsvConverterFactory.CreateConverter(CsvTypeCode.String, false)));
        }
        else
        {
            wrapper.AddProperty(
                new CsvProperty(
                    tpl.Item1,
                    tpl.Item3,
                    this.StringConverter));
        }
    }
Example #25
0
    private static CsvRecordWrapper InitCsvRecordWrapper()
    {
        var wrapper = new CsvRecordWrapper();

        // Store the stringConverter because you can reuse the same
        // converter for more than one property in CsvRecordWrapper.
        ICsvTypeConverter stringConverter =
            CsvConverterFactory.CreateConverter(CsvTypeCode.String, maybeDBNull: true);

        wrapper.AddProperty
        (
            new CsvProperty(PUPILS_NAME,
                            new string[] { PUPILS_NAME },
                            stringConverter)
        );
        wrapper.AddProperty
        (
            new CsvProperty(SUBJECT,
                            new string[] { SUBJECT },
                            stringConverter)
        );
        wrapper.AddProperty
        (
            new CsvProperty(LESSON_DAY,
                            new string[] { LESSON_DAY },
                            CsvConverterFactory
                            .CreateEnumConverter <DayOfWeek>("G", maybeDBNull: true))
        );
        wrapper.AddProperty
        (
            new CsvProperty(LESSON_BEGIN,
                            new string[] { LESSON_BEGIN },
                            CsvConverterFactory
                            .CreateConverter(CsvTypeCode.TimeSpan, maybeDBNull: true))
        );

        return(wrapper);
    }
Example #26
0
        public void CsvPropertyTest1()
        {
            var prop = new CsvProperty("Prop", new string[] { "Col1" }, CsvConverterFactory.CreateConverter(CsvTypeCode.String));

            Assert.IsInstanceOfType(prop, typeof(CsvProperty));
        }
Example #27
0
    public static void TestDeserializingClassesFromCsv()
    {
        const string csvFileName = "Objects.csv";

        // Create a nonstandard CSV-File
        File.WriteAllText(csvFileName, new StringBuilder()
                          .AppendLine(
                              "Unterrichtstag;Unterrichtsbeginn;Vollständiger Name;Unterrichtsfach;")
                          .AppendLine(
                              "Wednesday;14:30;Susi Meyer;Piano")
                          .AppendLine(
                              "Thursday;15:15;Carl Czerny;Piano;")
                          .AppendLine(
                              ";;Frederic Chopin")
                          .ToString());

        // Initialize a CsvRecordWrapper which retrieves the data from
        // the CSV-Columns and converts it to the right data type.
        // Aliases with wildcards can be used to match the column-headers
        // of the CSV file.
        var wrapper = new CsvRecordWrapper();

        // Reuse a converter for more than one property:
        ICsvTypeConverter stringConverter =
            CsvConverterFactory.CreateConverter(CsvTypeCode.String, nullable: true);

        wrapper.AddProperty
        (
            new CsvProperty("Name",
                            new string[] { "*name" },
                            stringConverter)
        );
        wrapper.AddProperty
        (
            new CsvProperty("Subject",
                            new string[] { "*subject", "*fach" },
                            stringConverter)
        );
        wrapper.AddProperty
        (
            new CsvProperty("LessonDay",
                            new string[] { "*day", "*tag" },
                            CsvConverterFactory
                            .CreateEnumConverter <DayOfWeek>(nullable: true))
        );
        wrapper.AddProperty
        (
            new CsvProperty("LessonBegin",
                            new string[] { "*begin?" },
                            CsvConverterFactory
                            .CreateConverter(CsvTypeCode.TimeSpan, nullable: true))
        );

        // Analyze the CSV file to determine the right parameters
        // for proper reading:
        var analyzer = new CsvAnalyzer();

        analyzer.Analyze(csvFileName);

        // Read the CSV file:
        using var reader =
                  new CsvReader(csvFileName,
                                analyzer.HasHeaderRow,
                                analyzer.Options,
                                analyzer.FieldSeparator);

        var pupilsList = new List <Pupil>();

        foreach (CsvRecord record in reader.Read())
        {
            wrapper.Record = record;

            // Using a dynamic variable enables you to assign
            // the properties without having to explicitely cast them
            // to the target data type:
            dynamic dynWrapper = wrapper;

            pupilsList.Add(new Pupil
            {
                Name        = dynWrapper.Name,
                LessonBegin = dynWrapper.LessonBegin,
                LessonDay   = dynWrapper.LessonDay,
                Subject     = dynWrapper.Subject
            });
        }

        // Write the results to Console:
        foreach (Pupil pupil in pupilsList)
        {
            Console.WriteLine(pupil);
            Console.WriteLine();
        }
    }
        public void ConvertToStringTest_ThrowOnDBNull()
        {
            ICsvTypeConverter conv = CsvConverterFactory.CreateConverter(CsvTypeCode.String);

            _ = conv.ConvertToString(DBNull.Value);
        }
        public void ConvertToStringTest_ThrowOnInvalidType()
        {
            ICsvTypeConverter conv = CsvConverterFactory.CreateConverter(CsvTypeCode.String);

            _ = conv.ConvertToString(4711);
        }
        public void MyTestMethod()
        {
            ICsvTypeConverter conv = CsvConverterFactory.CreateConverter(CsvTypeCode.ByteArray);

            _ = conv.ConvertToString(4711);
        }