Skip to content

Commit e243c66

Browse files
author
fabien.menager
committed
Add tests for all simple types
1 parent 3cc19b3 commit e243c66

6 files changed

Lines changed: 166 additions & 44 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace PhenX.EntityFrameworkCore.BulkInsert.Tests.DbContext;
66
public class TestDbContext : TestDbContextBase
77
{
88
public DbSet<TestEntity> TestEntities { get; set; } = null!;
9+
public DbSet<TestEntityWithSimpleTypes> TestEntitiesWithSimpleTypes { get; set; } = null!;
910
public DbSet<TestEntityWithJson> TestEntitiesWithJson { get; set; } = null!;
1011
public DbSet<TestEntityWithGuidId> TestEntitiesWithGuidId { get; set; } = null!;
1112
public DbSet<TestEntityWithConverters> TestEntitiesWithConverter { get; set; } = null!;

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,12 @@ public class TestEntity : TestEntityBase
1919
[Column("some_price")]
2020
public decimal Price { get; set; }
2121

22-
[Column("some_float")]
23-
public float Float { get; set; } = 10.1f;
24-
25-
[Column("some_double")]
26-
public double Double { get; set; } = 10.1d;
27-
2822
[Column("the_identifier")]
2923
public Guid Identifier { get; set; }
3024

3125
[Column("nullable_identifier")]
3226
public Guid? NullableIdentifier { get; set; }
3327

34-
public DateTime Created { get; set; }
35-
36-
public DateTime? Modified { get; set; }
37-
3828
[Column("string_enum_value")]
3929
public StringEnum StringEnumValue { get; set; }
4030

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System.ComponentModel.DataAnnotations.Schema;
2+
3+
using Microsoft.EntityFrameworkCore;
4+
5+
namespace PhenX.EntityFrameworkCore.BulkInsert.Tests.DbContext;
6+
7+
[PrimaryKey(nameof(Id))]
8+
[Table("test_entity_with_simple_types")]
9+
public class TestEntityWithSimpleTypes : TestEntityBase
10+
{
11+
public int Id { get; set; }
12+
13+
public required string StringValue { get; set; } = string.Empty;
14+
15+
public required bool BoolValue { get; set; }
16+
17+
public required byte ByteValue { get; set; }
18+
public required byte[]? ByteArrayValue { get; set; }
19+
public required sbyte SByteValue { get; set; }
20+
public required char CharValue { get; set; }
21+
22+
public required short ShortValue { get; set; }
23+
public required ushort UShortValue { get; set; }
24+
25+
public required int IntValue { get; set; }
26+
public required uint UIntValue { get; set; }
27+
28+
public required long LongValue { get; set; }
29+
public required ulong ULongValue { get; set; }
30+
31+
public required float FloatValue { get; set; }
32+
public required double DoubleValue { get; set; }
33+
public required decimal DecimalValue { get; set; }
34+
35+
public required DateTime DateTimeValue { get; set; }
36+
public required DateOnly DateOnlyValue { get; set; }
37+
public required TimeOnly TimeOnlyValue { get; set; }
38+
public required TimeSpan TimeSpanValue { get; set; }
39+
public required DateTimeOffset DateTimeOffsetValue { get; set; }
40+
41+
public required Guid GuidValue { get; set; }
42+
}

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

Lines changed: 112 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using FluentAssertions;
2+
using FluentAssertions.Extensions;
23

34
using PhenX.EntityFrameworkCore.BulkInsert.Enums;
45
using PhenX.EntityFrameworkCore.BulkInsert.Extensions;
@@ -35,8 +36,8 @@ public async Task InsertsEntities(InsertStrategy strategy)
3536
// Arrange
3637
var entities = new List<TestEntity>
3738
{
38-
new TestEntity { Name = $"{_run}_Entity1" },
39-
new TestEntity { Name = $"{_run}_Entity2" }
39+
new TestEntity { TestRun = _run, Name = $"{_run}_Entity1" },
40+
new TestEntity { TestRun = _run, Name = $"{_run}_Entity2" }
4041
};
4142

4243
// Act
@@ -56,11 +57,13 @@ public async Task InsertEntities_WithJson(InsertStrategy strategy)
5657
{
5758
new TestEntityWithJson
5859
{
60+
TestRun = _run,
5961
JsonArray = [1],
6062
JsonObject = new JsonDbObject { Code = 1, Name = "Test1" },
6163
},
6264
new TestEntityWithJson
6365
{
66+
TestRun = _run,
6467
JsonArray = [2],
6568
JsonObject = new JsonDbObject { Code = 2, Name = "Test2" },
6669
},
@@ -109,8 +112,8 @@ public async Task InsertEntities_MoveRows(InsertStrategy strategy)
109112
// Arrange
110113
var entities = new List<TestEntity>
111114
{
112-
new TestEntity { Name = $"{_run}_Entity1" },
113-
new TestEntity { Name = $"{_run}_Entity2" }
115+
new TestEntity { TestRun = _run, Name = $"{_run}_Entity1" },
116+
new TestEntity { TestRun = _run, Name = $"{_run}_Entity2" }
114117
};
115118

116119
// Act
@@ -149,6 +152,7 @@ public async Task InsertEntities_Many(InsertStrategy strategy)
149152
var entities = Enumerable.Range(1, count).Select(i => new TestEntity
150153
{
151154
Identifier = Guid.NewGuid(),
155+
TestRun = _run,
152156
Name = $"{_run}_Entity{i}",
153157
NumericEnumValue = (NumericEnum)(i % 2),
154158
Price = (decimal)(i * 0.1),
@@ -176,8 +180,8 @@ public async Task InsertEntities_AndReturn_WithEntityWithValueConverters(InsertS
176180

177181
var entities = new List<TestEntityWithConverters>
178182
{
179-
new TestEntityWithConverters() { Name = $"{_run}_Entity1", CreatedAt = now, Uri = null },
180-
new TestEntityWithConverters() { Name = $"{_run}_Entity2", CreatedAt = now.AddDays(-1), Uri = new Uri("http://example.com/test") }
183+
new TestEntityWithConverters() { TestRun = _run, Name = $"{_run}_Entity1", CreatedAt = now, Uri = null },
184+
new TestEntityWithConverters() { TestRun = _run, Name = $"{_run}_Entity2", CreatedAt = now.AddDays(-1), Uri = new Uri("http://example.com/test") }
181185
};
182186

183187
// Act
@@ -240,8 +244,8 @@ public async Task ThrowsWhenUsingWrongConfigurationType()
240244
// Arrange
241245
var entities = new List<TestEntity>
242246
{
243-
new TestEntity { Name = $"{_run}_Entity1" },
244-
new TestEntity { Name = $"{_run}_Entity2" }
247+
new TestEntity { TestRun = _run, Name = $"{_run}_Entity1" },
248+
new TestEntity { TestRun = _run, Name = $"{_run}_Entity2" }
245249
};
246250

247251
// Act & Assert
@@ -277,8 +281,8 @@ public async Task InsertEntities_WithGeneratedGuidId(InsertStrategy strategy)
277281
// Arrange
278282
var entities = new List<TestEntityWithGuidId>
279283
{
280-
new TestEntityWithGuidId { Id = Guid.NewGuid(), Name = $"{_run}_Entity1" },
281-
new TestEntityWithGuidId { Id = Guid.NewGuid(), Name = $"{_run}_Entity2" }
284+
new TestEntityWithGuidId { TestRun = _run, Id = Guid.NewGuid(), Name = $"{_run}_Entity1" },
285+
new TestEntityWithGuidId { TestRun = _run, Id = Guid.NewGuid(), Name = $"{_run}_Entity2" }
282286
};
283287

284288
// Act
@@ -299,16 +303,16 @@ public async Task HandleProgress(InsertStrategy strategy)
299303
// Arrange
300304
var entities = new List<TestEntity>
301305
{
302-
new TestEntity { Name = $"{_run}_Entity1" },
303-
new TestEntity { Name = $"{_run}_Entity2" },
304-
new TestEntity { Name = $"{_run}_Entity3" },
305-
new TestEntity { Name = $"{_run}_Entity4" },
306-
new TestEntity { Name = $"{_run}_Entity5" },
307-
new TestEntity { Name = $"{_run}_Entity6" },
308-
new TestEntity { Name = $"{_run}_Entity7" },
309-
new TestEntity { Name = $"{_run}_Entity8" },
310-
new TestEntity { Name = $"{_run}_Entity9" },
311-
new TestEntity { Name = $"{_run}_Entity10" },
306+
new TestEntity { TestRun = _run, Name = $"{_run}_Entity1" },
307+
new TestEntity { TestRun = _run, Name = $"{_run}_Entity2" },
308+
new TestEntity { TestRun = _run, Name = $"{_run}_Entity3" },
309+
new TestEntity { TestRun = _run, Name = $"{_run}_Entity4" },
310+
new TestEntity { TestRun = _run, Name = $"{_run}_Entity5" },
311+
new TestEntity { TestRun = _run, Name = $"{_run}_Entity6" },
312+
new TestEntity { TestRun = _run, Name = $"{_run}_Entity7" },
313+
new TestEntity { TestRun = _run, Name = $"{_run}_Entity8" },
314+
new TestEntity { TestRun = _run, Name = $"{_run}_Entity9" },
315+
new TestEntity { TestRun = _run, Name = $"{_run}_Entity10" },
312316
};
313317

314318
long progressCount = 0;
@@ -337,10 +341,10 @@ public async Task HandleNoProgress(InsertStrategy strategy)
337341
// Arrange
338342
var entities = new List<TestEntity>
339343
{
340-
new TestEntity { Name = $"{_run}_Entity1" },
341-
new TestEntity { Name = $"{_run}_Entity2" },
342-
new TestEntity { Name = $"{_run}_Entity3" },
343-
new TestEntity { Name = $"{_run}_Entity4" },
344+
new TestEntity { TestRun = _run, Name = $"{_run}_Entity1" },
345+
new TestEntity { TestRun = _run, Name = $"{_run}_Entity2" },
346+
new TestEntity { TestRun = _run, Name = $"{_run}_Entity3" },
347+
new TestEntity { TestRun = _run, Name = $"{_run}_Entity4" },
344348
};
345349

346350
var callCount = 0;
@@ -355,4 +359,88 @@ await _context.InsertWithStrategyAsync(strategy, entities, o =>
355359
// Assert
356360
Assert.Equal(0, callCount);
357361
}
362+
363+
[SkippableTheory]
364+
[CombinatorialData]
365+
public async Task InsertEntities_WithAllSimpleTypes(InsertStrategy strategy)
366+
{
367+
// Arrange
368+
var entities = new List<TestEntityWithSimpleTypes>
369+
{
370+
new TestEntityWithSimpleTypes
371+
{
372+
TestRun = _run,
373+
Id = 1,
374+
BoolValue = true,
375+
ByteValue = 1,
376+
ByteArrayValue =
377+
[
378+
1,
379+
2,
380+
3
381+
],
382+
SByteValue = -1,
383+
ShortValue = 2,
384+
IntValue = 3,
385+
LongValue = 4,
386+
FloatValue = 5.5f,
387+
DoubleValue = 6.6,
388+
DecimalValue = 7.7m,
389+
DateTimeValue = DateTime.UtcNow,
390+
DateTimeOffsetValue = DateTimeOffset.UtcNow,
391+
TimeSpanValue = TimeSpan.FromHours(1),
392+
StringValue = "Test String 2",
393+
CharValue = 'a',
394+
UShortValue = 10,
395+
UIntValue = 50,
396+
ULongValue = 200,
397+
DateOnlyValue = DateOnly.Parse("1985-10-31"),
398+
TimeOnlyValue = TimeOnly.Parse("12:00:00"),
399+
GuidValue = Guid.NewGuid(),
400+
},
401+
new TestEntityWithSimpleTypes
402+
{
403+
TestRun = _run,
404+
Id = 2,
405+
BoolValue = false,
406+
ByteValue = 10,
407+
ByteArrayValue =
408+
[
409+
4,
410+
5,
411+
6
412+
],
413+
SByteValue = -10,
414+
ShortValue = 20,
415+
IntValue = 30,
416+
LongValue = 40,
417+
FloatValue = 50.5f,
418+
DoubleValue = 60.6,
419+
DecimalValue = 70.7m,
420+
DateTimeValue = DateTime.UtcNow.AddDays(1),
421+
DateTimeOffsetValue = DateTimeOffset.UtcNow.AddDays(1),
422+
TimeSpanValue = TimeSpan.FromHours(2),
423+
StringValue = "Test String 2",
424+
CharValue = 'b',
425+
UShortValue = 50,
426+
UIntValue = 20,
427+
ULongValue = 100,
428+
DateOnlyValue = DateOnly.Parse("2023-10-01"),
429+
TimeOnlyValue = TimeOnly.Parse("12:00:00"),
430+
GuidValue = Guid.NewGuid(),
431+
}
432+
};
433+
434+
// Act
435+
var insertedEntities = await _context.InsertWithStrategyAsync(strategy, entities);
436+
437+
// Assert
438+
insertedEntities.Should()
439+
.BeEquivalentTo(entities, o => o
440+
.Using<DateTime>(ctx => ctx.Subject.Should().BeCloseTo(ctx.Expectation, 1.Seconds())).WhenTypeIs<DateTime>()
441+
.Using<DateTimeOffset>(ctx => ctx.Subject.Should().BeCloseTo(ctx.Expectation, 1.Seconds())).WhenTypeIs<DateTimeOffset>()
442+
.RespectingRuntimeTypes()
443+
.Excluding(e => e.Id)
444+
);
445+
}
358446
}

tests/PhenX.EntityFrameworkCore.BulkInsert.Tests/Tests/Geo/GeoTestsBase.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ namespace PhenX.EntityFrameworkCore.BulkInsert.Tests.Tests.Geo;
1414
public abstract class GeoTestsBase<TDbContext>(TestDbContainer dbContainer) : IAsyncLifetime
1515
where TDbContext : TestDbContextGeo, new()
1616
{
17+
private readonly Guid _run = Guid.NewGuid();
1718
private TDbContext _context = null!;
1819

1920
public async Task InitializeAsync()
@@ -37,8 +38,8 @@ public async Task InsertEntities_WithGeo(InsertStrategy strategy)
3738

3839
var entities = new List<TestEntityWithGeo>
3940
{
40-
new TestEntityWithGeo { GeoObject = geo1 },
41-
new TestEntityWithGeo { GeoObject = geo2 }
41+
new TestEntityWithGeo { TestRun = _run, GeoObject = geo1 },
42+
new TestEntityWithGeo { TestRun = _run, GeoObject = geo2 }
4243
};
4344

4445
// Act
@@ -59,8 +60,8 @@ public async Task InsertEntities_WithGeo_And_Default_SRID(InsertStrategy strateg
5960

6061
var entities = new List<TestEntityWithGeo>
6162
{
62-
new TestEntityWithGeo { GeoObject = geo1 },
63-
new TestEntityWithGeo { GeoObject = geo2 }
63+
new TestEntityWithGeo { TestRun = _run, GeoObject = geo1 },
64+
new TestEntityWithGeo { TestRun = _run, GeoObject = geo2 }
6465
};
6566

6667
// Act

tests/PhenX.EntityFrameworkCore.BulkInsert.Tests/Tests/Merge/MergeTestsBase.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ public async Task InsertEntities_MultipleTimes_WithGuidId(InsertStrategy strateg
6767
// Arrange
6868
var entities = new List<TestEntityWithGuidId>
6969
{
70-
new TestEntityWithGuidId { Id = Guid.NewGuid(), Name = $"{_run}_Entity1" },
71-
new TestEntityWithGuidId { Id = Guid.NewGuid(), Name = $"{_run}_Entity2" }
70+
new TestEntityWithGuidId { TestRun = _run,Id = Guid.NewGuid(), Name = $"{_run}_Entity1" },
71+
new TestEntityWithGuidId { TestRun = _run,Id = Guid.NewGuid(), Name = $"{_run}_Entity2" }
7272
};
7373

7474
// Act
@@ -97,8 +97,8 @@ public async Task InsertEntities_MultipleTimes_With_Conflict_On_Id(InsertStrateg
9797
// Arrange
9898
var entities = new List<TestEntity>
9999
{
100-
new TestEntity { Name = $"{_run}_Entity1" },
101-
new TestEntity { Name = $"{_run}_Entity2" }
100+
new TestEntity { TestRun = _run, Name = $"{_run}_Entity1" },
101+
new TestEntity { TestRun = _run, Name = $"{_run}_Entity2" }
102102
};
103103

104104
// Act
@@ -128,13 +128,13 @@ public async Task InsertEntities_WithConflict_SingleColumn(InsertStrategy strate
128128
Skip.If(_context.IsProvider(ProviderType.MySql));
129129

130130
// Arrange
131-
_context.TestEntities.Add(new TestEntity { Name = $"{_run}_Entity1" });
131+
_context.TestEntities.Add(new TestEntity { TestRun = _run,Name = $"{_run}_Entity1" });
132132
_context.SaveChanges();
133133
_context.ChangeTracker.Clear();
134134

135135
var entities = new List<TestEntity>
136136
{
137-
new TestEntity { Name = $"{_run}_Entity1" },
137+
new TestEntity { TestRun = _run, Name = $"{_run}_Entity1" },
138138
new TestEntity { TestRun = _run, Name = $"{_run}_Entity2" },
139139
};
140140

0 commit comments

Comments
 (0)