public void GetRecord_WhenClassAttributeIsUsedBooleansAreConvertedTheSameWay_ValuesConverted(string order,
                                                                                                     string catsOrDogsJumpInput, bool catsOrDogsJumpExpected,
                                                                                                     string frogsJumpInput, bool frogsJumpExpected)
        {
            // Arrange
            var rowReaderMock = new Mock <IRowReader>();

            rowReaderMock.SetupSequence(m => m.CanRead()).Returns(true).Returns(true).Returns(true).Returns(false);
            rowReaderMock.Setup(m => m.IsRowBlank).Returns(false);
            rowReaderMock.SetupSequence(m => m.ReadRow())
            .Returns(new List <string> {
                "Order", "CatsOrDogsJump", "FrogsJump"
            })
            .Returns(new List <string> {
                order, catsOrDogsJumpInput, frogsJumpInput
            });

            var classUnderTest = new CsvReaderService <CsvConverterBooleanAttributeReadData4>(rowReaderMock.Object);

            classUnderTest.Configuration.HasHeaderRow = true;

            // Act
            CsvConverterBooleanAttributeReadData4 row1 = classUnderTest.GetRecord();
            CsvConverterBooleanAttributeReadData4 row2 = classUnderTest.GetRecord();

            // Assert
            Assert.AreEqual(1, row1.Order);
            Assert.AreEqual(catsOrDogsJumpExpected, row1.CatsOrDogsJump);
            Assert.AreEqual(frogsJumpExpected, row1.FrogsJump);

            Assert.IsNull(row2, "There is no 2nd row!");
            rowReaderMock.VerifyAll();
        }
Esempio n. 2
0
        public void GetRecord_WhenClassAttributeIsUsedDoublesAreConvertedTheSameWay_ValuesComputed(
            string orderInput, int orderExpected,
            string perBodyFatInput, double perBodyFatExpected,
            string perBodyMuscelInput, double perBodyMuscleExpected)
        {
            // Arrange
            var rowReaderMock = new Mock <IRowReader>();

            rowReaderMock.SetupSequence(m => m.CanRead()).Returns(true).Returns(true).Returns(true).Returns(false);
            rowReaderMock.Setup(m => m.IsRowBlank).Returns(false);
            rowReaderMock.SetupSequence(m => m.ReadRow())
            .Returns(new List <string> {
                "Order", "PercentageBodyFat", "PercentageMuscle"
            })
            .Returns(new List <string> {
                orderInput, perBodyFatInput, perBodyMuscelInput
            });

            var classUnderTest = new CsvReaderService <CsvConverterNumberDoubleReadData2>(rowReaderMock.Object);

            classUnderTest.Configuration.HasHeaderRow = true;

            // Act
            CsvConverterNumberDoubleReadData2 row1 = classUnderTest.GetRecord();
            CsvConverterNumberDoubleReadData2 row2 = classUnderTest.GetRecord();

            // Assert
            Assert.AreEqual(orderExpected, row1.Order);
            Assert.AreEqual(perBodyFatExpected, row1.PercentageBodyFat);
            Assert.AreEqual(perBodyMuscleExpected, row1.PercentageMuscle);

            Assert.IsNull(row2, "There is no 2nd row!");
            rowReaderMock.VerifyAll();
        }
        public void GetRecord_ThrowsExceptionsForBogusData_ExceptionThrown(string order, string canJump)
        {
            // Arrange
            var rowReaderMock = new Mock <IRowReader>();

            rowReaderMock.SetupSequence(m => m.CanRead()).Returns(true).Returns(true).Returns(true).Returns(false);
            rowReaderMock.Setup(m => m.IsRowBlank).Returns(false);
            rowReaderMock.SetupSequence(m => m.ReadRow())
            .Returns(new List <string> {
                "Order", "CanJump"
            })
            .Returns(new List <string> {
                order, canJump
            });

            var classUnderTest = new CsvReaderService <CsvConverterBooleanAttributeReadData2>(rowReaderMock.Object);

            classUnderTest.Configuration.HasHeaderRow = true;

            // Act
            CsvConverterBooleanAttributeReadData2 row1 = classUnderTest.GetRecord();
            CsvConverterBooleanAttributeReadData2 row2 = classUnderTest.GetRecord();

            // Assert
            Assert.Fail("Should recieve an exception above for bogus input data");
        }
        [DataRow("1", " moose ", "moose")]  // 1st converter gets NO match and 2nd converter REMOVES spaces
        public void Convert_OrderMatters_FirstDoExactMatchAndThenRemoveSpaces(string order,
                                                                              string animalTypeInput, string animalTypeExpected)
        {
            // Arrange
            var rowReaderMock = new Mock <IRowReader>();

            rowReaderMock.SetupSequence(m => m.CanRead()).Returns(true).Returns(true).Returns(true).Returns(false);
            rowReaderMock.Setup(m => m.IsRowBlank).Returns(false);
            rowReaderMock.SetupSequence(m => m.ReadRow())
            .Returns(new List <string> {
                "Order", "Animal Type"
            })
            .Returns(new List <string> {
                order, animalTypeInput
            });

            var classUnderTest = new CsvReaderService <PropLevelAttributeOrderReadData2>(rowReaderMock.Object);

            classUnderTest.Configuration.HasHeaderRow = true;

            // Act
            PropLevelAttributeOrderReadData2 row1 = classUnderTest.GetRecord();
            PropLevelAttributeOrderReadData2 row2 = classUnderTest.GetRecord();

            // Assert
            Assert.AreEqual(1, row1.Order);
            Assert.AreEqual(animalTypeExpected, row1.AnimalType);

            Assert.IsNull(row2, "There is no 2nd row!");
            rowReaderMock.VerifyAll();
        }
