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 Checks_for_lines_that_have_a_valid_number_of_columns()
        {
            _subject.Open(contacts_test_data_cvs, CSVReaderWriter.Mode.Read);

            var hasValidFirstRow = _subject.Read("", "");

            Assert.That(hasValidFirstRow, "First row had no columns");
        }
        public void Should_read_line()
        {
            _CSVReaderWriter.Open(TestInputFile, CSVReaderWriter.Mode.Write);

            var columns = new string[] { Column1Value, Column2Value };

            _CSVReaderWriter.Write(columns);

            _stream.Position = 0;

            var canRead = _CSVReaderWriter.Read(string.Empty, string.Empty);

            Assert.That(canRead, Is.EqualTo(true));
        }
        public void FileReaderIsCallledWhenCallingRead()
        {
            var handleStreams = A.Fake <IHandleStreams>();

            handleStreams.StreamReader = new StreamReader(new MemoryStream(Encoding.Default.GetBytes("hello")));
            var mode = CSVReaderWriter.Mode.Read;

            A.CallTo(() => _fileOpener.Open("someFile", mode)).Returns(handleStreams);
            _csvReaderWriter.Open("someFile", mode);
            string column1, column2;

            _csvReaderWriter.Read(out column1, out column2);
            A.CallTo(() => _fileReader.Read(out column1, out column2, handleStreams))
            .MustHaveHappened();
        }
        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 Read_single_line_should_return_true_and_not_alter_values()
        {
            // Arrange
            var name    = _testName;
            var address = _testAddress;

            _sut.Open(_contactsCSVPath, CSVReaderWriter.Mode.Read);

            // Act
            var result = _sut.Read(name, address);

            // Assert
            Assert.IsTrue(result);
            Assert.AreEqual(_testName, name);
            Assert.AreEqual(_testAddress, address);
        }
        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);
        }
Esempio n. 9
0
        public void ReadCallsCorrectReadMethod()
        {
            _csvReader.Read().Returns(true);
            _csvReader.Read(out string name, out string address).Returns(false);
            var result = _csvReaderWriter.Read("name", "address");

            Assert.IsTrue(result);
        }
        public void Read_With_Out_Parameters_When_ReaderStream_Not_Initialized_Throws_Exception()
        {
            string column1       = null;
            string column2       = null;
            var    csvReadWriter = new CSVReaderWriter();

            Assert.Throws <Exception>(() => csvReadWriter.Read(out column1, out column2));
        }
        public void CSVReaderWriter_Read_NullReader()
        {
            string column1         = null;
            string column2         = null;
            var    csvReaderWriter = new CSVReaderWriter(null, null, delimiter);

            Assert.Throws <ApplicationException>(() => csvReaderWriter.Read(out column1, out column2));
        }
 public void ReadNotOpenedFile()
 {
     using (var reader = new CSVReaderWriter())
     {
         string column1, column2;
         Assert.Throws <CSVFileNotOpenException>(() => reader.Read(out column1, out column2));
     }
 }
 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));
     }
 }
Esempio n. 14
0
        public void Read_Method_Should_return_false_with_null_ref_input_string_parameters()
        {
            var    csvReaderWriter = new CSVReaderWriter();
            string column1, column2;
            var    result = csvReaderWriter.Read(out column1, out column2);

            Assert.False(result);
            Assert.Null(column1);
            Assert.Null(column2);
        }
        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);
        }
 public void InvokingCSVReaderWriter_WithStreamsContructor_Then_ReadsFileSuccesffully()
 {
     using (var readerWriter = new CSVReaderWriter(new CSVReader(new StreamReader(_currentDirectory + _csvReadTestData)), new CSVWriter(new StreamWriter(_currentDirectory + _csvWriteTestData))))
     {
         string col1, col2;
         var    actual = readerWriter.Read(out col1, out col2);
         Assert.IsTrue(actual);
         Assert.That(col1, Is.EqualTo("Shelby Macias"));
         Assert.That(col2, Is.EqualTo("3027 Lorem St.|Kokomo|Hertfordshire|L9T 3D5|England"));
     }
 }
        public void Read_should_call_Readers_read()
        {
            //arrange
            string col1;
            string col2;

            // act
            _csvReaderWriter.Read(out col1, out col2);

            // assert
            _csvStreamReader.Verify(c => c.ReadLine());
        }
        public void canReadFileDisgardingOutput()
        {
            CSVReaderWriter rw = new CSVReaderWriter();

            rw.Open(TestInputFile, Mode.Read);

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

            rw.Close();

            Assert.That(result, Is.True);
        }
        public void Process(string inputFile)
        {
            _reader.Open(inputFile, CSVReaderWriter.Mode.Read);

            string column1, column2;

            while (_reader.Read(out column1, out column2))
            {
                _mailShot.SendMailShot(column1, column2);
            }

            _reader.Close();
        }
 public void WhenInReadMode_WithValidFilePathAndEmptyText_Then_ReturnsNull()
 {
     using (var readerWriter = new CSVReaderWriter())
     {
         var mode = CSVReaderWriter.Mode.Read;
         readerWriter.Open(_currentDirectory + _csvBlankTestData, mode);
         string col1, col2;
         var    actual = readerWriter.Read(out col1, out col2);
         Assert.That(actual, Is.False);
         Assert.That(col1, Is.Null);
         Assert.That(col2, Is.Null);
     }
 }
        public void Read_ReaderIsDisposed_ThrowException()
        {
            var mockReader = new Mock <ICSVReader>();
            var csv        = new CSVReaderWriter(mockReader.Object, It.IsAny <ICSVWriter>());

            csv.Close();

            string c1;
            string c2;
            var    ex = Assert.Throws <NullReferenceException>(() => csv.Read(out c1, out c2));

            Assert.AreEqual("The file reader is closed. Please reopen it.", ex.Message);
        }
 public void WhenInReadMode_WithValidFilePath_Then_ReadSuccesffully()
 {
     using (var readerWriter = new CSVReaderWriter())
     {
         readerWriter.Open(_currentDirectory + _csvReadTestData, CSVReaderWriter.Mode.Read);
         string col1;
         string col2;
         var    actual = readerWriter.Read(out col1, out col2);
         Assert.That(actual, Is.True);
         Assert.That(col1, Is.EqualTo("Shelby Macias"));
         Assert.That(col2, Is.EqualTo(@"3027 Lorem St.|Kokomo|Hertfordshire|L9T 3D5|England"));
     }
 }
