< Summary

Information
Class: CSVReaderTests.RowReaderTests
Assembly: csvreadertests.dll
File(s): C:\skola_karlovka\RP\code\csv-validator\CSV_Validator\Tests\CSVReaderTests\RowReaderTests.cs
Line coverage
99%
Covered lines: 130
Uncovered lines: 1
Coverable lines: 131
Total lines: 193
Line coverage: 99.2%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

C:\skola_karlovka\RP\code\csv-validator\CSV_Validator\Tests\CSVReaderTests\RowReaderTests.cs

#LineLine coverage
 1using static System.Runtime.InteropServices.JavaScript.JSType;
 2using System;
 3using System.Data;
 4using ValidateLib.TabularData.Parsing;
 5using ValidateLib.Metadata.Descriptors;
 6using ValidateLib.Metadata.ParsingAndValidation;
 7using ValidateLib.ErrorsAndWarnings.Warnings;
 8
 9namespace CSVReaderTests
 10{
 11    public class RowReaderTests
 12    {
 13        public readonly string testFilesDirectory;
 114        private DialectDescriptor defaultDialect = new DialectDescriptor();
 15        private Flags defaultFlags;
 116        public RowReaderTests()
 117        {
 118            testFilesDirectory = Path.Combine(GetProjectDirectory(), "TestFiles","RowReaderTestFiles");
 119            defaultFlags = FlagsCreator.ExtractFlagsFromDialectDescriptor(defaultDialect);
 120        }
 21        static string GetProjectDirectory()
 122        {
 123            string? currentDirectory = Directory.GetCurrentDirectory();
 24
 125            while (!string.IsNullOrEmpty(currentDirectory))
 126            {
 127                string[] projectFiles = Directory.GetFiles(currentDirectory, "*.csproj");
 28
 129                if (projectFiles.Length > 0)
 130                    return currentDirectory;
 131                currentDirectory = Directory.GetParent(currentDirectory)?.FullName;
 132            }
 33
 034            throw new Exception("Could not find project directory.");
 35
 36
 137        }
 38
 39        [Fact]
 40        public void SimpleTestWithDefaulDialect()
 141        {
 142            var testFilePath = Path.Combine(testFilesDirectory, "simple.csv");
 143            var expectedLines = new string[]
 144            {
 145                "countryCode,latitude,longitude,name",
 146                "AD,42.546245,1.601554,Andorra",
 147                "AE,23.424076,53.847818,\"United Arab Emirates\"",
 148                "AF,33.93911,67.709953,Afghanistan"
 149
 150            };
 51
 152            using (FileStream fs = new FileStream(testFilePath, FileMode.Open, FileAccess.Read))
 153            {
 154                CustomStreamReader csr = new CustomStreamReader(fs, 50, 6);
 55
 156                RowReader rowReader = new RowReader(defaultFlags);
 157                foreach(var line in expectedLines)
 158                {
 159                    Assert.Equal(line, rowReader.ReadRow(csr));
 160                }
 161            }
 162        }
 63
 64        [Fact]
 65        public void SimpleTestWithDefaulDialectWithEscapedChar()
 166        {
 167            var testFilePath = Path.Combine(testFilesDirectory, "escaped.csv");
 168            var expectedLines = new string[]
 169            {
 170                "countryCode,latitude,longitude,name",
 171                "AD,42.546245,1.601554,Andorra",
 172                "AE,23.424076,53.847818,\"United \"\"Arab\"\" Emirates\"",
 173                "AF,33.93911,67.709953,Afghanistan"
 174
 175            };
 76
 177            using (FileStream fs = new FileStream(testFilePath, FileMode.Open, FileAccess.Read))
 178            {
 179                CustomStreamReader csr = new CustomStreamReader(fs, 50, 6);
 80
 181                RowReader rowReader = new RowReader(defaultFlags);
 182                foreach (var line in expectedLines)
 183                {
 184                    Assert.Equal(line, rowReader.ReadRow(csr));
 185                }
 186            }
 187        }
 88
 89        [Fact]
 90        public void TestQuotedNewLineDefaulDialect()
 191        {
 192            var testFilePath = Path.Combine(testFilesDirectory, "newline.csv");
 193            var expectedLines = new string[]
 194            {
 195                "countryCode,latitude,longitude,name",
 196                "AD,42.546245,1.601554,Andorra",
 197                "AE,23.424076,53.847818,\"United\r\n Arab Emirates\"",
 198                "AF,33.93911,67.709953,Afghanistan"
 199
 1100            };
 101
 1102            using (FileStream fs = new FileStream(testFilePath, FileMode.Open, FileAccess.Read))
 1103            {
 1104                CustomStreamReader csr = new CustomStreamReader(fs, 50, 6);
 105
 1106                RowReader rowReader = new RowReader(defaultFlags);
 1107                foreach (var line in expectedLines)
 1108                {
 1109                    Assert.Equal(line, rowReader.ReadRow(csr));
 1110                }
 1111            }
 1112        }
 113
 114        [Fact]
 115        public void SimpleTestWithDialectDifferentEndOfLine()
 1116        {
 1117            var testFilePathCsv = Path.Combine(testFilesDirectory, "Dialect","test01","countries.csv");
 1118            var testFilePathMetadata = Path.Combine(testFilesDirectory, "Dialect", "test01", "countries-metadata.json");
 119
 1120            var warnings = new List<Warning>();
 121
 1122            var tableDescriptor = MetadataParserValidator.ProcessTable(
 1123                warnings,
 1124                testFilePathMetadata
 1125            );
 126
 1127            var expectedLines = new string[]
 1128            {
 1129                "countryCode,latitude,longitude,name",
 1130                "AD,42.546245,1.601554,Andorra",
 1131                "AE,23.424076,53.847818,\"United Arab Emirates\"",
 1132                "AF,33.93911,67.709953,Afghanistan"
 1133
 1134            };
 135
 1136            using (FileStream fs = new FileStream(testFilePathCsv, FileMode.Open, FileAccess.Read))
 1137            {
 138
 139
 1140                Flags flags = FlagsCreator.ExtractFlagsFromDialectDescriptor(tableDescriptor.dialect!._value!);
 1141                RowReader rowReader = new RowReader(flags);
 142
 1143                CustomStreamReader csr = new CustomStreamReader(fs, 50, rowReader.MaximalLineTerminatorLength);
 1144                foreach (var line in expectedLines)
 1145                {
 1146                    Assert.Equal(line, rowReader.ReadRow(csr));
 1147                }
 1148            }
 149
 150
 1151        }
 152
 153
 154        [Fact]
 155        public void SimpleTestWithDialectDifferentQuoteChar()
 1156        {
 1157            var testFilePathCsv = Path.Combine(testFilesDirectory, "Dialect", "test02", "countries.csv");
 1158            var testFilePathMetadata = Path.Combine(testFilesDirectory, "Dialect", "test02", "countries-metadata.json");
 159
 1160            var warnings = new List<Warning>();
 161
 1162            var tableDescriptor = MetadataParserValidator.ProcessTable(
 1163                warnings,
 1164                testFilePathMetadata
 1165            );
 166
 1167            var expectedLines = new string[]
 1168            {
 1169                "countryCode,latitude,longitude,name",
 1170                "AD,42.546245,1.601554,Andorra",
 1171                "AE,23.424076,53.847818,QCHUnited\r\n Arab EmiratesQCH",
 1172                "AF,33.93911,67.709953,Afghanistan"
 1173
 1174            };
 175
 1176            using (FileStream fs = new FileStream(testFilePathCsv, FileMode.Open, FileAccess.Read))
 1177            {
 178
 179
 1180                Flags flags = FlagsCreator.ExtractFlagsFromDialectDescriptor(tableDescriptor.dialect!._value!);
 1181                RowReader rowReader = new RowReader(flags);
 182
 1183                CustomStreamReader csr = new CustomStreamReader(fs, 50, rowReader.MaximalLineTerminatorLength);
 1184                foreach (var line in expectedLines)
 1185                {
 1186                    Assert.Equal(line, rowReader.ReadRow(csr));
 1187                }
 1188            }
 189
 190
 1191        }
 192    }
 193}