-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathCosmosDataSinkExtensionTests.cs
More file actions
485 lines (418 loc) · 19.5 KB
/
CosmosDataSinkExtensionTests.cs
File metadata and controls
485 lines (418 loc) · 19.5 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
using Cosmos.DataTransfer.Interfaces;
using System.Dynamic;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
namespace Cosmos.DataTransfer.CosmosExtension.UnitTests
{
[TestClass]
public class CosmosDataSinkExtensionTests
{
[TestMethod]
public void CreateItemStream_WithDateString_PreservesFormat()
{
// Arrange - Simulate data read from source with ISO-8601 date string
var sourceSettings = RawJsonCosmosSerializer.GetDefaultSettings();
var sourceJson = "{\"id\": \"1\", \"event_time\": \"2023-12-19T00:00:00.000Z\"}";
var serializer = JsonSerializer.Create(sourceSettings);
using var reader = new JsonTextReader(new StringReader(sourceJson));
var sourceDict = serializer.Deserialize<Dictionary<string, object?>>(reader)!;
// Act - Create data item and build expando object (simulating the pipeline)
var dataItem = new CosmosDictionaryDataItem(sourceDict);
var expando = dataItem.BuildDynamicObjectTree()!;
// Serialize using the same settings that CreateItemStream uses
var json = JsonConvert.SerializeObject(expando, RawJsonCosmosSerializer.GetDefaultSettings());
// Assert - The date string format should be preserved
Assert.IsTrue(json.Contains("\"2023-12-19T00:00:00.000Z\""),
$"Date format should be preserved. Actual JSON: {json}");
}
[TestMethod]
public void CreateItemStream_WithNestedDateString_PreservesFormat()
{
// Arrange - Simulate data with nested date strings
var sourceSettings = RawJsonCosmosSerializer.GetDefaultSettings();
var sourceJson = "{\"id\": \"1\", \"data\": {\"created\": \"2023-12-19T00:00:00.000Z\", \"modified\": \"2023-12-20T12:30:45.123Z\"}}";
var serializer = JsonSerializer.Create(sourceSettings);
using var reader = new JsonTextReader(new StringReader(sourceJson));
var sourceDict = serializer.Deserialize<Dictionary<string, object?>>(reader)!;
// Act
var dataItem = new CosmosDictionaryDataItem(sourceDict);
var expando = dataItem.BuildDynamicObjectTree()!;
var json = JsonConvert.SerializeObject(expando, RawJsonCosmosSerializer.GetDefaultSettings());
// Assert
Assert.IsTrue(json.Contains("\"2023-12-19T00:00:00.000Z\""),
$"Created date format should be preserved. Actual JSON: {json}");
Assert.IsTrue(json.Contains("\"2023-12-20T12:30:45.123Z\""),
$"Modified date format should be preserved. Actual JSON: {json}");
}
[TestMethod]
public void CreateItemStream_WithDateStringArray_PreservesFormat()
{
// Arrange - Simulate data with date strings in array
var sourceSettings = RawJsonCosmosSerializer.GetDefaultSettings();
var sourceJson = "{\"id\": \"1\", \"timestamps\": [\"2023-12-19T00:00:00.000Z\", \"2023-12-20T00:00:00.000Z\"]}";
var serializer = JsonSerializer.Create(sourceSettings);
using var reader = new JsonTextReader(new StringReader(sourceJson));
var sourceDict = serializer.Deserialize<Dictionary<string, object?>>(reader)!;
// Act
var dataItem = new CosmosDictionaryDataItem(sourceDict);
var expando = dataItem.BuildDynamicObjectTree()!;
var json = JsonConvert.SerializeObject(expando, RawJsonCosmosSerializer.GetDefaultSettings());
// Assert
Assert.IsTrue(json.Contains("\"2023-12-19T00:00:00.000Z\""),
$"First date format should be preserved. Actual JSON: {json}");
Assert.IsTrue(json.Contains("\"2023-12-20T00:00:00.000Z\""),
$"Second date format should be preserved. Actual JSON: {json}");
}
[TestMethod]
public void BuildDynamicObjectTree_WithNestedArrays_WorksCorrectly()
{
var item = new CosmosDictionaryDataItem(new Dictionary<string, object?>()
{
{
"array",
new List<object?>
{
new List<object?>
{
new CosmosDictionaryDataItem(new Dictionary<string, object?>()
{
{ "id", "sub1-1" }
}),
new CosmosDictionaryDataItem(new Dictionary<string, object?>()
{
{ "id", "sub1-2" }
})
},
new List<object?>
{
new CosmosDictionaryDataItem(new Dictionary<string, object?>()
{
{ "id", "sub2-1" }
}),
}
}
}
});
dynamic obj = item.BuildDynamicObjectTree()!;
Assert.AreEqual(typeof(object[]), obj.array.GetType());
Assert.AreEqual(2, obj.array.Length);
var firstSubArray = obj.array[0];
Assert.AreEqual(typeof(object[]), firstSubArray.GetType());
Assert.AreEqual(2, firstSubArray.Length);
Assert.AreEqual("sub1-1", firstSubArray[0].id);
Assert.AreEqual("sub1-2", firstSubArray[1].id);
var secondSubArray = obj.array[1];
Assert.AreEqual(typeof(object[]), secondSubArray.GetType());
Assert.AreEqual(1, secondSubArray.Length);
Assert.AreEqual("sub2-1", secondSubArray[0].id);
}
[TestMethod]
public void BuildDynamicObjectTree_WithAnyCaseIds_UsesSourceIdValue()
{
var numeric = Random.Shared.Next();
var lower = Guid.NewGuid().ToString();
var upper = Guid.NewGuid().ToString();
var mixed = Guid.NewGuid().ToString();
var reversed = Guid.NewGuid().ToString();
var item = new CosmosDictionaryDataItem(new Dictionary<string, object?>()
{
{ "id", numeric },
});
dynamic obj = item.BuildDynamicObjectTree(requireStringId: true, preserveMixedCaseIds: false)!;
Assert.AreEqual(numeric.ToString(), obj.id);
item = new CosmosDictionaryDataItem(new Dictionary<string, object?>()
{
{ "id", lower },
});
obj = item.BuildDynamicObjectTree(requireStringId: true, preserveMixedCaseIds: false)!;
Assert.AreEqual(lower, obj.id);
item = new CosmosDictionaryDataItem(new Dictionary<string, object?>()
{
{ "ID", upper },
});
obj = item.BuildDynamicObjectTree(requireStringId: true, preserveMixedCaseIds: false)!;
Assert.AreEqual(upper, obj.id);
item = new CosmosDictionaryDataItem(new Dictionary<string, object?>()
{
{ "Id", mixed },
});
obj = item.BuildDynamicObjectTree(requireStringId: true, preserveMixedCaseIds: false)!;
Assert.AreEqual(mixed, obj.id);
item = new CosmosDictionaryDataItem(new Dictionary<string, object?>()
{
{ "iD", reversed },
});
obj = item.BuildDynamicObjectTree(requireStringId: true, preserveMixedCaseIds: false)!;
Assert.AreEqual(reversed, obj.id);
}
[TestMethod]
public void BuildDynamicObjectTree_WithPreservedMixedCaseIds_PassesThroughSourceValues()
{
var id = Random.Shared.Next();
var upper = Guid.NewGuid().ToString();
var mixed = Guid.NewGuid().ToString();
var item = new CosmosDictionaryDataItem(new Dictionary<string, object?>()
{
{ "id", id },
{ "ID", upper },
{ "Id", mixed }
});
dynamic obj = item.BuildDynamicObjectTree(requireStringId: true, preserveMixedCaseIds: true)!;
Assert.AreEqual(id.ToString(), obj.id);
Assert.AreEqual(upper, obj.ID);
Assert.AreEqual(mixed, obj.Id);
item = new CosmosDictionaryDataItem(new Dictionary<string, object?>()
{
{ "ID", upper },
{ "Id", mixed }
});
obj = item.BuildDynamicObjectTree(requireStringId: true, preserveMixedCaseIds: true)!;
Assert.AreEqual(upper, obj.ID);
Assert.AreEqual(mixed, obj.Id);
string? cosmosId = obj.id;
Assert.IsNotNull(cosmosId);
Assert.IsFalse(string.IsNullOrWhiteSpace(cosmosId));
}
[TestMethod]
public void BuildDynamicObjectTree_WithIgnoredNulls_ExcludesNullFields()
{
var item = new CosmosDictionaryDataItem(new Dictionary<string, object?>()
{
{ "id", "1" },
{ "nullField", null },
{
"array",
new List<object?>
{
new List<object?>
{
new CosmosDictionaryDataItem(new Dictionary<string, object?>()
{
{ "id", "sub1-1" },
{ "nullField", null },
}),
new CosmosDictionaryDataItem(new Dictionary<string, object?>()
{
{ "id", "sub1-2" }
})
},
new List<object?>
{
new CosmosDictionaryDataItem(new Dictionary<string, object?>()
{
{ "id", "sub2-1" },
{ "nullField", null },
}),
}
}
},
{ "child1",
new CosmosDictionaryDataItem(new Dictionary<string, object?>()
{
{ "id", "child1-1" },
})
},
{ "child2",
new CosmosDictionaryDataItem(new Dictionary<string, object?>()
{
{ "id", "child2-1" },
{ "nullField", null },
{ "child2_1",
new CosmosDictionaryDataItem(new Dictionary<string, object?>()
{
{ "id", "child2_1-1" },
{ "nullField", null },
})
}
})
}
});
dynamic obj = item.BuildDynamicObjectTree(ignoreNullValues: true)!;
Assert.IsFalse(HasProperty(obj, "nullField"));
Assert.AreEqual(typeof(object[]), obj.array.GetType());
Assert.AreEqual(2, obj.array.Length);
var firstSubArray = obj.array[0];
Assert.AreEqual(typeof(object[]), firstSubArray.GetType());
Assert.IsFalse(HasProperty(firstSubArray[0], "nullField"));
var secondSubArray = obj.array[1];
Assert.AreEqual(typeof(object[]), secondSubArray.GetType());
Assert.IsFalse(HasProperty(secondSubArray[0], "nullField"));
var child2 = obj.child2;
Assert.IsFalse(HasProperty(child2, "nullField"));
Assert.IsFalse(HasProperty(child2.child2_1, "nullField"));
}
[TestMethod]
public void BuildDynamicObjectTree_WithNulls_RetainsNullFields()
{
var item = new CosmosDictionaryDataItem(new Dictionary<string, object?>()
{
{ "id", "1" },
{ "nullField", null },
{
"array",
new List<object?>
{
new List<object?>
{
new CosmosDictionaryDataItem(new Dictionary<string, object?>()
{
{ "id", "sub1-1" },
{ "nullField", null },
}),
new CosmosDictionaryDataItem(new Dictionary<string, object?>()
{
{ "id", "sub1-2" }
})
},
new List<object?>
{
new CosmosDictionaryDataItem(new Dictionary<string, object?>()
{
{ "id", "sub2-1" },
{ "nullField", null },
}),
}
}
},
{ "child1",
new CosmosDictionaryDataItem(new Dictionary<string, object?>()
{
{ "id", "child1-1" },
})
},
{ "child2",
new CosmosDictionaryDataItem(new Dictionary<string, object?>()
{
{ "id", "child2-1" },
{ "nullField", null },
{ "child2_1",
new CosmosDictionaryDataItem(new Dictionary<string, object?>()
{
{ "id", "child2_1-1" },
{ "nullField", null },
})
}
})
}
});
dynamic obj = item.BuildDynamicObjectTree(ignoreNullValues: false)!;
Assert.IsTrue(HasProperty(obj, "nullField"));
Assert.IsNull(obj.nullField);
Assert.AreEqual(typeof(object[]), obj.array.GetType());
Assert.AreEqual(2, obj.array.Length);
var firstSubArray = obj.array[0];
Assert.AreEqual(typeof(object[]), firstSubArray.GetType());
Assert.IsTrue(HasProperty(firstSubArray[0],"nullField"));
Assert.IsNull(firstSubArray[0].nullField);
Assert.IsFalse(HasProperty(firstSubArray[1], "nullField"));
var secondSubArray = obj.array[1];
Assert.AreEqual(typeof(object[]), secondSubArray.GetType());
Assert.IsTrue(HasProperty(secondSubArray[0], "nullField"));
Assert.IsNull(secondSubArray[0].nullField);
var child2 = obj.child2;
Assert.IsTrue(HasProperty(child2, "nullField"));
Assert.IsNull(child2.nullField);
Assert.IsTrue(HasProperty(child2.child2_1, "nullField"));
Assert.IsNull(child2.child2_1.nullField);
}
public static bool HasProperty(object obj, string name)
{
if (obj is not ExpandoObject)
return obj.GetType().GetProperty(name) != null;
var values = (IDictionary<string, object>)obj;
return values.ContainsKey(name);
}
private static string? InvokeGetPropertyValue(ExpandoObject item, string propertyName)
{
var method = typeof(CosmosDataSinkExtension).GetMethod("GetPropertyValue",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
return method?.Invoke(null, new object[] { item, propertyName }) as string;
}
[TestMethod]
public void GetPropertyValue_WithSimpleProperty_ReturnsValue()
{
// Arrange
var expando = new ExpandoObject();
var dict = (IDictionary<string, object?>)expando;
dict["id"] = "test-id-123";
dict["name"] = "test-name";
// Act
var result = InvokeGetPropertyValue(expando, "id");
// Assert
Assert.IsNotNull(result);
Assert.AreEqual("test-id-123", result);
}
[TestMethod]
public void GetPropertyValue_WithNestedProperty_ReturnsValue()
{
// Arrange - Create nested structure matching the issue example
var expando = new ExpandoObject();
var dict = (IDictionary<string, object?>)expando;
dict["id"] = "test-id";
var nestedExpando = new ExpandoObject();
var nestedDict = (IDictionary<string, object?>)nestedExpando;
nestedDict["partitionkeyvalue2"] = "guid-value-123";
nestedDict["somevalue4"] = "other-guid";
nestedDict["UserName"] = "testuser";
dict["partitionkeyvalue1"] = nestedExpando;
// Act
var result = InvokeGetPropertyValue(expando, "partitionkeyvalue1/partitionkeyvalue2");
// Assert
Assert.IsNotNull(result);
Assert.AreEqual("guid-value-123", result);
}
[TestMethod]
public void GetPropertyValue_WithDeeplyNestedProperty_ReturnsValue()
{
// Arrange - Create deeply nested structure
var expando = new ExpandoObject();
var dict = (IDictionary<string, object?>)expando;
dict["id"] = "test-id";
var level1 = new ExpandoObject();
var level1Dict = (IDictionary<string, object?>)level1;
var level2 = new ExpandoObject();
var level2Dict = (IDictionary<string, object?>)level2;
var level3 = new ExpandoObject();
var level3Dict = (IDictionary<string, object?>)level3;
level3Dict["finalValue"] = "deeply-nested-value";
level2Dict["level3"] = level3;
level1Dict["level2"] = level2;
dict["level1"] = level1;
// Act
var result = InvokeGetPropertyValue(expando, "level1/level2/level3/finalValue");
// Assert
Assert.IsNotNull(result);
Assert.AreEqual("deeply-nested-value", result);
}
[TestMethod]
public void GetPropertyValue_WithMissingNestedProperty_ReturnsNull()
{
// Arrange
var expando = new ExpandoObject();
var dict = (IDictionary<string, object?>)expando;
dict["id"] = "test-id";
var nestedExpando = new ExpandoObject();
var nestedDict = (IDictionary<string, object?>)nestedExpando;
nestedDict["existingKey"] = "value";
dict["parent"] = nestedExpando;
// Act
var result = InvokeGetPropertyValue(expando, "parent/nonExistentKey");
// Assert
Assert.IsNull(result);
}
[TestMethod]
public void GetPropertyValue_WithNullIntermediateValue_ReturnsNull()
{
// Arrange
var expando = new ExpandoObject();
var dict = (IDictionary<string, object?>)expando;
dict["id"] = "test-id";
dict["parent"] = null;
// Act
var result = InvokeGetPropertyValue(expando, "parent/child");
// Assert
Assert.IsNull(result);
}
}
}