Esempio n. 23
0
        public void ShouldReadFromCSVReader()
        {
            string column1;
            string column2;

            _csvReader.Setup(x => x.Read(out column1, out column2)).Returns(true);
            var result = _csvReaderWriter.Read(out column1, out column2);

            _csvReader.Verify(x => x.Read(out column1, out column2));
            Assert.That(result, Is.True);

            _csvReaderWriter.Close();
        }
        public void CSVReaderWriter_Read_MultiValueTabbedData()
        {
            string         column1    = null;
            string         column2    = null;
            MockDataReader mockReader = new MockDataReader();

            mockReader.ReadLineDelegate = () => string.Join(delimiter.ToString(), new string[] { "data1", "data2", "data3" });

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

            Assert.IsTrue(csvReaderWriter.Read(out column1, out column2));
            Assert.AreEqual("data1", column1);
            Assert.AreEqual("data2", column2);
        }
        public void Read_WithNoOut_Split()
        {
            var    csvReader = new CSVReaderWriter();
            string string1   = null;
            string string2   = null;

            csvReader.Open(TestInputFileBlank, CSVReaderWriter.Mode.Read);

            var output = csvReader.Read(string1, string2);

            Assert.That(output, Is.True);
            Assert.That(string1, Is.Null);
            Assert.That(string2, Is.Null);
        }
        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. 27
0
        public void Read_Method_Should_read_first_line_of_file()
        {
            var csvReaderWriter = new CSVReaderWriter();

            csvReaderWriter.Open(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, TestInputFile), CSVReaderWriter.Mode.Read);

            string column1, column2;

            csvReaderWriter.Read(out column1, out column2);

            csvReaderWriter.Close();

            Assert.IsTrue(string.Equals(column1, "Shelby Macias"));
            Assert.IsTrue(string.Equals(column2, "3027 Lorem St.|Kokomo|Hertfordshire|L9T 3D5|England"));
        }
        public void Read_WithOut_Split()
        {
            var    csvReader = new CSVReaderWriter();
            string string1   = null;
            string string2   = null;

            csvReader.Open(TestInputFileBlank, CSVReaderWriter.Mode.Read);

            var output = csvReader.Read(out string1, out string2);

            csvReader.Close();
            Assert.That(output, Is.True);
            Assert.That(string1, Is.EqualTo("Shelby Macias"));
            Assert.That(string2, Is.EqualTo("3027 Lorem St.|Kokomo|Hertfordshire|L9T 3D5|England"));
        }
Esempio n. 29
0
        public void Process(string inputFile)
        {
            var reader = new CSVReaderWriter();

            reader.Open(inputFile, CSVReaderWriter.Mode.Read);

            string column1, column2;

            while (reader.Read(out column1, out column2))
            {
                emailProvider.Send(column1, column2);
            }

            reader.Close();
        }
Esempio n. 30
0
        public void Process(string inputFile)
        {
            var reader = new CSVReaderWriter(new CSVReader(), new CSVWriter());

            reader.Open(inputFile, CSVReaderWriter.Mode.Read);

            string column1, column2;

            while (reader.Read(out column1, out column2))
            {
                _mailShot.SendMailShot(column1, column2);
            }

            reader.Close();
        }