Skip to content

Commit f227757

Browse files
Merge pull request #111 from dotnetprojects/sql-server-default-values
Several fixes.
2 parents 5e453eb + a126f33 commit f227757

File tree

46 files changed

+1904
-876
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1904
-876
lines changed

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

Lines changed: 95 additions & 409 deletions
Large diffs are not rendered by default.

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

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,45 +5,8 @@
55

66
namespace Migrator.Tests.Providers.Base;
77

8-
public abstract class TransformationProviderSimpleBase
8+
public abstract class TransformationProviderSimpleBase : TransformationProviderBase
99
{
10-
protected ITransformationProvider Provider;
11-
12-
[TearDown]
13-
public virtual void TearDown()
14-
{
15-
DropTestTables();
16-
17-
Provider?.Rollback();
18-
}
19-
20-
protected void DropTestTables()
21-
{
22-
// Because MySql doesn't support schema transaction
23-
// we got to remove the tables manually... sad...
24-
try
25-
{
26-
Provider.RemoveTable("TestTwo");
27-
}
28-
catch (Exception)
29-
{
30-
}
31-
try
32-
{
33-
Provider.RemoveTable("Test");
34-
}
35-
catch (Exception)
36-
{
37-
}
38-
try
39-
{
40-
Provider.RemoveTable("SchemaInfo");
41-
}
42-
catch (Exception)
43-
{
44-
}
45-
}
46-
4710
public void AddDefaultTable()
4811
{
4912
Provider.AddTable("TestTwo",
@@ -75,4 +38,9 @@ public void AddTableWithPrimaryKey()
7538
new Column("bigstring", DbType.String, 50000)
7639
);
7740
}
41+
42+
public void AddPrimaryKey()
43+
{
44+
Provider.AddPrimaryKey("PK_Test", "Test", "Id");
45+
}
7846
}

src/Migrator.Tests/Providers/Base/TransformationProviderConstraintBase.cs renamed to src/Migrator.Tests/Providers/Generic/TransformationProviderGenericMiscConstraintBase.cs

Lines changed: 47 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
using System.Data;
33
using System.Linq;
44
using DotNetProjects.Migrator.Framework;
5+
using DotNetProjects.Migrator.Providers.Impl.SQLite;
56
using NUnit.Framework;
67

7-
namespace Migrator.Tests.Providers;
8+
namespace Migrator.Tests.Providers.Generic;
89

