|
1 | 1 | using Cosmos.DataTransfer.Interfaces;
|
| 2 | +using System.Dynamic; |
2 | 3 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
3 | 4 |
|
4 | 5 | namespace Cosmos.DataTransfer.CosmosExtension.UnitTests
|
@@ -132,5 +133,170 @@ public void BuildDynamicObjectTree_WithPreservedMixedCaseIds_PassesThroughSource
|
132 | 133 | Assert.IsNotNull(cosmosId);
|
133 | 134 | Assert.IsFalse(string.IsNullOrWhiteSpace(cosmosId));
|
134 | 135 | }
|
| 136 | + |
| 137 | + [TestMethod] |
| 138 | + public void BuildDynamicObjectTree_WithIgnoredNulls_ExcludesNullFields() |
| 139 | + { |
| 140 | + var item = new CosmosDictionaryDataItem(new Dictionary<string, object?>() |
| 141 | + { |
| 142 | + { "id", "1" }, |
| 143 | + { "nullField", null }, |
| 144 | + { |
| 145 | + "array", |
| 146 | + new List<object?> |
| 147 | + { |
| 148 | + new List<object?> |
| 149 | + { |
| 150 | + new CosmosDictionaryDataItem(new Dictionary<string, object?>() |
| 151 | + { |
| 152 | + { "id", "sub1-1" }, |
| 153 | + { "nullField", null }, |
| 154 | + }), |
| 155 | + new CosmosDictionaryDataItem(new Dictionary<string, object?>() |
| 156 | + { |
| 157 | + { "id", "sub1-2" } |
| 158 | + }) |
| 159 | + }, |
| 160 | + new List<object?> |
| 161 | + { |
| 162 | + new CosmosDictionaryDataItem(new Dictionary<string, object?>() |
| 163 | + { |
| 164 | + { "id", "sub2-1" }, |
| 165 | + { "nullField", null }, |
| 166 | + }), |
| 167 | + } |
| 168 | + } |
| 169 | + }, |
| 170 | + { "child1", |
| 171 | + new CosmosDictionaryDataItem(new Dictionary<string, object?>() |
| 172 | + { |
| 173 | + { "id", "child1-1" }, |
| 174 | + }) |
| 175 | + }, |
| 176 | + { "child2", |
| 177 | + new CosmosDictionaryDataItem(new Dictionary<string, object?>() |
| 178 | + { |
| 179 | + { "id", "child2-1" }, |
| 180 | + { "nullField", null }, |
| 181 | + { "child2_1", |
| 182 | + new CosmosDictionaryDataItem(new Dictionary<string, object?>() |
| 183 | + { |
| 184 | + { "id", "child2_1-1" }, |
| 185 | + { "nullField", null }, |
| 186 | + }) |
| 187 | + } |
| 188 | + }) |
| 189 | + } |
| 190 | + }); |
| 191 | + |
| 192 | + dynamic obj = item.BuildDynamicObjectTree(ignoreNullValues: true)!; |
| 193 | + |
| 194 | + Assert.IsFalse(HasProperty(obj, "nullField")); |
| 195 | + |
| 196 | + Assert.AreEqual(typeof(object[]), obj.array.GetType()); |
| 197 | + Assert.AreEqual(2, obj.array.Length); |
| 198 | + |
| 199 | + var firstSubArray = obj.array[0]; |
| 200 | + Assert.AreEqual(typeof(object[]), firstSubArray.GetType()); |
| 201 | + Assert.IsFalse(HasProperty(firstSubArray[0], "nullField")); |
| 202 | + |
| 203 | + var secondSubArray = obj.array[1]; |
| 204 | + Assert.AreEqual(typeof(object[]), secondSubArray.GetType()); |
| 205 | + Assert.IsFalse(HasProperty(secondSubArray[0], "nullField")); |
| 206 | + |
| 207 | + var child2 = obj.child2; |
| 208 | + Assert.IsFalse(HasProperty(child2, "nullField")); |
| 209 | + Assert.IsFalse(HasProperty(child2.child2_1, "nullField")); |
| 210 | + } |
| 211 | + |
| 212 | + [TestMethod] |
| 213 | + public void BuildDynamicObjectTree_WithNulls_RetainsNullFields() |
| 214 | + { |
| 215 | + var item = new CosmosDictionaryDataItem(new Dictionary<string, object?>() |
| 216 | + { |
| 217 | + { "id", "1" }, |
| 218 | + { "nullField", null }, |
| 219 | + { |
| 220 | + "array", |
| 221 | + new List<object?> |
| 222 | + { |
| 223 | + new List<object?> |
| 224 | + { |
| 225 | + new CosmosDictionaryDataItem(new Dictionary<string, object?>() |
| 226 | + { |
| 227 | + { "id", "sub1-1" }, |
| 228 | + { "nullField", null }, |
| 229 | + }), |
| 230 | + new CosmosDictionaryDataItem(new Dictionary<string, object?>() |
| 231 | + { |
| 232 | + { "id", "sub1-2" } |
| 233 | + }) |
| 234 | + }, |
| 235 | + new List<object?> |
| 236 | + { |
| 237 | + new CosmosDictionaryDataItem(new Dictionary<string, object?>() |
| 238 | + { |
| 239 | + { "id", "sub2-1" }, |
| 240 | + { "nullField", null }, |
| 241 | + }), |
| 242 | + } |
| 243 | + } |
| 244 | + }, |
| 245 | + { "child1", |
| 246 | + new CosmosDictionaryDataItem(new Dictionary<string, object?>() |
| 247 | + { |
| 248 | + { "id", "child1-1" }, |
| 249 | + }) |
| 250 | + }, |
| 251 | + { "child2", |
| 252 | + new CosmosDictionaryDataItem(new Dictionary<string, object?>() |
| 253 | + { |
| 254 | + { "id", "child2-1" }, |
| 255 | + { "nullField", null }, |
| 256 | + { "child2_1", |
| 257 | + new CosmosDictionaryDataItem(new Dictionary<string, object?>() |
| 258 | + { |
| 259 | + { "id", "child2_1-1" }, |
| 260 | + { "nullField", null }, |
| 261 | + }) |
| 262 | + } |
| 263 | + }) |
| 264 | + } |
| 265 | + }); |
| 266 | + |
| 267 | + dynamic obj = item.BuildDynamicObjectTree(ignoreNullValues: false)!; |
| 268 | + |
| 269 | + Assert.IsTrue(HasProperty(obj, "nullField")); |
| 270 | + Assert.IsNull(obj.nullField); |
| 271 | + |
| 272 | + Assert.AreEqual(typeof(object[]), obj.array.GetType()); |
| 273 | + Assert.AreEqual(2, obj.array.Length); |
| 274 | + |
| 275 | + var firstSubArray = obj.array[0]; |
| 276 | + Assert.AreEqual(typeof(object[]), firstSubArray.GetType()); |
| 277 | + Assert.IsTrue(HasProperty(firstSubArray[0],"nullField")); |
| 278 | + Assert.IsNull(firstSubArray[0].nullField); |
| 279 | + Assert.IsFalse(HasProperty(firstSubArray[1], "nullField")); |
| 280 | + |
| 281 | + var secondSubArray = obj.array[1]; |
| 282 | + Assert.AreEqual(typeof(object[]), secondSubArray.GetType()); |
| 283 | + Assert.IsTrue(HasProperty(secondSubArray[0], "nullField")); |
| 284 | + Assert.IsNull(secondSubArray[0].nullField); |
| 285 | + |
| 286 | + var child2 = obj.child2; |
| 287 | + Assert.IsTrue(HasProperty(child2, "nullField")); |
| 288 | + Assert.IsNull(child2.nullField); |
| 289 | + Assert.IsTrue(HasProperty(child2.child2_1, "nullField")); |
| 290 | + Assert.IsNull(child2.child2_1.nullField); |
| 291 | + } |
| 292 | + |
| 293 | + public static bool HasProperty(object obj, string name) |
| 294 | + { |
| 295 | + if (obj is not ExpandoObject) |
| 296 | + return obj.GetType().GetProperty(name) != null; |
| 297 | + |
| 298 | + var values = (IDictionary<string, object>)obj; |
| 299 | + return values.ContainsKey(name); |
| 300 | + } |
135 | 301 | }
|
136 | 302 | }
|
0 commit comments