Skip to content

Commit 670f8a1

Browse files
authored
Feature/more tests (#55)
* Fix nullable values handlings and add tests * Add more JSON tests
1 parent 17cca52 commit 670f8a1

6 files changed

Lines changed: 40 additions & 10 deletions

File tree

src/PhenX.EntityFrameworkCore.BulkInsert/Metadata/ColumnMetadata.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ internal sealed class ColumnMetadata(IProperty property, SqlDialectBuilder dial
2424

2525
public bool IsGenerated { get; } = property.ValueGenerated != ValueGenerated.Never;
2626

27-
public object? GetValue(object entity, BulkInsertOptions options)
27+
public object GetValue(object entity, BulkInsertOptions options)
2828
{
2929
var result = _getter(entity);
3030

@@ -40,7 +40,7 @@ internal sealed class ColumnMetadata(IProperty property, SqlDialectBuilder dial
4040
}
4141
}
4242

43-
return result;
43+
return result ?? DBNull.Value;
4444
}
4545

4646
private static Func<object, object?> BuildGetter(IProperty property)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace PhenX.EntityFrameworkCore.BulkInsert.Tests.DbContext;
2+
3+
public class JsonDbObject
4+
{
5+
public int Code { get; set; }
6+
7+
public string Name { get; set; } = string.Empty;
8+
}

tests/PhenX.EntityFrameworkCore.BulkInsert.Tests/DbContext/TestDbContext.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
3636

3737
modelBuilder.Entity<TestEntityWithJson>(b =>
3838
{
39-
b.Property(x => x.Json).AsJsonString("jsonb");
39+
b.Property(x => x.JsonArray).AsJsonString("jsonb");
40+
b.Property(x => x.JsonObject).AsJsonString("jsonb");
4041
});
4142

4243
modelBuilder.Entity<TestEntity>(b =>
@@ -54,7 +55,8 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
5455

5556
modelBuilder.Entity<TestEntityWithJson>(b =>
5657
{
57-
b.Property(x => x.Json).AsJsonString("json");
58+
b.Property(x => x.JsonArray).AsJsonString("json");
59+
b.Property(x => x.JsonObject).AsJsonString("json");
5860
});
5961

6062
modelBuilder.Entity<TestEntity>(b =>
@@ -72,7 +74,8 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
7274

7375
modelBuilder.Entity<TestEntityWithJson>(b =>
7476
{
75-
b.Property(x => x.Json).AsJsonString(null);
77+
b.Property(x => x.JsonArray).AsJsonString(null);
78+
b.Property(x => x.JsonObject).AsJsonString(null);
7679
});
7780

7881
modelBuilder.Entity<TestEntity>(b =>
@@ -90,7 +93,8 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
9093

9194
modelBuilder.Entity<TestEntityWithJson>(b =>
9295
{
93-
b.Property(x => x.Json).AsJsonString(null);
96+
b.Property(x => x.JsonArray).AsJsonString(null);
97+
b.Property(x => x.JsonObject).AsJsonString(null);
9498
});
9599

96100
modelBuilder.Entity<TestEntity>(b =>
@@ -108,7 +112,8 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
108112

109113
modelBuilder.Entity<TestEntityWithJson>(b =>
110114
{
111-
b.Property(x => x.Json).AsJsonString(null);
115+
b.Property(x => x.JsonArray).AsJsonString(null);
116+
b.Property(x => x.JsonObject).AsJsonString(null);
112117
});
113118

114119
modelBuilder.Entity<TestEntity>(b =>

tests/PhenX.EntityFrameworkCore.BulkInsert.Tests/DbContext/TestEntity.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ public class TestEntity : TestEntityBase
2222
[Column("the_identifier")]
2323
public Guid Identifier { get; set; }
2424

25+
[Column("nullable_identifier")]
26+
public Guid? NullableIdentifier { get; set; }
27+
28+
public DateTime Created { get; set; }
29+
30+
public DateTime? Modified { get; set; }
31+
2532
[Column("string_enum_value")]
2633
public StringEnum StringEnumValue { get; set; }
2734

tests/PhenX.EntityFrameworkCore.BulkInsert.Tests/DbContext/TestEntityWithJson.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@ public class TestEntityWithJson : TestEntityBase
99
[Key]
1010
public int Id { get; set; }
1111

12-
public List<int> Json { get; set; } = [];
12+
public List<int> JsonArray { get; set; } = [];
13+
14+
public JsonDbObject JsonObject { get; set; }
1315
}

tests/PhenX.EntityFrameworkCore.BulkInsert.Tests/Tests/Basic/BasicTestsBase.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,16 @@ public async Task InsertEntities_WithJson(InsertStrategy strategy)
5454
// Arrange
5555
var entities = new List<TestEntityWithJson>
5656
{
57-
new TestEntityWithJson { Json = [1] },
58-
new TestEntityWithJson { Json = [2] }
57+
new TestEntityWithJson
58+
{
59+
JsonArray = [1],
60+
JsonObject = new JsonDbObject { Code = 1, Name = "Test1" },
61+
},
62+
new TestEntityWithJson
63+
{
64+
JsonArray = [2],
65+
JsonObject = new JsonDbObject { Code = 2, Name = "Test2" },
66+
},
5967
};
6068

6169
// Act

0 commit comments

Comments
 (0)