|
1 | 1 | using Cosmos.DataTransfer.Interfaces;
|
| 2 | +using System.Dynamic; |
2 | 3 |
|
3 | 4 | namespace Cosmos.DataTransfer.CosmosExtension.UnitTests
|
4 | 5 | {
|
@@ -54,5 +55,170 @@ public void BuildDynamicObjectTree_WithNestedArrays_WorksCorrectly()
|
54 | 55 |
|
55 | 56 | Assert.AreEqual("sub2-1", secondSubArray[0].id);
|
56 | 57 | }
|
| 58 | + |
| 59 | + [TestMethod] |
| 60 | + public void BuildDynamicObjectTree_WithIgnoredNulls_ExcludesNullFields() |
| 61 | + { |
| 62 | + var item = new CosmosDictionaryDataItem(new Dictionary<string, object?>() |
| 63 | + { |
| 64 | + { "id", "1" }, |
| 65 | + { "nullField", null }, |
| 66 | + { |
| 67 | + "array", |
| 68 | + new List<object?> |
| 69 | + { |
| 70 | + new List<object?> |
| 71 | + { |
| 72 | + new CosmosDictionaryDataItem(new Dictionary<string, object?>() |
| 73 | + { |
| 74 | + { "id", "sub1-1" }, |
| 75 | + { "nullField", null }, |
| 76 | + }), |
| 77 | + new CosmosDictionaryDataItem(new Dictionary<string, object?>() |
| 78 | + { |
| 79 | + { "id", "sub1-2" } |
| 80 | + }) |
| 81 | + }, |
| 82 | + new List<object?> |
| 83 | + { |
| 84 | + new CosmosDictionaryDataItem(new Dictionary<string, object?>() |
| 85 | + { |
| 86 | + { "id", "sub2-1" }, |
| 87 | + { "nullField", null }, |
| 88 | + }), |
| 89 | + } |
| 90 | + } |
| 91 | + }, |
| 92 | + { "child1", |
| 93 | + new CosmosDictionaryDataItem(new Dictionary<string, object?>() |
| 94 | + { |
| 95 | + { "id", "child1-1" }, |
| 96 | + }) |
| 97 | + }, |
| 98 | + { "child2", |
| 99 | + new CosmosDictionaryDataItem(new Dictionary<string, object?>() |
| 100 | + { |
| 101 | + { "id", "child2-1" }, |
| 102 | + { "nullField", null }, |
| 103 | + { "child2_1", |
| 104 | + new CosmosDictionaryDataItem(new Dictionary<string, object?>() |
| 105 | + { |
| 106 | + { "id", "child2_1-1" }, |
| 107 | + { "nullField", null }, |
| 108 | + }) |
| 109 | + } |
| 110 | + }) |
| 111 | + } |
| 112 | + }); |
| 113 | + |
| 114 | + dynamic obj = item.BuildDynamicObjectTree(ignoreNullValues: true)!; |
| 115 | + |
| 116 | + Assert.IsFalse(HasProperty(obj, "nullField")); |
| 117 | + |
| 118 | + Assert.AreEqual(typeof(object[]), obj.array.GetType()); |
| 119 | + Assert.AreEqual(2, obj.array.Length); |
| 120 | + |
| 121 | + var firstSubArray = obj.array[0]; |
| 122 | + Assert.AreEqual(typeof(object[]), firstSubArray.GetType()); |
| 123 | + Assert.IsFalse(HasProperty(firstSubArray[0], "nullField")); |
| 124 | + |
| 125 | + var secondSubArray = obj.array[1]; |
| 126 | + Assert.AreEqual(typeof(object[]), secondSubArray.GetType()); |
| 127 | + Assert.IsFalse(HasProperty(secondSubArray[0], "nullField")); |
| 128 | + |
| 129 | + var child2 = obj.child2; |
| 130 | + Assert.IsFalse(HasProperty(child2, "nullField")); |
| 131 | + Assert.IsFalse(HasProperty(child2.child2_1, "nullField")); |
| 132 | + } |
| 133 | + |
| 134 | + [TestMethod] |
| 135 | + public void BuildDynamicObjectTree_WithNulls_RetainsNullFields() |
| 136 | + { |
| 137 | + var item = new CosmosDictionaryDataItem(new Dictionary<string, object?>() |
| 138 | + { |
| 139 | + { "id", "1" }, |
| 140 | + { "nullField", null }, |
| 141 | + { |
| 142 | + "array", |
| 143 | + new List<object?> |
| 144 | + { |
| 145 | + new List<object?> |
| 146 | + { |
| 147 | + new CosmosDictionaryDataItem(new Dictionary<string, object?>() |
| 148 | + { |
| 149 | + { "id", "sub1-1" }, |
| 150 | + { "nullField", null }, |
| 151 | + }), |
| 152 | + new CosmosDictionaryDataItem(new Dictionary<string, object?>() |
| 153 | + { |
| 154 | + { "id", "sub1-2" } |
| 155 | + }) |
| 156 | + }, |
| 157 | + new List<object?> |
| 158 | + { |
| 159 | + new CosmosDictionaryDataItem(new Dictionary<string, object?>() |
| 160 | + { |
| 161 | + { "id", "sub2-1" }, |
| 162 | + { "nullField", null }, |
| 163 | + }), |
| 164 | + } |
| 165 | + } |
| 166 | + }, |
| 167 | + { "child1", |
| 168 | + new CosmosDictionaryDataItem(new Dictionary<string, object?>() |
| 169 | + { |
| 170 | + { "id", "child1-1" }, |
| 171 | + }) |
| 172 | + }, |
| 173 | + { "child2", |
| 174 | + new CosmosDictionaryDataItem(new Dictionary<string, object?>() |
| 175 | + { |
| 176 | + { "id", "child2-1" }, |
| 177 | + { "nullField", null }, |
| 178 | + { "child2_1", |
| 179 | + new CosmosDictionaryDataItem(new Dictionary<string, object?>() |
| 180 | + { |
| 181 | + { "id", "child2_1-1" }, |
| 182 | + { "nullField", null }, |
| 183 | + }) |
| 184 | + } |
| 185 | + }) |
| 186 | + } |
| 187 | + }); |
| 188 | + |
| 189 | + dynamic obj = item.BuildDynamicObjectTree(ignoreNullValues: false)!; |
| 190 | + |
| 191 | + Assert.IsTrue(HasProperty(obj, "nullField")); |
| 192 | + Assert.IsNull(obj.nullField); |
| 193 | + |
| 194 | + Assert.AreEqual(typeof(object[]), obj.array.GetType()); |
| 195 | + Assert.AreEqual(2, obj.array.Length); |
| 196 | + |
| 197 | + var firstSubArray = obj.array[0]; |
| 198 | + Assert.AreEqual(typeof(object[]), firstSubArray.GetType()); |
| 199 | + Assert.IsTrue(HasProperty(firstSubArray[0],"nullField")); |
| 200 | + Assert.IsNull(firstSubArray[0].nullField); |
| 201 | + Assert.IsFalse(HasProperty(firstSubArray[1], "nullField")); |
| 202 | + |
| 203 | + var secondSubArray = obj.array[1]; |
| 204 | + Assert.AreEqual(typeof(object[]), secondSubArray.GetType()); |
| 205 | + Assert.IsTrue(HasProperty(secondSubArray[0], "nullField")); |
| 206 | + Assert.IsNull(secondSubArray[0].nullField); |
| 207 | + |
| 208 | + var child2 = obj.child2; |
| 209 | + Assert.IsTrue(HasProperty(child2, "nullField")); |
| 210 | + Assert.IsNull(child2.nullField); |
| 211 | + Assert.IsTrue(HasProperty(child2.child2_1, "nullField")); |
| 212 | + Assert.IsNull(child2.child2_1.nullField); |
| 213 | + } |
| 214 | + |
| 215 | + public static bool HasProperty(object obj, string name) |
| 216 | + { |
| 217 | + if (obj is not ExpandoObject) |
| 218 | + return obj.GetType().GetProperty(name) != null; |
| 219 | + |
| 220 | + var values = (IDictionary<string, object>)obj; |
| 221 | + return values.ContainsKey(name); |
| 222 | + } |
57 | 223 | }
|
58 | 224 | }
|
0 commit comments