-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathDataLineTest.cs
More file actions
173 lines (151 loc) · 7.21 KB
/
Copy pathDataLineTest.cs
File metadata and controls
173 lines (151 loc) · 7.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
using System;
using System.Collections.Generic;
using Xunit;
namespace FixedWidthParserWriter.Tests
{
public enum FixedWidthSettingsType
{
Regular = 0,
Dynamic = 1,
}
public class DataLineTest
{
[Theory]
[InlineData(FixedWidthSettingsType.Regular)]
[InlineData(FixedWidthSettingsType.Dynamic)]
public void LineParserTest(FixedWidthSettingsType settings)
{
var dynamicSettings = new Dictionary<string, FixedWidthAttribute>();
var attDescription = new FixedWidthLineFieldAttribute() { StructureTypeId = (int)ConfigType.Alpha, Start = 5, Length = 12 };
dynamicSettings.Add(nameof(InvoiceItem.Description), attDescription);
dynamicSettings.Add(nameof(InvoiceItem.StatusCode), null); // when null sett then attribute is ignored and field skipped
var dataLines = GetDataLines(ConfigType.Alpha);
var provider = new FixedWidthLinesProvider<InvoiceItem>();
bool hasRegularSettings = settings == FixedWidthSettingsType.Regular;
List<InvoiceItem> invoiceItems = hasRegularSettings ? provider.Parse(dataLines) // StructureTypeId argument not explicity set, default = 0 (ConfigType.Alpha)
: provider.Parse(dataLines, dynamicSettings: dynamicSettings);
List<InvoiceItem> expectedInvoiceItems =
[
new() { Number = 1, Description = "Laptop Dell xps13", Quantity = 1, Price = 821.00m, StatusCode = 1, ProductCode = 123},
new() { Number = 2, Description = "Monitor Asus 32''", Quantity = 2, Price = 478.00m, StatusCode = 2, ProductCode = 125}
];
if (!hasRegularSettings) // Dynamic Settings with Custom Description length of 12 characters
{
foreach (var expectedInvoiceItem in expectedInvoiceItems)
{
expectedInvoiceItem.Description = expectedInvoiceItem.Description[..12].Trim();
expectedInvoiceItem.StatusCode = 0;
}
}
for (int i = 0; i < 2; i++)
{
Assert.Equal(expectedInvoiceItems[i].Number, invoiceItems[i].Number);
Assert.Equal(expectedInvoiceItems[i].Description, invoiceItems[i].Description);
Assert.Equal(expectedInvoiceItems[i].Quantity, invoiceItems[i].Quantity);
Assert.Equal(expectedInvoiceItems[i].Price, invoiceItems[i].Price);
Assert.Equal(expectedInvoiceItems[i].Amount, invoiceItems[i].Amount);
Assert.Equal(expectedInvoiceItems[i].StatusCode, invoiceItems[i].StatusCode);
}
}
[Fact]
public void LineWriterTest()
{
var invoiceItems = new List<InvoiceItem>
{
new() { Number = 1, Description = "Laptop Dell xps13", Quantity = 1, Price = 821.00m, StatusCode = 1, ProductCode = 123},
new() { Number = 2, Description = "Monitor Asus 32''", Quantity = 2, Price = 478.00m, StatusCode = 2, ProductCode = 125}
};
List<string> resultLinesAlpha = new FixedWidthLinesProvider<InvoiceItem>().Write(invoiceItems, (int)ConfigType.Alpha);
List<string> resultLinesBeta = new FixedWidthLinesProvider<InvoiceItem>().Write(invoiceItems, (int)ConfigType.Beta);
string resultAlpha = string.Empty;
foreach (var line in resultLinesAlpha)
{
resultAlpha += line + Environment.NewLine;
}
string resultBeta = string.Empty;
foreach (var line in resultLinesBeta)
{
resultBeta += line + Environment.NewLine;
}
List<string> expectedLinesAlpha = GetDataLines(ConfigType.Alpha);
string expectedAlpha = string.Empty;
foreach (var line in expectedLinesAlpha)
{
expectedAlpha += line + Environment.NewLine;
};
List<string> expectedLinesBeta = GetDataLines(ConfigType.Beta);
string expectedBeta = string.Empty;
foreach (var line in expectedLinesBeta)
{
expectedBeta += line + Environment.NewLine;
};
Assert.Equal(expectedAlpha, resultAlpha);
Assert.Equal(expectedBeta, resultBeta);
}
[Fact]
public void LineParserNullableTest()
{
List<string> fileLines = GetDataLinesNullable();
var fields = new FixedWidthLinesProvider<NullableModel>().Parse(fileLines);
var expectedFields = new List<NullableModel>
{
new()
{
Bool = true,
Char = (char)"char"[0],
DateTime = new DateTime(2019, 1, 1),
Int32 = 1000,
Int64 = 1000,
Decimal = (decimal)1000.1,
Double = (double)1000.1,
Single = (Single)1000.1
},
new()
};
for (int i = 0; i < expectedFields.Count; i++)
{
Assert.Equal(expectedFields[i].Bool, fields[i].Bool);
Assert.Equal(expectedFields[i].Char, fields[i].Char);
Assert.Equal(expectedFields[i].Int32, fields[i].Int32);
Assert.Equal(expectedFields[i].Int64, fields[i].Int64);
Assert.Equal(expectedFields[i].Decimal, fields[i].Decimal);
Assert.Equal(expectedFields[i].Single, fields[i].Single);
Assert.Equal(expectedFields[i].Double, fields[i].Double);
Assert.Equal(expectedFields[i].DateTime, fields[i].DateTime);
}
}
public static List<string> GetDataLines(ConfigType formatType)
{
//var header ="No | Description | Qty | Price | Amount |";
List<string> dataLines = null;
switch (formatType)
{
case ConfigType.Alpha:
dataLines =
[
" 1.Laptop Dell xps13 1 821.00 821.001 123",
" 2.Monitor Asus 32'' 2 478.00 956.002 125"
];
break;
case ConfigType.Beta:
dataLines =
[
"0001Laptop Dell xps13 0000010000000821.000000000821.00100123",
"0002Monitor Asus 32'' 0000020000000478.000000000956.00200125"
];
break;
}
return dataLines;
}
public static List<string> GetDataLinesNullable()
{
List<string> dataLines =
[
"char 1000 1000 1000.10 1000.10 1000.10 2019-01-011",
" ",
"ooooooooo00000000001111111111111111111100000.0000000000000000000000.00000000.000000-00-003",
];
return dataLines;
}
}
}