@@ -23,8 +23,8 @@ public CollectionSerializationTests(TemporaryDatabaseFixture tempDatabase)
23
23
}
24
24
25
25
[ Theory ]
26
- [ InlineData ( 2 , 4 , 8 , 16 , 32 , 64 ) ]
27
26
[ InlineData ]
27
+ [ InlineData ( 2 , 4 , 8 , 16 , 32 , 64 ) ]
28
28
public void Int_array_round_trips ( params int [ ] expected )
29
29
{
30
30
var collection = TempDatabase . CreateTemporaryCollection < IntArrayEntity > ( nameof ( Int_array_round_trips ) + expected . Length ) ;
@@ -44,24 +44,61 @@ public void Int_array_round_trips(params int[] expected)
44
44
}
45
45
46
46
[ Fact ]
47
- public void Missing_int_array_defaults_to_null ( )
47
+ public void Missing_int_array_throws ( )
48
48
{
49
49
var collection = SetupIdOnlyCollection < IntArrayEntity > ( ) ;
50
50
using var db = SingleEntityDbContext . Create ( collection ) ;
51
51
52
+ Assert . Throws < KeyNotFoundException > ( ( ) => db . Entitites . FirstOrDefault ( ) ) ;
53
+ }
54
+
55
+ class IntArrayEntity : BaseIdEntity
56
+ {
57
+ public int [ ] anIntArray { get ; set ; }
58
+ }
59
+
60
+
61
+ [ Theory ]
62
+ [ InlineData ]
63
+ [ InlineData ( 2 , 4 , 8 , 16 , 32 , 64 ) ]
64
+ public void Nullable_int_array_round_trips ( params int [ ] expected )
65
+ {
66
+ var collection = TempDatabase . CreateTemporaryCollection < NullableIntArrayEntity > ( nameof ( Nullable_int_array_round_trips ) + expected . Length ) ;
67
+
68
+ {
69
+ using var db = SingleEntityDbContext . Create ( collection ) ;
70
+ db . Entitites . Add ( new NullableIntArrayEntity { anIntArray = expected } ) ;
71
+ db . SaveChanges ( ) ;
72
+ }
73
+
74
+ {
75
+ using var db = SingleEntityDbContext . Create ( collection ) ;
76
+ var result = db . Entitites . FirstOrDefault ( ) ;
77
+ Assert . NotNull ( result ) ;
78
+ Assert . Equal ( expected , result . anIntArray ) ;
79
+ }
80
+ }
81
+
82
+ [ Fact ]
83
+ public void Missing_nullable_int_array_defaults_to_null ( )
84
+ {
85
+ var collection = SetupIdOnlyCollection < NullableIntArrayEntity > ( ) ;
86
+ using var db = SingleEntityDbContext . Create ( collection ) ;
87
+
52
88
var result = db . Entitites . FirstOrDefault ( ) ;
53
89
Assert . NotNull ( result ) ;
54
90
Assert . Null ( result . anIntArray ) ;
55
91
}
56
92
57
- class IntArrayEntity : BaseIdEntity
93
+ class NullableIntArrayEntity : BaseIdEntity
58
94
{
59
- public int [ ] anIntArray { get ; set ; }
95
+ public int [ ] ? anIntArray { get ; set ; }
60
96
}
61
97
98
+
62
99
[ Theory ]
63
- [ InlineData ( "abc" , "def" , "ghi" , "and the rest" ) ]
64
100
[ InlineData ]
101
+ [ InlineData ( "abc" , "def" , "ghi" , "and the rest" ) ]
65
102
public void String_array_round_trips ( params string [ ] expected )
66
103
{
67
104
var collection =
@@ -82,34 +119,129 @@ public void String_array_round_trips(params string[] expected)
82
119
}
83
120
84
121
[ Fact ]
85
- public void Missing_string_array_defaults_to_null ( )
122
+ public void Missing_string_array_throws ( )
86
123
{
87
124
var collection = SetupIdOnlyCollection < StringArrayEntity > ( ) ;
88
125
using var db = SingleEntityDbContext . Create ( collection ) ;
89
126
127
+ Assert . Throws < KeyNotFoundException > ( ( ) => db . Entitites . FirstOrDefault ( ) ) ;
128
+ }
129
+
130
+ class StringArrayEntity : BaseIdEntity
131
+ {
132
+ public string [ ] aStringArray { get ; set ; }
133
+ }
134
+
135
+
136
+ [ Theory ]
137
+ [ InlineData ]
138
+ [ InlineData ( "abc" , "def" , "ghi" , "and the rest" ) ]
139
+ public void Nullable_string_array_round_trips ( params string [ ] expected )
140
+ {
141
+ var collection =
142
+ TempDatabase . CreateTemporaryCollection < NullableStringArrayEntity > ( nameof ( Nullable_string_array_round_trips ) + expected . Length ) ;
143
+
144
+ {
145
+ using var db = SingleEntityDbContext . Create ( collection ) ;
146
+ db . Entitites . Add ( new NullableStringArrayEntity { aStringArray = expected } ) ;
147
+ db . SaveChanges ( ) ;
148
+ }
149
+
150
+ {
151
+ using var db = SingleEntityDbContext . Create ( collection ) ;
152
+ var result = db . Entitites . FirstOrDefault ( ) ;
153
+ Assert . NotNull ( result ) ;
154
+ Assert . Equal ( expected , result . aStringArray ) ;
155
+ }
156
+ }
157
+
158
+ [ Fact ]
159
+ public void Missing_nullable_string_array_defaults_to_null ( )
160
+ {
161
+ var collection = SetupIdOnlyCollection < NullableStringArrayEntity > ( ) ;
162
+ using var db = SingleEntityDbContext . Create ( collection ) ;
163
+
90
164
var result = db . Entitites . FirstOrDefault ( ) ;
91
165
Assert . NotNull ( result ) ;
92
166
Assert . Null ( result . aStringArray ) ;
93
167
}
94
168
95
- class StringArrayEntity : BaseIdEntity
169
+ class NullableStringArrayEntity : BaseIdEntity
96
170
{
97
- public string [ ] aStringArray { get ; set ; }
171
+ public string [ ] ? aStringArray { get ; set ; }
172
+ }
173
+
174
+ [ Theory ]
175
+ [ InlineData ]
176
+ [ InlineData ( 1 , 2 , 3 , 4 ) ]
177
+ public void List_round_trips ( params int [ ] expected )
178
+ {
179
+ var collection =
180
+ TempDatabase . CreateTemporaryCollection < ListEntity > ( nameof ( List_round_trips ) + expected . Length ) ;
181
+
182
+ {
183
+ using var db = SingleEntityDbContext . Create ( collection ) ;
184
+ db . Entitites . Add ( new ListEntity { aList = new List < int > ( expected ) } ) ;
185
+ db . SaveChanges ( ) ;
186
+ }
187
+
188
+ {
189
+ using var db = SingleEntityDbContext . Create ( collection ) ;
190
+ var result = db . Entitites . FirstOrDefault ( ) ;
191
+ Assert . NotNull ( result ) ;
192
+ Assert . Equivalent ( expected , result . aList ) ;
193
+ }
98
194
}
99
195
100
196
[ Fact ]
101
- public void Missing_list_defaults_to_null ( )
197
+ public void Missing_list_throws ( )
102
198
{
103
199
var collection = SetupIdOnlyCollection < ListEntity > ( ) ;
104
200
using var db = SingleEntityDbContext . Create ( collection ) ;
105
201
202
+ Assert . Throws < KeyNotFoundException > ( ( ) => db . Entitites . FirstOrDefault ( ) ) ;
203
+ }
204
+
205
+ class ListEntity : BaseIdEntity
206
+ {
207
+ public List < int > aList { get ; set ; }
208
+ }
209
+
210
+ [ Theory ]
211
+ [ InlineData ]
212
+ [ InlineData ( 1 , 2 , 3 , 4 ) ]
213
+ public void Nullable_list_round_trips ( params int [ ] expected )
214
+ {
215
+ var collection =
216
+ TempDatabase . CreateTemporaryCollection < NullableListEntity > ( nameof ( Nullable_list_round_trips ) + expected . Length ) ;
217
+
218
+ {
219
+ using var db = SingleEntityDbContext . Create ( collection ) ;
220
+ db . Entitites . Add ( new NullableListEntity { aList = new List < int > ( expected ) } ) ;
221
+ db . SaveChanges ( ) ;
222
+ }
223
+
224
+ {
225
+ using var db = SingleEntityDbContext . Create ( collection ) ;
226
+ var result = db . Entitites . FirstOrDefault ( ) ;
227
+ Assert . NotNull ( result ) ;
228
+ Assert . Equivalent ( expected , result . aList ) ;
229
+ }
230
+ }
231
+
232
+ [ Fact ]
233
+ public void Missing_nullable_list_default_to_null ( )
234
+ {
235
+ var collection = SetupIdOnlyCollection < NullableListEntity > ( ) ;
236
+ using var db = SingleEntityDbContext . Create ( collection ) ;
237
+
106
238
var result = db . Entitites . FirstOrDefault ( ) ;
107
239
Assert . NotNull ( result ) ;
108
240
Assert . Null ( result . aList ) ;
109
241
}
110
242
111
- class ListEntity : BaseIdEntity
243
+ class NullableListEntity : BaseIdEntity
112
244
{
113
- public List < decimal > aList { get ; set ; }
245
+ public List < int > ? aList { get ; set ; }
114
246
}
115
247
}
0 commit comments