forked from bittercoder/Migrator.NET
-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathSQLiteTransformationProvider_AddPrimaryKeyTests.cs
More file actions
96 lines (79 loc) · 3.52 KB
/
SQLiteTransformationProvider_AddPrimaryKeyTests.cs
File metadata and controls
96 lines (79 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System;
using System.Data;
using System.Data.SQLite;
using System.Threading.Tasks;
using DotNetProjects.Migrator.Framework;
using DotNetProjects.Migrator.Providers.Impl.SQLite;
using Migrator.Tests.Providers.Generic;
using NUnit.Framework;
namespace Migrator.Tests.Providers.SQLite;
[TestFixture]
[Category("SQLite")]
public class SQLiteTransformationProvider_AddPrimaryTests : Generic_AddPrimaryTestsBase
{
[SetUp]
public async Task SetUpAsync()
{
await BeginSQLiteTransactionAsync();
}
[Test]
public void AddPrimaryKey_ColumnsInOtherOrderThanInColumnsList_Success()
{
// Arrange
const string columnName1 = "TestColumn";
const string columnName2 = "TestColumn2";
const string columnName3 = "TestColumn3";
const string tableName = "TestTable";
const string primaryKeyName = $"PK_{tableName}";
Provider.AddTable(tableName,
new Column(columnName1, DbType.String),
new Column(columnName2, DbType.Int32),
new Column(columnName3, DbType.Int32));
// Act
Provider.AddPrimaryKey(name: primaryKeyName, table: tableName, columns: [columnName3, columnName2]);
// Assert
var createTableScript = ((SQLiteTransformationProvider)Provider).GetSqlCreateTableScript(tableName);
Assert.That(createTableScript, Does.Contain("PRIMARY KEY (TestColumn3, TestColumn2))"));
}
[Test]
public void AddPrimaryKey_ColumnGuidNonComposite_ThrowsOnDuplicatesAndNulls()
{
const string tableName = "MyTableName";
const string columnName1 = "Column1";
var guid = Guid.NewGuid();
// Arrange/Act
Provider.AddTable(tableName,
new Column(columnName1, DbType.Guid, ColumnProperty.PrimaryKey)
);
Provider.Insert(tableName, [columnName1], [guid]);
Assert.Throws<SQLiteException>(() => Provider.Insert(tableName, [columnName1], [guid]));
Assert.Throws<SQLiteException>(() => Provider.Insert(tableName, [columnName1], [null]));
}
[Test]
public void AddPrimaryKey_ColumnGuidComposite_ThrowsOnDuplicatesAndNulls()
{
// Arrange
const string columnName1 = "TestColumn1";
const string columnName2 = "TestColumn2";
const string tableName = "TestTable";
const string primaryKeyName = $"PK_{tableName}";
var guid = Guid.NewGuid();
var guid2 = Guid.NewGuid();
Provider.AddTable(tableName,
new Column(columnName1, DbType.Guid),
new Column(columnName2, DbType.Guid));
// Act
Provider.AddPrimaryKey(name: primaryKeyName, table: tableName, columns: [columnName1, columnName2]);
// This is a normal SQLite behavior!
// NULL != NULL
// (A, NULL) != (A, NULL)
// Duplicates! You need to set NotNull if you want to prevent it!
Provider.Insert(tableName, [columnName1, columnName2], [guid, null]);
Provider.Insert(tableName, [columnName1, columnName2], [guid, null]);
Provider.Insert(tableName, [columnName1, columnName2], [guid, null]);
Provider.Insert(tableName, [columnName1, columnName2], [null, guid]);
Provider.Insert(tableName, [columnName1, columnName2], [null, guid]);
Provider.Insert(tableName, [columnName1, columnName2], [guid2, guid2]);
Assert.Throws<SQLiteException>(() => Provider.Insert(tableName, [columnName1, columnName2], [guid2, guid2]));
}
}