Esempio n. 1
0
        public void Write_should_create_file()
        {
            // Arrange
            _sut.Open(_outputTxtPath, CSVReaderWriter.Mode.Write);

            // Act
            _sut.Write(new[] { _testName, _testAddress });

            // Assert
            Assert.IsTrue(new FileInfo(_outputTxtPath).Exists);
        }
        public void Should_WriteCorrectlyToCsvWritter_WhenOpeningModeIsWrite()
        {
            // Arrange

            // Act
            _subject.Open("TestFileName.csv", CSVReaderWriter.Mode.Write);
            _subject.Write("Value1");

            // Assert
            _csvWriterMock.Verify(m => m.Write("Value1"), Times.Once);
        }
        public void FileWriterIsCalledWhenCallingWrite()
        {
            var columns       = new[] { "one", "two" };
            var handleStreams = A.Fake <IHandleStreams>();

            handleStreams.StreamWriter = new StreamWriter(new MemoryStream(Encoding.Default.GetBytes("hi")));
            var mode = CSVReaderWriter.Mode.Write;

            A.CallTo(() => _fileOpener.Open("someFile", mode)).Returns(handleStreams);
            _csvReaderWriter.Open("someFile", mode);
            _csvReaderWriter.Write(columns);
            A.CallTo(() => _fileWriter.Write(columns, handleStreams)).MustHaveHappened();
        }
        public void Should_write_line()
        {
            _CSVReaderWriter.Open(TestInputFile, CSVReaderWriter.Mode.Write);
            var columns = new string[] { Column1Value, Column2Value };

            _CSVReaderWriter.Write(columns);

            _stream.Position = 0;

            var addedLine = new StreamReader(_stream).ReadToEnd();

            Assert.That(addedLine, Is.EqualTo(string.Join("\t", columns) + "\r\n"));
        }
        public void WriteCsvFile()
        {
            //Write test file
            using (var writer = new CSVReaderWriter())
            {
                writer.Open(_testWriteDataFile, CSVReaderWriter.Mode.Write);


                foreach (var testAddress in _mailAddressTests)
                {
                    writer.Write(testAddress.Name, testAddress.Address);
                }
                writer.Close();
            }

            //Read test file
            var contacts = new List <Contact>();

            using (var reader = new CSVReaderWriter())
            {
                reader.Open(_testWriteDataFile, CSVReaderWriter.Mode.Read);
                string column1, column2;
                while (reader.Read(out column1, out column2))
                {
                    contacts.Add(new Contact(column1, column2));
                }

                reader.Close();
            }

            CollectionAssert.AreEqual(_mailAddressTests, contacts);
        }
Esempio n. 6
0
        public void Write_Method_Should_not_throw_Exception_when_empty_string_array_is_passed()
        {
            var csvReaderWriter = new CSVReaderWriter();

            Assert.DoesNotThrow(() => csvReaderWriter.Write(new string[] {}));

            csvReaderWriter.Close();
        }
 public void WhenInReadMode_WritingToAFile_Then_ThrowsAnError()
 {
     using (var readerWriter = new CSVReaderWriter())
     {
         var mode = CSVReaderWriter.Mode.Read;
         readerWriter.Open(_currentDirectory + _csvWriteTestData, mode);
         Assert.Throws <ArgumentNullException>(() => readerWriter.Write());
     }
 }
        public void Should_throw_on_write_columns_with_empty()
        {
            CSVReaderWriter csvReaderWriter = new CSVReaderWriter();

            string fileName = GetTempFilePath();

            csvReaderWriter.Open(fileName, CSVReaderWriter.Mode.Write);

            Assert.Throws <ArgumentException>(() => { csvReaderWriter.Write("COL 1", "", "Test"); });
        }
        public void Should_throw_on_write_null_columns()
        {
            CSVReaderWriter csvReaderWriter = new CSVReaderWriter();

            string fileName = GetTempFilePath();

            csvReaderWriter.Open(fileName, CSVReaderWriter.Mode.Write);

            Assert.Throws <ArgumentNullException>(() => { csvReaderWriter.Write(null); });
        }
        public void Write_WriterIsDisposed_ThrowException()
        {
            var csv = new CSVReaderWriter(It.IsAny <ICSVReader>(), null);

            csv.Close();

            var ex = Assert.Throws <NullReferenceException>(() => csv.Write(It.IsAny <string[]>()));

            Assert.AreEqual("The file writer is closed. Please reopen it.", ex.Message);
        }
