Skip to content

Commit 81c719e

Browse files
authored
Unittests and code cleanup (#293)
2 parents ef1a0f5 + 5b28e12 commit 81c719e

File tree

10 files changed

+226
-23
lines changed

10 files changed

+226
-23
lines changed

backend/src/BIE.DataPipeline/BIE.DataPipeline.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343

4444
<ItemGroup>
4545
<None Remove="yaml\unitTest.yaml" />
46+
<None Remove="yaml\unitTestShort.yaml" />
4647
</ItemGroup>
4748

4849
</Project>

backend/src/BIE.DataPipeline/Import/CsvImporter.cs

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
using System.Linq;
44
using System.Net;
55
using System.Reflection.PortableExecutable;
6+
using System.Runtime.CompilerServices;
67
using System.Text;
78
using System.Text.RegularExpressions;
89
using System.Threading.Tasks;
910
using BIE.DataPipeline;
1011
using Microsoft.VisualBasic.FileIO;
1112

13+
[assembly: InternalsVisibleTo("BIE.Tests")]
1214
namespace BIE.DataPipeline.Import
1315
{
1416
internal class CsvImporter : IImporter
@@ -58,14 +60,22 @@ public CsvImporter(DataSourceDescription? dataSourceDescription)
5860
}
5961
}
6062

61-
//tablename = name
63+
/// <summary>
64+
/// Returns the SQL table name for the data set given by the yaml file.
65+
/// </summary>
66+
/// <returns>The SQL table name.</returns>
6267
public string GetTableName()
6368
{
6469
return this.dataSourceDescription.table_name;
6570
}
6671

