Skip to content

Commit 83c582c

Browse files
authored
Merge pull request #8432 from kenjis/test-live-sqlite3
test: add test for SQLite3 getFieldData()
2 parents 0f43ba1 + efaa41a commit 83c582c

File tree

3 files changed

+164
-14
lines changed

3 files changed

+164
-14
lines changed

tests/system/Database/Live/SQLite/AlterTableTest.php renamed to tests/system/Database/Live/SQLite3/AlterTableTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* the LICENSE file that was distributed with this source code.
1010
*/
1111

12-
namespace CodeIgniter\Database\Live\SQLite;
12+
namespace CodeIgniter\Database\Live\SQLite3;
1313

1414
use CodeIgniter\Database\Exceptions\DataException;
1515
use CodeIgniter\Database\SQLite3\Forge;
@@ -43,12 +43,15 @@ protected function setUp(): void
4343
{
4444
parent::setUp();
4545

46+
if ($this->db->DBDriver !== 'SQLite3') {
47+
$this->markTestSkipped('This test is only for SQLite3.');
48+
}
49+
4650
$config = [
4751
'DBDriver' => 'SQLite3',
4852
'database' => ':memory:',
4953
'DBDebug' => true,
5054
];
51-
5255
$this->db = db_connect($config);
5356
$this->forge = Database::forge($config);
5457
$this->table = new Table($this->db, $this->forge);
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <[email protected]>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
namespace CodeIgniter\Database\Live\SQLite3;
13+
14+
use CodeIgniter\Database\SQLite3\Connection;
15+
use CodeIgniter\Database\SQLite3\Forge;
16+
use CodeIgniter\Test\CIUnitTestCase;
17+
use Config\Database;
18+
19+
/**
20+
* @group DatabaseLive
21+
*
22+
* @internal
23+
*/
24+
final class GetFieldDataTest extends CIUnitTestCase
25+
{
26+
/**
27+
* @var Connection
28+
*/
29+
protected $db;
30+
31+
private Forge $forge;
32+
33+
protected function setUp(): void
34+
{
35+
parent::setUp();
36+
37+
$this->db = Database::connect($this->DBGroup);
38+
if ($this->db->DBDriver !== 'SQLite3') {
39+
$this->markTestSkipped('This test is only for SQLite3.');
40+
}
41+
42+
$config = [
43+
'DBDriver' => 'SQLite3',
44+
'database' => 'database.db',
45+
'DBDebug' => true,
46+
];
47+
$this->db = db_connect($config);
48+
$this->forge = Database::forge($config);
49+
}
50+
51+
public function testGetFieldData(): void
52+
{
53+
$this->forge->dropTable('test1', true);
54+
55+
$this->forge->addField([
56+
'id' => [
57+
'type' => 'INT',
58+
'auto_increment' => true,
59+
],
60+
'text_not_null' => [
61+
'type' => 'VARCHAR',
62+
],
63+
'text_null' => [
64+
'type' => 'VARCHAR',
65+
'null' => true,
66+
],
67+
'int_default_0' => [
68+
'type' => 'INT',
69+
'default' => 0,
70+
],
71+
'text_default_null' => [
72+
'type' => 'VARCHAR',
73+
'default' => null,
74+
],
75+
'text_default_text_null' => [
76+
'type' => 'VARCHAR',
77+
'default' => 'null',
78+
],
79+
'text_default_abc' => [
80+
'type' => 'VARCHAR',
81+
'default' => 'abc',
82+
],
83+
]);
84+
$this->forge->addKey('id', true);
85+
$this->forge->createTable('test1');
86+
87+
$fields = $this->db->getFieldData('test1');
88+
89+
$this->assertJsonStringEqualsJsonString(
90+
json_encode([
91+
(object) [
92+
'name' => 'id',
93+
'type' => 'INTEGER',
94+
'max_length' => null,
95+
'default' => null, // The default value is not defined.
96+
'primary_key' => true,
97+
'nullable' => true,
98+
],
99+
(object) [
100+
'name' => 'text_not_null',
101+
'type' => 'VARCHAR',
102+
'max_length' => null,
103+
'default' => null, // The default value is not defined.
104+
'primary_key' => false,
105+
'nullable' => false,
106+
],
107+
(object) [
108+
'name' => 'text_null',
109+
'type' => 'VARCHAR',
110+
'max_length' => null,
111+
'default' => null, // The default value is not defined.
112+
'primary_key' => false,
113+
'nullable' => true,
114+
],
115+
(object) [
116+
'name' => 'int_default_0',
117+
'type' => 'INT',
118+
'max_length' => null,
119+
'default' => '0', // int 0
120+
'primary_key' => false,
121+
'nullable' => false,
122+
],
123+
(object) [
124+
'name' => 'text_default_null',
125+
'type' => 'VARCHAR',
126+
'max_length' => null,
127+
'default' => 'NULL', // NULL value
128+
'primary_key' => false,
129+
'nullable' => true,
130+
],
131+
(object) [
132+
'name' => 'text_default_text_null',
133+
'type' => 'VARCHAR',
134+
'max_length' => null,
135+
'default' => "'null'", // string "null"
136+
'primary_key' => false,
137+
'nullable' => false,
138+
],
139+
(object) [
140+
'name' => 'text_default_abc',
141+
'type' => 'VARCHAR',
142+
'max_length' => null,
143+
'default' => "'abc'", // string "abc"
144+
'primary_key' => false,
145+
'nullable' => false,
146+
],
147+
]),
148+
json_encode($fields)
149+
);
150+
151+
$this->forge->dropTable('test1', true);
152+
}
153+
}

tests/system/Database/Live/SQLite/GetIndexDataTest.php renamed to tests/system/Database/Live/SQLite3/GetIndexDataTest.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@
99
* the LICENSE file that was distributed with this source code.
1010
*/
1111

12-
namespace CodeIgniter\Database\Live\SQLite;
12+
namespace CodeIgniter\Database\Live\SQLite3;
1313

1414
use CodeIgniter\Database\SQLite3\Connection;
1515
use CodeIgniter\Database\SQLite3\Forge;
1616
use CodeIgniter\Test\CIUnitTestCase;
17-
use CodeIgniter\Test\DatabaseTestTrait;
1817
use Config\Database;
1918
use stdClass;
2019

@@ -25,15 +24,6 @@
2524
*/
2625
final class GetIndexDataTest extends CIUnitTestCase
2726
{
28-
use DatabaseTestTrait;
29-
30-
/**
31-
* In setUp() db connection is changed. So migration doesn't work
32-
*
33-
* @var bool
34-
*/
35-
protected $migrate = false;
36-
3727
/**
3828
* @var Connection
3929
*/
@@ -45,12 +35,16 @@ protected function setUp(): void
4535
{
4636
parent::setUp();
4737

38+
$this->db = Database::connect($this->DBGroup);
39+
if ($this->db->DBDriver !== 'SQLite3') {
40+
$this->markTestSkipped('This test is only for SQLite3.');
41+
}
42+
4843
$config = [
4944
'DBDriver' => 'SQLite3',
5045
'database' => 'database.db',
5146
'DBDebug' => true,
5247
];
53-
5448
$this->db = db_connect($config);
5549
$this->forge = Database::forge($config);
5650
}

0 commit comments

Comments
 (0)