Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions JsonFlatFileDataStore.Benchmark/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ private static void Main(string[] args)
{
var switcher = new BenchmarkSwitcher(new[]
{
typeof(TypedCollectionBenchmark),
typeof(DynamicCollectionBenchmark),
typeof(ObjectExtensionsBenchmark)
});
typeof(TypedCollectionBenchmark),
typeof(DynamicCollectionBenchmark),
typeof(ObjectExtensionsBenchmark)
});

switcher.Run(args);
}
Expand Down
120 changes: 63 additions & 57 deletions JsonFlatFileDataStore.Test/CollectionModificationTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Collections.Generic;
using System.Dynamic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Dynamic;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Threading.Tasks;

namespace JsonFlatFileDataStore.Test;

Expand Down Expand Up @@ -142,7 +142,7 @@ public async Task UpdateOneAsync_TypedUser_WrongCase()

var collection2 = store2.GetCollection<User>("users2");
await collection2.UpdateOneAsync(x => x.Id == 0, new { name = "new value" });
await collection2.UpdateOneAsync(x => x.Id == 1, JToken.Parse("{ name: \"new value 2\"} "));
await collection2.UpdateOneAsync(x => x.Id == 1, JsonNode.Parse("{ \"name\": \"new value 2\"} "));

var store3 = new DataStore(newFilePath);

Expand Down Expand Up @@ -203,9 +203,9 @@ public async Task UpdateOneAsync_TypedModel_InnerSimpleArray()
Id = Guid.NewGuid().ToString(),
Type = "empty",
Fragments = new List<string>
{
Guid.NewGuid().ToString()
}
{
Guid.NewGuid().ToString()
}
};

var insertResult = collection.InsertOne(newModel);
Expand All @@ -220,10 +220,10 @@ public async Task UpdateOneAsync_TypedModel_InnerSimpleArray()
{
Type = "filled",
Fragments = new List<string>
{
Guid.NewGuid().ToString(),
Guid.NewGuid().ToString()
}
{
Guid.NewGuid().ToString(),
Guid.NewGuid().ToString()
}
};

await collection2.UpdateOneAsync(e => e.Id == newModel.Id, updateData);
Expand Down Expand Up @@ -254,9 +254,9 @@ public async Task UpdateOneAsync_TypedModel_InnerSimpleIntArray()
Id = Guid.NewGuid().ToString(),
Type = "empty",
Fragments = new List<int>
{
1
}
{
1
}
};

var insertResult = collection.InsertOne(newModel);
Expand All @@ -271,10 +271,10 @@ public async Task UpdateOneAsync_TypedModel_InnerSimpleIntArray()
{
Type = "filled",
Fragments = new List<int>
{
2,
3
}
{
2,
3
}
};

await collection2.UpdateOneAsync(e => e.Id == newModel.Id, updateData);
Expand Down Expand Up @@ -319,10 +319,10 @@ public async Task UpdateOneAsync_TypedModel_NestedArrays()
{
Type = "filled",
NestedLists = new List<List<int>>
{
null,
new List<int> { 4 },
}
{
null,
new List<int> { 4 },
}
};

await collection2.UpdateOneAsync(e => e.Id == newModel.Id, updateData);
Expand Down Expand Up @@ -407,10 +407,10 @@ public async Task UpdateManyAsync_DynamicUser()

var newUsers = new[]
{
new { id = 20, name = "A1", age = 55 },
new { id = 21, name = "A2", age = 55 },
new { id = 22, name = "A3", age = 55 }
};
new { id = 20, name = "A1", age = 55 },
new { id = 21, name = "A2", age = 55 },
new { id = 22, name = "A3", age = 55 }
};

await collection.InsertManyAsync(newUsers);

Expand Down Expand Up @@ -445,19 +445,20 @@ public async Task UpdateManyAsync_JsonUser()
Assert.Equal(3, collection.Count);

var newUsersJson = @"
[
{ 'id': 20, 'name': 'A1', 'age': 55 },
{ 'id': 21, 'name': 'A2', 'age': 55 },
{ 'id': 22, 'name': 'A3', 'age': 55 }
]
";
[
{ ""id"": 20, ""name"": ""A1"", ""age"": 55 },
{ ""id"": 21, ""name"": ""A2"", ""age"": 55 },
{ ""id"": 22, ""name"": ""A3"", ""age"": 55 }
]
";