910
/// <summary>
10-
/// Base class for Provider tests for all tests including constraint oriented tests.
11+
/// Base class for provider tests for all tests including constraint oriented tests.
1112
/// </summary>
12-
public abstract class TransformationProviderConstraintBase : TransformationProviderBase
13+
public abstract class TransformationProviderGenericMiscConstraintBase : TransformationProviderGenericMiscTests
1314
{
1415
public void AddForeignKey()
1516
{
@@ -33,7 +34,7 @@ public void AddMultipleUniqueConstraint()
3334
Provider.AddUniqueConstraint("UN_Test_TestTwo", "TestTwo", "Id", "TestId");
3435
}
3536

36-
public void AddCheckConstraint()
37+
public void AddTestCheckConstraint()
3738
{
3839
Provider.AddCheckConstraint("CK_TestTwo_TestId", "TestTwo", "TestId>5");
3940
}
@@ -42,15 +43,23 @@ public void AddCheckConstraint()
4243
public void CanAddPrimaryKey()
4344
{
4445
AddPrimaryKey();
45-
Assert.That(Provider.PrimaryKeyExists("Test", "PK_Test"), Is.True);
46-
}
4746

48-
[Test]
49-
public void AddIndexedColumn()
50-
{
51-
Provider.AddColumn("TestTwo", "Test", DbType.String, 50, ColumnProperty.Indexed);
47+
if (Provider is SQLiteTransformationProvider)
48+
{
49+
Assert.Throws<NotSupportedException>(() => Provider.PrimaryKeyExists("Test", "PK_Test"));
50+
}
51+
else
52+
{
53+
Assert.That(Provider.PrimaryKeyExists("Test", "PK_Test"), Is.True);
54+
}
5255
}
5356

57+
// [Test]
58+
// public void AddIndexedColumn()
59+
// {
60+
// Provider.AddColumn("TestTwo", "Test", DbType.String, 50, ColumnProperty.Indexed);
61+
// }
62+
5463
[Test]
5564
public void AddUniqueColumn()
5665
{
@@ -81,8 +90,10 @@ public virtual void CanAddMultipleUniqueConstraint()
8190
[Test]
8291
public virtual void CanAddCheckConstraint()
8392
{
84-
AddCheckConstraint();
85-
Assert.That(Provider.ConstraintExists("TestTwo", "CK_TestTwo_TestId"), Is.True);
93+
AddTestCheckConstraint();
94+
var constraintExists = Provider.ConstraintExists("TestTwo", "CK_TestTwo_TestId");
95+
96+
Assert.That(constraintExists, Is.True);
8697
}
8798

8899
[Test]
@@ -105,51 +116,52 @@ public void RemoveUniqueConstraint()
105116
[Test]
106117
public virtual void RemoveCheckConstraint()
107118
{
108-
AddCheckConstraint();
119+
AddTestCheckConstraint();
109120
Provider.RemoveConstraint("TestTwo", "CK_TestTwo_TestId");
110121
Assert.That(Provider.ConstraintExists("TestTwo", "CK_TestTwo_TestId"), Is.False);
111122
}
112123

113124
[Test]
114125
public void RemoveUnexistingForeignKey()
115126
{
127+
// Arrange
116128
AddForeignKey();
117-
Provider.RemoveForeignKey("abc", "FK_Test_TestTwo");
118-
Provider.RemoveForeignKey("abc", "abc");
119-
Provider.RemoveForeignKey("Test", "abc");
129+
130+
// Act/Assert
131+
// Table does not exist.
132+
Assert.Throws<MigrationException>(() => Provider.RemoveForeignKey("NotExistingTable", "FK_Test_TestTwo"));
133+
134+
// Table exists but foreign key does not exist.
135+
if (Provider is SQLiteTransformationProvider)
136+
{
137+
Assert.Throws<MigrationException>(() => Provider.RemoveForeignKey("Test", "NotExistingForeignKey"));
138+
}
139+
else
140+
{
141+
Assert.That(() => Provider.RemoveForeignKey("Test", "NotExistingForeignKey"), Throws.Exception);
142+
}
120143
}
121144

122145
[Test]
123146
public void ConstraintExist()
124147
{
125148
AddForeignKey();
126149
Assert.That(Provider.ConstraintExists("TestTwo", "FK_Test_TestTwo"), Is.True);
127-
Assert.That(Provider.ConstraintExists("abc", "abc"), Is.False);
128-
}
129-
130-
[Test]
131-
public void AddTableWithCompoundPrimaryKey()
132-
{
133-
Provider.AddTable("Test",
134-
new Column("PersonId", DbType.Int32, ColumnProperty.PrimaryKey),
135-
new Column("AddressId", DbType.Int32, ColumnProperty.PrimaryKey)
136-
);
137-
138-
Assert.That(Provider.TableExists("Test"), Is.True, "Table doesn't exist");
139-
Assert.That(Provider.PrimaryKeyExists("Test", "PK_Test"), Is.True, "Constraint doesn't exist");
150+
Assert.That(Provider.ConstraintExists("TestTwo", "abc"), Is.False);
140151
}
141152

142153
[Test]
143154
public void AddTableWithCompoundPrimaryKeyShouldKeepNullForOtherProperties()
144155
{
145-
Provider.AddTable("Test",
146-
new Column("PersonId", DbType.Int32, ColumnProperty.PrimaryKey),
147-
new Column("AddressId", DbType.Int32, ColumnProperty.PrimaryKey),
148-
new Column("Name", DbType.String, 30, ColumnProperty.Null)
149-
);
156+
var testTableName = "Test";
157+
158+
Provider.AddTable(testTableName,
159+
new Column("PersonId", DbType.Int32, ColumnProperty.PrimaryKey),
160+
new Column("AddressId", DbType.Int32, ColumnProperty.PrimaryKey),
161+
new Column("Name", DbType.String, 30, ColumnProperty.Null)
162+
);
150163

151164
Assert.That(Provider.TableExists("Test"), Is.True, "Table doesn't exist");
152-
Assert.That(Provider.PrimaryKeyExists("Test", "PK_Test"), Is.True, "Constraint doesn't exist");
153165

154166
var column = Provider.GetColumnByName("Test", "Name");
155167

0 commit comments

Comments
 (0)