Exemple #1
0
        public void WriteOneHasExpectedResult()
        {
            var expectedHeader = _fixture.Create <DbaseFileHeader>();
            var expectedRecord = _fixture.GenerateDbaseRecord(expectedHeader.Schema.Fields);

            using (var stream = new MemoryStream())
            {
                using (var sut = new DbaseBinaryWriter(expectedHeader, new BinaryWriter(stream, Encoding.ASCII, true)))
                {
                    //Act
                    sut.Write(expectedRecord);
                }

                // Assert
                stream.Position = 0;

                using (var reader = new BinaryReader(stream, Encoding.ASCII, true))
                {
                    var actualHeader = DbaseFileHeader.Read(reader);
                    var actualRecord = new AnonymousDbaseRecord(actualHeader.Schema.Fields);
                    actualRecord.Read(reader);
                    var actualEndOfFile = reader.ReadByte();

                    Assert.Equal(expectedHeader, actualHeader);
                    Assert.Equal(expectedRecord, actualRecord, new DbaseRecordEqualityComparer());
                    Assert.Equal(DbaseRecord.EndOfFile, actualEndOfFile);
                }
            }
        }
        public void CanReadWrite()
        {
            var sut = _fixture.Create <DbaseRecord>();

            using (var stream = new MemoryStream())
            {
                using (var writer = new BinaryWriter(stream, Encoding.ASCII, true))
                {
                    sut.Write(writer);
                    writer.Flush();
                }

                stream.Position = 0;

                using (var reader = new BinaryReader(stream, Encoding.ASCII, true))
                {
                    var result = new AnonymousDbaseRecord(
                        sut.Values.Select(value => value.Field).ToArray()
                        );
                    result.Read(reader);

                    Assert.Equal(sut.IsDeleted, result.IsDeleted);
                    Assert.Equal(sut.Values, result.Values, new DbaseFieldValueEqualityComparer());
                }
            }
        }
Exemple #3
0
        public void ReadingCharacterWithDateTimeValueHasExpectedResult()
        {
            var value  = _fixture.Create <DateTime>();
            var fields = new[]
            {
                new DbaseField(
                    _fixture.Create <DbaseFieldName>(),
                    DbaseFieldType.DateTime,
                    ByteOffset.Initial,
                    new DbaseFieldLength(15),
                    new DbaseDecimalCount(0)
                    )
            };
            var values = new DbaseFieldValue[]
            {
                new DbaseDateTime(
                    fields[0],
                    value
                    )
            };

            using (var stream = new MemoryStream())
            {
                using (var writer = new BinaryWriter(stream, Encoding.ASCII, true))
                {
                    new AnonymousDbaseRecord(values).Write(writer);
                    writer.Flush();
                }

                stream.Position = 0;

                using (var reader = new BinaryReader(stream, Encoding.ASCII, true))
                {
                    fields[0] = new DbaseField(
                        fields[0].Name,
                        DbaseFieldType.Character,
                        ByteOffset.Initial,
                        new DbaseFieldLength(15),
                        new DbaseDecimalCount(0)
                        );

                    var sut = new AnonymousDbaseRecord(fields);

                    //Act
                    sut.Read(reader);

                    Assert.False(sut.IsDeleted);
                    Assert.Equal(new DbaseFieldValue[]
                    {
                        new DbaseDateTime(
                            fields[0],
                            value
                            )
                    }, sut.Values, new DbaseFieldValueEqualityComparer());
                }
            }
        }
Exemple #4
0
        public void ConstructionUsingValuesHasExpectedResult()
        {
            var fields = _fixture.CreateMany <DbaseField>().ToArray();
            var values = Array.ConvertAll(fields, field => field.CreateFieldValue());

            var sut = new AnonymousDbaseRecord(values);

            Assert.False(sut.IsDeleted);
            Assert.Same(values, sut.Values);
        }
Exemple #5
0
        public void ConstructionUsingFieldsHasExpectedResult()
        {
            var fields         = _fixture.CreateMany <DbaseField>().ToArray();
            var expectedValues = Array.ConvertAll(fields, field => field.CreateFieldValue());

            var sut = new AnonymousDbaseRecord(fields);

            Assert.False(sut.IsDeleted);
            Assert.Equal(expectedValues, sut.Values, new DbaseFieldValueEqualityComparer());
        }