Esempio n. 5
0
        public void GetRecord_ThrowsExceptionsForBogusData_ExceptionThrown(string orderInput, int orderExpected,
                                                                           string perBodyFatInput1, double perBodyFatExpected1,
                                                                           string perBodyFatInput2, double perBodyFatExpected2,
                                                                           string perBodyMuscelInput, double perBodyMuscleExpected)
        {
            // Arrange
            var rowReaderMock = new Mock <IRowReader>();

            rowReaderMock.SetupSequence(m => m.CanRead()).Returns(true).Returns(true).Returns(true).Returns(false);
            rowReaderMock.Setup(m => m.IsRowBlank).Returns(false);
            rowReaderMock.SetupSequence(m => m.ReadRow())
            .Returns(new List <string> {
                "Order", "PercentageBodyFat1", "PercentageBodyFat2", "PercentageMuscle"
            })
            .Returns(new List <string> {
                orderInput, perBodyFatInput1, perBodyFatInput2, perBodyMuscelInput
            });

            var classUnderTest = new CsvReaderService <CsvConverterNumberDoubleReadData3>(rowReaderMock.Object);

            classUnderTest.Configuration.HasHeaderRow = true;

            // Act
            CsvConverterNumberDoubleReadData3 row1 = classUnderTest.GetRecord();
            CsvConverterNumberDoubleReadData3 row2 = classUnderTest.GetRecord();

            // Assert
            Assert.Fail("Should recieve an exception above for bogus input data");
        }
        public void UserCanHaveOneConveterForReadingAndAnotherForWriting()
        {
            // Arrange
            var rowReaderMock = new Mock <IRowReader>();

            rowReaderMock.SetupSequence(m => m.CanRead()).Returns(true).Returns(true).Returns(false);
            rowReaderMock.Setup(m => m.IsRowBlank).Returns(false);
            rowReaderMock.SetupSequence(m => m.ReadRow())
            .Returns(new List <string> {
                "FirstName", "LastName"
            })
            .Returns(new List <string> {
                " John ", " Doe ",
            });

            var classUnderTest = new CsvReaderService <CsvConverterAttributeData4>(
                rowReaderMock.Object);

            classUnderTest.Configuration.HasHeaderRow = true;

            // Act
            CsvConverterAttributeData4 row1 = classUnderTest.GetRecord();
            CsvConverterAttributeData4 row2 = classUnderTest.GetRecord();

            // Assert
            Assert.AreEqual("John", row1.FirstName);
            Assert.AreEqual(" Doe ", row1.LastName);

            Assert.IsNull(row2, "There is no 2nd row!");
            rowReaderMock.VerifyAll();
        }
        public void IfTheUserDoesNotSpecifyAConverterOneIsCreatedAndUsed()
        {
            // Arrange
            var rowReaderMock = new Mock <IRowReader>();

            rowReaderMock.SetupSequence(m => m.CanRead()).Returns(true).Returns(true).Returns(false);
            rowReaderMock.Setup(m => m.IsRowBlank).Returns(false);
            rowReaderMock.SetupSequence(m => m.ReadRow())
            .Returns(new List <string> {
                "Number", "Tall"
            })
            .Returns(new List <string> {
                "1", "true",
            });

            var classUnderTest = new CsvReaderService <CsvConverterAttributeData3>(
                rowReaderMock.Object);

            classUnderTest.Configuration.HasHeaderRow = true;

            // Act
            CsvConverterAttributeData3 row1 = classUnderTest.GetRecord();
            CsvConverterAttributeData3 row2 = classUnderTest.GetRecord();

            // Assert
            Assert.AreEqual(1, row1.Order);

            Assert.IsNull(row2, "There is no 2nd row!");
            rowReaderMock.VerifyAll();
        }
        public void YouCanSpecifyBothAnIgnoreReadAndWriteOnAnObjectAndNotGetAnException()
        {
            // Arrange
            var rowReaderMock = new Mock <IRowReader>();

            rowReaderMock.SetupSequence(m => m.CanRead()).Returns(true).Returns(true).Returns(false);
            rowReaderMock.Setup(m => m.IsRowBlank).Returns(false);
            rowReaderMock.SetupSequence(m => m.ReadRow())
            .Returns(new List <string> {
                "Order", "Tall"
            })
            .Returns(new List <string> {
                "1", "true",
            });

            var classUnderTest = new CsvReaderService <CsvConverterAttributeData1>(
                rowReaderMock.Object);

            classUnderTest.Configuration.HasHeaderRow = true;

            // Act
            CsvConverterAttributeData1 row1 = classUnderTest.GetRecord();
            CsvConverterAttributeData1 row2 = classUnderTest.GetRecord();

            // Assert
            Assert.AreEqual(1, row1.Order);

            Assert.IsNull(row2, "There is no 2nd row!");
            rowReaderMock.VerifyAll();
        }
        public void GetRecord_ConvertDatesOfVariousFormats()
        {
            // Arrange
            var rowReaderMock = new Mock <IRowReader>();

            rowReaderMock.SetupSequence(m => m.CanRead()).Returns(true).Returns(true).Returns(true).Returns(false);
            rowReaderMock.Setup(m => m.IsRowBlank).Returns(false);
            rowReaderMock.SetupSequence(m => m.ReadRow())
            .Returns(new List <string> {
                "Order", "SomeDate1", "SomeDate2", "SomeDate3", "SomeDate4", "SomeDate5"
            })
            .Returns(new List <string> {
                "1", "2017-05-08 14:40:12", "5/8/2017", "Sunday, March 09, 2008", "3/9/2008 4:05 PM", "March, 2008"
            })
            .Returns(new List <string> {
                "2", "2018-05-27 08:50:18", "6/14/2019", "Saturday, May 05, 2018", "8/11/2018 5:15 PM", "June, 2018"
            })
            .Returns(new List <string> {
                "3", "2016-09-08 18:21:02", "9/18/2011", "Wednesday, March 19, 2008", "8/9/2008 4:05 PM", "January, 2014"
            });

            var classUnderTest = new CsvReaderService <CsvConverterDateTimeReadData1>(rowReaderMock.Object);

            classUnderTest.Configuration.HasHeaderRow = true;

            // Act
            CsvConverterDateTimeReadData1 row1 = classUnderTest.GetRecord();
            CsvConverterDateTimeReadData1 row2 = classUnderTest.GetRecord();
            CsvConverterDateTimeReadData1 row3 = classUnderTest.GetRecord();
            CsvConverterDateTimeReadData1 row4 = classUnderTest.GetRecord();

            // Assert
            Assert.AreEqual(1, row1.Order);
            Assert.AreEqual(new DateTime(2017, 5, 8, 14, 40, 12), row1.SomeDate1);
            Assert.AreEqual(new DateTime(2017, 5, 8, 00, 00, 00), row1.SomeDate2);
            Assert.AreEqual(new DateTime(2008, 3, 9, 00, 00, 00), row1.SomeDate3);
            Assert.AreEqual(new DateTime(2008, 3, 9, 16, 05, 00), row1.SomeDate4);
            Assert.AreEqual(new DateTime(2008, 3, 1, 00, 00, 00), row1.SomeDate5);

            Assert.AreEqual(2, row2.Order);
            Assert.AreEqual(new DateTime(2018, 5, 27, 8, 50, 18), row2.SomeDate1);
            Assert.AreEqual(new DateTime(2019, 6, 14, 00, 00, 00), row2.SomeDate2);
            Assert.AreEqual(new DateTime(2018, 5, 5, 00, 00, 00), row2.SomeDate3);
            Assert.AreEqual(new DateTime(2018, 8, 11, 17, 15, 00), row2.SomeDate4);
            Assert.AreEqual(new DateTime(2018, 6, 1, 00, 00, 00), row2.SomeDate5);

            Assert.AreEqual(3, row3.Order);
            Assert.AreEqual(new DateTime(2016, 9, 8, 18, 21, 02), row3.SomeDate1);
            Assert.AreEqual(new DateTime(2011, 9, 18, 00, 00, 00), row3.SomeDate2);
            Assert.AreEqual(new DateTime(2008, 3, 19, 00, 00, 00), row3.SomeDate3);
            Assert.AreEqual(new DateTime(2008, 8, 9, 16, 05, 00), row3.SomeDate4);
            Assert.AreEqual(new DateTime(2014, 1, 1, 00, 00, 00), row3.SomeDate5);

            Assert.IsNull(row4, "There is no 4th row!");
            rowReaderMock.VerifyAll();
        }
