Skip to content

Commit 4b7766a

Browse files
committed
tests: Add tests for foreign key parsing
1 parent f2c3c2b commit 4b7766a

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/tests/testsqlobjects.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,19 @@ void TestTable::withoutRowid()
7676
") WITHOUT ROWID;"));
7777
}
7878

79+
void TestTable::foreignKeys()
80+
{
81+
Table tt("testtable");
82+
FieldPtr f = FieldPtr(new Field("a", "integer"));
83+
f->setForeignKey("b(c)");
84+
tt.addField(f);
85+
86+
QCOMPARE(tt.sql(), QString("CREATE TABLE `testtable` (\n"
87+
"\t`a`\tinteger,\n"
88+
"\tFOREIGN KEY(`a`) REFERENCES b(c)\n"
89+
");"));
90+
}
91+
7992
void TestTable::parseSQL()
8093
{
8194
QString sSQL = "create TABLE hero (\n"
@@ -224,6 +237,21 @@ void TestTable::parseSQLEscapedQuotes()
224237
QCOMPARE(tab.fields().at(0)->defaultValue(), QString("'a''a'"));
225238
}
226239

240+
void TestTable::parseSQLForeignKeys()
241+
{
242+
QString sql = "CREATE TABLE foreign_key_test(a int, b int, foreign key (a) references x, foreign key (b) references w(z) on delete set null);";
243+
244+
Table tab = Table::parseSQL(sql).first;
245+
246+
QCOMPARE(tab.name(), QString("foreign_key_test"));
247+
QCOMPARE(tab.fields().at(0)->name(), QString("a"));
248+
QCOMPARE(tab.fields().at(0)->type(), QString("int"));
249+
QCOMPARE(tab.fields().at(0)->foreignKey(), QString("x"));
250+
QCOMPARE(tab.fields().at(1)->name(), QString("b"));
251+
QCOMPARE(tab.fields().at(1)->type(), QString("int"));
252+
QCOMPARE(tab.fields().at(1)->foreignKey(), QString("w ( z ) on delete set null"));
253+
}
254+
227255
void TestTable::createTableWithIn()
228256
{
229257
QString sSQL = "CREATE TABLE not_working("

src/tests/testsqlobjects.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ private slots:
1111
void autoincrement();
1212
void notnull();
1313
void withoutRowid();
14+
void foreignKeys();
1415

1516
void parseSQL();
1617
void parseSQLdefaultexpr();
@@ -21,6 +22,7 @@ private slots:
2122
void parseSQLWithoutRowid();
2223
void parseNonASCIIChars();
2324
void parseSQLEscapedQuotes();
25+
void parseSQLForeignKeys();
2426
void createTableWithIn();
2527
void createTableWithNotLikeConstraint();
2628
};

0 commit comments

Comments
 (0)