public void Should_be_intercept_current_row_while_converting_into_a_list() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- var stocksNullableWorksheetIndex = 5; var stocksValidationWorksheetIndex = 4; #if NETFRAMEWORK stocksNullableWorksheetIndex = 6; stocksValidationWorksheetIndex = 5; #endif //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- var optionalStocks = ExcelPackage1.ToList <StocksNullable>(stocksNullableWorksheetIndex, configuration => configuration.SkipCastingErrors() .SkipValidationErrors() .Intercept((current, rowIndex) => { current.Barcode = current.Barcode.Insert(0, "_"); })); var stocks = ExcelPackage1.ToList <StocksValidation>(stocksValidationWorksheetIndex, configuration => configuration.SkipCastingErrors() .SkipValidationErrors() .Intercept((current, rowIndex) => { current.Quantity += 10 + rowIndex; })); //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- optionalStocks.Any().Should().BeTrue(); optionalStocks.Count.Should().Be(3); optionalStocks.All(x => x.Barcode.StartsWith("_")).Should().Be(true); stocks.Min(x => x.Quantity).Should().BeGreaterThan(10); }
public void Should_convert_Excel_table_into_list_of_complex_objects() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- ExcelTable table = ExcelPackage1.GetTable("TEST3"); //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- List <Cars> list = table.ToList <Cars>(); //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- list.Count.Should().Be(5, "We have 5 rows"); // 'LicensePlate' property should be mapped by column index list.Count(x => string.IsNullOrWhiteSpace(x.LicensePlate)).Should().Be(1, "There is one without license plate"); // 'Manufacturer' property should be mapped to Manufacturers2 enum list.All(x => x.Manufacturer > 0).Should().BeTrue("All should have manufacturers"); // 'ManufacturingDate' property should be mapped by column name "Manufacturing date" list.Last().ManufacturingDate.Should().BeNull("The last one's manufacturing date is unknown"); list.Count(x => x.ManufacturingDate == null).Should().Be(1, "Only one manufacturig date is unknown"); list.Max(x => x.ManufacturingDate).Should().Be(new DateTime(2015, 3, 10), "Oldest was manufactured on 2015.03.10"); // 'Ready' property should be mapped to "Is ready for traffic?" column list.Single(x => x.LicensePlate == null).Should().BeEquivalentTo(list.Single(x => !x.Ready), "The one without the license plate is not ready"); // 'Price' property should be mapped automatically list.Max(x => x.Price).Should().Be(12000, "Highest price is 12000"); }
public void Should_throw_exception_if_casting_failed() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- ExcelTable table = ExcelPackage1.GetWorksheet("TEST1").GetTable("TEST1"); //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- Action action = () => table.ToList <EnumFailMap>(); //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- action.Should().Throw <ExcelException>() .And.Args.CellValue.Should().Be("MALE"); action.Should().Throw <ExcelException>() .And.Args.ExpectedType.Should().Be(typeof(Classes)); action.Should().Throw <ExcelException>() .And.Args.PropertyName.Should().Be("Gender"); action.Should().Throw <ExcelException>() .And.Args.ColumnName.Should().Be("Gender"); }
public void Should_convert_given_Excel_package_to_list_of_objects() { //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- var list = ExcelPackage1.ToList <DateMap>(); //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- list.Any().Should().BeTrue(); list.Count.Should().Be(5); }
public void Should_throw_an_exception_when_trying_to_get_an_excel_table_if_casting_error_occurred() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- ExcelTable tableWithoutHeaderRow = ExcelPackage1.GetWorksheet("TEST6").AsExcelTable(false); //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- Action act = () => tableWithoutHeaderRow.AsEnumerable <StocksNullable>(c => c.WithCastingExceptionMessage("Casting error occured on '{1}'")).ToList(); //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- act.Should().Throw <ExcelException>().WithMessage("Casting error occured on 'B2'"); }
public void Should_set_horizontal_alignment_of_the_worksheet() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- ExcelWorksheet worksheet = ExcelPackage1.GetWorksheet(4); //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- worksheet.SetHorizontalAlignment(ExcelHorizontalAlignment.Distributed); //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- worksheet.Cells.Style.HorizontalAlignment.Should().Be(ExcelHorizontalAlignment.Distributed); }
public void Should_throw_argument_exception_if_there_is_no_mapped_property_object() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- ExcelTable table = ExcelPackage1.GetWorksheet("TEST1").GetTable("TEST1"); //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- Action action = () => table.ToList <ObjectWithoutExcelTableAttributes>(); //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- action.Should().Throw <InvalidOperationException>().WithMessage($"Given object does not have any '{nameof(ExcelTableColumnAttribute)}'."); }
public void Should_given_address_range_not_be_empty_without_header_row() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- ExcelAddressBase address = ExcelPackage1.GetWorksheet("TEST7").GetValuedDimension(); //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- bool result = address.IsEmptyRange(); //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- result.Should().BeFalse(); }
public void Should_get_ExcelTable_as_enumerable() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- ExcelTable tableWithoutHeaderRow = ExcelPackage1.GetWorksheet("TEST6").AsExcelTable(false); //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- IEnumerable <StocksNullable> result = tableWithoutHeaderRow.AsEnumerable <StocksNullable>(c => c.SkipCastingErrors()); //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- result.Count().Should().Be(4); }
public void Should_table_not_be_empty() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- ExcelTable table = ExcelPackage1.GetWorksheet("TEST6").AsExcelTable(); //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- bool result = table.IsEmpty(); //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- result.Should().BeFalse(); }
public void Should_add_empty_worksheet_to_the_package() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- string randomName = GetRandomName(); //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- ExcelPackage1.AddWorksheet(randomName); //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- ExcelPackage1.GetWorksheet(randomName).Should().NotBe(null); }
public void Should_convert_an_Excel_package_into_a_DataSet() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- const int expectedCount = 9; //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- var dataSet = ExcelPackage1.ToDataSet(); //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- dataSet.Should().NotBeNull($"We have {expectedCount} tables"); dataSet.Tables.Count.Should().Be(expectedCount, $"We have {expectedCount} tables"); }
public void Should_change_background_color_of_the_worksheet() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- ExcelWorksheet worksheet = ExcelPackage1.GetWorksheet(2); //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- worksheet.SetBackgroundColor(Color.Brown, ExcelFillStyle.DarkTrellis); //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- worksheet.Cells.Style.Fill.PatternType.Should().Be(ExcelFillStyle.DarkTrellis); worksheet.Cells.Style.Fill.BackgroundColor.Rgb.Should().Be($"{Color.Brown.ToArgb() & 0xFFFFFFFF:X8}"); }
public void Should_convert_Excel_table_into_list_of_nullable_objects() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- ExcelTable table1 = ExcelPackage1.GetWorksheet("TEST2").GetTable(0); //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- List <CarNullable> list = table1.ToList <CarNullable>(); //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- list.Count(x => !x.Price.HasValue).Should().Be(2, "Should have two"); list.Count.Should().Be(5); }
public void Should_not_throw_exception_if_casting_error_occured_with_nullable() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- ExcelTable table = ExcelPackage1.GetWorksheet("TEST4").Tables["TEST4"]; //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- List <StocksNullable> list = table.ToList <StocksNullable>(configuration => configuration.SkipCastingErrors()); //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- list.Count(x => !x.UpdatedDate.HasValue).Should().Be(2, "Should have two"); list.Count(x => !x.Quantity.HasValue).Should().Be(2, "Should have two"); list.Count.Should().Be(4, "Should have four"); }
public void Should_map_string_values_into_Enums() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- ExcelTable table = ExcelPackage1.GetWorksheet("TEST1").GetTable("TEST1"); //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- List <EnumStringMap> list = table.ToList <EnumStringMap>(); //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- list.Count.Should().Be(5, "We have expected 5 elements"); list.Count(x => x.Gender == Genders.Male).Should().Be(3, "We have expected 3 males"); list.Count(x => x.Gender == Genders.Female).Should().Be(2, "We have expected 2 females"); }
public void Should_map_object_properties_with_column_index() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- ExcelTable table = ExcelPackage1.GetWorksheet("TEST1").GetTable("TEST1"); //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- List <IndexMap> list = table.ToList <IndexMap>(); //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- list.Count.Should().Be(5, "We have expected 5 elements"); list.First().Name.Should().Be("John", "We have expected John to be first"); list.First().Gender.Should().Be("MALE", "We have expected a male to be first"); }
public void Should_automatically_map_if_column_name_and_index_not_specified() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- ExcelTable table = ExcelPackage1.GetTable("TEST1"); //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- List <DefaultMap> list = table.ToList <DefaultMap>(); //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- list.Count.Should().Be(5, "We have expected 5 elements"); list.First().Name.Should().Be("John", "We have expected John to be first"); list.First().Gender.Should().Be("MALE", "We have expected a male to be first"); }
public void Should_change_font_color_of_the_worksheet() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- ExcelWorksheet worksheet = ExcelPackage1.GetWorksheet(3); //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- worksheet.SetFontColor(Color.BlueViolet); worksheet.SetFont(new Font("Verdana", 12)); //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- worksheet.Cells.Style.Font.Color.Rgb.Should().Be($"{Color.BlueViolet.ToArgb() & 0xFFFFFFFF:X8}"); worksheet.Cells.Style.Font.Name.Should().Be("Verdana"); }
public void Should_throw_exception_if_object_properties_not_mapped_correctly() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- ExcelTable table = ExcelPackage1.GetWorksheet("TEST1").GetTable("TEST1"); //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- Action action1 = () => table.ToList <ObjectWithWrongAttributeMappings>(); Action action2 = () => table.ToList <ObjectWithWrongAttributeMappings>(cfg => cfg.WithHeaderValidationExceptionMessage("'{0}' column not found.")); //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- action1.Should().Throw <ExcelValidationException>().WithMessage("'Firstname' column could not found on the worksheet."); action2.Should().Throw <ExcelValidationException>().WithMessage("'Firstname' column not found."); }
public void Should_add_a_copied_worksheet_to_the_package() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- string randomName = GetRandomName(); ExcelWorksheet copyWorksheet = ExcelPackage1.GetWorksheet(2); //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- ExcelPackage1.AddWorksheet(randomName, copyWorksheet); //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- ExcelPackage1.GetWorksheet(randomName).Should().NotBe(null); ExcelPackage1.GetWorksheet(randomName).Cells[1, 1, 3, 3].Value.Should().BeEquivalentTo(copyWorksheet.Cells[1, 1, 3, 3].Value); }
public void Should_get_dataBounds_of_Excel_table_without_header_row() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- ExcelTable tableWithHeaderRow = ExcelPackage1.GetWorksheet("TEST6").AsExcelTable(); //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- ExcelAddress dataBoundsWithoutHeader = tableWithHeaderRow.GetDataBounds(); //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- dataBoundsWithoutHeader.Start.Row.Should().Be(3); dataBoundsWithoutHeader.End.Row.Should().Be(5); dataBoundsWithoutHeader.Address.Should().Be("A3:C5"); }
public void Should_map_integer_values_into_Enum() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- ExcelTable table = ExcelPackage1.GetWorksheet("TEST1").GetTable(0); //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- List <EnumByteMap> list = table.ToList <EnumByteMap>(); //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- list.Count.Should().Be(5, "We have expected 5 elements"); list.Count(x => x.Class == Classes.Ten).Should().Be(2, "We have expected 2 in 10th class"); list.Count(x => x.Class == Classes.Nine).Should().Be(3, "We have expected 3 in 9th class"); }
public void Should_map_object_properties_with_different_column_names() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- ExcelTable table = ExcelPackage1.GetWorksheet("TEST1").GetTable("TEST1"); //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- List <NamedMap> list = table.ToList <NamedMap>(); //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- list.Count.Should().Be(5, "We have expected 5 elements"); list.First().FirstName.Should().Be("John", "We have expected John to be first"); list.First().Sex.Should().Be("MALE", "We have expected a male to be first"); list.First().NotMapped.Should().Be(null); }
public void Should_not_throw_exception_if_casting_failed() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- ExcelTable table = ExcelPackage1.GetWorksheet("TEST1").GetTable("TEST1"); //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- List <EnumFailMap> list = table.ToList <EnumFailMap>(configuration => configuration.SkipCastingErrors()); //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- list.Should().NotBeNull("We should get the list"); list.All(x => !string.IsNullOrWhiteSpace(x.Name)).Should().BeTrue("All names should be there"); list.All(x => x.Gender == 0).Should().BeTrue("All genders should be 0"); }
public void Should_given_address_range_be_empty_with_header_row() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- ExcelAddressBase address = ExcelPackage1.GetWorksheet("TEST7").GetValuedDimension(); ExcelAddressBase address2 = ExcelPackage1.Workbook.Worksheets.Add(GetRandomName()).ChangeCellValue(1, 1, "").GetValuedDimension(); //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- bool result = address.IsEmptyRange(true); bool result2 = address2.IsEmptyRange(true); //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- result.Should().BeTrue(); result2.Should().BeTrue(); }
public void Should_convert_Excel_table_into_list_of_objects() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- ExcelTable table = ExcelPackage1.GetTable("TEST1"); //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- List <DateMap> list = table.ToList <DateMap>(); //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- list.Count.Should().Be(5, "We have expected 5 elements"); list.First(x => x.Name == "Adam").BirthDate.Should().Be(new DateTime(1981, 4, 2), "Adam' birthday is 1981.04.02"); list.Min(x => x.BirthDate).Should().Be(new DateTime(1979, 12, 1), "Oldest one was born on 1979.12.01"); }
public void Should_extract_all_tables_from_an_Excel_package() { //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- var tables = ExcelPackage1.GetAllTables().ToList(); //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- tables.Should().NotBeNull("We have 4 tables"); tables.Count().Should().Be(4, "We have 4 tables"); ExcelPackage1.HasTable("TEST2").Should().BeTrue("We have TEST2 table"); ExcelPackage1.HasTable("test2").Should().BeTrue("Table names are case insensitive"); ExcelPackage1.GetWorksheet("TEST2").GetTable("TEST2").Should().BeEquivalentTo(ExcelPackage1.GetTable("TEST2"), "We are accessing the same objects"); ExcelPackage1.HasTable("NOTABLE").Should().BeFalse("We don't have NOTABLE table"); ExcelPackage1.GetTable("NOTABLE").Should().BeNull("We don't have NOTABLE table"); }
public void Should_generate_ExcelPackage_with_optional_columns() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- ExcelWorksheet worksheetWithOptionalColumns = ExcelPackage1.GetWorksheet("WithOptionalFields"); var list = worksheetWithOptionalColumns.ToList <ExcelWithOptionalFields>(); const string expectedWorksheetName = "worksheet-name"; //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- var result = list.ToWorksheet(expectedWorksheetName).ToExcelPackage(); //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- var generatedWorksheet = result.GetWorksheet(expectedWorksheetName); generatedWorksheet.ToList <ExcelWithOptionalFields>().Count.Should().Be(2); }
public void Should_validate_Excel_table_and_return_casting_errors() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- ExcelTable table = ExcelPackage1.GetTable("TEST3"); //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- List <ExcelExceptionArgs> validationResults = table.Validate <WrongCars>().ToList(); //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- table.Should().NotBeNull("We have TEST3 table"); validationResults.Should().NotBeNull("We have errors here"); validationResults.Count.Should().Be(2, "We have 2 errors"); validationResults.Exists(x => x.CellAddress.Address.Equals("C6", StringComparison.InvariantCultureIgnoreCase)).Should().BeTrue("Toyota is not in the enumeration"); validationResults.Exists(x => x.CellAddress.Address.Equals("D7", StringComparison.InvariantCultureIgnoreCase)).Should().BeTrue("Date is null"); }