Esempio n. 11
0
        public void Should_write_with_the_CSVWriter()
        {
            string[] columns = { };

            _csvReaderWriter.Write(columns);

            _csvWriter.Verify(x => x.Write(columns));

            _csvReaderWriter.Close();
        }
Esempio n. 12
0
        public void WriteToFile_AndThenReadWrittenContent()
        {
            CSVReaderWriter.Open(TestOutputFile, CSVReaderWriter.Mode.Write);

            var columns = new string[] { "Column1", "Column2" };

            CSVReaderWriter.Write(columns);

            CSVReaderWriter.Close();

            CSVReaderWriter.Open(TestOutputFile, CSVReaderWriter.Mode.Read);

            string column1, column2;
            var    read = CSVReaderWriter.Read(out column1, out column2);

            Assert.IsTrue(read);
            Assert.IsNotNullOrEmpty(column1);
            Assert.IsNotNullOrEmpty(column2);
            Assert.That(column1.Equals(columns[0]));
            Assert.That(column2.Equals(columns[1]));
        }
        public void Write_ExistingFile()
        {
            var csvWriter = new CSVReaderWriter();

            csvWriter.Open(TestExistingOutputFile, CSVReaderWriter.Mode.Write);

            csvWriter.Write("testing 1-1", "testing 1-2");
            csvWriter.Write("testing 2-1", "testing 2-2", "testing 2-3");

            csvWriter.Close();

            var expected = "testing 1-1\ttesting 1-2\r\ntesting 2-1\ttesting 2-2\ttesting 2-3\r\n";

            string output;

            using (var outFile = File.OpenText(TestExistingOutputFile))
            {
                output = outFile.ReadToEnd();
            }
            Assert.That(expected, Is.EqualTo(output));
        }
        public void Write_should_call_Writers_write()
        {
            //arrange
            string col1 = "column 1";
            string col2 = "column 2";

            // act
            _csvReaderWriter.Write(col1, col2);

            // assert
            _csvWriter.Verify(c => c.Write(col1, col2));
        }
        public void canNotWriteToFileIfInReadMode()
        {
            CSVReaderWriter rw = new CSVReaderWriter();

            try
            {
                rw.Open(TestInputFileForEditing, Mode.Read);
                rw.Write(new string[] { "some data col1", "some more data for col2" });
            } finally
            {
                rw.Close();
            }
        }
Esempio n. 16
0
        public void Write_Method_Should_write_to_the_file()
        {
            var csvReaderWriter = new CSVReaderWriter();

            csvReaderWriter.Open(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, TestOutputFile), CSVReaderWriter.Mode.Write);

            csvReaderWriter.Write(new string[] { "This", "should", "be", "written", "to", "the", "file" });

            csvReaderWriter.Close();

            Assert.True(File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, TestOutputFile)));
            File.Delete(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, TestOutputFile));
        }
        public void Read_When_Only_First_Column_Exists_Throws_Exception()
        {
            var csvReadWriter = new CSVReaderWriter();

            csvReadWriter.Open(@"test_data\test.txt", CSVReaderWriter.Mode.Write);
            csvReadWriter.Write("test line 1 Column 1");// The Read functionality forces to create the file with two columns which is quite rigid.
            csvReadWriter.Close();
            string column1 = null;
            string column2 = null;

            csvReadWriter.Open(@"test_data\test.txt", CSVReaderWriter.Mode.Read);

            Assert.Throws <IndexOutOfRangeException>(() => csvReadWriter.Read(out column1, out column2));
        }
