Skip to content

Commit

Permalink
fix: update functionality for collection with nested collection
Browse files Browse the repository at this point in the history
  • Loading branch information
ttu committed Oct 5, 2024
1 parent 56764b5 commit 12415fa
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
87 changes: 87 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>>
{
null,
new List<int> { 4 },
}
};

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(1, updated.Fragments[0].First());
Assert.Equal(4, updated.Fragments[1].First());
Assert.Equal(3, updated.Fragments[2].First());
Assert.Equal("filled", updated.Type);

UTHelpers.Down(newFilePath);
}

[Fact]
public async Task UpdateOneAsync_Predicate_Id_DynamicUser()
{
Expand Down Expand Up @@ -988,5 +1037,43 @@ public async Task UpdateInnerFloatArray_TypedWorld()

UTHelpers.Down(newFilePath);
}

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

var store = new DataStore(newFilePath);

var collection = store.GetCollection("employee");

var ja = new JArray { "Hello World!" };

var jObj = new JObject()
{
["custom_id"] = 11,
["anotherArray"] = new JArray { ja },
};

await collection.InsertOneAsync(jObj);

var original = collection.Find(e => e.custom_id == 11).First();
Assert.Equal(0, original.id);
Assert.Equal(11, original.custom_id);
Assert.Equal("Hello World!", original.anotherArray[0][0]);

var update = new JObject()
{
["custom_id"] = 12,
["anotherArray"] = new JArray { new JArray { "Other text" } },
};

await collection.UpdateOneAsync(e => e.custom_id == 11, update);

var updated = collection.Find(e => e.custom_id == 12).First();
Assert.Equal("Other text", updated.anotherArray[0][0]);

UTHelpers.Down(newFilePath);
}
}
}
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
8 changes: 8 additions & 0 deletions JsonFlatFileDataStore/ObjectExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,10 @@ private static void HandleTypedEnumerable(object source, object destination, dyn

if (type.GetTypeInfo().IsValueType || type == typeof(string))
targetArray[i] = sourceValue;
else if (IsDictionary(type))
targetArray[i] = sourceValue;
else if (IsEnumerable(type))
targetArray[i] = sourceValue;
else
CopyProperties(sourceValue, targetArray[i]);
}
Expand Down Expand Up @@ -342,6 +346,10 @@ Type GetTypeFromTargetItem(IList target, int index)

if (targetType.GetTypeInfo().IsValueType || targetType == typeof(string))
targetArray[i] = sourceValue;
else if (IsDictionary(targetType))
targetArray[i] = sourceValue;
else if (IsEnumerable(targetType))
targetArray[i] = sourceValue;
else
CopyProperties(sourceValue, targetArray[i]);
}
Expand Down

0 comments on commit 12415fa

Please sign in to comment.