forked from sethreno/schemazen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForeignKeyTester.cs
100 lines (83 loc) · 3.6 KB
/
ForeignKeyTester.cs
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
97
98
99
100
using System.Linq;
using NUnit.Framework;
using SchemaZen.Library.Models;
namespace SchemaZen.Tests {
[TestFixture]
public class ForeignKeyTester {
public void TestMultiColumnKey() {
var t1 = new Table("dbo", "t1");
t1.Columns.Add(new Column("c2", "varchar", 10, false, null));
t1.Columns.Add(new Column("c1", "int", false, null));
t1.AddConstraint(new Constraint("pk_t1", "PRIMARY KEY", "c1,c2"));
var t2 = new Table("dbo", "t2");
t2.Columns.Add(new Column("c1", "int", false, null));
t2.Columns.Add(new Column("c2", "varchar", 10, false, null));
t2.Columns.Add(new Column("c3", "int", false, null));
var fk = new ForeignKey(t2, "fk_test", "c3,c2", t1, "c1,c2");
var db = new Database("TESTDB");
db.Tables.Add(t1);
db.Tables.Add(t2);
db.ForeignKeys.Add(fk);
db.Connection = TestHelper.GetConnString("TESTDB");
db.ExecCreate(true);
db.Load();
Assert.AreEqual("c3", db.FindForeignKey("fk_test", "dbo").Columns[0]);
Assert.AreEqual("c2", db.FindForeignKey("fk_test", "dbo").Columns[1]);
Assert.AreEqual("c1", db.FindForeignKey("fk_test", "dbo").RefColumns[0]);
Assert.AreEqual("c2", db.FindForeignKey("fk_test", "dbo").RefColumns[1]);
db.ExecCreate(true);
}
[Test]
public void TestScript() {
var person = new Table("dbo", "Person");
person.Columns.Add(new Column("id", "int", false, null));
person.Columns.Add(new Column("name", "varchar", 50, false, null));
person.Columns.Find("id").Identity = new Identity(1, 1);
person.AddConstraint(new Constraint("PK_Person", "PRIMARY KEY", "id"));
var address = new Table("dbo", "Address");
address.Columns.Add(new Column("id", "int", false, null));
address.Columns.Add(new Column("personId", "int", false, null));
address.Columns.Add(new Column("street", "varchar", 50, false, null));
address.Columns.Add(new Column("city", "varchar", 50, false, null));
address.Columns.Add(new Column("state", "char", 2, false, null));
address.Columns.Add(new Column("zip", "varchar", 5, false, null));
address.Columns.Find("id").Identity = new Identity(1, 1);
address.AddConstraint(new Constraint("PK_Address", "PRIMARY KEY", "id"));
var fk = new ForeignKey(address, "FK_Address_Person", "personId", person, "id", "",
"CASCADE");
TestHelper.ExecSql(person.ScriptCreate(), "");
TestHelper.ExecSql(address.ScriptCreate(), "");
TestHelper.ExecSql(fk.ScriptCreate(), "");
TestHelper.ExecSql("drop table Address", "");
TestHelper.ExecSql("drop table Person", "");
}
[Test]
public void TestScriptForeignKeyWithNoName() {
var t1 = new Table("dbo", "t1");
t1.Columns.Add(new Column("c2", "varchar", 10, false, null));
t1.Columns.Add(new Column("c1", "int", false, null));
t1.AddConstraint(new Constraint("pk_t1", "PRIMARY KEY", "c1,c2"));
var t2 = new Table("dbo", "t2");
t2.Columns.Add(new Column("c1", "int", false, null));
t2.Columns.Add(new Column("c2", "varchar", 10, false, null));
t2.Columns.Add(new Column("c3", "int", false, null));
var fk = new ForeignKey(t2, "fk_ABCDEF", "c3,c2", t1, "c1,c2");
fk.IsSystemNamed = true;
var db = new Database("TESTDB");
db.Tables.Add(t1);
db.Tables.Add(t2);
db.ForeignKeys.Add(fk);
db.Connection = TestHelper.GetConnString("TESTDB");
db.ExecCreate(true);
db.Load();
Assert.AreEqual(1, db.ForeignKeys.Count);
var fkCopy = db.ForeignKeys.Single();
Assert.AreEqual("c3", fkCopy.Columns[0]);
Assert.AreEqual("c2", fkCopy.Columns[1]);
Assert.AreEqual("c1", fkCopy.RefColumns[0]);
Assert.AreEqual("c2", fkCopy.RefColumns[1]);
Assert.IsTrue(fkCopy.IsSystemNamed);
db.ExecCreate(true);
}
}
}