Example #1
0
        public void OpenCallsCorrectOpenMethod()
        {
            _csvReader.Open(_testFile).Returns(true);
            var result = _csvReaderWriter.Open(_testFile, CSVReaderWriter.Mode.Read);

            Assert.IsTrue(result);
        }
 public void Should_open_when_parameters_are_clean()
 {
     Assert.DoesNotThrow(() =>
     {
         _CSVReaderWriter.Open(TestInputFile, CSVReaderWriter.Mode.Read);
     });
 }
Example #3
0
        public void ShouldOpenCSVReader()
        {
            _csvReaderWriter.Open(Filename, CSVReaderWriter.Mode.Read);

            _csvReader.Verify(x => x.Open(Filename));

            _csvReaderWriter.Close();
        }
        public void Opening_csv_file_for_read_should_be_done_by_reader()
        {
            //arrange

            //act
            _csvReaderWriter.Open(FileName, Mode.Read);

            //assert
            _csvStreamReader.Verify(s => s.Open(FileName));
        }
        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));
        }
Example #6
0
 public void Open_With_Read_Mode_Should_Not_Throw_Exception()
 {
     // Arrange
     // Act
     // Assert
     Assert.DoesNotThrow(() => _sut.Open(_contactsCSVPath, CSVReaderWriter.Mode.Read));
 }
        public void canOpenAndCloseFile()
        {
            CSVReaderWriter rw = new CSVReaderWriter();

            rw.Open(TestInputFile, Mode.Read);
            rw.Close();
        }
        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);
        }
        private Tuple <int, Exception> CountLines(string filename, bool withOutValues)
        {
            var       csvReader = new CSVReaderWriter();
            string    string1   = null;
            string    string2   = null;
            Exception ex        = null;

            csvReader.Open(filename, CSVReaderWriter.Mode.Read);
            int lineCount = 0;

            try
            {
                while (withOutValues ? csvReader.Read(out string1, out string2) : csvReader.Read(string1, string2))
                {
                    lineCount++;
                }
            }
            catch (Exception e)
            {
                ex = e;
            }

            csvReader.Close();

            return(new Tuple <int, Exception>(lineCount, ex));
        }
        public void Process(string inputFile)
        {
            try
            {
                var reader = new CSVReaderWriter();
                reader.Open(inputFile, CSVReaderWriter.Mode.Read);

                string column1, column2;

                while (reader.Read(out column1, out column2))
                {
                    if (_mailShot is v1.IMailShot)
                    {
                        ((v1.IMailShot)(_mailShot)).SendMailShot(column1, column2);
                    }
                    else if (_mailShot is v2.IMailShot)
                    {
                        ((v2.IMailShot)(_mailShot)).SendEmailMailShot(column1, column2);
                    }

                    //_mailShot.SendMailShot(column1, column2);
                }

                //reader.Close();
            }
            catch (Exception ex)
            {
                //log error to identify and rectify
            }
        }
        public void Should_read_columns_from_file()
        {
            CSVReaderWriter csvReaderWriter = new CSVReaderWriter();

            string fileName = GetTempFilePath();


            using (FileStream outputStream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                using (StreamWriter writer = new StreamWriter(outputStream))
                {
                    writer.WriteLine("ITEM 1\titeM 2");
                }
            }


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

            string column1 = string.Empty;
            string column2 = string.Empty;


            Assert.True(csvReaderWriter.Read(out column1, out column2));

            Assert.AreEqual("ITEM 1", column1);

            Assert.AreEqual("iteM 2", column2);
        }
        public void CSVReaderWriter_Read_NullWriterOpen()
        {
            var csvReaderWriter = new CSVReaderWriter(null, null, delimiter);

            Assert.Throws <ApplicationException>(() => csvReaderWriter.Open("somedata", Mode.Read));
            Assert.Throws <ApplicationException>(() => csvReaderWriter.OpenRead("somedata"));
        }
        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 Should_throw_on_invalid_open_filemode()
        {
            CSVReaderWriter csvReaderWriter = new CSVReaderWriter();

            string fileName = GetTempFilePath();

            Assert.Throws <ArgumentException>(() => { csvReaderWriter.Open(fileName, CSVReaderWriter.Mode.Write | CSVReaderWriter.Mode.Read); });
        }
 public void WhenInWriteMode_WithNonExistingFile_Then_NewFileIsCreated()
 {
     using (var readerWriter = new CSVReaderWriter())
     {
         var mode = CSVReaderWriter.Mode.Write;
         Assert.DoesNotThrow(() => readerWriter.Open(_currentDirectory + _nonExistingFile, mode));
     }
 }
