Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public virtual string BuildMoveDataSql<T>(

if (returnedColumns.Count != 0)
{
q.Append("RETURNING ");
q.Append(" RETURNING ");
q.AppendJoin(", ", returnedColumns.Select(p => p.QuotedColumName));
q.AppendLine();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace PhenX.EntityFrameworkCore.BulkInsert.Tests.DbContext;
[PrimaryKey(nameof(Id))]
[Index(nameof(Name), IsUnique = true)]
[Table("test_entity")]
public class TestEntity
public class TestEntity : TestEntityBase
{
public int Id { get; set; }

Expand All @@ -19,9 +19,6 @@ public class TestEntity
[Column("some_price")]
public decimal Price { get; set; }

[Column("test_run")]
public Guid TestRun { get; set; }

[Column("the_identifier")]
public Guid Identifier { get; set; }

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.ComponentModel.DataAnnotations.Schema;

namespace PhenX.EntityFrameworkCore.BulkInsert.Tests.DbContext;

public abstract class TestEntityBase
{
[Column("test_run")]
public Guid TestRun { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace PhenX.EntityFrameworkCore.BulkInsert.Tests.DbContext;

[Table("test_entity_with_converters")]
public class TestEntityWithConverters
public class TestEntityWithConverters : TestEntityBase
{
public int Id { get; set; }

Expand All @@ -14,8 +14,5 @@ public class TestEntityWithConverters

[Column("created_at")]
public DateTime CreatedAt { get; set; }

[Column("test_run")]
public Guid TestRun { get; set; }
}

Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

using NetTopologySuite.Geometries;

namespace PhenX.EntityFrameworkCore.BulkInsert.Tests.DbContext;

[Table("test_entity_geo")]
public class TestEntityWithGeo
public class TestEntityWithGeo : TestEntityBase
{
[Key]
public int Id { get; set; }

public Geometry GeoObject { get; set; } = null!;

[Column("test_run")]
public Guid TestRun { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@
namespace PhenX.EntityFrameworkCore.BulkInsert.Tests.DbContext;

[Table("test_entity_guids")]
public class TestEntityWithGuidId
public class TestEntityWithGuidId : TestEntityBase
{
[Key]
public Guid Id { get; set; }

[Column("name")]
[MaxLength(100)]
public string Name { get; set; } = string.Empty;

[Column("test_run")]
public Guid TestRun { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@
namespace PhenX.EntityFrameworkCore.BulkInsert.Tests.DbContext;

[Table("test_entity_json")]
public class TestEntityWithJson
public class TestEntityWithJson : TestEntityBase
{
[Key]
public int Id { get; set; }

public List<int> Json { get; set; } = [];

[Column("test_run")]
public Guid TestRun { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
Expand All @@ -13,8 +13,10 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="FluentAssertions" Version="7.2.0" />
Comment thread
PhenX marked this conversation as resolved.
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="Xunit.Combinatorial" Version="1.6.24" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
65 changes: 65 additions & 0 deletions tests/PhenX.EntityFrameworkCore.BulkInsert.Tests/TestHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using Microsoft.EntityFrameworkCore;

using PhenX.EntityFrameworkCore.BulkInsert.Enums;
using PhenX.EntityFrameworkCore.BulkInsert.Extensions;
using PhenX.EntityFrameworkCore.BulkInsert.Options;
using PhenX.EntityFrameworkCore.BulkInsert.Tests.DbContext;

using Xunit;

namespace PhenX.EntityFrameworkCore.BulkInsert.Tests;

public enum InsertStrategy
{
Insert,
InsertReturn,
InsertAsync,
InsertReturnAsync
}

public static class TestHelpers
{
public static async Task<List<T>> InsertWithStrategyAsync<T>(
this TestDbContextBase dbContext,
InsertStrategy strategy,
List<T> entities,
Action<BulkInsertOptions>? configure = null,
OnConflictOptions<T>? onConflict = null)
where T : TestEntityBase
{
Skip.If(strategy is InsertStrategy.InsertReturn or InsertStrategy.InsertReturnAsync && dbContext.IsProvider(ProviderType.MySql));

var runId = Guid.NewGuid();
if (entities.Any(x => x.TestRun == default))
{
foreach (var entity in entities)
{
if (entity.TestRun == default)
{
entity.TestRun = runId;
}
}
}
else if (entities.Count > 0)
{
runId = entities[0].TestRun;
}

var actualConfigure = configure ?? (_ => { });
switch (strategy)
{
case InsertStrategy.InsertReturn:
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My thought was to have two enums or two bools, but it works well like this too, as long as there are no more than two parameters

return dbContext.ExecuteBulkInsertReturnEntities(entities, actualConfigure, onConflict);
case InsertStrategy.InsertReturnAsync:
return await dbContext.ExecuteBulkInsertReturnEntitiesAsync(entities, actualConfigure, onConflict);
case InsertStrategy.Insert:
dbContext.ExecuteBulkInsert(entities, actualConfigure, onConflict);
return dbContext.Set<T>().Where(x => x.TestRun == runId).ToList();
case InsertStrategy.InsertAsync:
await dbContext.ExecuteBulkInsertAsync(entities, actualConfigure, onConflict);
return await dbContext.Set<T>().Where(x => x.TestRun == runId).ToListAsync();
default:
throw new NotImplementedException();
}
}
}
Loading
Loading