Skip to content

Commit 73a4094

Browse files
JaBistDuNarrischJaBistDuNarrisch
authored andcommitted
Added TableExists and FK exists checks
1 parent e40d34e commit 73a4094

14 files changed

+109
-30
lines changed

src/Migrator.Tests/Providers/Base/TransformationProviderSimpleBase.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,9 @@ public void AddTableWithPrimaryKey()
3838
new Column("bigstring", DbType.String, 50000)
3939
);
4040
}
41+
42+
public void AddPrimaryKey()
43+
{
44+
Provider.AddPrimaryKey("PK_Test", "Test", "Id");
45+
}
4146
}

src/Migrator.Tests/Providers/Generic/TransformationProviderGenericMiscConstraintBase.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using System.Data;
33
using System.Linq;
44
using DotNetProjects.Migrator.Framework;
5+
using DotNetProjects.Migrator.Providers.Impl.SQLite;
6+
using DryIoc;
57
using NUnit.Framework;
68

79
namespace Migrator.Tests.Providers.Generic;
@@ -113,22 +115,32 @@ public virtual void RemoveCheckConstraint()
113115
[Test]
114116
public void RemoveUnexistingForeignKey()
115117
{
118+
// Arrange
116119
AddForeignKey();
117-
Provider.RemoveForeignKey("abc", "FK_Test_TestTwo");
118-
Provider.RemoveForeignKey("abc", "abc");
119-
Provider.RemoveForeignKey("Test", "abc");
120+
121+
// Act/Assert
122+
// Table does not exist.
123+
Assert.Throws<MigrationException>(() => Provider.RemoveForeignKey("NotExistingTable", "FK_Test_TestTwo"));
124+
125+
// Table exists but foreign key does not exist.
126+
if (Provider is SQLiteTransformationProvider)
127+
{
128+
Assert.Throws<MigrationException>(() => Provider.RemoveForeignKey("Test", "NotExistingForeignKey"));
129+
}
130+
else
131+
{
132+
Assert.That(() => Provider.RemoveForeignKey("Test", "NotExistingForeignKey"), Throws.Exception);
133+
}
120134
}
121135

122136
[Test]
123137
public void ConstraintExist()
124138
{
125139
AddForeignKey();
126140
Assert.That(Provider.ConstraintExists("TestTwo", "FK_Test_TestTwo"), Is.True);
127-
Assert.That(Provider.ConstraintExists("abc", "abc"), Is.False);
141+
Assert.That(Provider.ConstraintExists("TestTwo", "abc"), Is.False);
128142
}
129143

