Skip to content

Commit

Permalink
feat: support guid id-fields
Browse files Browse the repository at this point in the history
  • Loading branch information
ttu committed Dec 21, 2023
1 parent b9b77f0 commit 9d9a1d8
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 3 deletions.
28 changes: 27 additions & 1 deletion JsonFlatFileDataStore.Test/CollectionQueryTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Linq;
using System;
using System.Linq;
using Newtonsoft.Json.Linq;
using Xunit;

Expand Down Expand Up @@ -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<Guid>(nextId);

collection.InsertOne(new { helloField = nextId });

nextId = collection.GetNextIdValue();
Assert.IsType<Guid>(nextId);
}

[Fact]
public void GetNextIdValue_StringType_AnonymousType()
{
Expand Down
32 changes: 30 additions & 2 deletions JsonFlatFileDataStore.Test/DataStoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ public void FileNotFound_CreateNewFile_Encrypted()

UTHelpers.Down(path);
}

[Fact]
public void File_Has_Correct_PropertyNames_DynamicCollection()
{
Expand Down Expand Up @@ -604,7 +604,7 @@ public void File_Has_Correct_PropertyNames_TypedCollection()
var assertCollection = store.GetCollection<Employee>();
Assert.Equal(3, assertCollection.Count);
}

[Fact]
public void File_Has_Correct_PropertyNames_Single_Item()
{
Expand All @@ -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<TestModelWithGuid>();

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; }
Expand Down
10 changes: 10 additions & 0 deletions JsonFlatFileDataStore.Test/TestModels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

}
}
6 changes: 6 additions & 0 deletions JsonFlatFileDataStore/DocumentCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,9 @@ private bool ExecuteLocked(Func<List<T>, bool> func, List<T> data)
if (primaryKeyValue is Int64)
return (int)primaryKeyValue;

if (Guid.TryParse(primaryKeyValue, out Guid guidValue))
return guidValue;

return primaryKeyValue;
}
}
Expand All @@ -411,6 +414,9 @@ private bool ExecuteLocked(Func<List<T>, bool> func, List<T> data)
if (keyValue is Int64)
return (int)keyValue + 1;

if (keyValue is Guid)
return Guid.NewGuid();

return ParseNextIntegerToKeyValue(keyValue.ToString());
}

Expand Down

0 comments on commit 9d9a1d8

Please sign in to comment.