Skip to content

Commit 09f9b34

Browse files
authored
test: add duplicate property test and fix xunit warnings (#112)
1 parent a94829d commit 09f9b34

File tree

3 files changed

+52
-12
lines changed

3 files changed

+52
-12
lines changed

JsonFlatFileDataStore.Test/DataStoreDisposeTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public async Task DataStore_Dispose(string testName, bool useDispose)
4141
await Task.Delay(1000);
4242

4343
if (sw.ElapsedMilliseconds > maxTimeMs)
44-
Assert.False(true, "Timeout");
44+
Assert.Fail("Timeout");
4545
}
4646

4747
while (useDispose == storeRef.IsAlive)
@@ -50,7 +50,7 @@ public async Task DataStore_Dispose(string testName, bool useDispose)
5050
GC.Collect();
5151

5252
if (sw.ElapsedMilliseconds > maxTimeMs)
53-
Assert.False(true, "Timeout");
53+
Assert.Fail("Timeout");
5454
}
5555

5656
// If DataStore is not disposed, it should still be alive

JsonFlatFileDataStore.Test/DataStoreTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public void GetCollection_TypedCollection_NameParameter()
8686

8787
var collection = store.GetCollection<Movie>("movies");
8888
Assert.Equal(2, collection.Count);
89-
Assert.Single(collection.AsQueryable().Where(e => e.Name == "Predator"));
89+
Assert.Single(collection.AsQueryable(), e => e.Name == "Predator");
9090

9191
UTHelpers.Down(newFilePath);
9292
}
@@ -111,7 +111,7 @@ public void GetCollection_Mocked()
111111
var result = collection.InsertOne(new Movie { Name = "Predator" });
112112
Assert.True(result);
113113
Assert.Equal(2, collection.Count);
114-
Assert.Single(collection.AsQueryable().Where(e => e.Name == "Predator"));
114+
Assert.Single(collection.AsQueryable(), e => e.Name == "Predator");
115115
}
116116

117117
[Fact]
@@ -223,9 +223,9 @@ public async Task Readme_Example2()
223223
var results = collection.AsQueryable().Where(x => x.age < 40);
224224

225225
Assert.True(results.Count() == 3);
226-
Assert.Single(results.Where(e => e.name == "Raymond Doe"));
227-
Assert.Single(results.Where(e => e.name == "Andy Doe"));
228-
Assert.Single(results.Where(e => e.name == "Karl"));
226+
Assert.Single(results, e => e.name == "Raymond Doe");
227+
Assert.Single(results, e => e.name == "Andy Doe");
228+
Assert.Single(results, e => e.name == "Karl");
229229

230230
UTHelpers.Down(pathToJson);
231231
}
@@ -400,7 +400,7 @@ public async Task Readme_Example3()
400400
// Use LINQ to query items
401401
var results = collection.AsQueryable().Where(e => e.Age > 30);
402402

403-
Assert.Single(results.Where(e => e.Name == "John Doe"));
403+
Assert.Single(results, e => e.Name == "John Doe");
404404

405405
UTHelpers.Down(pathToJson);
406406
}

JsonFlatFileDataStore.Test/FileContentTests.cs

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
2+
using System.Dynamic;
23
using System.IO;
4+
using System.Linq;
35
using System.Text.RegularExpressions;
46
using System.Threading.Tasks;
57
using Xunit;
@@ -15,11 +17,11 @@ public void FileNotFound_CreateNewFile()
1517

1618
var storeFileNotFound = new DataStore(path);
1719
var collectionKeys = storeFileNotFound.GetKeys();
18-
Assert.Equal(0, collectionKeys.Count);
20+
Assert.Empty(collectionKeys);
1921

2022
var storeFileFound = new DataStore(path);
2123
var collectionKeysFileFound = storeFileFound.GetKeys();
22-
Assert.Equal(0, collectionKeysFileFound.Count);
24+
Assert.Empty(collectionKeysFileFound);
2325

2426
UTHelpers.Down(path);
2527
}
@@ -73,11 +75,11 @@ public void Encrypted_FileNotFound_CreateNewFile()
7375

7476
var storeFileNotFound = new DataStore(path, encryptionKey: "53cr3t");
7577
var collectionKeys = storeFileNotFound.GetKeys();
76-
Assert.Equal(0, collectionKeys.Count);
78+
Assert.Empty(collectionKeys);
7779

7880
var storeFileFound = new DataStore(path, encryptionKey: "53cr3t");
7981
var collectionKeysFileFound = storeFileFound.GetKeys();
80-
Assert.Equal(0, collectionKeysFileFound.Count);
82+
Assert.Empty(collectionKeysFileFound);
8183

8284
UTHelpers.Down(path);
8385
}
@@ -225,6 +227,44 @@ public async Task WriteToFile_UpperCamelCase()
225227
UTHelpers.Down(newFilePath);
226228
}
227229

230+
[Fact]
231+
public async Task DynamicCollection_UpdateWrongCase_NoDuplicateProperties()
232+
{
233+
var newFilePath = UTHelpers.Up();
234+
235+
var store = new DataStore(newFilePath);
236+
237+
var collection = store.GetCollection("user");
238+
Assert.Equal(3, collection.Count);
239+
240+
await collection.InsertOneAsync(new { id = 11, name = "Teddy", age = 21 });
241+
242+
// Update with wrong case - capital A in Age
243+
dynamic source = new ExpandoObject();
244+
source.Age = 22;
245+
var updateResult = await collection.UpdateOneAsync(e => e.id == 11, source as object);
246+
Assert.True(updateResult);
247+
248+
// Read the raw JSON and verify no duplicate properties with different casing
249+
var json = File.ReadAllText(newFilePath);
250+
var ageCountLower = Regex.Matches(json, "\"age\"").Count;
251+
var ageCountUpper = Regex.Matches(json, "\"Age\"").Count;
252+
253+
// Property 'age' (lowercase) should exist in the JSON
254+
Assert.True(ageCountLower > 0);
255+
// Property 'Age' (uppercase) should NOT exist - no duplicate properties with different casing
256+
Assert.Equal(0, ageCountUpper);
257+
258+
// Verify the value is correct
259+
var store2 = new DataStore(newFilePath);
260+
var collection2 = store2.GetCollection("user");
261+
var updated = collection2.Find(e => e.id == 11).First();
262+
Assert.Equal(22, updated.age);
263+
Assert.Equal("Teddy", updated.name);
264+
265+
UTHelpers.Down(newFilePath);
266+
}
267+
228268
public class Employee
229269
{
230270
public int Id { get; set; }

0 commit comments

Comments
 (0)