1
+ using System ;
2
+ using System . Collections . Generic ;
3
+ using System . Collections . ObjectModel ;
4
+ using Castle . Core . Logging ;
5
+ using Microsoft . Extensions . Logging ;
6
+ using Moq ;
7
+ using NUnit . Framework ;
8
+ using NUnit . Framework . Constraints ;
9
+ using SqlCollaborative . Azure . DataPipelineTools . Common ;
10
+ using SqlCollaborative . Azure . DataPipelineTools . DataLake ;
11
+
12
+ namespace DataPipelineTools . Tests . Common . FilterFactoryTests
13
+ {
14
+ [ TestFixture ]
15
+ public class CreateTests : TestBase < FilterFactory < TestPoco > >
16
+ {
17
+ [ SetUp ]
18
+ public void Setup ( )
19
+ {
20
+ MockLogger . Reset ( ) ;
21
+ SetupConsoleLogging ( ) ;
22
+ }
23
+
24
+ #region Given_ColumNameIsNotValidProperty
25
+ static string [ ] InvalidColumnNames = { "Some missing property" , null , "" } ;
26
+
27
+ [ TestCaseSource ( nameof ( InvalidColumnNames ) ) ]
28
+ public void Given_ColumNameIsNotValidProperty_Should_ReturnFilterWithIsValidEqualsFalse ( string propertyName )
29
+ {
30
+ var result = FilterFactory < TestPoco > . Create ( propertyName , string . Empty , MockLogger . Object ) ;
31
+
32
+ Assert . That ( result . IsValid , Is . EqualTo ( false ) ) ;
33
+ }
34
+
35
+ [ TestCaseSource ( nameof ( InvalidColumnNames ) ) ]
36
+ public void Given_ColumNameIsNotValidProperty_Should_ReturnFilterWithErrorMessageValue ( string propertyName )
37
+ {
38
+ var result = FilterFactory < TestPoco > . Create ( propertyName , string . Empty , MockLogger . Object ) ;
39
+
40
+ Assert . That ( result . ErrorMessage , Is . Not . Null ) ;
41
+ }
42
+
43
+ [ TestCaseSource ( nameof ( InvalidColumnNames ) ) ]
44
+ public void Given_ColumNameIsNotValidProperty_Should_LogWarningOnce ( string propertyName )
45
+ {
46
+ var result = FilterFactory < TestPoco > . Create ( propertyName , string . Empty , MockLogger . Object ) ;
47
+
48
+ MockLogger . VerifyLogging ( LogLevel . Warning , Times . Once ( ) ) ;
49
+ }
50
+ #endregion Given_ColumNameIsNotValidProperty
51
+
52
+
53
+
54
+ #region Given_OperatorIsNotValid
55
+ static string [ ] InvalidOperators = { "eq:" , "=5" , null , "" } ;
56
+
57
+ [ TestCaseSource ( nameof ( InvalidOperators ) ) ]
58
+ public void Given_OperatorIsNotValid_Should_ReturnFilterWithIsValidEqualsFalse ( string filter )
59
+ {
60
+ var result = FilterFactory < TestPoco > . Create ( nameof ( TestPoco . StringProp ) , filter , MockLogger . Object ) ;
61
+
62
+ Assert . That ( result . IsValid , Is . EqualTo ( false ) ) ;
63
+ }
64
+
65
+ [ TestCaseSource ( nameof ( InvalidOperators ) ) ]
66
+ public void Given_OperatorIsNotValid_Should_ReturnFilterWithErrorMessageValue ( string filter )
67
+ {
68
+ var result = FilterFactory < TestPoco > . Create ( nameof ( TestPoco . StringProp ) , filter , MockLogger . Object ) ;
69
+
70
+ Assert . That ( result . ErrorMessage , Is . Not . Null ) ;
71
+ }
72
+
73
+ [ TestCaseSource ( nameof ( InvalidOperators ) ) ]
74
+ public void Given_OperatorIsNotValid_Should_LogWarningOnce ( string filter )
75
+ {
76
+ var result = FilterFactory < TestPoco > . Create ( nameof ( TestPoco . StringProp ) , filter , MockLogger . Object ) ;
77
+
78
+ MockLogger . VerifyLogging ( LogLevel . Warning , Times . Once ( ) ) ;
79
+ }
80
+ #endregion Given_OperatorIsNotValid
81
+
82
+
83
+
84
+ #region Given_ValueDoesNotCastToNamedColumnType
85
+ static readonly string [ ] ValidNonStringColumnTypes =
86
+ {
87
+ nameof ( TestPoco . BoolProp ) ,
88
+ nameof ( TestPoco . Int16Prop ) ,
89
+ nameof ( TestPoco . IntProp ) ,
90
+ nameof ( TestPoco . Int64Prop ) ,
91
+ nameof ( TestPoco . DoubleProp ) ,
92
+ nameof ( TestPoco . DecimalProp ) ,
93
+ nameof ( TestPoco . DateTimeProp )
94
+ } ;
95
+ private const string ValueDoesNotCastToNamedColumnTypeValue = "eq:abc" ;
96
+
97
+ [ TestCaseSource ( nameof ( ValidNonStringColumnTypes ) ) ]
98
+ public void Given_ValueDoesNotCastToNamedColumnType_Should_ReturnFilterWithIsValidEqualsFalse ( string propertyName )
99
+ {
100
+ var result = FilterFactory < TestPoco > . Create ( propertyName , ValueDoesNotCastToNamedColumnTypeValue , MockLogger . Object ) ;
101
+
102
+ Assert . That ( result . IsValid , Is . EqualTo ( false ) ) ;
103
+ }
104
+
105
+ [ TestCaseSource ( nameof ( InvalidOperators ) ) ]
106
+ public void Given_ValueDoesNotCastToNamedColumnType_Should_ReturnFilterWithErrorMessageValue ( string propertyName )
107
+ {
108
+ var result = FilterFactory < TestPoco > . Create ( propertyName , ValueDoesNotCastToNamedColumnTypeValue , MockLogger . Object ) ;
109
+
110
+ Assert . That ( result . ErrorMessage , Is . Not . Null ) ;
111
+ }
112
+
113
+ [ TestCaseSource ( nameof ( InvalidOperators ) ) ]
114
+ public void Given_ValueDoesNotCastToNamedColumnType_Should_LogWarningOnce ( string propertyName )
115
+ {
116
+ var result = FilterFactory < TestPoco > . Create ( propertyName , ValueDoesNotCastToNamedColumnTypeValue , MockLogger . Object ) ;
117
+
118
+ MockLogger . VerifyLogging ( LogLevel . Warning , Times . Once ( ) ) ;
119
+ }
120
+ #endregion Given_ValueDoesNotCastToNamedColumnType
121
+
122
+
123
+ #region Given_ValidColumnNameAndFilter
124
+ private static readonly Dictionary < string , string > ValidColumnNamesAndValues = new Dictionary < string , string > {
125
+ { nameof ( TestPoco . StringProp ) , "hello" } ,
126
+ { nameof ( TestPoco . BoolProp ) , "true" } ,
127
+ { nameof ( TestPoco . Int16Prop ) , "42" } ,
128
+ { nameof ( TestPoco . IntProp ) , "42" } ,
129
+ { nameof ( TestPoco . Int64Prop ) , "42" } ,
130
+ { nameof ( TestPoco . DoubleProp ) , "42.1" } ,
131
+ { nameof ( TestPoco . DecimalProp ) , "42.1" } ,
132
+ { nameof ( TestPoco . DateTimeProp ) , "2021-01-01T12:00:00" }
133
+ } ;
134
+ private static readonly string [ ] SimpleFilterTypes = { "eq" , "ne" , "lt" , "gt" , "le" , "ge" } ;
135
+
136
+ [ Combinatorial ]
137
+ public void Given_ValidColumnNameAndFilter_Should_ReturnFilterWithIsValidEqualsTrue (
138
+ [ ValueSource ( nameof ( ValidColumnNamesAndValues ) ) ] KeyValuePair < string , string > propertyName ,
139
+ [ ValueSource ( nameof ( SimpleFilterTypes ) ) ] string filter
140
+ )
141
+ {
142
+ var result = FilterFactory < TestPoco > . Create ( propertyName . Key , $ "{ filter } :{ propertyName . Value } ", MockLogger . Object ) ;
143
+
144
+ Assert . That ( result . IsValid , Is . EqualTo ( true ) ) ;
145
+ }
146
+
147
+ [ Combinatorial ]
148
+ public void Given_ValidColumnNameAndFilter_Should_ReturnFilterErrorMessageIsNull (
149
+ [ ValueSource ( nameof ( ValidColumnNamesAndValues ) ) ] KeyValuePair < string , string > propertyName ,
150
+ [ ValueSource ( nameof ( SimpleFilterTypes ) ) ] string filter
151
+ )
152
+ {
153
+ var result = FilterFactory < TestPoco > . Create ( propertyName . Key , $ "{ filter } :{ propertyName . Value } ", MockLogger . Object ) ;
154
+
155
+ Assert . That ( result . ErrorMessage , Is . Null ) ;
156
+ }
157
+
158
+ [ Combinatorial ]
159
+ public void Given_ValidColumnNameAndFilter_Should_LogZeroWarnings (
160
+ [ ValueSource ( nameof ( ValidColumnNamesAndValues ) ) ] KeyValuePair < string , string > propertyName ,
161
+ [ ValueSource ( nameof ( SimpleFilterTypes ) ) ] string filter
162
+ )
163
+ {
164
+ var result = FilterFactory < TestPoco > . Create ( propertyName . Key , $ "{ filter } :{ propertyName . Value } ", MockLogger . Object ) ;
165
+
166
+ MockLogger . VerifyLogging ( LogLevel . Warning , Times . Never ( ) ) ;
167
+ }
168
+ #endregion Given_ValidColumnNameAndFilter
169
+
170
+
171
+ #region Given_LikeFilter
172
+ [ Test ]
173
+ public void Given_LikeFilterWithStringColumn_Should_ReturnFilterWithIsValidEqualsTrue ( )
174
+ {
175
+ var result = FilterFactory < TestPoco > . Create ( nameof ( TestPoco . StringProp ) , $ "like:hello*", MockLogger . Object ) ;
176
+
177
+ Assert . That ( result . IsValid , Is . EqualTo ( true ) ) ;
178
+ }
179
+
180
+ [ Test ]
181
+ public void Given_LikeFilterWithStringColumn_Should_ReturnFilterErrorMessageIsNull ( )
182
+ {
183
+ var result = FilterFactory < TestPoco > . Create ( nameof ( TestPoco . StringProp ) , $ "like:hello*", MockLogger . Object ) ;
184
+
185
+ Assert . That ( result . ErrorMessage , Is . Null ) ;
186
+ }
187
+
188
+ [ Test ]
189
+ public void Given_LikeFilterWithStringColumn_Should_LogZeroWarnings ( )
190
+ {
191
+ var result = FilterFactory < TestPoco > . Create ( nameof ( TestPoco . StringProp ) , $ "like:hello*", MockLogger . Object ) ;
192
+
193
+ MockLogger . VerifyLogging ( LogLevel . Warning , Times . Never ( ) ) ;
194
+ }
195
+
196
+ [ TestCaseSource ( nameof ( ValidNonStringColumnTypes ) ) ]
197
+ public void Given_LikeFilterWithNonStringColumn_Should_ReturnFilterWithIsValidEqualsFalse ( string propertyName )
198
+ {
199
+ var result = FilterFactory < TestPoco > . Create ( propertyName , $ "like:hello*", MockLogger . Object ) ;
200
+
201
+ Assert . That ( result . IsValid , Is . EqualTo ( false ) ) ;
202
+ }
203
+
204
+ [ TestCaseSource ( nameof ( ValidNonStringColumnTypes ) ) ]
205
+ public void Given_LikeFilterWithNonStringColumn_Should_ReturnFilterWithErrorMessageValue ( string propertyName )
206
+ {
207
+ var result = FilterFactory < TestPoco > . Create ( propertyName , $ "like:hello*", MockLogger . Object ) ;
208
+
209
+ Assert . That ( result . ErrorMessage , Is . Not . Null ) ;
210
+ }
211
+
212
+ [ TestCaseSource ( nameof ( ValidNonStringColumnTypes ) ) ]
213
+ public void Given_LikeFilterWithNonStringColumn_Should_LogWarningOnce ( string propertyName )
214
+ {
215
+ var result = FilterFactory < TestPoco > . Create ( propertyName , $ "like:hello*", MockLogger . Object ) ;
216
+
217
+ MockLogger . VerifyLogging ( LogLevel . Warning , Times . Once ( ) ) ;
218
+ }
219
+ #endregion Given_LikeFilter
220
+ }
221
+ }
0 commit comments