From 36d61500d053a7081a04e3bd3e7e2f86187b57b7 Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 19 Jan 2024 10:47:00 +0900 Subject: [PATCH 1/5] test: add test for getFieldData() on SQLite3 --- .../Database/Live/SQLite/GetFieldDataTest.php | 151 ++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 tests/system/Database/Live/SQLite/GetFieldDataTest.php diff --git a/tests/system/Database/Live/SQLite/GetFieldDataTest.php b/tests/system/Database/Live/SQLite/GetFieldDataTest.php new file mode 100644 index 000000000000..48f78ea4d3bc --- /dev/null +++ b/tests/system/Database/Live/SQLite/GetFieldDataTest.php @@ -0,0 +1,151 @@ + + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + +namespace CodeIgniter\Database\Live\SQLite; + +use CodeIgniter\Database\SQLite3\Connection; +use CodeIgniter\Database\SQLite3\Forge; +use CodeIgniter\Test\CIUnitTestCase; +use Config\Database; + +/** + * @group DatabaseLive + * + * @internal + */ +final class GetFieldDataTest extends CIUnitTestCase +{ + /** + * @var Connection + */ + protected $db; + + private Forge $forge; + + protected function setUp(): void + { + parent::setUp(); + + $this->db = Database::connect($this->DBGroup); + if ($this->db->DBDriver !== 'SQLite3') { + $this->markTestSkipped('This test is only for SQLite3.'); + } + + $config = [ + 'DBDriver' => 'SQLite3', + 'database' => 'database.db', + 'DBDebug' => true, + ]; + $this->db = db_connect($config); + $this->forge = Database::forge($config); + } + + public function testGetFieldData(): void + { + $this->forge->dropTable('test1', true); + + $this->forge->addField([ + 'id' => [ + 'type' => 'INT', + 'auto_increment' => true, + ], + 'text_not_null' => [ + 'type' => 'VARCHAR', + ], + 'text_null' => [ + 'type' => 'VARCHAR', + 'null' => true, + ], + 'int_default_0' => [ + 'type' => 'INT', + 'default' => 0, + ], + 'text_default_null' => [ + 'type' => 'VARCHAR', + 'default' => null, + ], + 'text_default_text_null' => [ + 'type' => 'VARCHAR', + 'default' => 'null', + ], + 'text_default_abc' => [ + 'type' => 'VARCHAR', + 'default' => 'abc', + ], + ]); + $this->forge->addKey('id', true); + $this->forge->createTable('test1'); + + $fields = $this->db->getFieldData('test1'); + + $this->assertJsonStringEqualsJsonString( + json_encode([ + (object) [ + 'name' => 'id', + 'type' => 'INTEGER', + 'max_length' => null, + 'default' => null, // The default value is not defined. + 'primary_key' => true, + 'nullable' => true, + ], + (object) [ + 'name' => 'text_not_null', + 'type' => 'VARCHAR', + 'max_length' => null, + 'default' => null, // The default value is not defined. + 'primary_key' => false, + 'nullable' => false, + ], + (object) [ + 'name' => 'text_null', + 'type' => 'VARCHAR', + 'max_length' => null, + 'default' => null, // The default value is not defined. + 'primary_key' => false, + 'nullable' => true, + ], + (object) [ + 'name' => 'int_default_0', + 'type' => 'INT', + 'max_length' => null, + 'default' => '0', // int 0 + 'primary_key' => false, + 'nullable' => false, + ], + (object) [ + 'name' => 'text_default_null', + 'type' => 'VARCHAR', + 'max_length' => null, + 'default' => 'NULL', // NULL value + 'primary_key' => false, + 'nullable' => true, + ], + (object) [ + 'name' => 'text_default_text_null', + 'type' => 'VARCHAR', + 'max_length' => null, + 'default' => "'null'", // string "null" + 'primary_key' => false, + 'nullable' => false, + ], + (object) [ + 'name' => 'text_default_abc', + 'type' => 'VARCHAR', + 'max_length' => null, + 'default' => "'abc'", // string "abc" + 'primary_key' => false, + 'nullable' => false, + ], + ]), + json_encode($fields) + ); + } +} From 2938cdfab7be074d18c68e8d07fc79468733eb77 Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 19 Jan 2024 10:50:06 +0900 Subject: [PATCH 2/5] test: rename folder name to exact same name as DB driver --- .../system/Database/Live/{SQLite => SQLite3}/AlterTableTest.php | 2 +- .../Database/Live/{SQLite => SQLite3}/GetFieldDataTest.php | 2 +- .../Database/Live/{SQLite => SQLite3}/GetIndexDataTest.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) rename tests/system/Database/Live/{SQLite => SQLite3}/AlterTableTest.php (99%) rename tests/system/Database/Live/{SQLite => SQLite3}/GetFieldDataTest.php (99%) rename tests/system/Database/Live/{SQLite => SQLite3}/GetIndexDataTest.php (98%) diff --git a/tests/system/Database/Live/SQLite/AlterTableTest.php b/tests/system/Database/Live/SQLite3/AlterTableTest.php similarity index 99% rename from tests/system/Database/Live/SQLite/AlterTableTest.php rename to tests/system/Database/Live/SQLite3/AlterTableTest.php index 74d53cb0783d..1a66c3a9018a 100644 --- a/tests/system/Database/Live/SQLite/AlterTableTest.php +++ b/tests/system/Database/Live/SQLite3/AlterTableTest.php @@ -9,7 +9,7 @@ * the LICENSE file that was distributed with this source code. */ -namespace CodeIgniter\Database\Live\SQLite; +namespace CodeIgniter\Database\Live\SQLite3; use CodeIgniter\Database\Exceptions\DataException; use CodeIgniter\Database\SQLite3\Forge; diff --git a/tests/system/Database/Live/SQLite/GetFieldDataTest.php b/tests/system/Database/Live/SQLite3/GetFieldDataTest.php similarity index 99% rename from tests/system/Database/Live/SQLite/GetFieldDataTest.php rename to tests/system/Database/Live/SQLite3/GetFieldDataTest.php index 48f78ea4d3bc..978a6704e570 100644 --- a/tests/system/Database/Live/SQLite/GetFieldDataTest.php +++ b/tests/system/Database/Live/SQLite3/GetFieldDataTest.php @@ -9,7 +9,7 @@ * the LICENSE file that was distributed with this source code. */ -namespace CodeIgniter\Database\Live\SQLite; +namespace CodeIgniter\Database\Live\SQLite3; use CodeIgniter\Database\SQLite3\Connection; use CodeIgniter\Database\SQLite3\Forge; diff --git a/tests/system/Database/Live/SQLite/GetIndexDataTest.php b/tests/system/Database/Live/SQLite3/GetIndexDataTest.php similarity index 98% rename from tests/system/Database/Live/SQLite/GetIndexDataTest.php rename to tests/system/Database/Live/SQLite3/GetIndexDataTest.php index c8b34d7ee651..491ff7a8a20e 100644 --- a/tests/system/Database/Live/SQLite/GetIndexDataTest.php +++ b/tests/system/Database/Live/SQLite3/GetIndexDataTest.php @@ -9,7 +9,7 @@ * the LICENSE file that was distributed with this source code. */ -namespace CodeIgniter\Database\Live\SQLite; +namespace CodeIgniter\Database\Live\SQLite3; use CodeIgniter\Database\SQLite3\Connection; use CodeIgniter\Database\SQLite3\Forge; From 0499e8167f3e2ed0af385a9899757a1919e9aa35 Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 19 Jan 2024 11:05:54 +0900 Subject: [PATCH 3/5] test: add markTestSkipped() --- tests/system/Database/Live/SQLite3/AlterTableTest.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/system/Database/Live/SQLite3/AlterTableTest.php b/tests/system/Database/Live/SQLite3/AlterTableTest.php index 1a66c3a9018a..4a06a8ada091 100644 --- a/tests/system/Database/Live/SQLite3/AlterTableTest.php +++ b/tests/system/Database/Live/SQLite3/AlterTableTest.php @@ -43,12 +43,15 @@ protected function setUp(): void { parent::setUp(); + if ($this->db->DBDriver !== 'SQLite3') { + $this->markTestSkipped('This test is only for SQLite3.'); + } + $config = [ 'DBDriver' => 'SQLite3', 'database' => ':memory:', 'DBDebug' => true, ]; - $this->db = db_connect($config); $this->forge = Database::forge($config); $this->table = new Table($this->db, $this->forge); From 3b60067d68dbc29fe0d6bd8a3f39677d7fb8c5ff Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 19 Jan 2024 11:07:17 +0900 Subject: [PATCH 4/5] test: add markTestSkipped() and remove DatabaseTestTrait --- .../Database/Live/SQLite3/GetIndexDataTest.php | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/tests/system/Database/Live/SQLite3/GetIndexDataTest.php b/tests/system/Database/Live/SQLite3/GetIndexDataTest.php index 491ff7a8a20e..6fdd81b345eb 100644 --- a/tests/system/Database/Live/SQLite3/GetIndexDataTest.php +++ b/tests/system/Database/Live/SQLite3/GetIndexDataTest.php @@ -14,7 +14,6 @@ use CodeIgniter\Database\SQLite3\Connection; use CodeIgniter\Database\SQLite3\Forge; use CodeIgniter\Test\CIUnitTestCase; -use CodeIgniter\Test\DatabaseTestTrait; use Config\Database; use stdClass; @@ -25,15 +24,6 @@ */ final class GetIndexDataTest extends CIUnitTestCase { - use DatabaseTestTrait; - - /** - * In setUp() db connection is changed. So migration doesn't work - * - * @var bool - */ - protected $migrate = false; - /** * @var Connection */ @@ -45,6 +35,11 @@ protected function setUp(): void { parent::setUp(); + $this->db = Database::connect($this->DBGroup); + if ($this->db->DBDriver !== 'SQLite3') { + $this->markTestSkipped('This test is only for SQLite3.'); + } + $config = [ 'DBDriver' => 'SQLite3', 'database' => 'database.db', From efaa41ac402b229a92acc6ad846232d67a62a930 Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 19 Jan 2024 11:20:33 +0900 Subject: [PATCH 5/5] test: remove test1 table after testing --- tests/system/Database/Live/SQLite3/GetFieldDataTest.php | 2 ++ tests/system/Database/Live/SQLite3/GetIndexDataTest.php | 1 - 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/system/Database/Live/SQLite3/GetFieldDataTest.php b/tests/system/Database/Live/SQLite3/GetFieldDataTest.php index 978a6704e570..252c6485f7f8 100644 --- a/tests/system/Database/Live/SQLite3/GetFieldDataTest.php +++ b/tests/system/Database/Live/SQLite3/GetFieldDataTest.php @@ -147,5 +147,7 @@ public function testGetFieldData(): void ]), json_encode($fields) ); + + $this->forge->dropTable('test1', true); } } diff --git a/tests/system/Database/Live/SQLite3/GetIndexDataTest.php b/tests/system/Database/Live/SQLite3/GetIndexDataTest.php index 6fdd81b345eb..cf05b7a7f07b 100644 --- a/tests/system/Database/Live/SQLite3/GetIndexDataTest.php +++ b/tests/system/Database/Live/SQLite3/GetIndexDataTest.php @@ -45,7 +45,6 @@ protected function setUp(): void 'database' => 'database.db', 'DBDebug' => true, ]; - $this->db = db_connect($config); $this->forge = Database::forge($config); }