Esempio n. 10
0
        public void GetRecord_CanComputeDoublePlacesOnMultipleRows_ValuesComputed()
        {
            // Arrange
            var rowReaderMock = new Mock <IRowReader>();

            rowReaderMock.SetupSequence(m => m.CanRead()).Returns(true).Returns(true).Returns(true).Returns(false);
            rowReaderMock.Setup(m => m.IsRowBlank).Returns(false);
            rowReaderMock.SetupSequence(m => m.ReadRow())
            .Returns(new List <string> {
                "Order", "PercentageBodyFat", "PercentageMuscle", "Length", "LengthArms"
            })
            .Returns(new List <string> {
                "1", "34.56789", "78.33212", "98.34222", "67.94783"
            })
            .Returns(new List <string> {
                "2", "67.89004", "79.33212", "87.38278", "68.94783"
            })
            .Returns(new List <string> {
                "3", "948.5334", "80.33212", "7645.322", "69.94783"
            });

            var classUnderTest = new CsvReaderService <CsvConverterNumberDoubleReadData1>(rowReaderMock.Object);

            classUnderTest.Configuration.HasHeaderRow = true;

            // Act
            CsvConverterNumberDoubleReadData1 row1 = classUnderTest.GetRecord();
            CsvConverterNumberDoubleReadData1 row2 = classUnderTest.GetRecord();
            CsvConverterNumberDoubleReadData1 row3 = classUnderTest.GetRecord();
            CsvConverterNumberDoubleReadData1 row4 = classUnderTest.GetRecord();

            // Assert
            Assert.AreEqual(1, row1.Order);
            Assert.AreEqual(34.57, row1.PercentageBodyFat);
            Assert.AreEqual(78.3, row1.PercentageMuscle);
            Assert.AreEqual(98.342, row1.Length);
            Assert.AreEqual(67.9478, row1.LengthArms);

            Assert.AreEqual(2, row2.Order);
            Assert.AreEqual(67.89, row2.PercentageBodyFat);
            Assert.AreEqual(79.3, row2.PercentageMuscle);
            Assert.AreEqual(87.383, row2.Length);
            Assert.AreEqual(68.9478, row2.LengthArms);

            Assert.AreEqual(3, row3.Order);
            Assert.AreEqual(80.3, row3.PercentageMuscle);
            Assert.AreEqual(948.53, row3.PercentageBodyFat);
            Assert.AreEqual(7645.322, row3.Length);
            Assert.AreEqual(69.9478, row3.LengthArms);

            Assert.IsNull(row4, "There is no 4th row!");
            rowReaderMock.VerifyAll();
        }
        public void IfColumnNameIsSpecifiedAtTheClassLevel_YouGetCsvConverterAttributeException()
        {
            // Arrange
            var rowReaderMock = new Mock <IRowReader>();

            rowReaderMock.SetupSequence(m => m.CanRead()).Returns(true).Returns(true).Returns(false);
            rowReaderMock.Setup(m => m.IsRowBlank).Returns(false);
            rowReaderMock.SetupSequence(m => m.ReadRow())
            .Returns(new List <string> {
                "Name"
            })
            .Returns(new List <string> {
                "John"
            });

            var classUnderTest = new CsvReaderService <CsvConverterAttributeData6>(
                rowReaderMock.Object);

            classUnderTest.Configuration.HasHeaderRow = true;

            // Act
            var row1 = classUnderTest.GetRecord();

            // Assert
            Assert.Fail("An exception should have been genearted for using ColumnIndex at the class level");
        }