Example #16
0
        public void Open_Method_to_write_Should_not_throw_Exception()
        {
            var csvReaderWriter = new CSVReaderWriter();

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

            csvReaderWriter.Close();
        }
 public void WhenInReadMode_WithNonExistingFileOrInvalidFilePath_Then_ThrowsFileNotFoundException()
 {
     using (var readerWriter = new CSVReaderWriter())
     {
         var mode = CSVReaderWriter.Mode.Read;
         Assert.Throws <FileNotFoundException>(() => readerWriter.Open(_currentDirectory + @"\sss", mode));
     }
 }
Example #18
0
        public async Task ReadAsync_Empty_Returns_Empty_Enumeration()
        {
            using (var csv = new CSVReaderWriter())
            {
                csv.Open(EmptyFile, CSVReaderWriter.Mode.Read);

                Assert.That(await csv.ReadAsync(), Is.Empty);
            }
        }
 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 WhenInValidMode_Then_ThrowsException()
        {
            //declare both read and write mode
            var mode = CSVReaderWriter.Mode.Read | CSVReaderWriter.Mode.Write;

            using (var readerWriter = new CSVReaderWriter())
            {
                Assert.Throws <Exception>(() => readerWriter.Open(_currentDirectory + _csvReadTestData, mode));
            }
        }
 public void WhenInWriteMode_ReadingAFile_Then_ThrowsAnError()
 {
     using (var readerWriter = new CSVReaderWriter())
     {
         var    mode = CSVReaderWriter.Mode.Write;
         string col1, col2;
         readerWriter.Open(_currentDirectory + _csvReadTestData, mode);
         Assert.Throws <ArgumentNullException>(() => readerWriter.Read(out col1, out col2));
     }
 }
        public void Read_With_Out_Parameters_When_ReaderStream_Initialized_Reads_Data()
        {
            string column1       = null;
            string column2       = null;
            var    csvReadWriter = new CSVReaderWriter();

            csvReadWriter.Open(@"test_data\contacts.csv", CSVReaderWriter.Mode.Read);
            csvReadWriter.Read(out column1, out column2);
            Assert.NotNull(column1);
            Assert.NotNull(column2);
        }
Example #25
0
        public void Should_throw_with_not_found_file_with_read_mode()
        {
            string notFoundFile = @"notfound.csv";

            var csvReaderWriter = new CSVReaderWriter();

            Assert.Throws <FileNotFoundException>(delegate
            {
                csvReaderWriter.Open(notFoundFile, CSVReaderWriter.Mode.Read);
            });
        }
Example #26
0
        public async Task ReadAsync_One_Column_Returns_One_Column()
        {
            using (var csv = new CSVReaderWriter())
            {
                csv.Open(OneColumnFile, CSVReaderWriter.Mode.Read);

                var result = await csv.ReadAsync();

                Assert.That(result, Is.Not.Empty);
                Assert.That(result.First(), Is.EqualTo("hello"));
            }
        }
        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));
            }
        }
Example #29
0
        public async Task ReadAsync_Returns_Expected_Column_Count()
        {
            using (var csv = new CSVReaderWriter())
            {
                csv.Open(ContactsFile, CSVReaderWriter.Mode.Read);

                string[] result = (await csv.ReadAsync()).ToArray();

                Assert.That(result[0], Is.EqualTo("Shelby Macias"));
                Assert.That(result[1], Is.EqualTo("3027 Lorem St.|Kokomo|Hertfordshire|L9T 3D5|England"));
            }
        }
Example #30
0
        public void Should_create_file_for_missing_file_with_write_mode()
        {
            var missingFileToCreate = Path.Combine(TestDataOutputDirectory, "new.csv");

            TestUtils.DeleteFile(missingFileToCreate); // make sure it definately doesn't exist!

            var csvReaderWriter = new CSVReaderWriter();

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

            Assert.True(File.Exists(missingFileToCreate));
        }