public void CanReadDoubleWithPercentSign() { // Arrange // Arrange // Arrange var originalData = new List <NumberTestExampe1>(); originalData.Add(new NumberTestExampe1 { Id = 1, DoubleValue = "56.1%" }); originalData.Add(new NumberTestExampe1 { Id = 2, DoubleValue = "-45.2%" }); originalData.Add(new NumberTestExampe1 { Id = 3, DoubleValue = "123%" }); // Act // Act // Act // Using a different class with similar properties WITHOUT the ignore attribute to prove that nothing was truly written // if we used the same class the reader would not even look for the Name field. var saveAndReadHelper = new SaveAndReadHelper <NumberTestExampe1, NumberTestExampe2>(); List <NumberTestExampe2> actualList = saveAndReadHelper.SaveAndRead(originalData, false); // Assert // Assert // Assert CompareDoubleValue(actualList, 1, .561d); CompareDoubleValue(actualList, 2, -.452d); CompareDoubleValue(actualList, 3, 1.23d); }
public void CanReadDecimalWithExponentNotation() { // Arrange // Arrange // Arrange var originalData = new List <NumberTestExampe1>(); originalData.Add(new NumberTestExampe1 { Id = 1, DecimalValue = "123.30" }); originalData.Add(new NumberTestExampe1 { Id = 2, DecimalValue = "-1.6530975699424744E-8" }); originalData.Add(new NumberTestExampe1 { Id = 3, DecimalValue = "1.6530E+5" }); // Act // Act // Act // Using a different class with similar properties WITHOUT the ignore attribute to prove that nothing was truly written // if we used the same class the reader would not even look for the Name field. var saveAndReadHelper = new SaveAndReadHelper <NumberTestExampe1, NumberTestExampe2>(); List <NumberTestExampe2> actualList = saveAndReadHelper.SaveAndRead(originalData, false); // Assert // Assert // Assert CompareDecimalValue(actualList, 1, 123.30m); CompareDecimalValue(actualList, 2, -.000000016530975699424744m); CompareDecimalValue(actualList, 3, 165300.00m); }
public void We_get_do_not_get_a_header_column_missing_errors_when_we_use_ClassToExcelAttribute_IsOptional() { // Arrange // Arrange // Arrange _messages.Clear(); var originalData = new List <OptionalTestsExampe1>(); originalData.Add(new OptionalTestsExampe1 { Id = 1, LastName = "McMasters" }); originalData.Add(new OptionalTestsExampe1 { Id = 2, LastName = "Jones" }); // Act // Act // Act // Using a different class with similar properties WITHOUT the ignore attribute to prove that nothing was truly written // if we used the same class the reader would not even look for the Name field. var saveAndReadHelper = new SaveAndReadHelper <OptionalTestsExampe1, OptionalTestsExampe2>(); List <OptionalTestsExampe2> actualList = saveAndReadHelper.SaveAndRead(originalData, true, LogMessage, LogMessage); // Assert // Assert // Assert Assert.IsFalse( _messages.Any( w => w.MessageType == ClassToExcelMessageType.HeaderProblem && w.ColumnName == "First Name"), "We should not have any header problems listed because the 'First Name' column is marked as optional!"); }
public void When_Ignore_is_used_with_the_ClassToExcelAttribute_data_is_not_read_from_the_file() { // Arrange // Arrange // Arrange var originalData = new List <IgnoreTestsExampe2>(); originalData.Add(new IgnoreTestsExampe2 { Id = 1, Name = "Some Name" }); originalData.Add(new IgnoreTestsExampe2 { Id = 2, Name = "Some Other Name" }); // Act // Act // Act // Using a different class with similar properties WITHOUT the ignore attribute to prove that nothing was truly written // if we used the same class the reader would not even look for the Name field. var saveAndReadHelper = new SaveAndReadHelper <IgnoreTestsExampe2, IgnoreTestsExampe1>(); List <IgnoreTestsExampe1> actualList = saveAndReadHelper.SaveAndRead(originalData, true); // Assert // Assert // Assert IgnoreTestsExampe1 data1 = actualList.FirstOrDefault(w => w.Id == 1); Assert.IsNotNull(data1, "Unable to find Id == 1 so data was not written properly"); Assert.IsTrue(string.IsNullOrEmpty(data1.Name)); IgnoreTestsExampe1 data2 = actualList.FirstOrDefault(w => w.Id == 2); Assert.IsNotNull(data2, "Unable to find Id == 2 so data was not written properly"); Assert.IsTrue(string.IsNullOrEmpty(data2.Name)); }
public void CanReadAndWriteRandomChickenDataWithNoHeaders() { const bool hasHeaderRow = false; // Arrange List <Chicken> expectedList = ChickenCreator.Work(20); // Act var saveAndReadHelper = new SaveAndReadHelper <Chicken, Chicken>(); var actualList = saveAndReadHelper.SaveAndRead(expectedList, hasHeaderRow); // Assert ChickenHelper.CompareExpectedWithActualAndAssertIfNotEqual(expectedList, actualList); }
public void CanReadAndWriteRandomPersonDataWithHeaders() { const bool hasHeaderRow = true; // Arrange List <Person> expectedList = PersonCreator.Work(20); // Act var saveAndReadHelper = new SaveAndReadHelper <Person, Person>(); var actualList = saveAndReadHelper.SaveAndRead(expectedList, hasHeaderRow); // Assert PersonHelper.CompareExpectedWithActualAndAssertIfNotEqual(expectedList, actualList); }
public void Object_properties_are_ignored() { // Arrange // Arrange // Arrange var originalData = new List <IgnoreTestsExampe4>(); originalData.Add(new IgnoreTestsExampe4 { Id = 1, Name = "Random Name 1", Owner = new IgnoreTestsExampe1() { Id = 4, Name = "Owner dude" } }); originalData.Add(new IgnoreTestsExampe4 { Id = 2, Name = "Random Name 2", Owner = new IgnoreTestsExampe1() { Id = 5, Name = "Owner dude3" } }); // Act // Act // Act // Using a different class with similar properties WITHOUT the ignore attribute to prove that nothing was truly written // if we used the same class the reader would not even look for the Name field. var saveAndReadHelper = new SaveAndReadHelper <IgnoreTestsExampe4, IgnoreTestsExampe4>(); List <IgnoreTestsExampe4> actualList = saveAndReadHelper.SaveAndRead(originalData, true); // Assert // Assert // Assert IgnoreTestsExampe4 data1 = actualList.FirstOrDefault(w => w.Id == 1); Assert.IsNotNull(data1, "Unable to find Id == 1 so data was not written properly"); Assert.AreEqual("Random Name 1", data1.Name); Assert.IsNull(data1.Owner); IgnoreTestsExampe4 data2 = actualList.FirstOrDefault(w => w.Id == 2); Assert.IsNotNull(data2, "Unable to find Id == 2 so data was not written properly"); Assert.AreEqual("Random Name 2", data2.Name); Assert.IsNull(data2.Owner); }
public void PropertyNames_do_not_need_to_match_if_ClassToExcelAttribute_ColumnName_is_specified_and_the_file_contains_a_header_row() { // Arrange // Arrange // Arrange var originalData = new List <ColumnNameTestsExampe1>(); originalData.Add(new ColumnNameTestsExampe1 { Id = 1, FName = "Tom", LName = "McMasters" }); originalData.Add(new ColumnNameTestsExampe1 { Id = 2, FName = "David", LName = "Jones" }); // Act // Act // Act // Using a different class with similar properties WITHOUT the ignore attribute to prove that nothing was truly written // if we used the same class the reader would not even look for the Name field. var saveAndReadHelper = new SaveAndReadHelper <ColumnNameTestsExampe1, ColumnNameTestsExampe2>(); List <ColumnNameTestsExampe2> actualList = saveAndReadHelper.SaveAndRead(originalData, true); // Assert // Assert // Assert ColumnNameTestsExampe1 expected1 = originalData.FirstOrDefault(w => w.Id == 1); ColumnNameTestsExampe2 actual1 = actualList.FirstOrDefault(w => w.Id == 1); Assert.IsNotNull(actual1, "Unable to find Id == 1 so data was not written properly"); Assert.AreEqual(expected1.FName, actual1.FirstName, "Expected first name to be equal!"); Assert.AreEqual(expected1.LName, actual1.LastName, "Expected last name to be equal!"); ColumnNameTestsExampe1 expected2 = originalData.FirstOrDefault(w => w.Id == 2); ColumnNameTestsExampe2 actual2 = actualList.FirstOrDefault(w => w.Id == 2); Assert.IsNotNull(actual2, "Unable to find Id == 2 so data was not written properly"); Assert.AreEqual(expected2.FName, actual2.FirstName, "Expected first name to be equal!"); Assert.AreEqual(expected2.LName, actual2.LastName, "Expected last name to be equal!"); }
public void When_a_header_is_present_the_ClassToExcelAttribute_ColumnName_takes_priority_over_Order() { // Arrange // Arrange // Arrange var originalData = new List <OrderTestsExampe1>(); originalData.Add(new OrderTestsExampe1 { Id = 1, FirstName = "Tom", LastName = "McMasters" }); originalData.Add(new OrderTestsExampe1 { Id = 2, FirstName = "David", LastName = "Jones" }); // Act // Act // Act // Using a different class with similar properties WITHOUT the ignore attribute to prove that nothing was truly written // if we used the same class the reader would not even look for the Name field. var saveAndReadHelper = new SaveAndReadHelper <OrderTestsExampe1, OrderTestsExampe2>(); List <OrderTestsExampe2> actualList = saveAndReadHelper.SaveAndRead(originalData, true); // Assert // Assert // Assert OrderTestsExampe1 expected1 = originalData.FirstOrDefault(w => w.Id == 1); OrderTestsExampe2 actual1 = actualList.FirstOrDefault(w => w.Id == 1); Assert.IsNotNull(actual1, "Unable to find Id == 1 so data was not written properly"); Assert.AreEqual(expected1.FirstName, actual1.FirstName, "Expected first name to be equal!"); Assert.AreEqual(expected1.LastName, actual1.LastName, "Expected last name to be equal!"); OrderTestsExampe1 expected2 = originalData.FirstOrDefault(w => w.Id == 2); OrderTestsExampe2 actual2 = actualList.FirstOrDefault(w => w.Id == 2); Assert.IsNotNull(actual2, "Unable to find Id == 2 so data was not written properly"); Assert.AreEqual(expected2.FirstName, actual2.FirstName, "Expected first name to be equal!"); Assert.AreEqual(expected2.LastName, actual2.LastName, "Expected last name to be equal!"); }
public void CanReadDoubles() { // Arrange // Arrange // Arrange var originalData = new List <NumberTestExampe1>(); originalData.Add(new NumberTestExampe1 { Id = 1, DoubleValue = "123.30" }); originalData.Add(new NumberTestExampe1 { Id = 2, DoubleValue = "-124.67" }); // To round-trip the double, using "R" option // http://www.hanselman.com/blog/WhyYouCantDoubleParseDoubleMaxValueToStringOrSystemOverloadExceptionsWhenUsingDoubleParse.aspx originalData.Add(new NumberTestExampe1 { Id = 3, DoubleValue = double.MaxValue.ToString("R") }); originalData.Add(new NumberTestExampe1 { Id = 4, DoubleValue = double.MinValue.ToString("R") }); // Act // Act // Act // Using a different class with similar properties WITHOUT the ignore attribute to prove that nothing was truly written // if we used the same class the reader would not even look for the Name field. var saveAndReadHelper = new SaveAndReadHelper <NumberTestExampe1, NumberTestExampe2>(); List <NumberTestExampe2> actualList = saveAndReadHelper.SaveAndRead(originalData, false); // Assert // Assert // Assert CompareDoubleValue(actualList, 1, 123.30d); CompareDoubleValue(actualList, 2, -124.67d); CompareDoubleValue(actualList, 3, double.MaxValue); CompareDoubleValue(actualList, 4, double.MinValue); }
public void CanReadDecimals() { // Arrange // Arrange // Arrange var originalData = new List <NumberTestExampe1>(); originalData.Add(new NumberTestExampe1 { Id = 1, DecimalValue = "123.30" }); originalData.Add(new NumberTestExampe1 { Id = 2, DecimalValue = "-123.54" }); originalData.Add(new NumberTestExampe1 { Id = 3, DecimalValue = Decimal.MaxValue.ToString() }); originalData.Add(new NumberTestExampe1 { Id = 4, DecimalValue = Decimal.MinValue.ToString() }); // Act // Act // Act // Using a different class with similar properties WITHOUT the ignore attribute to prove that nothing was truly written // if we used the same class the reader would not even look for the Name field. var saveAndReadHelper = new SaveAndReadHelper <NumberTestExampe1, NumberTestExampe2>(); List <NumberTestExampe2> actualList = saveAndReadHelper.SaveAndRead(originalData, false); // Assert // Assert // Assert CompareDecimalValue(actualList, 1, 123.30m); CompareDecimalValue(actualList, 2, -123.54m); CompareDecimalValue(actualList, 3, decimal.MaxValue); CompareDecimalValue(actualList, 4, decimal.MinValue); }
public void When_data_has_a_private_setter_data_is_written_to_the_file() { // If you want to ignore a property, users should use the Ignore property on the ClassToExcelAttribute // I'm testing this because the reader and writer use the same base class method for reflecting over properties. // Arrange // Arrange // Arrange var originalData = new List <IgnoreTestsExampe3>(); originalData.Add(new IgnoreTestsExampe3 { Id = 1 }); originalData.Add(new IgnoreTestsExampe3 { Id = 2 }); // Act // Act // Act // Using a different class with similar properties WITHOUT the ignore attribute to prove that nothing was truly written // if we used the same class the reader would not even look for the Name field. var saveAndReadHelper = new SaveAndReadHelper <IgnoreTestsExampe3, IgnoreTestsExampe2>(); List <IgnoreTestsExampe2> actualList = saveAndReadHelper.SaveAndRead(originalData, true); // Assert // Assert // Assert IgnoreTestsExampe2 data1 = actualList.FirstOrDefault(w => w.Id == 1); Assert.IsNotNull(data1, "Unable to find Id == 1 so data was not written properly"); Assert.AreEqual("Some Name", data1.Name); IgnoreTestsExampe2 data2 = actualList.FirstOrDefault(w => w.Id == 2); Assert.IsNotNull(data2, "Unable to find Id == 2 so data was not written properly"); Assert.AreEqual("Some Name", data2.Name); }