Esempio n. 12
0
        private void CustomConverterLoadCsvButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var dialog = LoadFile(CustomConverterCSVFile.Text);
                if (dialog.ShowDialog() != true)
                {
                    return;
                }

                using (var fs = File.OpenRead(dialog.FileName))
                    using (var sr = new StreamReader(fs, Encoding.Default))
                    {
                        ICsvReaderService <Person> reader = new CsvReaderService <Person>(sr);
                        reader.Configuration.BlankRowsAreReturnedAsNull = true;

                        while (reader.CanRead())
                        {
                            Person record = reader.GetRecord();
                            if (record != null)
                            {
                                LogMessage(record.ToString());
                            }
                        }
                    }
            }
            catch (Exception ex)
            {
                LogError(ex);
            }
        }
        public void SpecifyingAltColumnNamesOnAPostConverter_YouGetCsvConverterAttributeException()
        {
            // Arrange
            var rowReaderMock = new Mock <IRowReader>();

            rowReaderMock.SetupSequence(m => m.CanRead()).Returns(true).Returns(true).Returns(false);
            rowReaderMock.Setup(m => m.IsRowBlank).Returns(false);
            rowReaderMock.SetupSequence(m => m.ReadRow())
            .Returns(new List <string> {
                "FName", "LastName"
            })
            .Returns(new List <string> {
                " John ", " Doe ",
            });

            var classUnderTest = new CsvReaderService <CsvConverterAttributeData14>(
                rowReaderMock.Object);

            classUnderTest.Configuration.HasHeaderRow = true;

            // Act
            var row1 = classUnderTest.GetRecord();

            // Assert
            Assert.Fail("Should have got an error for specifing the AltColumnNames on a post-converter");
        }