72+
/// <summary>
73+
/// Creates a comma seperated sting with all column names for the SQL table.
74+
/// </summary>
75+
/// <returns>The SQL column name string.</returns>
6776
public string GetHeaderString()
6877
{
78+
//if the string is not empty the result can be returned instantly
6979
if (!headerString.Equals(""))
7080
{
7181
return headerString;
@@ -82,6 +92,11 @@ public string GetHeaderString()
8292
return headerString;
8393
}
8494

95+
/// <summary>
96+
/// Reads a line from the csv file.
97+
/// </summary>
98+
/// <param name="nextLine">An output parameter that returns a line or an empty string.</param>
99+
/// <returns>A boolean indicating if the end of the file has been reached.</returns>
85100
public bool ReadLine(out string nextLine)
86101
{
87102
string[]? line;
@@ -90,13 +105,6 @@ public bool ReadLine(out string nextLine)
90105

91106
while (builder.Length == 0)
92107
{
93-
// Console.Write($"trying to write");
94-
95-
// if (parser.EndOfData)
96-
// {
97-
// return false;
98-
// }
99-
100108
line = parser.ReadFields();
101109
if (line == null)
102110
{
@@ -106,7 +114,6 @@ public bool ReadLine(out string nextLine)
106114

107115
if (line.Length == 0)
108116
{
109-
//TODO what to do with empty lines
110117
Console.WriteLine("Line is empty");
111118
//Read next line
112119
nextLine = "";
@@ -116,12 +123,21 @@ public bool ReadLine(out string nextLine)
116123

117124
foreach (var (i, yamlIndex)in columnIndexes)
118125
{
126+
//checks if the line has not enougth content for the expected yaml columns.
127+
if(i >= line.Length)
128+
{
129+
Console.WriteLine("Line does not match the number of expected columns");
130+
//Read next line
131+
builder.Clear();
132+
break;
133+
}
134+
119135
//check if the value can be empty
120136
if (dataSourceDescription.table_cols[yamlIndex].is_not_nullable && line[i] == "")
121137
{
122138
Console.WriteLine("Line does not match not null criteria");
123139
//Read next line
124-
nextLine = "";
140+
builder.Clear();
125141
break;
126142
}
127143

@@ -130,20 +146,16 @@ public bool ReadLine(out string nextLine)
130146

131147
if (columnTypes[i] == typeof(string))
132148
{
133-
// nextLine += $"'{line[i]}',";
134149
builder.Append($"'{line[i]}',");
135150
continue;
136151
}
137152

138-
// nextLine += string.Format("{0},", Convert.ChangeType(line[i], columnTypes[i]));
139-
// nextLine += $"{line[i]},";
140153
builder.Append($"{line[i]},");
141154
}
142155
}
143156

144157
builder.Length--; // this removes the last comma
145158

146-
// nextLine = RemoveLastComma(nextLine);
147159
nextLine = builder.ToString();
148160

149161
return true;

backend/src/BIE.DataPipeline/Import/YamlImporter.cs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using System.ComponentModel;
2+
using System.Runtime.CompilerServices;
23
using System.Text;
34
using YamlDotNet.Serialization;
45

56
// ReSharper disable InconsistentNaming
67

8+
[assembly: InternalsVisibleTo("BIE.Tests")]
79
namespace BIE.DataPipeline.Import
810
{
911
public static class YamlImporter
@@ -73,6 +75,22 @@ public char delimiter
7375
/// </summary>
7476
public List<DataSourceColumn> table_cols { get; set; }
7577

78+
public override bool Equals(object? obj)
79+
{
80+
if (obj == null) return false;
81+
82+
DataSourceDescription des = obj as DataSourceDescription;
83+
84+
if (!this.source.Equals(des.source)) return false;
85+
if (!this.options.Equals(des.options)) return false;
86+
if (!this.table_name.Equals(des.table_name)) return false;
87+
for (int i = 0; i < this.table_cols.Count; i++)
88+
{
89+
if (!this.table_cols[i].Equals(des.table_cols[i])) return false;
90+
}
91+
return true;
92+
}
93+
7694
public class DataSourceLocation
7795
{
7896
/// <summary>
@@ -89,6 +107,18 @@ public class DataSourceLocation
89107
/// the format of the data
90108
/// </summary>
91109
public string data_format { get; set; }
110+
111+
public override bool Equals(object? obj)
112+
{
113+
if (obj == null) return false;
114+
115+
DataSourceLocation des = obj as DataSourceLocation;
116+
117+
if (this.type != des.type) return false;
118+
if (this.location != des.location) return false;
119+
if (this.data_format != des.data_format) return false;
120+
return true;
121+
}
92122
}
93123

94124
public class DataSourceOptions
@@ -116,6 +146,18 @@ public InsertBehaviour if_table_exists
116146
get => mIf_table_exists;
117147
set => mIf_table_exists = value;
118148
}
149+
150+
public override bool Equals(object? obj)
151+
{
152+
if (obj == null) return false;
153+
154+
DataSourceOptions des = obj as DataSourceOptions;
155+
156+
if (this.skip_lines != des.skip_lines) return false;
157+
if (this.discard_null_rows != des.discard_null_rows) return false;
158+
if (this.if_table_exists != des.if_table_exists) return false;
159+
return true;
160+
}
119161
}
120162

121163
public class DataSourceColumn
@@ -145,6 +187,20 @@ public string type
145187
/// </summary>
146188
[DefaultValue(false)]
147189
public bool is_not_nullable { get; set; }
190+
191+
192+
public override bool Equals(object? obj)
193+
{
194+
if (obj == null) return false;
195+
196+
DataSourceColumn des = obj as DataSourceColumn;
197+
198+
if (this.name != des.name) return false;
199+
if (this.name_in_table != des.name_in_table) return false;
200+
if (this.type != des.type) return false;
201+
if (this.is_not_nullable != des.is_not_nullable) return false;
202+
return true;
203+
}
148204
}
149205
}
150206

backend/src/BIE.DataPipeline/entrypoint.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ echo "insert data sets"
66

77
directory="./yaml"
88
yamlfiles=$(find "$directory" -type f -name "*.yaml")
9-
excludeFiles=("unitTest.yaml" "example.yaml")
9+
excludeFiles=("unitTest.yaml" "unitTestShort.yaml")
1010

1111
# Check if yaml_files is not empty
1212
if [ -n "$yamlfiles" ]; then

backend/src/BIE.DataPipeline/yaml/unitTest.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
# describe the source
22
source:
33
# link | filepath
4-
type: URL
5-
location: https://data.bundesnetzagentur.de/Bundesnetzagentur/SharedDocs/Downloads/DE/Sachgebiete/Energie/Unternehmen_Institutionen/E_Mobilitaet/Ladesaeulenregister.csv
4+
type: filepath
5+
location: .\..\..\..\testData\unitTest.csv
66
data_format: CSV
77
options:
88
# skip lines at the beginning
9-
skip_lines: 10
9+
skip_lines: 7
1010
# discard any rows that have null values
1111
discard_null_rows: false
1212
# how to deal with existing table. Options: ignore, replace, skip (default).
1313
if_table_exists: replace
14-
table_name: EV_charging_stations
14+
table_name: unitTestTable
1515
delimiter: ";"
1616

1717
table_cols:
18-
- name: testDefault
18+
- name: header1
1919
name_in_table: testDefault
2020
is_not_nullable: true
21-
- name: testVarChar
21+
- name: header2
2222
name_in_table: testVarChar
2323
type: VARCHAR
2424
- name: testBool
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# describe the source
2+
source:
3+
# link | filepath
4+
type: filepath
5+
location: .\..\..\..\testData\unitTest.csv
6+
data_format: CSV
7+
options:
8+
# skip lines at the beginning
9+
skip_lines: 7
10+
# discard any rows that have null values
11+
discard_null_rows: false
12+
# how to deal with existing table. Options: ignore, replace, skip (default).
13+
if_table_exists: replace
14+
table_name: unitTestTable
15+
delimiter: ";"
16+
17+
table_cols:
18+
- name: colName
19+
name_in_table: colTableName
20+
type: VARCHAR
21+
22+
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using BIE.DataPipeline.Import;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace BIE.Tests
9+
{
10+
internal class CsvImporterTest
11+
{
12+
private CsvImporter csvImporter;
13+
14+
[SetUp]
15+
public void SetUpCsvImporter()
16+
{
17+
csvImporter = new CsvImporter(YamlImporter.GetSourceDescription(@".\yaml\unitTest.yaml"));
18+
}
19+
20+
[Test]
21+
public void TestGetTableName()
22+
{
23+
Assert.That(csvImporter.GetTableName(), Is.EqualTo("unitTestTable"));
24+
}
25+
26+
[Test]
27+
public void TestGetHeaderString()
28+
{
29+
string expected = "testDefault,testVarChar,checkValue,testBoolean,testInt,testInteger,testFloat,testDouble,testDecimal";
30+
string actual = csvImporter.GetHeaderString();
31+
Assert.That(actual, Is.EqualTo(expected));
32+
}
33+
34+
[Test]
35+
public void TestReadLine()
36+
{
37+
string[] expected =
38+
{
39+
"'name1','1',1.5,true,,,,,",
40+
"'name3','3',3.5,true,,,,,",
41+
"'name4','4',4.5,false,,,,,",
42+
"'name5','5',5.5,false,,,,,",
43+
string.Empty,
44+
};
45+
46+
for (int i = 0; i < 5; i++) {
47+
string actual = "";
48+
csvImporter.ReadLine(out actual);
49+
Assert.That(actual, Is.EqualTo(expected[i]));
50+
}
51+
}
52+
}
53+
}

backend/src/BIE.Tests/ImporterHelperTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ public void TestRemoveLastBrackets()
6565
public void TestReadYamlHeader()
6666
{
6767
string[] expected = {
68-
"testDefault",
69-
"testVarChar",
68+
"header1",
69+
"header2",
7070
"testBool",
7171
"testBoolean",
7272
"testInt",
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using BIE.DataPipeline.Import;
2+
using MySqlX.XDevAPI.Common;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
using static BIE.DataPipeline.Import.DataSourceDescription;
9+
10+
namespace BIE.Tests
11+
{
12+
internal class YamlImporterTest
13+
{
14+
[Test]
15+
public void TestGetSourceDescription()
16+
{
17+
DataSourceDescription expected = new DataSourceDescription();
18+
DataSourceLocation expectedLocation = new DataSourceLocation();
19+
expectedLocation.type = "filepath";
20+
expectedLocation.data_format = "CSV";
21+
expectedLocation.location = ".\\..\\..\\..\\testData\\unitTest.csv";
22+
expected.source = expectedLocation;
23+
expected.delimiter = ';';
24+
DataSourceOptions expectedOptions = new DataSourceOptions();
25+
expectedOptions.discard_null_rows = false;
26+
expectedOptions.skip_lines = 7;
27+
expectedOptions.if_table_exists = InsertBehaviour.replace;
28+
expected.options = expectedOptions;
29+
DataSourceColumn expectedColumn = new DataSourceColumn();
30+
expectedColumn.is_not_nullable = false;
31+
expectedColumn.name = "colName";
32+
expectedColumn.name_in_table = "colTableName";
33+
expectedColumn.type = "VARCHAR";
34+
expected.table_cols = new List<DataSourceColumn>
35+
{
36+
expectedColumn,
37+
};
38+
expected.table_name = "unitTestTable";
39+
40+
DataSourceDescription actual = YamlImporter.GetSourceDescription(@".\yaml\unitTestShort.yaml");
41+
Assert.True(actual.Equals(expected));
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)