Exemple #1
0
        public void SimpleParseFirst()
        {
            var parser = new TsvParser(noneIn: "none", noneOut: "mynone");

            string[][] str =
            {
                new[] { "1",     "2",     "3",     "4" },
                new[] { "test1", "test2", "none" },
                new[] { "none",  "test3", "test4", ""  }
            };

            var obj = parser.Parse(str);

            Assert.AreEqual("test4", obj[1, 2]);
            Assert.AreEqual("mynone", obj[1, 3]);
            Assert.AreEqual("mynone", obj[0, 2]);
            Assert.AreEqual("mynone", obj[0, 3]);
            Assert.AreEqual("2", obj.Header[1]);
            Assert.AreEqual(4, obj.ColumnCount);
            Assert.AreEqual("mynone", obj.None);
            Assert.AreEqual(2, obj.RowCount);
            Assert.AreEqual(2, obj.Rows.GetLength(0));
            Assert.AreEqual(4, obj.Rows.GetLength(1));
            Assert.AreEqual("test1", obj.GetRow(0)[0]);
            Assert.AreEqual("mynone", obj.GetColumn("1")[1]);
            Assert.AreEqual(-1, obj.GetColumnIndex("33"));
        }
Exemple #2
0
    public void TestTsvParser()
    {
        var text   = "ab\tcd\t\"efg,\t\"\t56789\t!!!\nhijk\tl\tmn\t43210";
        var parser = new TsvParser(text);
        var col    = parser.ToList();

        Assert.AreEqual(2, col.Count);
        Assert.AreEqual(5, col[0].Count);
        Assert.AreEqual("ab", col[0][0]);
        Assert.AreEqual("cd", col[0][1]);
        Assert.AreEqual("efg,\t", col[0][2]);
        Assert.AreEqual("56789", col[0][3]);
        Assert.AreEqual("!!!", col[0][4]);
        Assert.AreEqual(4, col[1].Count);
        Assert.AreEqual("hijk", col[1][0]);
        Assert.AreEqual("l", col[1][1]);
        Assert.AreEqual("mn", col[1][2]);
        Assert.AreEqual("43210", col[1][3]);

        var models = parser.ConvertTo <JsonModel>(new[] { "A", "B", "C", "Num" }).ToList();

        Assert.AreEqual(2, models.Count);
        Assert.AreEqual("ab", models[0].A);
        Assert.AreEqual("cd", models[0].B);
        Assert.AreEqual("efg,\t", models[0].C);
        Assert.AreEqual(56789, models[0].Num);
        Assert.AreEqual("hijk", models[1].A);
        Assert.AreEqual("l", models[1].B);
        Assert.AreEqual("mn", models[1].C);
        Assert.AreEqual(43210, models[1].Num);

        var jsons = parser.ConvertTo <JsonObjectNode>(new[] { "A", "B", "C", "Num" }).ToList();

        Assert.AreEqual(2, jsons.Count);
        Assert.AreEqual("ab", jsons[0].TryGetStringValue("A"));
        Assert.AreEqual("cd", jsons[0].TryGetStringValue("B"));
        Assert.AreEqual("efg,\t", jsons[0].TryGetStringValue("C"));
        Assert.AreEqual(56789, jsons[0].TryGetInt32Value("Num"));
        Assert.AreEqual("hijk", jsons[1].TryGetStringValue("A"));
        Assert.AreEqual("l", jsons[1].TryGetStringValue("B"));
        Assert.AreEqual("mn", jsons[1].TryGetStringValue("C"));
        Assert.AreEqual(43210, jsons[1].TryGetInt32Value("Num"));

        using var stream = IO.CharsReader.ToStream(text, Encoding.UTF8);
        parser           = new TsvParser(stream);
        col = parser.ToList();

        Assert.AreEqual(2, col.Count);
        Assert.AreEqual(5, col[0].Count);
        Assert.AreEqual("ab", col[0][0]);
        Assert.AreEqual("cd", col[0][1]);
        Assert.AreEqual("efg,\t", col[0][2]);
        Assert.AreEqual("56789", col[0][3]);
        Assert.AreEqual("!!!", col[0][4]);
        Assert.AreEqual(4, col[1].Count);
        Assert.AreEqual("hijk", col[1][0]);
        Assert.AreEqual("l", col[1][1]);
        Assert.AreEqual("mn", col[1][2]);
        Assert.AreEqual("43210", col[1][3]);
    }
Exemple #3
0
        public void Parse_Invalid_Tsv_Input()
        {
            var shortName = "InvalidFile";

            using var validTsvStream = StringToStream("");
            var tsvParser = new TsvParser();

            void Act() => tsvParser.FromStream(shortName, validTsvStream);

            Assert.Throws <TsvParsingException>(Act);
        }
Exemple #4
0
        public void TsvListSecond()
        {
            var parser = new TsvParser(noneIn: "none", noneOut: "mynone");

            string[][] str =
            {
                new[] { "1",     "1",     "3",     "4" },
                new[] { "test1", "test2", "none" },
                new[] { "none",  "test3", "test4", ""  }
            };

            var obj = parser.Parse(str);
        }
