public void WhenPriceIsInvalid_ThenItThrowsLineImportException(string line)
        {
            var act       = new TestDelegate(() => ClothsFactory.FromCsvString(line));
            var exception = Assert.Throws <LineImportException>(act);

            Assert.IsTrue(exception.InnerException.Message.Contains("The import price could not be parsed to a valid decimal"));
        }
Пример #2
0
        public async Task <List <Cloth> > ImportFromCsv(Stream csv, bool skipFirstLine = true)
        {
            var cloths = new List <Cloth>();

            using (var sr = new StreamReader(csv))
            {
                bool endOfFile;
                bool firstLine = true;
                do
                {
                    var line = await sr.ReadLineAsync();

                    if (!firstLine || !skipFirstLine)
                    {
                        if (!string.IsNullOrWhiteSpace(line))
                        {
                            cloths.Add(ClothsFactory.FromCsvString(line));
                        }
                    }

                    endOfFile = string.IsNullOrWhiteSpace(line);
                    firstLine = false;
                } while (!endOfFile);
            }

            return(cloths);
        }
        public void WhenProductIdIsInvalid_ThenItThrowsLineImportException(string line)
        {
            var act       = new TestDelegate(() => ClothsFactory.FromCsvString(line));
            var exception = Assert.Throws <LineImportException>(act);

            Assert.IsTrue(exception.InnerException.Message.Contains("Product ID could not be converted to a long"));
        }
        public void WhenImportLineContainsUnexpectedFieldCount_ThenItThrowsLineImportException(string line)
        {
            var act       = new TestDelegate(() => ClothsFactory.FromCsvString(line));
            var exception = Assert.Throws <LineImportException>(act);

            Assert.IsTrue(exception.InnerException.Message.Contains("Failed to import CSV line as Cloth"));
        }
        public void WhenImportingLineUntrimmed_ThenPropertiesKeepWhitespace(string line, int id, string name, string description, decimal priceInDollars, string category)
        {
            var cloth = ClothsFactory.FromCsvString(line, false);

            Assert.AreEqual(cloth.ProductId, id);
            Assert.AreEqual(cloth.Name, name);
            Assert.AreEqual(cloth.Description, description);
            Assert.AreEqual(cloth.Price.Amount, priceInDollars);
            Assert.AreEqual(cloth.Category, category);
        }
        public void WhenImportLineIsCorrect_ThenItReturnsClothObject(string line, int id, string name, string description, decimal priceInDollars, string category)
        {
            var cloth = ClothsFactory.FromCsvString(line);

            Assert.AreEqual(cloth.ProductId, id);
            Assert.AreEqual(cloth.Name, name);
            Assert.AreEqual(cloth.Description, description);
            Assert.AreEqual(cloth.Price.Amount, priceInDollars);
            Assert.AreEqual(cloth.Category, category);
        }
        public void WhenClothLineIsExported_ThenLineIsFormatted()
        {
            var decimalSeperator     = CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator;
            var importCsvString      = "1234,name,description,80,pants";
            var expectedOutputString = $"1234,name,description,80{decimalSeperator}00,pants";
            var cloth = ClothsFactory.FromCsvString(importCsvString);
            var actualOutputString = cloth.ToCsvString();

            Assert.AreEqual(expectedOutputString, actualOutputString);
        }