11using System ;
2+ using System . Dynamic ;
23using System . IO ;
4+ using System . Linq ;
35using System . Text . RegularExpressions ;
46using System . Threading . Tasks ;
57using 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