コード例 #1
0
        public void AddToCsvRepoWorksWhenFileDoesNotExist(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.Add(input);
                csvContext.SaveChanges();

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

                // Assert

                Assert.Contains(input.Value, received);
            }
            finally
            {
                repository?.Dispose();

                outputFile.Delete();
            }
        }
コード例 #2
0
        public void AddToCsvRepoWorksWhenNonEmptyFileExists(MockPoco input)
        {
            // Arrange

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


            using (var streamWriter = outputFile.CreateText())
            {
                streamWriter.Write("Id,Value\r\n5,test5\r\n");
            }
            CsvContext <MockPoco> csvContext = null;
            CsvRepo <MockPoco>    repository = null;

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

                // Act

                repository.Add(input);
                csvContext.SaveChanges();

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

                // Assert

                Assert.Contains("test5", received);
                Assert.Contains(input.Value, received);
            }
            finally
            {
                repository?.Dispose();

                outputFile.Delete();
            }
        }
コード例 #3
0
        public void AddThrowsWhenAttemptedToAddExisitngId()
        {
            // Arrange

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

            CsvContext <MockPoco> csvContext = null;
            CsvRepo <MockPoco>    repository = null;
            //
            var set = new HashSet <int>();

            set.Add(1);
            set.Add(1);
            //
            using (var streamWriter = outputFile.CreateText())
            {
                streamWriter.Write("Id,Value\r\n5,test5\r\n");
            }

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

                // Act & Assert

                Assert.Throws <ArgumentException>(() => repository.Add(new MockPoco {
                    Id = 5
                }));
            }
            finally
            {
                repository?.Dispose();

                outputFile.Delete();
            }
        }
コード例 #4
0
        public void UpdateUpdatesAfterChangesCommited(MockPoco input)
        {
            string       oldValue = input.Value;
            const string newValue = "newValue5";
            // 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.Add(input);
                csvContext.SaveChanges();
                input.Value = newValue;
                csvContext.SaveChanges();

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

                // Assert

                Assert.Contains(newValue, received);
                Assert.DoesNotContain(oldValue, received);
            }
            finally
            {
                repository?.Dispose();

                outputFile.Delete();
            }
        }