From 123d987ca6a3f77ac2faa8eab87b7927536a9781 Mon Sep 17 00:00:00 2001 From: "Mark A. Connelly" <16519297+markconnellypro@users.noreply.github.com> Date: Sun, 3 Mar 2024 22:21:22 -0500 Subject: [PATCH] Separate each test into its own function; add testAssertTypeReturning function for which databases support RETURNING --- .../Database/Live/WriteTypeQueryTest.php | 258 +++++++++++++++--- 1 file changed, 220 insertions(+), 38 deletions(-) diff --git a/tests/system/Database/Live/WriteTypeQueryTest.php b/tests/system/Database/Live/WriteTypeQueryTest.php index 264528edb7f7..befb0327b89b 100644 --- a/tests/system/Database/Live/WriteTypeQueryTest.php +++ b/tests/system/Database/Live/WriteTypeQueryTest.php @@ -28,6 +28,25 @@ final class WriteTypeQueryTest extends CIUnitTestCase protected $refresh = true; protected $seed = CITestSeeder::class; + /** + * Whether CodeIgniter ignores RETURNING for isWriteType. + * + * Currently, only Postgre is supported by CodeIgniter. + * This method should be updated if support for RETURNING + * is expanded to other databases. + * + * @param string $dbDriver + */ + private function testAssertTypeReturning($dbDriver): string + { + $assertType = 'assertTrue'; + if ($dbDriver === 'Postgre') { + $assertType = 'assertFalse'; + } + + return $assertType; + } + public function testSet(): void { $sql = 'SET FOREIGN_KEY_CHECKS=0'; @@ -35,7 +54,7 @@ public function testSet(): void $this->assertTrue($this->db->isWriteType($sql)); } - public function testInsert(): void + public function testInsertBuilder(): void { $builder = $this->db->table('jobs'); @@ -47,11 +66,42 @@ public function testInsert(): void $sql = $builder->getCompiledInsert(); $this->assertTrue($this->db->isWriteType($sql)); + } + + public function testInsertOne(): void + { + $sql = "INSERT INTO my_table (col1, col2) VALUES ('Joe', 'Cool');"; + + $this->assertTrue($this->db->isWriteType($sql)); + } + + public function testInsertMulti(): void + { + $sql = <<<'SQL' + INSERT INTO my_table (col1, col2) + VALUES ('Joe', 'Cool') + RETURNING id; + SQL; + + $this->assertTrue($this->db->isWriteType($sql)); + } + + public function testInsertWithOne(): void + { + $sql = "WITH seqvals AS (SELECT '3' AS seqval) INSERT INTO my_table (col1, col2) SELECT 'Joe', seqval FROM seqvals;"; + $this->assertTrue($this->db->isWriteType($sql)); + } + + public function testInsertWithOneNoSpace(): void + { $sql = "WITH seqvals AS (SELECT '3' AS seqval)INSERT INTO my_table (col1, col2) SELECT 'Joe', seqval FROM seqvals;"; $this->assertTrue($this->db->isWriteType($sql)); + } + public function testInsertWithMulti(): void + { $sql = <<<'SQL' WITH seqvals AS (SELECT '3' AS seqval) INSERT INTO my_table (col1, col2) @@ -60,28 +110,50 @@ public function testInsert(): void SQL; $this->assertTrue($this->db->isWriteType($sql)); + } - $assertionType = 'assertTrue'; - if ($this->db->DBDriver === 'Postgre') { - $assertionType = 'assertFalse'; - } - + public function testInsertOneReturning(): void + { $sql = "INSERT INTO my_table (col1, col2) VALUES ('Joe', 'Cool') RETURNING id;"; - $this->{$assertionType}($this->db->isWriteType($sql)); - - $sql = "WITH seqvals AS (SELECT '3' AS seqval)INSERT INTO my_table (col1, col2) SELECT 'Joe', seqval FROM seqvals RETURNING id;"; + $assertType = $this->testAssertTypeReturning($this->db->DBDriver); - $this->{$assertionType}($this->db->isWriteType($sql)); + $this->{$assertType}($this->db->isWriteType($sql)); + } + public function testInsertMultiReturning(): void + { $sql = <<<'SQL' INSERT INTO my_table (col1, col2) VALUES ('Joe', 'Cool') RETURNING id; SQL; - $this->{$assertionType}($this->db->isWriteType($sql)); + $assertType = $this->testAssertTypeReturning($this->db->DBDriver); + + $this->{$assertType}($this->db->isWriteType($sql)); + } + + public function testInsertWithOneReturning(): void + { + $sql = "WITH seqvals AS (SELECT '3' AS seqval) INSERT INTO my_table (col1, col2) SELECT 'Joe', seqval FROM seqvals RETURNING id;"; + + $assertType = $this->testAssertTypeReturning($this->db->DBDriver); + + $this->{$assertType}($this->db->isWriteType($sql)); + } + + public function testInsertWithOneReturningNoSpace(): void + { + $sql = "WITH seqvals AS (SELECT '3' AS seqval)INSERT INTO my_table (col1, col2) SELECT 'Joe', seqval FROM seqvals RETURNING id;"; + + $assertType = $this->testAssertTypeReturning($this->db->DBDriver); + + $this->{$assertType}($this->db->isWriteType($sql)); + } + public function testInsertWithMultiReturning(): void + { $sql = <<<'SQL' WITH seqvals AS (SELECT '3' AS seqval) INSERT INTO my_table (col1, col2) @@ -90,21 +162,54 @@ public function testInsert(): void RETURNING id; SQL; - $this->{$assertionType}($this->db->isWriteType($sql)); + $assertType = $this->testAssertTypeReturning($this->db->DBDriver); + + $this->{$assertType}($this->db->isWriteType($sql)); } - public function testUpdate(): void + public function testUpdateBuilder(): void { $builder = new BaseBuilder('jobs', $this->db); $builder->testMode()->where('id', 1)->update(['name' => 'Programmer'], null, null); $sql = $builder->getCompiledInsert(); $this->assertTrue($this->db->isWriteType($sql)); + } + + public function testUpdateOne(): void + { + $sql = "UPDATE my_table SET col1 = 'foo' WHERE id = 2;"; + + $this->assertTrue($this->db->isWriteType($sql)); + } + + public function testUpdateMulti(): void + { + $sql = <<<'SQL' + UPDATE my_table + SET col1 = 'foo' + WHERE id = 2; + SQL; + + $this->assertTrue($this->db->isWriteType($sql)); + } + + public function testUpdateWithOne(): void + { + $sql = "WITH seqvals AS (SELECT '3' AS seqval) UPDATE my_table SET col1 = seqval FROM seqvals WHERE id = 2;"; + + $this->assertTrue($this->db->isWriteType($sql)); + } + public function testUpdateWithOneNoSpace(): void + { $sql = "WITH seqvals AS (SELECT '3' AS seqval)UPDATE my_table SET col1 = seqval FROM seqvals WHERE id = 2;"; $this->assertTrue($this->db->isWriteType($sql)); + } + public function testUpdateWithMulti(): void + { $sql = <<<'SQL' WITH seqvals AS (SELECT '3' AS seqval) UPDATE my_table @@ -114,20 +219,19 @@ public function testUpdate(): void SQL; $this->assertTrue($this->db->isWriteType($sql)); + } - $assertionType = 'assertTrue'; - if ($this->db->DBDriver === 'Postgre') { - $assertionType = 'assertFalse'; - } - + public function testUpdateOneReturning(): void + { $sql = "UPDATE my_table SET col1 = 'foo' WHERE id = 2 RETURNING *;"; - $this->{$assertionType}($this->db->isWriteType($sql)); - - $sql = "WITH seqvals AS (SELECT '3' AS seqval)UPDATE my_table SET col1 = seqval FROM seqvals WHERE id = 2 RETURNING *;"; + $assertType = $this->testAssertTypeReturning($this->db->DBDriver); - $this->{$assertionType}($this->db->isWriteType($sql)); + $this->{$assertType}($this->db->isWriteType($sql)); + } + public function testUpdateMultiReturning(): void + { $sql = <<<'SQL' UPDATE my_table SET col1 = 'foo' @@ -135,8 +239,31 @@ public function testUpdate(): void RETURNING *; SQL; - $this->{$assertionType}($this->db->isWriteType($sql)); + $assertType = $this->testAssertTypeReturning($this->db->DBDriver); + + $this->{$assertType}($this->db->isWriteType($sql)); + } + + public function testUpdateWithOneReturning(): void + { + $sql = "WITH seqvals AS (SELECT '3' AS seqval) UPDATE my_table SET col1 = seqval FROM seqvals WHERE id = 2 RETURNING *;"; + + $assertType = $this->testAssertTypeReturning($this->db->DBDriver); + + $this->{$assertType}($this->db->isWriteType($sql)); + } + + public function testUpdateWithOneReturningNoSpace(): void + { + $sql = "WITH seqvals AS (SELECT '3' AS seqval)UPDATE my_table SET col1 = seqval FROM seqvals WHERE id = 2 RETURNING *;"; + + $assertType = $this->testAssertTypeReturning($this->db->DBDriver); + $this->{$assertType}($this->db->isWriteType($sql)); + } + + public function testUpdateWithMultiReturning(): void + { $sql = <<<'SQL' WITH seqvals AS (SELECT '3' AS seqval) UPDATE my_table @@ -146,24 +273,52 @@ public function testUpdate(): void RETURNING *; SQL; - $this->{$assertionType}($this->db->isWriteType($sql)); + $assertType = $this->testAssertTypeReturning($this->db->DBDriver); + + $this->{$assertType}($this->db->isWriteType($sql)); } - public function testDelete(): void + public function testDeleteBuilder(): void { $builder = $this->db->table('jobs'); $sql = $builder->testMode()->delete(['id' => 1], null, true); $this->assertTrue($this->db->isWriteType($sql)); + } + public function testDeleteOne(): void + { $sql = 'DELETE FROM my_table WHERE id = 2;'; $this->assertTrue($this->db->isWriteType($sql)); + } + + public function testDeleteMulti(): void + { + $sql = <<<'SQL' + DELETE FROM my_table + WHERE id = 2; + SQL; + + $this->assertTrue($this->db->isWriteType($sql)); + } + + public function testDeleteWithOne(): void + { + $sql = "WITH seqvals AS (SELECT '3' AS seqval) DELETE FROM my_table JOIN seqvals ON col1 = seqval;"; + $this->assertTrue($this->db->isWriteType($sql)); + } + + public function testDeleteWithOneNoSpace(): void + { $sql = "WITH seqvals AS (SELECT '3' AS seqval)DELETE FROM my_table JOIN seqvals ON col1 = seqval;"; $this->assertTrue($this->db->isWriteType($sql)); + } + public function testDeleteWithMulti(): void + { $sql = <<<'SQL' WITH seqvals AS (SELECT '3' AS seqval) @@ -173,28 +328,50 @@ public function testDelete(): void SQL; $this->assertTrue($this->db->isWriteType($sql)); + } - $assertionType = 'assertTrue'; - if ($this->db->DBDriver === 'Postgre') { - $assertionType = 'assertFalse'; - } - + public function testDeleteOneReturning(): void + { $sql = 'DELETE FROM my_table WHERE id = 2 RETURNING *;'; - $this->{$assertionType}($this->db->isWriteType($sql)); - - $sql = "WITH seqvals AS (SELECT '3' AS seqval)DELETE FROM my_table JOIN seqvals ON col1 = seqval RETURNING *;"; + $assertType = $this->testAssertTypeReturning($this->db->DBDriver); - $this->{$assertionType}($this->db->isWriteType($sql)); + $this->{$assertType}($this->db->isWriteType($sql)); + } + public function testDeleteMultiReturning(): void + { $sql = <<<'SQL' DELETE FROM my_table WHERE id = 2 RETURNING *; SQL; - $this->{$assertionType}($this->db->isWriteType($sql)); + $assertType = $this->testAssertTypeReturning($this->db->DBDriver); + + $this->{$assertType}($this->db->isWriteType($sql)); + } + + public function testDeleteWithOneReturning(): void + { + $sql = "WITH seqvals AS (SELECT '3' AS seqval) DELETE FROM my_table JOIN seqvals ON col1 = seqval RETURNING *;"; + + $assertType = $this->testAssertTypeReturning($this->db->DBDriver); + + $this->{$assertType}($this->db->isWriteType($sql)); + } + + public function testDeleteWithOneReturningNoSpace(): void + { + $sql = "WITH seqvals AS (SELECT '3' AS seqval)DELETE FROM my_table JOIN seqvals ON col1 = seqval RETURNING *;"; + + $assertType = $this->testAssertTypeReturning($this->db->DBDriver); + + $this->{$assertType}($this->db->isWriteType($sql)); + } + public function testDeleteWithMultiReturning(): void + { $sql = <<<'SQL' WITH seqvals AS (SELECT '3' AS seqval) @@ -204,7 +381,9 @@ public function testDelete(): void RETURNING *; SQL; - $this->{$assertionType}($this->db->isWriteType($sql)); + $assertType = $this->testAssertTypeReturning($this->db->DBDriver); + + $this->{$assertType}($this->db->isWriteType($sql)); } public function testReplace(): void @@ -225,19 +404,22 @@ public function testReplace(): void $this->assertTrue($this->db->isWriteType($sql)); } - public function testCreate(): void + public function testCreateDatabase(): void { $sql = 'CREATE DATABASE foo'; $this->assertTrue($this->db->isWriteType($sql)); } - public function testDrop(): void + public function testDropDatabase(): void { $sql = 'DROP DATABASE foo'; $this->assertTrue($this->db->isWriteType($sql)); + } + public function testDropTable(): void + { $sql = 'DROP TABLE foo'; $this->assertTrue($this->db->isWriteType($sql));