Ejemplo n.º 1
0
        public void TryReadRecord_StreamProvidedWithMultipleRecords_AllRecordsAreRead()
        {
            var records     = "Record 1\nRecord 2";
            var recordsRead = 0;

            bool couldReadRecord;
            IEnumerable <FieldFailure> failures;

            using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(records)))
                using (var dataReader = new TextStreamReader(stream))
                {
                    dataReader.Open();

                    do
                    {
                        couldReadRecord = dataReader.TryReadRecord(out var record, out failures);

                        if (couldReadRecord)
                        {
                            Assert.AreEqual(records.Split('\n')[++recordsRead - 1], record);
                        }
                    }while (couldReadRecord || failures?.Count() > 0);
                }

            Assert.AreEqual(2, recordsRead);
        }
Ejemplo n.º 2
0
        public void TryReadRecord_StreamFactoryProvidedWithOneRecord_RecordIsRead()
        {
            var records     = "Record 1";
            var recordsRead = 0;

            bool couldReadRecord;
            IEnumerable <FieldFailure> failures;

            using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(records)))
            {
                var streamFactory = MockRepository.GenerateMock <IStreamFactory>();

                streamFactory.Expect(x => x.Create()).Return(stream).Repeat.Once();

                using (var dataReader = new TextStreamReader(streamFactory))
                {
                    dataReader.Open();

                    do
                    {
                        couldReadRecord = dataReader.TryReadRecord(out var record, out failures);

                        if (couldReadRecord)
                        {
                            Assert.AreEqual(records.Split('\n')[++recordsRead - 1], record);
                        }
                    }while (couldReadRecord || failures?.Count() > 0);
                }
            }

            Assert.AreEqual(1, recordsRead);
        }
Ejemplo n.º 3
0
        public void TryReadRecord_RecordFailsToMap_RecordIsNotReadAndFailureIsReturned()
        {
            var records  = "Record 1";
            var failures = new List <FieldFailure>()
            {
                new FieldFailure()
            };

            var recordMapper = MockRepository.GenerateMock <IRecordMapper>();

            recordMapper.Stub(x => x.TryMap(
                                  Arg <object> .Is.Equal(records.Split('\n')[0]),
                                  out Arg <object> .Out(null).Dummy,
                                  out Arg <IEnumerable <FieldFailure> > .Out(failures).Dummy))
            .Return(false);

            using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(records)))
                using (var dataReader = new TextStreamReader(stream, recordMapper))
                {
                    dataReader.Open();

                    var couldReadRecord = dataReader.TryReadRecord(out var returnedRecord, out var returnedFailures);

                    Assert.IsFalse(couldReadRecord);
                    Assert.IsNull(returnedRecord);
                    Assert.AreEqual(failures, returnedFailures);
                }
        }
Ejemplo n.º 4
0
        public void MultiThreadedTest()
        {
            using (var input = new MemoryStream(Encoding.UTF8.GetBytes(Resources.LargerFile)))
                using (var output = new MemoryStream())
                {
                    var loader = new TextStreamReader();
                    var writer = new RxTextStreamWriter();

                    var processors = new[]
                    {
                        new NullConverter(),
                    };

                    var app = new App(new NullLogger(), loader, writer, processors);

                    var config = new Configuration
                    {
                        Input   = input,
                        Output  = output,
                        Threads = 3,
                    };

                    app.Run(config);

                    output.Seek(0, SeekOrigin.Begin);
                    var result        = Encoding.UTF8.GetString(output.ToArray());
                    var actualLines   = result.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
                    var expectedLines = Resources.LargerFile.Split(new[] { Environment.NewLine }, StringSplitOptions.None);

                    CollectionAssert.AreEqual(expectedLines, actualLines);
                }
        }
Ejemplo n.º 5
0
        public void SmokeTest()
        {
            using (var input = new MemoryStream(Encoding.UTF8.GetBytes(Resources.SmallFile)))
                using (var output = new MemoryStream())
                {
                    var loader = new TextStreamReader();
                    var writer = new RxTextStreamWriter();

                    var processors = new[]
                    {
                        new HelperConverter(),
                    };

                    var app = new App(new NullLogger(), loader, writer, processors);

                    var config = new Configuration
                    {
                        Input   = input,
                        Output  = output,
                        Threads = 1,
                    };

                    app.Run(config);

                    output.Seek(0, SeekOrigin.Begin);
                    var result = Encoding.UTF8.GetString(output.ToArray());

                    Assert.AreEqual("2:4:6\r\n8:10:12\r\n", result);
                }
        }
Ejemplo n.º 6
0
        public void TryReadRecord_StreamProvidedWithZeroRecords_NoRecordsAreRead()
        {
            var records = String.Empty;

            using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(records)))
                using (var dataReader = new TextStreamReader(stream))
                {
                    dataReader.Open();

                    var couldReadRecord = dataReader.TryReadRecord(out var record, out var failures);

                    Assert.IsFalse(couldReadRecord);
                }
        }
Ejemplo n.º 7
0
        public void TryReadRecord_RecordMapperProvidedWithOneRecord_RecordsIsReadAndMapped()
        {
            var records       = "Record 1";
            var mappedRecords = "Mapped Record 1";
            var recordsRead   = 0;

            var recordMapper = MockRepository.GenerateMock <IRecordMapper>();

            recordMapper.Expect(x => x.TryMap(
                                    Arg <object> .Is.Equal(records.Split('\n')[0]),
                                    out Arg <object> .Out(mappedRecords.Split('\n')[0]).Dummy,
                                    out Arg <IEnumerable <FieldFailure> > .Out(null).Dummy))
            .Return(true)
            .Repeat.Once();

            bool couldReadRecord;
            IEnumerable <FieldFailure> failures;

            using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(records)))
                using (var dataReader = new TextStreamReader(stream, recordMapper))
                {
                    dataReader.Open();

                    do
                    {
                        couldReadRecord = dataReader.TryReadRecord(out var record, out failures);

                        if (couldReadRecord)
                        {
                            Assert.AreEqual(mappedRecords.Split('\n')[++recordsRead - 1], record);
                        }
                    }while (couldReadRecord || failures?.Count() > 0);
                }

            recordMapper.VerifyAllExpectations();

            Assert.AreEqual(1, recordsRead);
        }
Ejemplo n.º 8
0
 public void SetUp()
 {
     sut = new TextStreamReader();
 }