Esempio n. 18
0
        public void Should_write_columns_faster_with_updates()
        {
            CSVReaderWriter csvReaderWriter = new CSVReaderWriter();

            string fileName = GetTempFilePath();

            csvReaderWriter.Open(fileName, CSVReaderWriter.Mode.Write);

            DateTime startTime = DateTime.Now;

            for (int i = 0; i < 10000; i++)
            {
                csvReaderWriter.Write("COL 1", "column 2", "Test");
            }
            DateTime endTime = DateTime.Now;

            csvReaderWriter.Close();


            TimeSpan updatedDuration = endTime.Subtract(startTime);



            CSVReaderWriterForAnnotation oldCsvReaderWriter = new CSVReaderWriterForAnnotation();

            fileName = GetTempFilePath();

            oldCsvReaderWriter.Open(fileName, CSVReaderWriterForAnnotation.Mode.Write);

            startTime = DateTime.Now;
            for (int i = 0; i < 100000; i++)
            {
                oldCsvReaderWriter.Write("COL 1", "column 2", "Test");
            }
            endTime = DateTime.Now;

            oldCsvReaderWriter.Close();


            TimeSpan oldDuration = endTime.Subtract(startTime);


            string newTime = updatedDuration.ToString("c");
            string oldTime = oldDuration.ToString("c");

            System.Diagnostics.Debug.Write(newTime);
            System.Diagnostics.Debug.Write(oldTime);

            Assert.Less(updatedDuration.Ticks, oldDuration.Ticks);
        }
        public void CSVReaderWriter_Write_ValidNullData()
        {
            bool           writeLineCalled = false;
            MockDataWriter mockWriter      = new MockDataWriter();

            mockWriter.WriteLineDelegate =
                (s) =>
            {
                Assert.AreEqual(string.Empty, s);
                writeLineCalled = true;
            };
            var csvReaderWriter = new CSVReaderWriter(null, mockWriter, delimiter);

            csvReaderWriter.Write(null);
            Assert.True(writeLineCalled);
        }
        public void CSVReaderWriter_Write_ValidSingleValueData()
        {
            bool           writeLineCalled = false;
            MockDataWriter mockWriter      = new MockDataWriter();

            mockWriter.WriteLineDelegate =
                (s) =>
            {
                Assert.AreEqual("somedata", s);
                writeLineCalled = true;
            };
            var csvReaderWriter = new CSVReaderWriter(null, mockWriter, delimiter);

            csvReaderWriter.Write("somedata");
            Assert.True(writeLineCalled);
        }
        public void Open_For_Write_When_WriterStream_Initialized_Write_Data()
        {
            var csvReadWriter = new CSVReaderWriter();

            csvReadWriter.Open(@"test_data\test.txt", CSVReaderWriter.Mode.Write);
            csvReadWriter.Write("test line 1 Column 1", "test line 1 Column 2");// The functionality forces to create the file with two columns which is quite rigid.
            csvReadWriter.Close();
            string column1 = null;
            string column2 = null;

            csvReadWriter.Open(@"test_data\test.txt", CSVReaderWriter.Mode.Read);
            csvReadWriter.Read(out column1, out column2);
            csvReadWriter.Close();
            Assert.AreEqual("test line 1 Column 1", column1);
            Assert.AreEqual("test line 1 Column 2", column2);
        }
        public void CSVReaderWriter_Write_ValidMultiValueData()
        {
            bool           writeLineCalled = false;
            MockDataWriter mockWriter      = new MockDataWriter();

            mockWriter.WriteLineDelegate =
                (s) =>
            {
                Assert.AreEqual(string.Join(delimiter.ToString(), new string[] { "data1", "data2", "data3" }), s);
                writeLineCalled = true;
            };

            var csvReaderWriter = new CSVReaderWriter(null, mockWriter, delimiter);

            csvReaderWriter.Write("data1", "data2", "data3");
            Assert.True(writeLineCalled);
        }
        public void Write_Method_can_write_to_file()
        {
            string expectedFilePath = @"test_data\file_with_content_to_write_to.txt";
            var    fileToWriteTo    = new FileInfo(expectedFilePath);

            fileToWriteTo.Delete();

            Assert.That(!fileToWriteTo.Exists, string.Format("File should have been deleted {0} \n but still exists", fileToWriteTo.FullName));

            _subject.Open(expectedFilePath, CSVReaderWriter.Mode.Write);

            string[] columns = new string[] { "First Column", "Second column", "Third column" };
            _subject.Write(columns);
            _subject.Close();

            var actualFile = new FileInfo(expectedFilePath);

            Assert.That(actualFile.Exists, string.Format("File was not created in {0}", actualFile.FullName));
            Assert.That(actualFile.Length, Is.Not.EqualTo(0), string.Format("File was empty in {0}", actualFile.FullName));
        }
        public void TestCSVWrite()
        {
            CSVReaderWriter writer = new CSVReaderWriter();

            writer.Open(testOutputFile, CSVReaderWriter.Mode.Write);

            string[] line = File.ReadAllLines(testInputFile);

            int maxLines = 5;

            for (int i = 0; i < maxLines; i++)
            {
                writer.Write(line[i].Split(separator));
            }

            var outputFile = new FileInfo(testOutputFile);

            Assert.IsTrue(outputFile.Exists);
            Assert.IsTrue(outputFile.Length > 0);
            Assert.AreEqual(File.ReadAllLines(outputFile.FullName).Count(), maxLines);
        }
        public void canWriteToFile()
        {
            CSVReaderWriter rw = new CSVReaderWriter();

            //write  could generate a random string to compare with the read but keeping it simple for now
            rw.Open(TestInputFileForEditing, Mode.Write);
            rw.Write(new string[] { "some data col1 asdfasdfasdf", "some more data for col2" });
            rw.Close();

            //read what was written
            rw.Open(TestInputFileForEditing, Mode.Read);

            string col1 = "", col2 = "";
            bool   result = rw.Read(out col1, out col2);

            rw.Close();

            Assert.That(col1, Is.EquivalentTo("some data col1 asdfasdfasdf"));
            Assert.That(col2, Is.EquivalentTo("some more data for col2"));
            Assert.That(result, Is.True);
        }
        public void WhenInWriteMode_WithValidFilePath_Then_WritesSuccesffully()
        {
            using (var readerWriter = new CSVReaderWriter())
            {
                var mode = CSVReaderWriter.Mode.Write;
                var expectedWiteText1 = @"I am a writer";
                var expectedWiteText2 = @"I just wrote this";
                readerWriter.Open(_currentDirectory + _csvWriteTestData, mode);
                readerWriter.Write(expectedWiteText1, expectedWiteText2);

                //then open the file and verify written content
                mode = CSVReaderWriter.Mode.Read;
                string col1, col2;
                readerWriter.Open(_currentDirectory + _csvWriteTestData, mode);

                var actual = readerWriter.Read(out col1, out col2);
                Assert.IsTrue(actual);
                Assert.That(col1, Is.EqualTo(expectedWiteText1));
                Assert.That(col2, Is.EqualTo(expectedWiteText2));
            }
        }
