Exemplo n.º 1
0
        public void Cannot_Load_Invalid_Csv_Format()
        {
            //preparation - create context with created options passed as argument
            RequestContext context = new RequestContext(CreateNewContextOptions());

            //arrange
            var fileMock = new Mock <IFormFile>();

            List <RequestJson> requests = new List <RequestJson>
            {
                new RequestJson {
                    ClientId  = "1",
                    Name      = "test",
                    Price     = "2.2",
                    Quantity  = "10",
                    RequestId = "1"
                },
                new RequestJson {
                    ClientId  = "1",
                    Name      = "test",
                    Price     = "2.2",
                    Quantity  = "100",
                    RequestId = "1"
                }
            };

            StringWriter stringWriter    = new StringWriter();
            string       requestsCsvTest = "";

            using (var csv = new CsvWriter(stringWriter))
            {
                csv.Configuration.Delimiter = ",";
                csv.WriteRecords(requests);
                requestsCsvTest = stringWriter.ToString();
            }

            //setup mock file using a memory stream
            var content  = requestsCsvTest;
            var fileName = "test.csv";
            var ms       = new MemoryStream();
            var writer   = new StreamWriter(ms);

            writer.Write(content);
            writer.Flush();
            ms.Position = 0;
            fileMock.Setup(_ => _.OpenReadStream()).Returns(ms);
            fileMock.Setup(_ => _.FileName).Returns(fileName);
            fileMock.Setup(_ => _.ContentType).Returns("application / vnd.ms - excel");
            fileMock.Setup(_ => _.Length).Returns(ms.Length);

            CsvService csvService = new CsvService(context);

            csvService.LoadToDb(fileMock.Object);

            var requestTest = (from r in context.Requests
                               where r.ClientId == 1
                               select r);
            var orderTest = (from o in context.Orders
                             where o.ClientId == 1
                             select o);

            Assert.AreEqual(0, requestTest.Count());
            Assert.AreEqual(0, orderTest.Count());
        }