Skip to content

Commit

Permalink
Tests for handling nested arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
ttu committed Oct 4, 2024
1 parent a654ca9 commit 4955106
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 13 deletions.
49 changes: 49 additions & 0 deletions JsonFlatFileDataStore.Test/CollectionModificationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,55 @@ public async Task UpdateOneAsync_TypedModel_InnerSimpleIntArray()
UTHelpers.Down(newFilePath);
}

[Fact]
public async Task UpdateOneAsync_TypedModel_NestedArrays()
{
var newFilePath = UTHelpers.Up();

var store = new DataStore(newFilePath);

var collection = store.GetCollection<TestModelWithNestedArray>();
Assert.Equal(0, collection.Count);

var newModel = new TestModelWithNestedArray
{
Id = Guid.NewGuid().ToString(),
Type = "empty",
Fragments = new List<List<int>> { new List<int> { 1 }, new List<int> { 2 }, new List<int> { 3 } }
};

var insertResult = collection.InsertOne(newModel);
Assert.True(insertResult);
Assert.Equal(1, collection.Count);

var store2 = new DataStore(newFilePath);
var collection2 = store2.GetCollection<TestModelWithNestedArray>();
Assert.Equal(1, collection2.Count);

var updateData = new
{
Type = "filled",
Fragments = new List<List<int>>
{
new List<int> { 1 },
new List<int> { 2 },
new List<int> { 3 }
}
};

await collection2.UpdateOneAsync(e => e.Id == newModel.Id, updateData);

var store3 = new DataStore(newFilePath);
var collection3 = store3.GetCollection<TestModelWithNestedArray>();
var updated = collection3.Find(e => e.Id == newModel.Id).First();
Assert.Equal(3, updated.Fragments.Count());
Assert.Equal(2, updated.Fragments.First().First());
Assert.Equal(3, updated.Fragments.Last().First());
Assert.Equal("filled", updated.Type);

UTHelpers.Down(newFilePath);
}

[Fact]
public async Task UpdateOneAsync_Predicate_Id_DynamicUser()
{
Expand Down
7 changes: 7 additions & 0 deletions JsonFlatFileDataStore.Test/TestModels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ public class TestModelWithIntArray
public List<int> Fragments { get; set; }
}

public class TestModelWithNestedArray
{
public string Id { get; set; }
public string Type { get; set; }
public List<List<int>> Fragments { get; set; }
}

public class User
{
public int Id { get; set; }
Expand Down
122 changes: 109 additions & 13 deletions JsonFlatFileDataStore/ExpandoObjectConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private static void AddPropertyToExpando(IDictionary<string, object> expando, st
}
else
{
// Handle other numeric types as needed
// TODO: Handle other numeric types as needed
throw new JsonException("Unsupported numeric type");
}
break;
Expand All @@ -95,24 +95,120 @@ private static void AddPropertyToExpando(IDictionary<string, object> expando, st
var arrayValues = new List<object>();
foreach (var arrayElement in propertyValue.EnumerateArray())
{
if (arrayElement.ValueKind == JsonValueKind.Object)
switch (arrayElement.ValueKind)
{
var nestedExpandoInArray = new ExpandoObject();
var nestedDictionaryInArray = (IDictionary<string, object>)nestedExpandoInArray;
foreach (var nestedPropertyInArray in arrayElement.EnumerateObject())
{
AddPropertyToExpando(nestedDictionaryInArray, nestedPropertyInArray.Name, nestedPropertyInArray.Value);
}
arrayValues.Add(nestedExpandoInArray);
}
else
{
arrayValues.Add(arrayElement.GetString()); // Adjust this for other value types if needed
case JsonValueKind.Undefined:
case JsonValueKind.Null:
arrayValues.Add(null);
break;

case JsonValueKind.False:
arrayValues.Add(false);
break;

case JsonValueKind.True:
arrayValues.Add(true);
break;

case JsonValueKind.Number:
if (arrayElement.TryGetInt32(out int arrayElementIntValue))
{
arrayValues.Add(arrayElementIntValue);
}
else if (arrayElement.TryGetInt64(out long longValue))
{
arrayValues.Add(longValue);
}
else if (arrayElement.TryGetDouble(out double doubleValue))
{
arrayValues.Add(doubleValue);
}
else if (arrayElement.TryGetDecimal(out decimal decimalValue))
{
arrayValues.Add(decimalValue);
}
else
{
throw new JsonException("Unsupported numeric type");
}
break;

case JsonValueKind.String:
arrayValues.Add(arrayElement.GetString());
break;

case JsonValueKind.Object:
var nestedExpandoInArray = new ExpandoObject();
var nestedDictionaryInArray = (IDictionary<string, object>)nestedExpandoInArray;
foreach (var nestedPropertyInArray in arrayElement.EnumerateObject())
{
AddPropertyToExpando(nestedDictionaryInArray, nestedPropertyInArray.Name, nestedPropertyInArray.Value);
}
arrayValues.Add(nestedExpandoInArray);
break;

case JsonValueKind.Array:
// Recursively handle nested arrays
var nestedArray = new List<object>();
foreach (var nestedArrayElement in arrayElement.EnumerateArray())
{
if (nestedArrayElement.ValueKind == JsonValueKind.Object)
{
var nestedExpandoInNestedArray = new ExpandoObject();
var nestedDictionaryInNestedArray = (IDictionary<string, object>)nestedExpandoInNestedArray;
foreach (var nestedPropertyInNestedArray in nestedArrayElement.EnumerateObject())
{
AddPropertyToExpando(nestedDictionaryInNestedArray, nestedPropertyInNestedArray.Name, nestedPropertyInNestedArray.Value);
}
nestedArray.Add(nestedExpandoInNestedArray);
}
else if (nestedArrayElement.ValueKind == JsonValueKind.Array)
{
// Recursively handle deeper nested arrays
nestedArray.Add(ProcessNestedArray(nestedArrayElement));
}
else
{
nestedArray.Add(nestedArrayElement.ToString());
}
}
arrayValues.Add(nestedArray);
break;
}
}
expando[propertyName] = arrayValues;
break;
}
}

private static object ProcessNestedArray(JsonElement arrayElement)
{
var arrayValues = new List<object>();

foreach (var element in arrayElement.EnumerateArray())
{
if (element.ValueKind == JsonValueKind.Object)
{
var nestedExpando = new ExpandoObject();
var nestedDictionary = (IDictionary<string, object>)nestedExpando;
foreach (var nestedProperty in element.EnumerateObject())
{
AddPropertyToExpando(nestedDictionary, nestedProperty.Name, nestedProperty.Value);
}
arrayValues.Add(nestedExpando);
}
else if (element.ValueKind == JsonValueKind.Array)
{
// Recursively handle further nested arrays
arrayValues.Add(ProcessNestedArray(element));
}
else
{
arrayValues.Add(element.ToString());
}
}

return arrayValues;
}
}
}

0 comments on commit 4955106

Please sign in to comment.