Skip to content

Commit

Permalink
Merge pull request #8432 from kenjis/test-live-sqlite3
Browse files Browse the repository at this point in the history
test: add test for SQLite3 getFieldData()
  • Loading branch information
kenjis authored Jan 20, 2024
2 parents 0f43ba1 + efaa41a commit 83c582c
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
153 changes: 153 additions & 0 deletions tests/system/Database/Live/SQLite3/GetFieldDataTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?php

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace CodeIgniter\Database\Live\SQLite3;

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)
);

$this->forge->dropTable('test1', true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@
* 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;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\DatabaseTestTrait;
use Config\Database;
use stdClass;

Expand All @@ -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
*/
Expand All @@ -45,12 +35,16 @@ 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);
}
Expand Down

0 comments on commit 83c582c

Please sign in to comment.