var newUsers = JToken.Parse(newUsersJson);
var newUsersArray = JsonNode.Parse(newUsersJson).AsArray();
var newUsers = newUsersArray.Select(n => n as dynamic);

await collection.InsertManyAsync(newUsers);

var newUserJson = "{ 'id': 23, 'name': 'A4', 'age': 22 }";
var newUser = JToken.Parse(newUserJson);
var newUserJson = "{ \"id\": 23, \"name\": \"A4\", \"age\": 22 }";
var newUser = JsonNode.Parse(newUserJson);

await collection.InsertOneAsync(newUser);

Expand Down Expand Up @@ -494,10 +495,10 @@ public void UpdateMany_TypedUser()

var newUsers = new[]
{
new User { Id = 20, Name = "A1", Age = 55 },
new User { Id = 21, Name = "A2", Age = 55 },
new User { Id = 22, Name = "A3", Age = 55 }
};
new User { Id = 20, Name = "A1", Age = 55 },
new User { Id = 21, Name = "A2", Age = 55 },
new User { Id = 22, Name = "A3", Age = 55 }
};

collection.InsertMany(newUsers);

Expand Down Expand Up @@ -623,7 +624,7 @@ public void ReplaceOne_Upsert_DynamicWithInnerData()
var collection = store.GetCollection("sensor");

var success = collection.ReplaceOne(e => e.id == 11,
JToken.Parse("{ 'id': 11, 'mac': 'F4:A5:74:89:16:57', 'data': { 'temperature': 20.5 } }"),
JsonNode.Parse("{ \"id\": 11, \"mac\": \"F4:A5:74:89:16:57\", \"data\": { \"temperature\": 20.5 } }"),
true);
Assert.True(success);

Expand Down Expand Up @@ -884,13 +885,14 @@ public void UpdateOne_InnerExpandos()
collection.InsertOne(user);

var patchData = new Dictionary<string, object>
{
{ "Age", 41 },
{ "name", "James" },
{ "Work", new Dictionary<string, object> { { "Name", "ACME" } } }
};
var jobject = JObject.FromObject(patchData);
dynamic patchExpando = JsonConvert.DeserializeObject<ExpandoObject>(jobject.ToString());
{
{ "Age", 41 },
{ "name", "James" },
{ "Work", new Dictionary<string, object> { { "Name", "ACME" } } }
};
var jsonString = JsonSerializer.Serialize(patchData);
var options = new JsonSerializerOptions { Converters = { new SystemExpandoObjectConverter() } };
dynamic patchExpando = JsonSerializer.Deserialize<ExpandoObject>(jsonString, options);

collection.UpdateOne(i => i.Id == 4, patchExpando as object);

Expand Down Expand Up @@ -1043,25 +1045,29 @@ public async Task UpdateComplexObject_Dynamic()

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

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

var jObj = new JObject()
var data = new
{
["custom_id"] = 11,
["nestedArray"] = new JArray { ja },
custom_id = 11,
nestedArray = new[]
{
new[] { "Hello World!" }
}
};

await collection.InsertOneAsync(jObj);
await collection.InsertOneAsync(data);

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.nestedArray[0][0]);

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

await collection.UpdateOneAsync(e => e.custom_id == 11, update);
Expand Down
14 changes: 7 additions & 7 deletions JsonFlatFileDataStore.Test/CollectionQueryTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Newtonsoft.Json.Linq;
using System.Text.Json.Nodes;

namespace JsonFlatFileDataStore.Test;

Expand Down Expand Up @@ -132,22 +132,22 @@ public void GetNextIdValue_StringType_JToken()
var collection = store.GetCollection("collectionWithStringId");

// Insert seed value with upsert
collection.ReplaceOne(e => e, JToken.Parse("{ 'myId': 'test1' }"), true);
collection.ReplaceOne(e => e, JsonNode.Parse("{ \"myId\": \"test1\" }"), true);

var nextId = collection.GetNextIdValue();
Assert.Equal("test2", nextId);

var nextUpdate = JToken.Parse("{ 'myId': 'somethingWrong2' }");
var nextUpdate = JsonNode.Parse("{ \"myId\": \"somethingWrong2\" }");
collection.InsertOne(nextUpdate);
Assert.Equal(nextId, nextUpdate["myId"]);
Assert.Equal(nextId, nextUpdate["myId"].ToString());

nextId = collection.GetNextIdValue();
Assert.Equal("test3", nextId);

