Ejemplo n.º 1
0
        public void AddStock(Company company)
        {
            // Arrange

            var fileName   = Path.GetRandomFileName();
            var outputFile = new FileInfo(Path.ChangeExtension(fileName, "csv"));
            CsvContext <StockQuote> csvContext = null;
            CsvRepo <StockQuote>    repository = null;

            try
            {
                csvContext = new StockCsvContext(outputFile)
                {
                    Culture = CultureInfo.InvariantCulture
                };
                repository = new CsvRepo <StockQuote>(csvContext);

                // Act

                repository.AddRange(company.Quotes);
                csvContext.SaveChanges();

                var received = File.ReadAllText(outputFile.FullName);
                // Assert

                var actual   = received.Split(Environment.NewLine).Length;
                var expected = company.Quotes.Count + 2;
                Assert.Equal(expected, actual);
            }
            finally
            {
                repository?.Dispose();
                outputFile.Delete();
            }
        }
Ejemplo n.º 2
0
        public void RemoveRangeRemovesRangeWhenTheSameCollection(List <MockPoco> input)
        {
            // Arrange

            var fileName   = Path.GetRandomFileName();
            var outputFile = new FileInfo(Path.ChangeExtension(fileName, "csv"));

            CsvContext <MockPoco> csvContext = null;
            CsvRepo <MockPoco>    repository = null;

            try
            {
                csvContext = new CsvContext <MockPoco>(outputFile)
                {
                    Culture = CultureInfo.InvariantCulture
                };
                repository = new CsvRepo <MockPoco>(csvContext);

                // Act

                repository.AddRange(input);
                var unfortunateReference = repository.GetAll();
                repository.RemoveRange(unfortunateReference);
                csvContext.SaveChanges();


                // Assert

                var             rawAllText     = File.ReadAllText(outputFile.FullName);
                List <MockPoco> fromFileParsed = null;
                using (var csv = new CsvReader(new StringReader(rawAllText)))
                {
                    fromFileParsed = csv.GetRecords <MockPoco>().ToList();
                }

                var actual = fromFileParsed.GroupBy(x => x.Value).OrderBy(x => x.Key).Select(g => new { g.Key, Count = g.Count() }).ToList();

                Assert.Empty(actual);
            }
            finally
            {
                repository?.Dispose();

                outputFile.Delete();
            }
        }