Skip to content

Commit cd5d759

Browse files
authored
Merge pull request #158 from dotnetprojects/copilot/sub-pr-157
Add test coverage for quoted table names in SQLite foreign key constraints
2 parents 4fa9047 + 56b7cce commit cd5d759

1 file changed

Lines changed: 135 additions & 0 deletions

File tree

src/Migrator.Tests/Providers/SQLite/SQLiteTransformationProvider_GetForeignKeysTests.cs

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,139 @@ public void RenameColumn_HavingASingleForeignKeyPointingToTheTargetColumn_Single
5353
Assert.That(foreignKeyConstraints.Single(x => x.Name == foreignKeyStringB).ChildColumns, Is.EqualTo([childColumnFKToParentBProperty1, childColumnFKToParentBProperty2]));
5454
Assert.That(foreignKeyConstraints.Single(x => x.Name == foreignKeyStringB).ParentColumns, Is.EqualTo([parentBProperty1, parentBProperty2]));
5555
}
56+
57+
[Test]
58+
public void GetForeignKeyConstraints_WithReservedWordGroup_ReturnsCorrectForeignKeys()
59+
{
60+
// Arrange - Testing with SQLite reserved word "group" as parent table name
61+
const string parentTable = "group";
62+
const string parentColumn = "Id";
63+
const string childTable = "Orders";
64+
const string childColumn = "GroupId";
65+
const string foreignKeyName = "FK_Orders_Group";
66+
67+
Provider.AddTable(parentTable, new Column(parentColumn, DbType.Int32, ColumnProperty.PrimaryKey));
68+
Provider.AddTable(childTable,
69+
new Column("Id", DbType.Int32, ColumnProperty.PrimaryKey),
70+
new Column(childColumn, DbType.Int32)
71+
);
72+
73+
Provider.AddForeignKey(foreignKeyName, childTable, childColumn, parentTable, parentColumn);
74+
75+
// Act
76+
var foreignKeyConstraints = Provider.GetForeignKeyConstraints(childTable);
77+
78+
// Assert
79+
Assert.That(foreignKeyConstraints.Length, Is.EqualTo(1));
80+
var fk = foreignKeyConstraints.Single();
81+
Assert.That(fk.Name, Is.EqualTo(foreignKeyName));
82+
Assert.That(fk.ParentTable, Is.EqualTo(parentTable));
83+
Assert.That(fk.ParentColumns, Is.EqualTo(new[] { parentColumn }));
84+
Assert.That(fk.ChildTable, Is.EqualTo(childTable));
85+
Assert.That(fk.ChildColumns, Is.EqualTo(new[] { childColumn }));
86+
}
87+
88+
[Test]
89+
public void GetForeignKeyConstraints_WithReservedWordOrder_ReturnsCorrectForeignKeys()
90+
{
91+
// Arrange - Testing with SQLite reserved word "order" as parent table name
92+
const string parentTable = "order";
93+
const string parentColumn = "OrderId";
94+
const string childTable = "LineItems";
95+
const string childColumn = "OrderRef";
96+
const string foreignKeyName = "FK_LineItems_Order";
97+
98+
Provider.AddTable(parentTable, new Column(parentColumn, DbType.Int32, ColumnProperty.PrimaryKey));
99+
Provider.AddTable(childTable,
100+
new Column("Id", DbType.Int32, ColumnProperty.PrimaryKey),
101+
new Column(childColumn, DbType.Int32)
102+
);
103+
104+
Provider.AddForeignKey(foreignKeyName, childTable, childColumn, parentTable, parentColumn);
105+
106+
// Act
107+
var foreignKeyConstraints = Provider.GetForeignKeyConstraints(childTable);
108+
109+
// Assert
110+
Assert.That(foreignKeyConstraints.Length, Is.EqualTo(1));
111+
var fk = foreignKeyConstraints.Single();
112+
Assert.That(fk.Name, Is.EqualTo(foreignKeyName));
113+
Assert.That(fk.ParentTable, Is.EqualTo(parentTable));
114+
Assert.That(fk.ParentColumns, Is.EqualTo(new[] { parentColumn }));
115+
Assert.That(fk.ChildTable, Is.EqualTo(childTable));
116+
Assert.That(fk.ChildColumns, Is.EqualTo(new[] { childColumn }));
117+
}
118+
119+
[Test]
120+
public void GetForeignKeyConstraints_WithReservedWordKey_ReturnsCorrectForeignKeys()
121+
{
122+
// Arrange - Testing with SQLite reserved word "key" as parent table name
123+
const string parentTable = "key";
124+
const string parentColumn = "KeyId";
125+
const string childTable = "Locks";
126+
const string childColumn = "KeyRef";
127+
const string foreignKeyName = "FK_Locks_Key";
128+
129+
Provider.AddTable(parentTable, new Column(parentColumn, DbType.Int32, ColumnProperty.PrimaryKey));
130+
Provider.AddTable(childTable,
131+
new Column("Id", DbType.Int32, ColumnProperty.PrimaryKey),
132+
new Column(childColumn, DbType.Int32)
133+
);
134+
135+
Provider.AddForeignKey(foreignKeyName, childTable, childColumn, parentTable, parentColumn);
136+
137+
// Act
138+
var foreignKeyConstraints = Provider.GetForeignKeyConstraints(childTable);
139+
140+
// Assert
141+
Assert.That(foreignKeyConstraints.Length, Is.EqualTo(1));
142+
var fk = foreignKeyConstraints.Single();
143+
Assert.That(fk.Name, Is.EqualTo(foreignKeyName));
144+
Assert.That(fk.ParentTable, Is.EqualTo(parentTable));
145+
Assert.That(fk.ParentColumns, Is.EqualTo(new[] { parentColumn }));
146+
Assert.That(fk.ChildTable, Is.EqualTo(childTable));
147+
Assert.That(fk.ChildColumns, Is.EqualTo(new[] { childColumn }));
148+
}
149+
150+
[Test]
151+
public void GetForeignKeyConstraints_WithMultipleForeignKeysIncludingQuoted_ReturnsAllCorrectly()
152+
{
153+
// Arrange - Testing combination of regular and quoted table names
154+
const string regularParent = "NormalTable";
155+
const string quotedParent = "order";
156+
const string regularColumn = "NormalId";
157+
const string quotedColumn = "OrderId";
158+
const string childTable = "MixedChild";
159+
const string childColumnRegular = "NormalRef";
160+
const string childColumnQuoted = "OrderRef";
161+
const string fkRegular = "FK_Mixed_Normal";
162+
const string fkQuoted = "FK_Mixed_Order";
163+
164+
Provider.AddTable(regularParent, new Column(regularColumn, DbType.Int32, ColumnProperty.PrimaryKey));
165+
Provider.AddTable(quotedParent, new Column(quotedColumn, DbType.Int32, ColumnProperty.PrimaryKey));
166+
Provider.AddTable(childTable,
167+
new Column("Id", DbType.Int32, ColumnProperty.PrimaryKey),
168+
new Column(childColumnRegular, DbType.Int32),
169+
new Column(childColumnQuoted, DbType.Int32)
170+
);
171+
172+
Provider.AddForeignKey(fkRegular, childTable, childColumnRegular, regularParent, regularColumn);
173+
Provider.AddForeignKey(fkQuoted, childTable, childColumnQuoted, quotedParent, quotedColumn);
174+
175+
// Act
176+
var foreignKeyConstraints = Provider.GetForeignKeyConstraints(childTable);
177+
178+
// Assert
179+
Assert.That(foreignKeyConstraints.Length, Is.EqualTo(2));
180+
181+
var fkReg = foreignKeyConstraints.Single(x => x.Name == fkRegular);
182+
Assert.That(fkReg.ParentTable, Is.EqualTo(regularParent));
183+
Assert.That(fkReg.ParentColumns, Is.EqualTo(new[] { regularColumn }));
184+
Assert.That(fkReg.ChildColumns, Is.EqualTo(new[] { childColumnRegular }));
185+
186+
var fkQuo = foreignKeyConstraints.Single(x => x.Name == fkQuoted);
187+
Assert.That(fkQuo.ParentTable, Is.EqualTo(quotedParent));
188+
Assert.That(fkQuo.ParentColumns, Is.EqualTo(new[] { quotedColumn }));
189+
Assert.That(fkQuo.ChildColumns, Is.EqualTo(new[] { childColumnQuoted }));
190+
}
56191
}

0 commit comments

Comments
 (0)