Exemple #6
0
        public void FromBytesReturnsExpectedResult()
        {
            var fields = _fixture.CreateMany <DbaseField>().ToArray();

            var record = new AnonymousDbaseRecord(fields);
            var bytes  = record.ToBytes(Encoding.UTF8);
            var sut    = new AnonymousDbaseRecord(fields);

            sut.FromBytes(bytes, Encoding.UTF8);

            Assert.False(sut.IsDeleted);
            Assert.Equal(record.Values, sut.Values, new DbaseFieldValueEqualityComparer());
        }
Exemple #7
0
        public void ToBytesReturnsExpectedResult()
        {
            var fields = _fixture.CreateMany <DbaseField>().ToArray();
            var values = Array.ConvertAll(fields, field => field.CreateFieldValue());

            var sut = new AnonymousDbaseRecord(values);

            var result = sut.ToBytes(Encoding.UTF8);

            using (var stream = new MemoryStream())
                using (var writer = new BinaryWriter(stream, Encoding.UTF8))
                {
                    sut.Write(writer);
                    writer.Flush();

                    Assert.Equal(stream.ToArray(), result);
                }
        }
Exemple #8
0
        public static DbaseRecord[] GenerateDbaseRecords(this IFixture fixture, DbaseField[] fields,
                                                         DbaseRecordCount count)
        {
            var records   = new DbaseRecord[count.ToInt32()];
            var inspector = new GenerateValueInspector(fixture);

            for (var index = 0; index < records.Length; index++)
            {
                var record = new AnonymousDbaseRecord(fields);
                foreach (var value in record.Values)
                {
                    value.Inspect(inspector);
                }

                records[index] = record;
            }

            return(records);
        }
Exemple #9
0
        public void WriteManyHasExpectedResult()
        {
            var expectedHeader  = _fixture.Create <DbaseFileHeader>();
            var expectedRecords =
                _fixture.GenerateDbaseRecords(expectedHeader.Schema.Fields, expectedHeader.RecordCount);

            using (var stream = new MemoryStream())
            {
                using (var sut = new DbaseBinaryWriter(expectedHeader, new BinaryWriter(stream, Encoding.ASCII, true)))
                {
                    //Act
                    foreach (var expectedRecord in expectedRecords)
                    {
                        sut.Write(expectedRecord);
                    }
                }

                // Assert
                stream.Position = 0;

                using (var reader = new BinaryReader(stream, Encoding.ASCII, true))
                {
                    var actualHeader  = DbaseFileHeader.Read(reader);
                    var actualRecords = new DbaseRecord[actualHeader.RecordCount.ToInt32()];
                    for (var index = 0; index < actualRecords.Length; index++)
                    {
                        var actualRecord = new AnonymousDbaseRecord(actualHeader.Schema.Fields);
                        actualRecord.Read(reader);
                        actualRecords[index] = actualRecord;
                    }

                    var actualEndOfFile = reader.ReadByte();

                    Assert.Equal(expectedHeader, actualHeader);
                    Assert.Equal(expectedRecords, actualRecords, new DbaseRecordEqualityComparer());
                    Assert.Equal(DbaseRecord.EndOfFile, actualEndOfFile);
                }
            }
        }
        public void ReadingUnacceptableIsDeletedFlagHasExpectedResult()
        {
            using (var stream = new MemoryStream())
            {
                using (var writer = new BinaryWriter(stream, Encoding.ASCII, true))
                {
                    var unacceptable = new Generator <byte>(_fixture).First(candidate => candidate != 0x20 && candidate != 0x2A);
                    writer.Write(unacceptable);
                    writer.Flush();
                }

                stream.Position = 0;

                using (var reader = new BinaryReader(stream, Encoding.ASCII, true))
                {
                    var sut = new AnonymousDbaseRecord(new DbaseField[0]);

                    //Act
                    Assert.Throws <DbaseRecordException>(() => sut.Read(reader));
                }
            }
        }
        public void ReadingEndOfFileHasExpectedResult()
        {
            using (var stream = new MemoryStream())
            {
                using (var writer = new BinaryWriter(stream, Encoding.ASCII, true))
                {
                    writer.Write(DbaseRecord.EndOfFile);
                    writer.Flush();
                }

                stream.Position = 0;

                using (var reader = new BinaryReader(stream, Encoding.ASCII, true))
                {
                    var sut = new AnonymousDbaseRecord(new DbaseField[0]);

                    //Act
                    var exception = Assert.Throws <EndOfStreamException>(() => sut.Read(reader));
                    Assert.Equal("The end of file marker was reached.", exception.Message);
                }
            }
        }