130-
131-
132144
[Test]
133145
public void AddTableWithCompoundPrimaryKeyShouldKeepNullForOtherProperties()
134146
{
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System.Threading.Tasks;
2+
using Migrator.Tests.Providers.Base;
3+
using NUnit.Framework;
4+
5+
namespace Migrator.Tests.Providers.OracleProvider;
6+
7+
[TestFixture]
8+
[Category("Oracle")]
9+
public class OracleTransformationProvider_PrimaryKeyExistsTests : TransformationProviderSimpleBase
10+
{
11+
[SetUp]
12+
public async Task SetUpAsync()
13+
{
14+
await BeginOracleTransactionAsync();
15+
}
16+
17+
[Test]
18+
public void CanAddPrimaryKey()
19+
{
20+
AddTable();
21+
AddPrimaryKey();
22+
Assert.That(Provider.PrimaryKeyExists("Test", "PK_Test"), Is.True);
23+
}
24+
}
Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,16 @@
1-
using DotNetProjects.Migrator.Providers;
2-
using DotNetProjects.Migrator.Providers.Impl.PostgreSQL;
1+
using System.Threading.Tasks;
32
using Migrator.Tests.Providers.Base;
4-
using Migrator.Tests.Settings;
5-
using Migrator.Tests.Settings.Config;
63
using NUnit.Framework;
74

8-
namespace Migrator.Tests.Providers.PostgreSQL;
5+
namespace Migrator.Tests.Providers.PostgreSQL.Base;
96

107
[TestFixture]
118
[Category("Postgre")]
129
public abstract class PostgreSQLTransformationProviderTestBase : TransformationProviderSimpleBase
1310
{
1411
[SetUp]
15-
public void SetUp()
12+
public async Task SetUpAsync()
1613
{
17-
var configReader = new ConfigurationReader();
18-
var connectionString = configReader.GetDatabaseConnectionConfigById(DatabaseConnectionConfigIds.PostgreSQL)
19-
?.ConnectionString;
20-
21-
if (string.IsNullOrEmpty(connectionString))
22-
{
23-
throw new IgnoreException("No Postgre ConnectionString is Set.");
24-
}
25-
26-
DbProviderFactories.RegisterFactory("Npgsql", () => Npgsql.NpgsqlFactory.Instance);
27-
28-
Provider = new PostgreSQLTransformationProvider(new PostgreSQLDialect(), connectionString, null, "default", "Npgsql");
29-
Provider.BeginTransaction();
30-
31-
AddDefaultTable();
14+
await BeginPostgreSQLTransactionAsync();
3215
}
3316
}

src/Migrator.Tests/Providers/PostgreSQL/PostgreSQLTransformationProvider_AddTableTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Data;
33
using DotNetProjects.Migrator.Framework;
4+
using Migrator.Tests.Providers.PostgreSQL.Base;
45
using NUnit.Framework;
56

67
namespace Migrator.Tests.Providers.PostgreSQL;

src/Migrator.Tests/Providers/PostgreSQL/PostgreSQLTransformationProvider_GetColumnContent_SizeTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Data;
33
using DotNetProjects.Migrator.Framework;
4+
using Migrator.Tests.Providers.PostgreSQL.Base;
45
using NUnit.Framework;
56

67
namespace Migrator.Tests.Providers.PostgreSQL;

src/Migrator.Tests/Providers/PostgreSQL/PostgreSQLTransformationProvider_GetColumnsTypeTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Data;
22
using System.Linq;
33
using DotNetProjects.Migrator.Framework;
4+
using Migrator.Tests.Providers.PostgreSQL.Base;
45
using NUnit.Framework;
56

67
namespace Migrator.Tests.Providers.PostgreSQL;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Data;
2+
using System.Linq;
3+
using DotNetProjects.Migrator.Framework;
4+
using Migrator.Tests.Providers.PostgreSQL.Base;
5+
using NUnit.Framework;
6+
7+
namespace Migrator.Tests.Providers.PostgreSQL;
8+
9+
[TestFixture]
10+
[Category("Postgre")]
11+
public class PostgreSQLTransformationProvider_PrimaryKeyExistsTests : PostgreSQLTransformationProviderTestBase
12+
{
13+
[Test]
14+
public void CanAddPrimaryKey()
15+
{
16+
AddTable();
17+
AddPrimaryKey();
18+
Assert.That(Provider.PrimaryKeyExists("Test", "PK_Test"), Is.True);
19+
}
20+
}

src/Migrator.Tests/Providers/PostgreSQL/PostgreSQLTransformationProvider_PrimaryKeyWithIdentityTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Data;
33
using DotNetProjects.Migrator.Framework;
4+
using Migrator.Tests.Providers.PostgreSQL.Base;
45
using Npgsql;
56
using NUnit.Framework;
67

src/Migrator.Tests/Providers/PostgreSQL/PostgreSQLTransformationProvider_TableExistsTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Data;
22
using DotNetProjects.Migrator.Framework;
3+
using Migrator.Tests.Providers.PostgreSQL.Base;
34
using NUnit.Framework;
45

56
namespace Migrator.Tests.Providers.PostgreSQL;

0 commit comments

Comments
 (0)