-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
199 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
src/tests/Hrimsoft.SqlBulk.PostgreSql.IntegrationTests/BulkUpsert/CompositePkTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Hrimsoft.SqlBulk.PostgreSql.IntegrationTests.TestModels; | ||
using Microsoft.Extensions.Logging.Abstractions; | ||
using Moq; | ||
using Npgsql; | ||
using NUnit.Framework; | ||
|
||
namespace Hrimsoft.SqlBulk.PostgreSql.IntegrationTests.BulkUpsert | ||
{ | ||
public class CompositePkTests | ||
{ | ||
private readonly TestConfiguration _configuration; | ||
|
||
private IPostgreSqlBulkService _testService; | ||
private BulkServiceOptions _bulkServiceOptions; | ||
|
||
public CompositePkTests() | ||
{ | ||
// As upsert command was implemented only in postgre version of 9.5+ | ||
_configuration = new TestConfiguration("Postgres_higher_than_9_4"); | ||
} | ||
|
||
[SetUp] | ||
public async Task SetUp() | ||
{ | ||
var truncateTableCmd = "truncate \"unit_tests\".\"entity_with_composite_pk\";"; | ||
|
||
await using var connection = new NpgsqlConnection(_configuration.ConnectionString); | ||
await using var command = new NpgsqlCommand(truncateTableCmd, connection); | ||
await connection.OpenAsync(); | ||
await command.ExecuteNonQueryAsync(); | ||
|
||
_bulkServiceOptions = new BulkServiceOptions(); | ||
_bulkServiceOptions.AddEntityProfile<TestEntityWithCompositePk>(new EntityWithCompositePkProfile()); | ||
|
||
var insertCommandBuilder = new InsertSqlCommandBuilder(NullLoggerFactory.Instance); | ||
var deleteCommandBuilder = new Mock<IDeleteSqlCommandBuilder>().Object; | ||
var updateCommandBuilder = new Mock<IUpdateSqlCommandBuilder>().Object; | ||
var upsertCommandBuilder = new UpsertSqlCommandBuilder(NullLoggerFactory.Instance); | ||
|
||
_testService = new NpgsqlCommandsBulkService( | ||
_bulkServiceOptions, | ||
NullLoggerFactory.Instance, | ||
insertCommandBuilder, | ||
updateCommandBuilder, | ||
deleteCommandBuilder, | ||
upsertCommandBuilder); | ||
} | ||
|
||
[Test] | ||
public async Task Should_insert() | ||
{ | ||
var elements = new List<TestEntityWithCompositePk> | ||
{ | ||
new TestEntityWithCompositePk {UserId = 1, Column2 = 2, Column3 = 3}, | ||
}; | ||
await using var connection = new NpgsqlConnection(_configuration.ConnectionString); | ||
await _testService.UpsertAsync(connection, elements, CancellationToken.None); | ||
var query = "select * from unit_tests.entity_with_composite_pk;"; | ||
using var command = new NpgsqlCommand(query, connection); | ||
using (var reader = command.ExecuteReader()) { | ||
var count = 0; | ||
while (reader.Read()) { | ||
count++; | ||
Assert.AreEqual(1, count); | ||
Assert.AreEqual(1, (int)reader["user_id"]); | ||
Assert.AreEqual(2, (int)reader["column2"]); | ||
Assert.AreEqual(3, (int)reader["column3"]); | ||
} | ||
await reader.CloseAsync(); | ||
} | ||
} | ||
|
||
[Test] | ||
public async Task Should_update() | ||
{ | ||
var elements = new List<TestEntityWithCompositePk> | ||
{ | ||
new TestEntityWithCompositePk {UserId = 1, Column2 = 2, Column3 = 5}, | ||
}; | ||
await using var connection = new NpgsqlConnection(_configuration.ConnectionString); | ||
await _testService.InsertAsync(connection, elements, CancellationToken.None); | ||
elements[0].Column3 = 10; // this item should be updated | ||
// This item should be inserted | ||
await _testService.UpsertAsync(connection, elements, CancellationToken.None); | ||
|
||
var query = "select * from unit_tests.entity_with_composite_pk;"; | ||
using var command = new NpgsqlCommand(query, connection); | ||
using (var reader = command.ExecuteReader()) { | ||
var count = 0; | ||
while (reader.Read()) { | ||
count++; | ||
Assert.AreEqual(1, count); | ||
Assert.AreEqual(1, (int)reader["user_id"]); | ||
Assert.AreEqual(2, (int)reader["column2"]); | ||
Assert.AreEqual(10, (int)reader["column3"]); | ||
} | ||
await reader.CloseAsync(); | ||
} | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...s/Hrimsoft.SqlBulk.PostgreSql.IntegrationTests/TestModels/EntityWithCompositePkProfile.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
namespace Hrimsoft.SqlBulk.PostgreSql.IntegrationTests.TestModels | ||
{ | ||
public class EntityWithCompositePkProfile : EntityProfile | ||
{ | ||
public EntityWithCompositePkProfile(int maximumSentElements = 0) | ||
: base(typeof(TestEntityWithCompositePk)) | ||
{ | ||
this.MaximumSentElements = maximumSentElements; | ||
|
||
this.ToTable("entity_with_composite_pk", "unit_tests"); | ||
this.HasUniqueConstraint("PK_entity_with_composite_pk"); | ||
this.HasPropertyAsPartOfUniqueConstraint<TestEntityWithCompositePk, int>(entity => entity.UserId) | ||
.ThatIsPrivateKey(); | ||
this.HasPropertyAsPartOfUniqueConstraint<TestEntityWithCompositePk, int>(entity => entity.Column2) | ||
.ThatIsPrivateKey(); | ||
this.HasProperty<TestEntityWithCompositePk, int>(entity => entity.Column3); | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...ests/Hrimsoft.SqlBulk.PostgreSql.IntegrationTests/TestModels/TestEntityWithCompositePk.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace Hrimsoft.SqlBulk.PostgreSql.IntegrationTests.TestModels | ||
{ | ||
public class TestEntityWithCompositePk | ||
{ | ||
public int UserId { get; set; } | ||
public int Column2 { get; set; } | ||
public int Column3 { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters