-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathJsonFileSinkTests.cs
More file actions
113 lines (96 loc) · 4.28 KB
/
Copy pathJsonFileSinkTests.cs
File metadata and controls
113 lines (96 loc) · 4.28 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
using Cosmos.DataTransfer.Common;
using Cosmos.DataTransfer.Common.UnitTests;
using Microsoft.Extensions.Logging.Abstractions;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Cosmos.DataTransfer.JsonExtension.UnitTests
{
[TestClass]
public class JsonFileSinkTests
{
[TestMethod]
public async Task WriteAsync_WithFlatObjects_WritesToValidFile()
{
var sink = new JsonFileSink();
var data = new List<DictionaryDataItem>
{
new(new Dictionary<string, object?>
{
{ "Id", 1 },
{ "Name", "One" },
}),
new(new Dictionary<string, object?>
{
{ "Id", 2 },
{ "Name", "Two" },
}),
new(new Dictionary<string, object?>
{
{ "Id", 3 },
{ "Name", "Three" },
}),
};
string outputFile = $"{DateTime.Now:yy-MM-dd}_FS_Output.json";
var config = TestHelpers.CreateConfig(new Dictionary<string, string>
{
{ "FilePath", outputFile }
});
await sink.WriteAsync(data.ToAsyncEnumerable(), config, new JsonFileSource(), NullLogger.Instance);
var outputData = JsonConvert.DeserializeObject<List<TestDataObject>>(await File.ReadAllTextAsync(outputFile));
Assert.IsTrue(outputData.Any(o => o.Id == 1 && o.Name == "One"));
Assert.IsTrue(outputData.Any(o => o.Id == 2 && o.Name == "Two"));
Assert.IsTrue(outputData.Any(o => o.Id == 3 && o.Name == "Three"));
}
[TestMethod]
public async Task WriteAsync_WithNestedDictionaries_SerializesCorrectly()
{
// Test case for the MongoDB nested elements issue
var sink = new JsonFileSink();
var data = new List<DictionaryDataItem>
{
new(new Dictionary<string, object?>
{
{ "_id", new Dictionary<string, object?> { { "$oid", "some_id" } } },
{ "thread_id", "thread_id" },
{ "content", new List<Dictionary<string, object?>>
{
new Dictionary<string, object?>
{
{ "text", "a message text" },
{ "type", "text" }
}
}
},
{ "role", "user" }
})
};
string outputFile = $"{DateTime.Now:yy-MM-dd}_FS_Nested_Output.json";
var config = TestHelpers.CreateConfig(new Dictionary<string, string>
{
{ "FilePath", outputFile }
});
await sink.WriteAsync(data.ToAsyncEnumerable(), config, new JsonFileSource(), NullLogger.Instance);
var jsonContent = await File.ReadAllTextAsync(outputFile);
var outputArray = JArray.Parse(jsonContent);
Assert.AreEqual(1, outputArray.Count);
var doc = outputArray[0] as JObject;
Assert.IsNotNull(doc);
// Verify _id is an object with $oid field
var idObj = doc["_id"] as JObject;
Assert.IsNotNull(idObj, "_id should be an object");
Assert.AreEqual("some_id", idObj["$oid"]?.ToString());
// Verify thread_id is a string
Assert.AreEqual("thread_id", doc["thread_id"]?.ToString());
// Verify content is an array of objects
var contentArray = doc["content"] as JArray;
Assert.IsNotNull(contentArray, "content should be an array");
Assert.AreEqual(1, contentArray.Count);
var contentItem = contentArray[0] as JObject;
Assert.IsNotNull(contentItem, "content item should be an object");
Assert.AreEqual("a message text", contentItem["text"]?.ToString());
Assert.AreEqual("text", contentItem["type"]?.ToString());
// Verify role is a string
Assert.AreEqual("user", doc["role"]?.ToString());
}
}
}