Esempio n. 14
0
        private void LoadCsvButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var dialog = new Microsoft.Win32.OpenFileDialog();
                dialog.InitialDirectory = string.IsNullOrEmpty(CSVFile.Text) ?
                                          "c:\\" : Path.GetDirectoryName(CSVFile.Text);
                dialog.FileName = CSVFile.Text;
                if (dialog.ShowDialog() != true)
                {
                    return;
                }

                using (var fs = File.OpenRead(dialog.FileName))
                    using (var sr = new StreamReader(fs, Encoding.Default))
                    {
                        var csv = new CsvReaderService <Employee>(sr);
                        csv.Configuration.BlankRowsAreReturnedAsNull = true;

                        while (csv.CanRead())
                        {
                            Employee record = csv.GetRecord();
                            LogMessage(record.ToString());
                        }
                    }
            }
            catch (Exception ex)
            {
                LogError(ex);
            }
        }
        public void ReadingCsv_ClassLevelPropertiesCanReplaceStringsMoreThanOneProperty_ValuesConverted()
        {
            // Arrange
            var rowReaderMock = new Mock <IRowReader>();

            rowReaderMock.SetupSequence(m => m.CanRead()).Returns(true).Returns(true).Returns(true).Returns(false);
            rowReaderMock.Setup(m => m.IsRowBlank).Returns(false);
            rowReaderMock.SetupSequence(m => m.ReadRow())
            .Returns(new List <string> {
                "Order", "SomeText", "OtherText"
            })
            .Returns(new List <string> {
                "1", "dog", "hey1"
            })
            .Returns(new List <string> {
                "2", "Dog", "hey2"
            })
            .Returns(new List <string> {
                "3", "Rocks", "hey3"
            });

            var classUnderTest = new CsvReaderService <CsvConverterStringOldAndNewReadData2>(rowReaderMock.Object);

            classUnderTest.Configuration.HasHeaderRow = true;

            // Act
            CsvConverterStringOldAndNewReadData2 row1 = classUnderTest.GetRecord();
            CsvConverterStringOldAndNewReadData2 row2 = classUnderTest.GetRecord();
            CsvConverterStringOldAndNewReadData2 row3 = classUnderTest.GetRecord();
            CsvConverterStringOldAndNewReadData2 row4 = classUnderTest.GetRecord();

            // Assert
            Assert.AreEqual(1, row1.Order);
            Assert.AreEqual("cat", row1.SomeText);
            Assert.AreEqual("hey1", row1.OtherText, "This text should NOT have been touched");

            Assert.AreEqual(2, row2.Order);
            Assert.AreEqual("Dog", row2.SomeText);
            Assert.AreEqual("jelly", row2.OtherText);

            Assert.AreEqual(3, row3.Order);
            Assert.AreEqual("Rocks", row3.SomeText);
            Assert.AreEqual("hey3", row3.OtherText, "This text should NOT have been touched");

            Assert.IsNull(row4, "There is no 4th row!");
            rowReaderMock.VerifyAll();
        }