Esempio n. 27
0
        public void Should_create_email_mailshot()
        {
            using (var reader = new CSVReaderWriter()) {
                bool status = reader.Open(TestOutputDir + "TestEmail.csv", CSVReaderWriter.Mode.Write);
                if (status)
                {
                    reader.Write("A Test Name", "*****@*****.**");
                }
            }

            using (var reader = new CSVReaderWriter())
            {
                bool status = reader.Open(TestOutputDir + "TestEmail.csv", CSVReaderWriter.Mode.Read);
                if (status)
                {
                    string column1, column2;
                    reader.Read(out column1, out column2);
                    Assert.AreEqual("A Test Name", column1);
                }
            }
        }
Esempio n. 28
0
        public void Should_write_columns()
        {
            CSVReaderWriter csvReaderWriter = new CSVReaderWriter();

            string fileName = GetTempFilePath();

            csvReaderWriter.Open(fileName, CSVReaderWriter.Mode.Write);

            csvReaderWriter.Write("COL 1", "column 2", "Test");

            csvReaderWriter.Close();

            string contents = string.Empty;

            using (FileStream inputStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                using (StreamReader reader = new StreamReader(inputStream))
                {
                    contents = reader.ReadToEnd();
                }
            }

            Assert.IsNotNullOrEmpty(contents);

            //strip newline
            contents = contents.Replace(Environment.NewLine, string.Empty);


            string[] columnsFromFile = contents.Split('\t');

            Assert.AreEqual(3, columnsFromFile.Length);


            Assert.AreEqual("COL 1", columnsFromFile[0]);

            Assert.AreEqual("column 2", columnsFromFile[1]);

            Assert.AreEqual("Test", columnsFromFile[2]);
        }
Esempio n. 29
0
        public void Read_and_Write_Methods_Should_read_and_write_file()
        {
            var reader  = new CSVReaderWriter();
            var writer  = new CSVReaderWriter();
            int counter = 0;

            reader.Open(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, TestInputFile), CSVReaderWriter.Mode.Read);
            writer.Open(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, TestOutputFile), CSVReaderWriter.Mode.Write);

            string column1, column2;

            while (reader.Read(out column1, out column2))
            {
                writer.Write(new string[] { column1, column2 });
                counter++;
            }

            reader.Close();
            writer.Close();

            Assert.That(counter, Is.EqualTo(229));
            Assert.True(File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, TestOutputFile)));
            File.Delete(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, TestOutputFile));
        }
        public void CSVReaderWriter_Write_NullWriter()
        {
            var csvReaderWriter = new CSVReaderWriter(null, null, delimiter);

            Assert.Throws <ApplicationException>(() => csvReaderWriter.Write("somedata"));
        }