Пример #1
0
        public void ShouldReturnErrorsUsingClosedXMLAndCustomMessages()
        {
            var serviceCollection = new ServiceCollection();

            serviceCollection.AddWorksheetParser <FakeClass>(x => x.WithParser <ClosedXmlParser <FakeClass> >().WithMap <MyWorksheetMap>().WithMessages <CustomMessages>());

            var serviceProvider = serviceCollection.BuildServiceProvider();

            var expectedItens = new List <FakeClass>
            {
                FakeClass.CreateItem(),
                FakeClass.CreateItem(false, false, false),
                FakeClass.CreateItem(false, false, true)
            };

            var parser = serviceProvider.GetService <WorksheetParser <FakeClass> >();

            expectedItens.ForEach(x => x.Bonus *= 0.1M);
            var validationResult = new ValidationResult <FakeClass>();
            var error            = new Error("Can't not be null field TX_NAME", 3, 2);

            validationResult.AddError(error);
            validationResult.AddItem(expectedItens.First());
            validationResult.AddItem(expectedItens.Last());

            var headers = new MyWorksheetMap().GetFields().Select(s => s.Name).ToList();

            using var worksheet = CreateStream(expectedItens, headers);
            var itens = parser.Parse(worksheet, worksheetName);

            itens.Should().BeEquivalentTo(validationResult);
        }
Пример #2
0
        public void ShouldParseSheetUsingClosedXML()
        {
            var serviceCollection = new ServiceCollection();

            serviceCollection.AddWorksheetParser <FakeClass>(x => x.WithParser <ClosedXmlParser <FakeClass> >().WithMap <MyWorksheetMap>());

            var serviceProvider = serviceCollection.BuildServiceProvider();

            var expectedItens = new List <FakeClass>();

            for (var i = 0; i < 5; i++)
            {
                expectedItens.Add(FakeClass.CreateItem(i % 2 == 0, i % 5 == 0));
            }

            var parser = serviceProvider.GetService <WorksheetParser <FakeClass> >();

            var headers = new MyWorksheetMap().GetFields().Select(s => s.Name).ToList();

            using var worksheet = CreateStream(expectedItens, headers);
            var itens = parser.Parse(worksheet, worksheetName);

            expectedItens.ForEach(x => x.Bonus = x.Bonus * 0.1M);
            var validationResult = new ValidationResult <FakeClass>();

            validationResult.AddItem(expectedItens);
            itens.Should().BeEquivalentTo(validationResult);
        }
        public void ShouldParseItens()
        {
            var itens = new List <FakeClass>
            {
                FakeClass.CreateItem(),
                FakeClass.CreateItem(true),
                FakeClass.CreateItem(),
                FakeClass.CreateItem(true, true),
                FakeClass.CreateItem(),
                FakeClass.CreateItem(false, true),
            };

            var worksheet = new MyWorksheet(itens);
            var parser    = new WorksheetInterpreter <FakeClass>(new ValueSetter(new Converter()), new MyWorksheetMap(), new MessageErrors());
            var rowsFake  = parser.Parse(worksheet);

            itens.ForEach(i => i.Bonus = i.Bonus * 0.1M);
            var validationResult = new ValidationResult <FakeClass>();

            validationResult.AddItem(itens);

            rowsFake.Should().BeEquivalentTo(validationResult);
        }
Пример #4
0
        public void ShouldCreateNewColumnsWithErrors()
        {
            var serviceCollection = new ServiceCollection();

            serviceCollection.AddWorksheetParser <FakeClass>(x => x.WithParser <ClosedXmlParser <FakeClass> >().WithMap <MyWorksheetMap>());

            var serviceProvider = serviceCollection.BuildServiceProvider();

            var expectedItens = new List <FakeClass>
            {
                FakeClass.CreateItem(),
                FakeClass.CreateItem(false, false, false),
                FakeClass.CreateItem(false, false, true)
            };

            var parser = serviceProvider.GetService <WorksheetParser <FakeClass> >();

            expectedItens.ForEach(x => x.Bonus *= 0.1M);
            var validationResult = new ValidationResult <FakeClass>();
            var error            = new Error("Field TX_NAME can´t not be null", 3, 2);

            validationResult.AddError(error);
            validationResult.AddItem(expectedItens.First());
            validationResult.AddItem(expectedItens.Last());

            var headers = new MyWorksheetMap().GetFields().Select(s => s.Name).ToList();

            using var worksheet = CreateStream(expectedItens, headers);
            var itens = parser.Parse(worksheet, worksheetName);

            using var streamWithErros = parser.WriteErrorsWithSummary(worksheet, worksheetName, itens.Errors);

            using var workbookWithErrors = new XLWorkbook(streamWithErros);
            var reader = new ClosedXmlReader(workbookWithErrors, worksheetName);

            reader.CountColumns().Should().Be(headers.Count() + 1);
        }