Esempio n. 16
0
        public void GetRecord_CanLoadHeaderRow_ValuesComputed()
        {
            // Arrange
            var rowReaderMock = new Mock <IRowReader>();

            rowReaderMock.SetupSequence(m => m.CanRead()).Returns(true).Returns(true).Returns(true).Returns(false);
            rowReaderMock.Setup(m => m.IsRowBlank).Returns(false);
            rowReaderMock.SetupSequence(m => m.ReadRow())
            .Returns(new List <string> {
                "Number", "PercentageBodyFat", "PercentageMuscle", "Length", "LengthArms"
            })
            .Returns(new List <string> {
                "1", "34.56789", "78.33212", "98.34222", "67.94783"
            })
            .Returns(new List <string> {
                "2", "67.89004", "79.33212", "87.38278", "68.94783"
            });

            var classUnderTest = new CsvReaderService <ReadHeaderTestsData1>(rowReaderMock.Object);

            classUnderTest.Configuration.HasHeaderRow = true;

            // TODO: TESTING Mapping
            //here

            // Act
            ReadHeaderTestsData1 row1 = classUnderTest.GetRecord();
            ReadHeaderTestsData1 row2 = classUnderTest.GetRecord();
            ReadHeaderTestsData1 row3 = classUnderTest.GetRecord();

            // Assert
            Assert.AreEqual(1, row1.Order);
            Assert.AreEqual(34.57, row1.PercentageBodyFat);
            Assert.AreEqual(78.3, row1.PercentageMuscle);
            Assert.AreEqual(98.342, row1.Length);
            Assert.AreEqual(67.9478, row1.LengthArms);

            Assert.AreEqual(2, row2.Order);
            Assert.AreEqual(67.89, row2.PercentageBodyFat);
            Assert.AreEqual(79.3, row2.PercentageMuscle);
            Assert.AreEqual(87.383, row2.Length);
            Assert.AreEqual(68.9478, row2.LengthArms);

            Assert.IsNull(row3, "There is no 3rd row!");
            rowReaderMock.VerifyAll();
        }
        public void GetRecord_ConvertBooleansOfVariousFormatsWithoutAnAttribute_ValuesConverted()
        {
            // Arrange
            var rowReaderMock = new Mock <IRowReader>();

            rowReaderMock.SetupSequence(m => m.CanRead()).Returns(true).Returns(true).Returns(true).Returns(false);
            rowReaderMock.Setup(m => m.IsRowBlank).Returns(false);
            rowReaderMock.SetupSequence(m => m.ReadRow())
            .Returns(new List <string> {
                "Order", "Bool1", "Bool2", "Bool3", "Bool4", "Bool5"
            })
            .Returns(new List <string> {
                "1", "Yes", "Y", "True", "T", "1"
            })
            .Returns(new List <string> {
                "2", "No", "N", "False", "F", "0"
            });

            var classUnderTest = new CsvReaderService <CsvConverterBooleanAttributeReadData1>(rowReaderMock.Object);

            classUnderTest.Configuration.HasHeaderRow = true;

            // Act
            CsvConverterBooleanAttributeReadData1 row1 = classUnderTest.GetRecord();
            CsvConverterBooleanAttributeReadData1 row2 = classUnderTest.GetRecord();
            CsvConverterBooleanAttributeReadData1 row3 = classUnderTest.GetRecord();

            // Assert
            Assert.AreEqual(1, row1.Order);
            Assert.AreEqual(true, row1.Bool1);
            Assert.AreEqual(true, row1.Bool2);
            Assert.AreEqual(true, row1.Bool3);
            Assert.AreEqual(true, row1.Bool4);
            Assert.AreEqual(true, row1.Bool5);

            Assert.AreEqual(2, row2.Order);
            Assert.AreEqual(false, row2.Bool1);
            Assert.AreEqual(false, row2.Bool2);
            Assert.AreEqual(false, row2.Bool3);
            Assert.AreEqual(false, row2.Bool4);
            Assert.AreEqual(false, row2.Bool5);

            Assert.IsNull(row3, "There is no 3rd row!");
            rowReaderMock.VerifyAll();
        }
        public void GetRecord_WhenClassAttributeIsUsedDatesAreConvertedTheSameWay_ValuesConverted(string order,
                                                                                                  string someDate1, int month1, int day1, int year1,
                                                                                                  string someDate2, int month2, int day2, int year2)
        {
            // Arrange
            var rowReaderMock = new Mock <IRowReader>();

            rowReaderMock.SetupSequence(m => m.CanRead()).Returns(true).Returns(true).Returns(true).Returns(false);
            rowReaderMock.Setup(m => m.IsRowBlank).Returns(false);
            rowReaderMock.SetupSequence(m => m.ReadRow())
            .Returns(new List <string> {
                "Order", "SomeDate1", "SomeDate2"
            })
            .Returns(new List <string> {
                order, someDate1, someDate2
            });

            var classUnderTest = new CsvReaderService <CsvConverterDateTimeReadData2>(rowReaderMock.Object);

            classUnderTest.Configuration.HasHeaderRow = true;

            // Act
            CsvConverterDateTimeReadData2 row1 = classUnderTest.GetRecord();
            CsvConverterDateTimeReadData2 row2 = classUnderTest.GetRecord();

            // Assert
            Assert.AreEqual(1, row1.Order);
            Assert.AreEqual(year1, row1.SomeDate1.Year);
            Assert.AreEqual(month1, row1.SomeDate1.Month);
            Assert.AreEqual(day1, row1.SomeDate1.Day);
            Assert.AreEqual(year2, row1.SomeDate2.Value.Year);
            Assert.AreEqual(month2, row1.SomeDate2.Value.Month);
            Assert.AreEqual(day2, row1.SomeDate2.Value.Day);

            Assert.IsNull(row2, "There is no 2nd row!");
            rowReaderMock.VerifyAll();
        }
Esempio n. 19
0
        public void GetRecord_WhenClassAttributeIsUsedYouCanOverrideIndividualProperties_ValuesComputed(
            string orderInput, int orderExpected,
            string perBodyFatInput1, double perBodyFatExpected1,
            string perBodyFatInput2, double perBodyFatExpected2,
            string perBodyMuscelInput, double perBodyMuscleExpected)
        {
            // Arrange
            var rowReaderMock = new Mock <IRowReader>();

            rowReaderMock.SetupSequence(m => m.CanRead()).Returns(true).Returns(true).Returns(true).Returns(false);
            rowReaderMock.Setup(m => m.IsRowBlank).Returns(false);
            rowReaderMock.SetupSequence(m => m.ReadRow())
            .Returns(new List <string> {
                "Order", "PercentageBodyFat1", "PercentageBodyFat2", "PercentageMuscle"
            })
            .Returns(new List <string> {
                orderInput, perBodyFatInput1, perBodyFatInput2, perBodyMuscelInput
            });

            var classUnderTest = new CsvReaderService <CsvConverterNumberDoubleReadData3>(rowReaderMock.Object);

            classUnderTest.Configuration.HasHeaderRow = true;

            // Act
            CsvConverterNumberDoubleReadData3 row1 = classUnderTest.GetRecord();
            CsvConverterNumberDoubleReadData3 row2 = classUnderTest.GetRecord();

            // Assert
            Assert.AreEqual(orderExpected, row1.Order);
            Assert.AreEqual(perBodyFatExpected1, row1.PercentageBodyFat1, "PercentageBodyFat1 MidpointRounding.ToEven");
            Assert.AreEqual(perBodyFatExpected2, row1.PercentageBodyFat2, "PercentageBodyFat2 MidpointRounding.AwayFromZero");
            Assert.AreEqual(perBodyMuscleExpected, row1.PercentageMuscle);

            Assert.IsNull(row2, "There is no 2nd row!");
            rowReaderMock.VerifyAll();
        }