nextUpdate = JToken.Parse("{ 'xxx': 111 }");
nextUpdate = JsonNode.Parse("{ \"xxx\": 111 }");
collection.InsertOne(nextUpdate);
Assert.Equal(nextId, nextUpdate["myId"]);
Assert.Equal(111, nextUpdate["xxx"]);
Assert.Equal(nextId, nextUpdate["myId"].GetValue<string>());
Assert.Equal(111, nextUpdate["xxx"].GetValue<int>());

nextId = collection.GetNextIdValue();
Assert.Equal("test4", nextId);
Expand Down
64 changes: 33 additions & 31 deletions JsonFlatFileDataStore.Test/CopyPropertiesTests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Dynamic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Text.Json;
using Xunit;

namespace JsonFlatFileDataStore.Test;

Expand Down Expand Up @@ -73,10 +73,10 @@ public void CopyProperties_TypedFamily()
var family = new Family
{
Parents = new List<Parent>
{
new Parent { Name = "Jim", Age = 52 },
new Parent { Name = "Theodor", Age = 14 }
},
{
new Parent { Name = "Jim", Age = 52 },
new Parent { Name = "Theodor", Age = 14 }
},
Address = new Address { City = "Helsinki" }
};

Expand Down Expand Up @@ -123,10 +123,10 @@ public void CopyProperties_DynamicFamily()
sParent.Age = 14;

family.Parents = new List<ExpandoObject>
{
fParent,
sParent,
};
{
fParent,
sParent,
};

family.Address = new ExpandoObject();
family.Address.City = "Helsinki";
Expand Down Expand Up @@ -228,9 +228,9 @@ public void CopyProperties_TypedFamilyParents()
var family = new Family
{
Parents = new List<Parent>
{
new Parent { Name = "Jim", Age = 52 }
},
{
new Parent { Name = "Jim", Age = 52 }
},
Address = new Address { City = "Helsinki" }
};

Expand All @@ -251,13 +251,14 @@ public void CopyProperties_DynamicWithInnerExpandos()
user.work = work;

var patchData = new Dictionary<string, object>
{
{ "age", 41 },
{ "name", "James" },
{ "work", new Dictionary<string, object> { { "name", "ACME" } } }
};
var jobject = JObject.FromObject(patchData);
dynamic patchExpando = JsonConvert.DeserializeObject<ExpandoObject>(jobject.ToString());
{
{ "age", 41 },
{ "name", "James" },
{ "work", new Dictionary<string, object> { { "name", "ACME" } } }
};
var jsonString = JsonSerializer.Serialize(patchData);
var options = new JsonSerializerOptions { Converters = { new SystemExpandoObjectConverter() } };
dynamic patchExpando = JsonSerializer.Deserialize<ExpandoObject>(jsonString, options);

ObjectExtensions.CopyProperties(patchExpando, user);
Assert.Equal("James", user.name);
Expand Down Expand Up @@ -296,10 +297,10 @@ public void CopyProperties_DynamicEmptyWithInnerDictionary()
sensor.mac = "F4:A5:74:89:16:57";
sensor.timestamp = null;
sensor.data = new Dictionary<string, object>
{
{ "temperature", 24.3 },
{ "identifier", null }
};
{
{ "temperature", 24.3 },
{ "identifier", null }
};

ObjectExtensions.CopyProperties(sensor, destination);

Expand All @@ -319,13 +320,14 @@ public void CopyProperties_TypedWithInnerExpandos()
};

var patchData = new Dictionary<string, object>
{
{ "Age", 41 },
{ "Name", "James" },
{ "Work", new Dictionary<string, object> { { "Name", "ACME" } } }
};
var jobject = JObject.FromObject(patchData);
dynamic patchExpando = JsonConvert.DeserializeObject<ExpandoObject>(jobject.ToString());
{
{ "Age", 41 },
{ "Name", "James" },
{ "Work", new Dictionary<string, object> { { "Name", "ACME" } } }
};
var jsonString = JsonSerializer.Serialize(patchData);
var options = new JsonSerializerOptions { Converters = { new SystemExpandoObjectConverter() } };
dynamic patchExpando = JsonSerializer.Deserialize<ExpandoObject>(jsonString, options);

ObjectExtensions.CopyProperties(patchExpando, user);
Assert.Equal("James", user.Name);
Expand Down
Loading
Loading