Skip to content

test: add test for SQLite3 getFieldData() #8432

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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