Esempio n. 20
0
        public void GetRecord_BlankRowsAreReturnedAsNull_BlankRowsIgnored()
        {
            var tempFileName = Path.GetTempFileName();

            try
            {
                using (var fs = File.Create(tempFileName))
                    using (var sw = new StreamWriter(fs))
                    {
                        sw.WriteLine("Name,Age");
                        sw.WriteLine("Fido,1");
                        sw.WriteLine(",");
                        sw.WriteLine("");
                        sw.WriteLine("Killer,2");
                        sw.WriteLine("Jaws,3");
                        sw.WriteLine("   ");
                        sw.WriteLine("Fluffy,4");
                        sw.WriteLine("John the dog,5");
                        sw.WriteLine("   ");
                    }

                var result = new List <Dog>();
                using (var fs = File.OpenRead(tempFileName))
                    using (var sr = new StreamReader(fs))
                    {
                        var reader = new CsvReaderService <Dog>(sr);
                        reader.Configuration.HasHeaderRow = true;
                        reader.Configuration.BlankRowsAreReturnedAsNull = true; // HERE IS WHAT WE ARE TESTING!!
                        while (reader.CanRead())
                        {
                            var data = reader.GetRecord();
                            if (data != null)
                            {
                                result.Add(data);
                            }
                        }
                    }

                Assert.AreEqual(5, result.Count, "Expecting only 5 dogs!");
            }
            catch
            {
                File.Delete(tempFileName);
                throw;
            }
        }
Esempio n. 21
0
        public void GetRecord_CanHandelClassWithOneProperty_DataRead(int expectedNumberOfRows, bool blankRowsAreReturnedAsNull)
        {
            var tempFileName = Path.GetTempFileName();

            try
            {
                using (var fs = File.Create(tempFileName))
                    using (var sw = new StreamWriter(fs))
                    {
                        sw.WriteLine("Name");
                        sw.WriteLine("George");
                        sw.WriteLine("");
                        sw.WriteLine("Bob");
                        sw.WriteLine("Tom");
                        sw.WriteLine("   ");
                        sw.WriteLine("Ralph");
                        sw.WriteLine("James");
                        sw.WriteLine("   ");
                    }

                var result = new List <ClassWithOneProperty>();
                using (var fs = File.OpenRead(tempFileName))
                    using (var sr = new StreamReader(fs))
                    {
                        var reader = new CsvReaderService <ClassWithOneProperty>(sr);
                        reader.Configuration.HasHeaderRow = true;
                        reader.Configuration.BlankRowsAreReturnedAsNull = blankRowsAreReturnedAsNull;
                        while (reader.CanRead())
                        {
                            var data = reader.GetRecord();
                            if (data != null || blankRowsAreReturnedAsNull == false)
                            {
                                result.Add(data);
                            }
                        }
                    }

                Assert.AreEqual(expectedNumberOfRows, result.Count, $"Expecting {expectedNumberOfRows} names!");
            }
            catch
            {
                File.Delete(tempFileName);
                throw;
            }
        }
Esempio n. 22
0
 private static void ReadItems(string fileName)
 {
     using (var fs = File.OpenRead(fileName))
         using (var sr = new StreamReader(fs))
         {
             ICsvReaderService <TestData> reader = new CsvReaderService <TestData>(sr);
             reader.Configuration.HasHeaderRow = true;
             reader.Configuration.BlankRowsAreReturnedAsNull = true;
             while (reader.CanRead())
             {
                 TestData item = reader.GetRecord();
                 if (item != null)
                 {
                     Console.WriteLine(item.FieldDescription);
                 }
             }
         }
 }
        public void CanReadAndWriteData()
        {
            string fileName = GetTestFileNameAndPath();

            var dataList = new List <OddlyShapedData>();

            using (var fs = File.OpenRead(fileName))
                using (var sr = new StreamReader(fs, Encoding.Default))
                {
                    var csv = new CsvReaderService <OddlyShapedData>(sr);
                    csv.Configuration.HasHeaderRow = true;
                    csv.Configuration.BlankRowsAreReturnedAsNull         = true;
                    csv.Configuration.ThrowExceptionIfColumnCountChanges = false;

                    while (csv.CanRead())
                    {
                        dataList.Add(csv.GetRecord());
                    }
                }

            OddlyShapedData data1 = dataList.FirstOrDefault(w => w.FirstName == "David");

            Assert.IsNotNull(data1, "Could not find record with David Jackson");
            Assert.AreEqual("Jackson", data1.LastName);
            Assert.AreEqual(45, data1.Age);
            Assert.AreEqual(72, data1.HeightInInches);

            OddlyShapedData data2 = dataList.FirstOrDefault(w => w.FirstName == "Heather");

            Assert.IsNotNull(data2, "Could not find record with Heather Thomas");
            Assert.AreEqual("Thomas", data2.LastName);
            Assert.AreEqual(0, data2.Age);
            Assert.AreEqual(0, data2.HeightInInches);


            OddlyShapedData data3 = dataList.FirstOrDefault(w => w.FirstName == "Janet");

            Assert.IsNotNull(data3, "Could not find record with Janet Reno");
            Assert.AreEqual("Reno", data3.LastName);
            Assert.AreEqual(65, data3.Age);
            Assert.AreEqual(70, data3.HeightInInches);
        }
