Skip to content

Commit 1b95119

Browse files
committed
Add FOREIGN KEY tests
1 parent 2f93dbc commit 1b95119

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

tests/WP_SQLite_Driver_Tests.php

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6581,4 +6581,116 @@ public function testRollbackNonExistentTransactionSavepoint(): void {
65816581
$this->expectExceptionMessage( 'no such savepoint: sp1' );
65826582
$this->assertQuery( 'ROLLBACK TO SAVEPOINT sp1' );
65836583
}
6584+
6585+
public function testForeignKeyOnUpdateNoAction(): void {
6586+
$this->assertQuery( 'CREATE TABLE t1 (id INT PRIMARY KEY)' );
6587+
$this->assertQuery( 'CREATE TABLE t2 (id INT, FOREIGN KEY (id) REFERENCES t1 (id) ON UPDATE NO ACTION)' );
6588+
$this->assertQuery( 'INSERT INTO t1 (id) VALUES (1)' );
6589+
$this->assertQuery( 'INSERT INTO t2 (id) VALUES (1)' );
6590+
6591+
$this->expectException( 'WP_SQLite_Driver_Exception' );
6592+
$this->expectExceptionMessage( 'SQLSTATE[23000]: Integrity constraint violation: 19 FOREIGN KEY constraint failed' );
6593+
$this->assertQuery( 'UPDATE t1 SET id = 2 WHERE id = 1' );
6594+
}
6595+
6596+
public function testForeignKeyOnUpdateRestrict(): void {
6597+
$this->assertQuery( 'CREATE TABLE t1 (id INT PRIMARY KEY)' );
6598+
$this->assertQuery( 'CREATE TABLE t2 (id INT, FOREIGN KEY (id) REFERENCES t1 (id) ON UPDATE RESTRICT)' );
6599+
$this->assertQuery( 'INSERT INTO t1 (id) VALUES (1)' );
6600+
$this->assertQuery( 'INSERT INTO t2 (id) VALUES (1)' );
6601+
6602+
$this->expectException( 'WP_SQLite_Driver_Exception' );
6603+
$this->expectExceptionMessage( 'SQLSTATE[23000]: Integrity constraint violation: 19 FOREIGN KEY constraint failed' );
6604+
$this->assertQuery( 'UPDATE t1 SET id = 2 WHERE id = 1' );
6605+
}
6606+
6607+
public function testForeignKeyOnUpdateCascade(): void {
6608+
$this->assertQuery( 'CREATE TABLE t1 (id INT PRIMARY KEY)' );
6609+
$this->assertQuery( 'CREATE TABLE t2 (id INT, FOREIGN KEY (id) REFERENCES t1 (id) ON UPDATE CASCADE)' );
6610+
$this->assertQuery( 'INSERT INTO t1 (id) VALUES (1)' );
6611+
$this->assertQuery( 'INSERT INTO t2 (id) VALUES (1)' );
6612+
6613+
$this->assertQuery( 'UPDATE t1 SET id = 2 WHERE id = 1' );
6614+
$result = $this->assertQuery( 'SELECT * FROM t2' );
6615+
$this->assertEquals( array( (object) array( 'id' => '2' ) ), $result );
6616+
}
6617+
6618+
public function testForeignKeyOnUpdateSetNull(): void {
6619+
$this->assertQuery( 'CREATE TABLE t1 (id INT PRIMARY KEY)' );
6620+
$this->assertQuery( 'CREATE TABLE t2 (id INT, FOREIGN KEY (id) REFERENCES t1 (id) ON UPDATE SET NULL)' );
6621+
$this->assertQuery( 'INSERT INTO t1 (id) VALUES (1)' );
6622+
$this->assertQuery( 'INSERT INTO t2 (id) VALUES (1)' );
6623+
6624+
$this->assertQuery( 'UPDATE t1 SET id = 2 WHERE id = 1' );
6625+
$result = $this->assertQuery( 'SELECT * FROM t2' );
6626+
$this->assertEquals( array( (object) array( 'id' => null ) ), $result );
6627+
}
6628+
6629+
public function testForeignKeyOnUpdateSetDefault(): void {
6630+
$this->assertQuery( 'CREATE TABLE t1 (id INT PRIMARY KEY)' );
6631+
$this->assertQuery( 'CREATE TABLE t2 (id INT DEFAULT 0, FOREIGN KEY (id) REFERENCES t1 (id) ON UPDATE SET DEFAULT)' );
6632+
$this->assertQuery( 'INSERT INTO t1 (id) VALUES (0)' );
6633+
$this->assertQuery( 'INSERT INTO t1 (id) VALUES (1)' );
6634+
$this->assertQuery( 'INSERT INTO t2 (id) VALUES (1)' );
6635+
6636+
$this->assertQuery( 'UPDATE t1 SET id = 2 WHERE id = 1' );
6637+
$result = $this->assertQuery( 'SELECT * FROM t2' );
6638+
$this->assertEquals( array( (object) array( 'id' => '0' ) ), $result );
6639+
}
6640+
6641+
public function testForeignKeyOnDeleteNoAction(): void {
6642+
$this->assertQuery( 'CREATE TABLE t1 (id INT PRIMARY KEY)' );
6643+
$this->assertQuery( 'CREATE TABLE t2 (id INT, FOREIGN KEY (id) REFERENCES t1 (id) ON DELETE NO ACTION)' );
6644+
$this->assertQuery( 'INSERT INTO t1 (id) VALUES (1)' );
6645+
$this->assertQuery( 'INSERT INTO t2 (id) VALUES (1)' );
6646+
6647+
$this->expectException( 'WP_SQLite_Driver_Exception' );
6648+
$this->expectExceptionMessage( 'SQLSTATE[23000]: Integrity constraint violation: 19 FOREIGN KEY constraint failed' );
6649+
$this->assertQuery( 'DELETE FROM t1 WHERE id = 1' );
6650+
}
6651+
6652+
public function testForeignKeyOnDeleteRestrict(): void {
6653+
$this->assertQuery( 'CREATE TABLE t1 (id INT PRIMARY KEY)' );
6654+
$this->assertQuery( 'CREATE TABLE t2 (id INT, FOREIGN KEY (id) REFERENCES t1 (id) ON DELETE RESTRICT)' );
6655+
$this->assertQuery( 'INSERT INTO t1 (id) VALUES (1)' );
6656+
$this->assertQuery( 'INSERT INTO t2 (id) VALUES (1)' );
6657+
6658+
$this->expectException( 'WP_SQLite_Driver_Exception' );
6659+
$this->expectExceptionMessage( 'SQLSTATE[23000]: Integrity constraint violation: 19 FOREIGN KEY constraint failed' );
6660+
$this->assertQuery( 'DELETE FROM t1 WHERE id = 1' );
6661+
}
6662+
6663+
public function testForeignKeyOnDeleteCascade(): void {
6664+
$this->assertQuery( 'CREATE TABLE t1 (id INT PRIMARY KEY)' );
6665+
$this->assertQuery( 'CREATE TABLE t2 (id INT, FOREIGN KEY (id) REFERENCES t1 (id) ON DELETE CASCADE)' );
6666+
$this->assertQuery( 'INSERT INTO t1 (id) VALUES (1)' );
6667+
$this->assertQuery( 'INSERT INTO t2 (id) VALUES (1)' );
6668+
6669+
$this->assertQuery( 'DELETE FROM t1 WHERE id = 1' );
6670+
$result = $this->assertQuery( 'SELECT * FROM t2' );
6671+
$this->assertEquals( array(), $result );
6672+
}
6673+
6674+
public function testForeignKeyOnDeleteSetNull(): void {
6675+
$this->assertQuery( 'CREATE TABLE t1 (id INT PRIMARY KEY)' );
6676+
$this->assertQuery( 'CREATE TABLE t2 (id INT, FOREIGN KEY (id) REFERENCES t1 (id) ON DELETE SET NULL)' );
6677+
$this->assertQuery( 'INSERT INTO t1 (id) VALUES (1)' );
6678+
$this->assertQuery( 'INSERT INTO t2 (id) VALUES (1)' );
6679+
6680+
$this->assertQuery( 'DELETE FROM t1 WHERE id = 1' );
6681+
$result = $this->assertQuery( 'SELECT * FROM t2' );
6682+
$this->assertEquals( array( (object) array( 'id' => null ) ), $result );
6683+
}
6684+
6685+
public function testForeignKeyOnDeleteSetDefault(): void {
6686+
$this->assertQuery( 'CREATE TABLE t1 (id INT PRIMARY KEY)' );
6687+
$this->assertQuery( 'CREATE TABLE t2 (id INT DEFAULT 0, FOREIGN KEY (id) REFERENCES t1 (id) ON DELETE SET DEFAULT)' );
6688+
$this->assertQuery( 'INSERT INTO t1 (id) VALUES (0)' );
6689+
$this->assertQuery( 'INSERT INTO t1 (id) VALUES (1)' );
6690+
$this->assertQuery( 'INSERT INTO t2 (id) VALUES (1)' );
6691+
6692+
$this->assertQuery( 'DELETE FROM t1 WHERE id = 1' );
6693+
$result = $this->assertQuery( 'SELECT * FROM t2' );
6694+
$this->assertEquals( array( (object) array( 'id' => '0' ) ), $result );
6695+
}
65846696
}

0 commit comments

Comments
 (0)