Skip to content
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

test: add tests for getFieldData() type #8482

Merged
merged 1 commit into from
Feb 10, 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
58 changes: 58 additions & 0 deletions tests/system/Database/Live/AbstractGetFieldDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,64 @@ protected function createTableForDefault()
$this->forge->createTable($this->table);
}

protected function createTableForType()
{
$this->forge->dropTable($this->table, true);

// missing types:
// TINYINT,MEDIUMINT,BIT,YEAR,BINARY,VARBINARY,TINYTEXT,LONGTEXT,
// JSON,Spatial data types
// `id` must be INTEGER else SQLite3 error on not null for autoincrement field.
$fields = [
'id' => ['type' => 'INTEGER', 'constraint' => 20, 'auto_increment' => true],
'type_varchar' => ['type' => 'VARCHAR', 'constraint' => 40, 'null' => true],
'type_char' => ['type' => 'CHAR', 'constraint' => 10, 'null' => true],
// TEXT should not be used on SQLSRV. It is deprecated.
'type_text' => ['type' => 'TEXT', 'null' => true],
'type_smallint' => ['type' => 'SMALLINT', 'null' => true],
'type_integer' => ['type' => 'INTEGER', 'null' => true],
'type_float' => ['type' => 'FLOAT', 'null' => true],
'type_numeric' => ['type' => 'NUMERIC', 'constraint' => '18,2', 'null' => true],
'type_date' => ['type' => 'DATE', 'null' => true],
'type_time' => ['type' => 'TIME', 'null' => true],
// On SQLSRV `datetime2` is recommended.
'type_datetime' => ['type' => 'DATETIME', 'null' => true],
'type_timestamp' => ['type' => 'TIMESTAMP', 'null' => true],
'type_bigint' => ['type' => 'BIGINT', 'null' => true],
'type_real' => ['type' => 'REAL', 'null' => true],
'type_enum' => ['type' => 'ENUM', 'constraint' => ['appel', 'pears'], 'null' => true],
'type_set' => ['type' => 'SET', 'constraint' => ['one', 'two'], 'null' => true],
'type_mediumtext' => ['type' => 'MEDIUMTEXT', 'null' => true],
'type_double' => ['type' => 'DOUBLE', 'null' => true],
'type_decimal' => ['type' => 'DECIMAL', 'constraint' => '18,4', 'null' => true],
'type_blob' => ['type' => 'BLOB', 'null' => true],
'type_boolean' => ['type' => 'BOOLEAN', 'null' => true],
];

if ($this->db->DBDriver === 'Postgre') {
unset(
$fields['type_enum'],
$fields['type_set'],
$fields['type_mediumtext'],
$fields['type_double'],
$fields['type_blob']
);
}

if ($this->db->DBDriver === 'SQLSRV') {
unset(
$fields['type_set'],
$fields['type_mediumtext'],
$fields['type_double'],
$fields['type_blob']
);
}

$this->forge->addField($fields);
$this->forge->addKey('id', true);
$this->forge->createTable($this->table);
}

abstract public function testGetFieldDataDefault(): void;

protected function assertSameFieldData(array $expected, array $actual)
Expand Down
179 changes: 179 additions & 0 deletions tests/system/Database/Live/MySQLi/GetFieldDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,183 @@ public function testGetFieldDataDefault(): void
];
$this->assertSameFieldData($expected, $fields);
}

public function testGetFieldDataType(): void
{
$this->createTableForType();

$fields = $this->db->getFieldData($this->table);

$expected = [
0 => (object) [
'name' => 'id',
'type' => 'int',
'max_length' => $this->isOldMySQL() ? 20 : null,
'nullable' => false,
'default' => null,
'primary_key' => 1,
],
1 => (object) [
'name' => 'type_varchar',
'type' => 'varchar',
'max_length' => 40,
'nullable' => true,
'default' => null,
'primary_key' => 0,
],
2 => (object) [
'name' => 'type_char',
'type' => 'char',
'max_length' => 10,
'nullable' => true,
'default' => null,
'primary_key' => 0,
],
3 => (object) [
'name' => 'type_text',
'type' => 'text',
'max_length' => null,
'nullable' => true,
'default' => null,
'primary_key' => 0,
],
4 => (object) [
'name' => 'type_smallint',
'type' => 'smallint',
'max_length' => $this->isOldMySQL() ? 6 : null,
'nullable' => true,
'default' => null,
'primary_key' => 0,
],
5 => (object) [
'name' => 'type_integer',
'type' => 'int',
'max_length' => $this->isOldMySQL() ? 11 : null,
'nullable' => true,
'default' => null,
'primary_key' => 0,
],
6 => (object) [
'name' => 'type_float',
'type' => 'float',
'max_length' => null,
'nullable' => true,
'default' => null,
'primary_key' => 0,
],
7 => (object) [
'name' => 'type_numeric',
'type' => 'decimal',
'max_length' => 18,
'nullable' => true,
'default' => null,
'primary_key' => 0,
],
8 => (object) [
'name' => 'type_date',
'type' => 'date',
'max_length' => null,
'nullable' => true,
'default' => null,
'primary_key' => 0,
],
9 => (object) [
'name' => 'type_time',
'type' => 'time',
'max_length' => null,
'nullable' => true,
'default' => null,
'primary_key' => 0,
],
10 => (object) [
'name' => 'type_datetime',
'type' => 'datetime',
'max_length' => null,
'nullable' => true,
'default' => null,
'primary_key' => 0,
],
11 => (object) [
'name' => 'type_timestamp',
'type' => 'timestamp',
'max_length' => null,
'nullable' => true,
'default' => null,
'primary_key' => 0,
],
12 => (object) [
'name' => 'type_bigint',
'type' => 'bigint',
'max_length' => $this->isOldMySQL() ? 20 : null,
'nullable' => true,
'default' => null,
'primary_key' => 0,
],
13 => (object) [
'name' => 'type_real',
'type' => 'double',
'max_length' => null,
'nullable' => true,
'default' => null,
'primary_key' => 0,
],
14 => (object) [
'name' => 'type_enum',
'type' => 'enum',
'max_length' => null,
'nullable' => true,
'default' => null,
'primary_key' => 0,
],
15 => (object) [
'name' => 'type_set',
'type' => 'set',
'max_length' => null,
'nullable' => true,
'default' => null,
'primary_key' => 0,
],
16 => (object) [
'name' => 'type_mediumtext',
'type' => 'mediumtext',
'max_length' => null,
'nullable' => true,
'default' => null,
'primary_key' => 0,
],
17 => (object) [
'name' => 'type_double',
'type' => 'double',
'max_length' => null,
'nullable' => true,
'default' => null,
'primary_key' => 0,
],
18 => (object) [
'name' => 'type_decimal',
'type' => 'decimal',
'max_length' => 18,
'nullable' => true,
'default' => null,
'primary_key' => 0,
],
19 => (object) [
'name' => 'type_blob',
'type' => 'blob',
'max_length' => null,
'nullable' => true,
'default' => null,
'primary_key' => 0,
],
20 => (object) [
'name' => 'type_boolean',
'type' => 'tinyint',
'max_length' => 1,
'nullable' => true,
'default' => null,
'primary_key' => 0,
],
];
$this->assertSameFieldData($expected, $fields);
}
}
Loading
Loading