Esempio n. 24
0
        private static List <Squirrel> ReadSquirrels(string squirrelFileName)
        {
            var result = new List <Squirrel>();

            using (var fs = File.OpenRead(squirrelFileName))
                using (var sr = new StreamReader(fs))
                {
                    ICsvReaderService <Squirrel> reader = new CsvReaderService <Squirrel>(sr);
                    reader.Configuration.HasHeaderRow = true;
                    reader.Configuration.BlankRowsAreReturnedAsNull = true;
                    while (reader.CanRead())
                    {
                        Squirrel item = reader.GetRecord();
                        if (item != null)
                        {
                            result.Add(item);
                        }
                    }
                }

            return(result);
        }
Esempio n. 25
0
        public void CanReadAndWriteData()
        {
            string fileName = GetTestFileNameAndPath();

            var studentList = new List <Graduation>();

            using (var fs = File.OpenRead(fileName))
                using (var sr = new StreamReader(fs, Encoding.Default))
                {
                    var csv = new CsvReaderService <Graduation>(sr);
                    csv.Configuration.HasHeaderRow = true;
                    csv.Configuration.BlankRowsAreReturnedAsNull = true;

                    while (csv.CanRead())
                    {
                        studentList.Add(csv.GetRecord());
                    }
                }

            // Create a temp file
            string tempFileName = Path.Combine(
                Path.GetDirectoryName(fileName),
                Path.GetFileNameWithoutExtension(fileName) + "Temp.csv");

            using (var fs = File.Create(tempFileName))
                using (var sw = new StreamWriter(fs, Encoding.Default))
                {
                    var service = new CsvWriterService <Graduation>(sw);
                    service.Configuration.HasHeaderRow = true;
                    foreach (var student in studentList)
                    {
                        service.WriteRecord(student);
                    }
                }

            Assert.IsTrue(FilesAreEqual(fileName, tempFileName));
        }
        private void LoadFileButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var dialog = LoadFile("c:\\temp\\Person.csv");
                if (dialog.ShowDialog() != true)
                {
                    return;
                }

                LogMessage("Before overriding");
                using (var fs = File.OpenRead(dialog.FileName))
                    using (var sr = new StreamReader(fs))
                    {
                        ICsvReaderService <Person> reader = new CsvReaderService <Person>(sr);

                        while (reader.CanRead())
                        {
                            var person = reader.GetRecord();
                            LogMessage(person.ToString());
                        }
                    }

                LogMessage("After overriding");
                using (var fs = File.OpenRead(dialog.FileName))
                    using (var sr = new StreamReader(fs))
                    {
                        // Keep in mind that your replacement converter must be standalone.
                        // You cannot rely in any default converters.
                        // Default converters created when the service class is created.
                        ICsvReaderService <Person> reader = new CsvReaderService <Person>(sr);

                        // Replace a converter
                        reader.DefaultConverterFactory.ReplaceConverter(typeof(string), typeof(CsvConverterStringTextLengthEnforcer));

                        // Create all the column mappings.
                        reader.Init();

                        #region This converter has settings different that the defaults when it was constructed
                        // Well, you will have to find the columns with the converter that need changing

                        // We could just find string properties since it is the default, but that is problematic.
                        // What if a class property, which is a string, has a custom converter on it?
                        // So, let's just find the properties that are using our new default converter.
                        List <ColumnToPropertyMap> columnList = reader.FindColumnsByConverterType(typeof(CsvConverterStringTextLengthEnforcer));

                        foreach (ColumnToPropertyMap oneColumn in columnList)
                        {
                            if (oneColumn.ReadConverter is CsvConverterStringTextLengthEnforcer converter)
                            {
                                converter.MinimumLength = 5;
                                converter.MaximumLength = 6;
                                converter.CharacterToAddToShortStrings = '-';
                            }
                        }
                        #endregion

                        while (reader.CanRead())
                        {
                            var person = reader.GetRecord();
                            LogMessage(person.ToString());
                        }
                    }
            }
            catch (Exception ex)
            {
                LogError(ex);
            }
        }