Skip to content

Commit

Permalink
fix: handle guid id-field correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
ttu committed Dec 21, 2023
1 parent b9b77f0 commit fa10e48
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
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; }

}
}
3 changes: 3 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 Down

0 comments on commit fa10e48

Please sign in to comment.