Exemple #5
0
        static void ExecuteFlow(AppSettings settings, string connectionString)
        {
            var parser    = new TsvParser();
            var validator = new TsvFileValidator();

            using var fillService = new MySqlDatabaseImportService(connectionString);
            var loader = new TsvLoader(parser, validator, fillService);

            loader.Load(
                settings.FilesToImport ?? throw new AppSettingsMissingException(nameof(settings.FilesToImport)),
                settings.DbSchemaFile ?? throw new AppSettingsMissingException(nameof(settings.DbSchemaFile)),
                settings.DatabaseName ?? throw new AppSettingsMissingException(nameof(settings.DatabaseName))
                );
        }
Exemple #6
0
        public void Parse_Valid_Tsv_Input()
        {
            var shortName = "ValidFile";

            using var validTsvStream = StringToStream("Id\tName\n\n123\t321");
            var expectedHeaders = new [] { "Id", "Name" };
            var expectedRows    = new [] { new [] { "123", "321" } };

            var tsvParser = new TsvParser();
            var file      = tsvParser.FromStream(shortName, validTsvStream);

            Assert.Equal(file.Headers, expectedHeaders);
            Assert.Equal(file.Rows.Length, expectedRows.Length);
            Assert.Equal(file.Rows[0], expectedRows[0]);
        }
Exemple #7
0
        public void FileTest()
        {
            var parser = new TsvParser(noneIn: "\\N");
            var obj    = parser.ParseFile("Assets\\data.tsv");

            obj.AddHeader("TestHeader");
            var val = obj.AddHeader("TestHeader");

            obj.AddRow();
            obj[1, 1] = "TestMessage";

            Assert.AreEqual(221, obj.RowCount);
            Assert.AreEqual(7, obj.ColumnCount);
            Assert.AreEqual(val, false);
            Assert.AreEqual(obj[1, 1], "TestMessage");
        }
Exemple #8
0
        public void SimpleParseSecond()
        {
            var parser = new TsvParser(firstIsHeader: false, noneIn: "none", noneOut: "mynone");
            var text   = "test1\ttest2\test3\nnone\ttest4\ttest5";
            var obj    = parser.Parse(text);

            obj.AddHeader("newHeder1", new[] { "newTest1", "newTest2" });
            obj.AddRow(new [] { "newRowItem1", "newRowItem2" });

            Assert.AreEqual("Item1", obj.Header[1]);
            Assert.AreEqual("newHeder1", obj.Header[3]);
            Assert.AreEqual(3, obj.RowCount);
            Assert.AreEqual(4, obj.ColumnCount);
            Assert.AreEqual("newRowItem2", obj.GetRow(2)[1]);
            Assert.AreEqual("mynone", obj.GetRow(2)[2]);
            Assert.AreEqual("newTest1", obj.GetRow(0)[3]);
        }
Exemple #9
0
        public CityService()
        {
            TsvParser parser = new TsvParser()
            {
                Filepath = "cities_canada-usa.tsv"
            };
            CityStorage cityStorage = new CityStorage
            {
                Parser      = parser,
                WordStorage = new WordTree()
            };

            cityStorage.LoadData();
            _cityStorage = cityStorage;

            _scorer     = new Scorer();
            _serializer = new JsonSerializer();
        }
        private void MenuItemImportTsv_OnClick(object sender, RoutedEventArgs e)
        {
            var dialog = new Microsoft.Win32.OpenFileDialog()
            {
                Title       = "Select TSV-formatted file to load",
                Filter      = "Supported files|*.tsv;*.txt",
                Multiselect = false
            };

            var result = dialog.ShowDialog();

            if (result != null && result.Value)
            {
                using (var stream = new StreamReader(dialog.OpenFile()))
                {
                    SetMap(TsvParser.Parse(stream));
                }
            }
        }
Exemple #11
0
        public void TsvConvenienceSubclass()
        {
            const int NumRecords = 3;
            const int NumValues  = 4;

            var builder = CreateTable(TsvReader.ValueDelimeter, TsvReader.RecordDelimeter, NumRecords, NumValues);
            var parser  = new TsvParser(builder.ToString());

            var numRecords = parser.RecordsLength;

            Assert.AreEqual(NumRecords, numRecords);
            for (var recordNum = 0; recordNum < numRecords; recordNum++)
            {
                var numValues = parser.GetRecordLength(recordNum);
                Assert.AreEqual(NumValues, numValues);
                for (var valueNum = 0; valueNum < numValues; valueNum++)
                {
                    var value = parser.GetValue(recordNum, valueNum);
                    Assert.AreEqual($"Value {recordNum}-{valueNum}", value.ToString());
                }
            }
        }
 public void Setup()
 {
     _parser = new TsvParser();
     _folder = Path.GetFullPath(@"..\..\TestFolder\FilesForTest\");
 }