This repository has been archived by the owner on Dec 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 636
/
ForeignKeyAttributeTests.cs
194 lines (159 loc) · 5.84 KB
/
ForeignKeyAttributeTests.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
using NUnit.Framework;
using ServiceStack.DataAnnotations;
namespace ServiceStack.OrmLite.Tests
{
[TestFixtureOrmLite]
public class ForeignKeyAttributeTests : OrmLiteProvidersTestBase
{
public ForeignKeyAttributeTests(DialectContext context) : base(context) {}
[OneTimeSetUp]
public void Setup()
{
using var dbConn = OpenDbConnection();
dbConn.DropTable<TypeWithOnDeleteAndUpdateCascade>();
dbConn.DropTable<TypeWithOnDeleteSetNull>();
dbConn.DropTable<TypeWithOnDeleteSetDefault>();
dbConn.DropTable<TypeWithOnDeleteRestrict>();
dbConn.DropTable<TypeWithOnDeleteNoAction>();
dbConn.DropTable<TypeWithOnDeleteCascade>();
dbConn.DropTable<TypeWithSimpleForeignKey>();
dbConn.DropTable<ReferencedType>();
dbConn.CreateTable<ReferencedType>();
}
[Test]
public void CanCreateSimpleForeignKey()
{
using var dbConn = OpenDbConnection();
dbConn.DropAndCreateTable<TypeWithSimpleForeignKey>();
}
[Test]
public void CanCreateForeignWithOnDeleteCascade()
{
using var dbConn = OpenDbConnection();
dbConn.DropAndCreateTable<TypeWithOnDeleteCascade>();
}
[Test]
[IgnoreDialect(Dialect.Sqlite, "no support for cascade deletes")]
public void CascadesOnDelete()
{
// TODO: group tests around db features
Setup();
using var db = OpenDbConnection();
db.CreateTableIfNotExists<TypeWithOnDeleteCascade>();
db.Save(new ReferencedType { Id = 1 });
db.Save(new TypeWithOnDeleteCascade { RefId = 1 });
Assert.AreEqual(1, db.Select<ReferencedType>().Count);
Assert.AreEqual(1, db.Select<TypeWithOnDeleteCascade>().Count);
db.Delete<ReferencedType>(r => r.Id == 1);
Assert.AreEqual(0, db.Select<ReferencedType>().Count);
Assert.AreEqual(0, db.Select<TypeWithOnDeleteCascade>().Count);
}
[Test]
public void CanCreateForeignWithOnDeleteCascadeAndOnUpdateCascade()
{
using var dbConn = OpenDbConnection();
dbConn.DropAndCreateTable<TypeWithOnDeleteAndUpdateCascade>();
}
[Test]
[IgnoreDialect(Tests.Dialect.Sqlite, "Not supported in sqlite?")]
public void CanCreateForeignWithOnDeleteNoAction()
{
using var dbConn = OpenDbConnection();
dbConn.DropAndCreateTable<TypeWithOnDeleteNoAction>();
}
[Test]
public void CanCreateForeignWithOnDeleteRestrict()
{
using var dbConn = OpenDbConnection();
dbConn.DropAndCreateTable<TypeWithOnDeleteRestrict>();
}
[Test]
public void CanCreateForeignWithOnDeleteSetDefault()
{
// ignoring Not supported in InnoDB: http://stackoverflow.com/a/1498015/85785
if (DialectProvider == MySqlDialect.Provider)
{
Assert.Ignore("MySql FK's not supported, skipping");
return;
}
using var dbConn = OpenDbConnection();
dbConn.DropAndCreateTable<TypeWithOnDeleteSetDefault>();
}
[Test]
public void CanCreateForeignWithOnDeleteSetNull()
{
using var dbConn = OpenDbConnection();
dbConn.DropAndCreateTable<TypeWithOnDeleteSetNull>();
}
[Test]
public void Can_Skip_creating_ForeignKeys()
{
OrmLiteConfig.SkipForeignKeys = true;
Setup();
using (var db = OpenDbConnection())
{
db.CreateTableIfNotExists<TypeWithOnDeleteCascade>();
db.Save(new ReferencedType { Id = 1 });
db.Save(new TypeWithOnDeleteCascade { RefId = 1 });
db.Delete<ReferencedType>(r => r.Id == 1);
Assert.That(db.Select<ReferencedType>().Count, Is.EqualTo(0));
Assert.That(db.Select<TypeWithOnDeleteCascade>().Count, Is.EqualTo(1));
}
OrmLiteConfig.SkipForeignKeys = false;
}
}
public class ReferencedType
{
public int Id { get; set; }
}
public class TypeWithSimpleForeignKey
{
[AutoIncrement]
public int Id { get; set; }
[References(typeof(ReferencedType))]
public int RefId { get; set; }
}
public class TypeWithOnDeleteCascade
{
[AutoIncrement]
public int Id { get; set; }
[ForeignKey(typeof(ReferencedType), OnDelete = "CASCADE")]
public int? RefId { get; set; }
}
public class TypeWithOnDeleteAndUpdateCascade
{
[AutoIncrement]
public int Id { get; set; }
[ForeignKey(typeof(ReferencedType), OnDelete = "CASCADE", OnUpdate = "CASCADE")]
public int? RefId { get; set; }
}
public class TypeWithOnDeleteNoAction
{
[AutoIncrement]
public int Id { get; set; }
[ForeignKey(typeof(ReferencedType), OnDelete = "NO ACTION")]
public int? RefId { get; set; }
}
public class TypeWithOnDeleteRestrict
{
[AutoIncrement]
public int Id { get; set; }
[ForeignKey(typeof(ReferencedType), OnDelete = "RESTRICT")]
public int? RefId { get; set; }
}
public class TypeWithOnDeleteSetDefault
{
[AutoIncrement]
public int Id { get; set; }
[Default(typeof(int), "17")]
[ForeignKey(typeof(ReferencedType), OnDelete = "SET DEFAULT")]
public int RefId { get; set; }
}
public class TypeWithOnDeleteSetNull
{
[AutoIncrement]
public int Id { get; set; }
[ForeignKey(typeof(ReferencedType), OnDelete = "SET NULL")]
public int? RefId { get; set; }
}
}