diff --git a/JsonFlatFileDataStore.Test/CollectionQueryTests.cs b/JsonFlatFileDataStore.Test/CollectionQueryTests.cs index 24dffdd..3606060 100644 --- a/JsonFlatFileDataStore.Test/CollectionQueryTests.cs +++ b/JsonFlatFileDataStore.Test/CollectionQueryTests.cs @@ -1,4 +1,5 @@ -using System.Linq; +using System; +using System.Linq; using Newtonsoft.Json.Linq; using Xunit; @@ -81,6 +82,31 @@ public void GetNextIdValue_StringType() Assert.Equal("SomeValue1", nextId); } + [Fact] + public void GetNextIdValue_GuidType() + { + var newFilePath = UTHelpers.Up(); + + var store = new DataStore(newFilePath, keyProperty: "helloField"); + + var collection = store.GetCollection("collectionWithGuidId"); + + var shouldBeNone = collection.GetNextIdValue(); + Assert.Equal(0, shouldBeNone); + + collection.InsertOne(new { helloField = Guid.NewGuid() }); + + var inserted = collection.AsQueryable().First(); + + var nextId = collection.GetNextIdValue(); + Assert.IsType(nextId); + + collection.InsertOne(new { helloField = nextId }); + + nextId = collection.GetNextIdValue(); + Assert.IsType(nextId); + } + [Fact] public void GetNextIdValue_StringType_AnonymousType() { diff --git a/JsonFlatFileDataStore.Test/DataStoreTests.cs b/JsonFlatFileDataStore.Test/DataStoreTests.cs index f994b03..d741f80 100644 --- a/JsonFlatFileDataStore.Test/DataStoreTests.cs +++ b/JsonFlatFileDataStore.Test/DataStoreTests.cs @@ -556,7 +556,7 @@ public void FileNotFound_CreateNewFile_Encrypted() UTHelpers.Down(path); } - + [Fact] public void File_Has_Correct_PropertyNames_DynamicCollection() { @@ -604,7 +604,7 @@ public void File_Has_Correct_PropertyNames_TypedCollection() var assertCollection = store.GetCollection(); Assert.Equal(3, assertCollection.Count); } - + [Fact] public void File_Has_Correct_PropertyNames_Single_Item() { @@ -621,6 +621,34 @@ public void File_Has_Correct_PropertyNames_Single_Item() Assert.Equal(1, propCount); } + [Fact] + public void Model_With_Guids() + { + var pathToJson = UTHelpers.Up(); + + var store = new DataStore(pathToJson); + + var typedCollection = store.GetCollection(); + + var itemId = Guid.NewGuid(); + var secondGuid = Guid.NewGuid(); + var thirdGuid = Guid.NewGuid(); + + typedCollection.InsertOne(new TestModelWithGuid { Id = itemId, Name = "Jim", OtherGuid = secondGuid }); + typedCollection.ReplaceOne(e => e.Id == itemId, new TestModelWithGuid { Id = itemId, Name = "Barry", OtherGuid = secondGuid }); + typedCollection.UpdateOne(e => e.Id == itemId, new { Name = "Sandels" }); + typedCollection.UpdateOne(e => e.Id == itemId, new { OtherGuid = thirdGuid }); + + var userTyped = typedCollection + .AsQueryable() + .Single(p => p.Name == "Sandels"); + + Assert.Equal("Sandels", userTyped.Name); + Assert.Equal(thirdGuid, userTyped.OtherGuid); + + UTHelpers.Down(pathToJson); + } + public class Employee { public int Id { get; set; } diff --git a/JsonFlatFileDataStore.Test/TestModels.cs b/JsonFlatFileDataStore.Test/TestModels.cs index 33618b6..e6b2459 100644 --- a/JsonFlatFileDataStore.Test/TestModels.cs +++ b/JsonFlatFileDataStore.Test/TestModels.cs @@ -163,4 +163,14 @@ public class World public float CameraRotationY { get; set; } } + + public class TestModelWithGuid + { + public Guid Id { get; set; } + + public string Name { get; set; } + + public Guid OtherGuid { get; set; } + + } } \ No newline at end of file diff --git a/JsonFlatFileDataStore/DocumentCollection.cs b/JsonFlatFileDataStore/DocumentCollection.cs index 469cdc9..bac9ff0 100644 --- a/JsonFlatFileDataStore/DocumentCollection.cs +++ b/JsonFlatFileDataStore/DocumentCollection.cs @@ -394,6 +394,9 @@ private bool ExecuteLocked(Func, bool> func, List data) if (primaryKeyValue is Int64) return (int)primaryKeyValue; + if (Guid.TryParse(primaryKeyValue, out Guid guidValue)) + return guidValue; + return primaryKeyValue; } } @@ -411,6 +414,9 @@ private bool ExecuteLocked(Func, bool> func, List data) if (keyValue is Int64) return (int)keyValue + 1; + if (keyValue is Guid) + return Guid.NewGuid(); + return ParseNextIntegerToKeyValue(keyValue.ToString()); }