Skip to content

Commit adf8728

Browse files
committed
started to write tests for field types
1 parent b510bc3 commit adf8728

1 file changed

Lines changed: 187 additions & 0 deletions

File tree

tests/FieldTest.php

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
<?php
2+
use PHPUnit\Framework\TestCase;
3+
4+
final class FieldTest extends TestCase{
5+
6+
function testBasicFields(){
7+
8+
$tbl = $this->get_first_table("CREATE TABLE foo (bar INT)");
9+
$this->assertEquals(count($tbl['fields']), 1);
10+
$this->assertEquals($tbl['fields'][0]['type'], "INT");
11+
$this->assertEquals($tbl['fields'][0]['name'], "bar");
12+
13+
$tbl = $this->get_first_table("CREATE TABLE foo (bar INT, baz TINYINT)");
14+
$this->assertEquals(count($tbl['fields']), 2);
15+
$this->assertEquals($tbl['fields'][0]['type'], "INT");
16+
$this->assertEquals($tbl['fields'][0]['name'], "bar");
17+
$this->assertEquals($tbl['fields'][1]['type'], "TINYINT");
18+
$this->assertEquals($tbl['fields'][1]['name'], "baz");
19+
}
20+
21+
function testSimpleFields(){
22+
23+
# DATE
24+
# YEAR
25+
# TINYBLOB
26+
# BLOB
27+
# MEDIUMBLOB
28+
# LONGBLOB
29+
# JSON
30+
31+
$tbl = $this->get_first_table("CREATE TABLE foo (bar DATE)");
32+
$this->assertEquals($tbl['fields'], [
33+
[
34+
'name' => "bar",
35+
'type' => "DATE",
36+
]
37+
]);
38+
}
39+
40+
function testInts(){
41+
42+
$tbl = $this->get_first_table("CREATE TABLE foo (bar TINYINT)");
43+
$this->assertEquals($tbl['fields'], [
44+
[
45+
'name' => "bar",
46+
'type' => "TINYINT",
47+
]
48+
]);
49+
50+
$tbl = $this->get_first_table("CREATE TABLE foo (bar smallint (4))");
51+
$this->assertEquals($tbl['fields'], [
52+
[
53+
'name' => "bar",
54+
'type' => "SMALLINT",
55+
'length' => "4",
56+
]
57+
]);
58+
59+
$tbl = $this->get_first_table("CREATE TABLE foo (bar MEDIUMINT UNSIGNED)");
60+
$this->assertEquals($tbl['fields'], [
61+
[
62+
'name' => "bar",
63+
'type' => "MEDIUMINT",
64+
'unsigned' => true,
65+
]
66+
]);
67+
68+
$tbl = $this->get_first_table("CREATE TABLE foo (bar INT ZEROFILL)");
69+
$this->assertEquals($tbl['fields'], [
70+
[
71+
'name' => "bar",
72+
'type' => "INT",
73+
'zerofill' => true,
74+
]
75+
]);
76+
77+
$tbl = $this->get_first_table("CREATE TABLE foo (bar BIGINT(20) UNSIGNED ZEROFILL)");
78+
$this->assertEquals($tbl['fields'], [
79+
[
80+
'name' => "bar",
81+
'type' => "BIGINT",
82+
'length' => "20",
83+
'unsigned' => true,
84+
'zerofill' => true,
85+
]
86+
]);
87+
88+
89+
}
90+
91+
function testFloats(){
92+
93+
# REAL[(length,decimals)] [UNSIGNED] [ZEROFILL]
94+
# DOUBLE[(length,decimals)] [UNSIGNED] [ZEROFILL]
95+
# FLOAT[(length,decimals)] [UNSIGNED] [ZEROFILL]
96+
}
97+
98+
function testNumerics(){
99+
100+
# DECIMAL[(length[,decimals])] [UNSIGNED] [ZEROFILL]
101+
# NUMERIC[(length[,decimals])] [UNSIGNED] [ZEROFILL]
102+
}
103+
104+
function testTimes(){
105+
106+
# TIME[(fsp)]
107+
# TIMESTAMP[(fsp)]
108+
# DATETIME[(fsp)]
109+
}
110+
111+
function testBits(){
112+
113+
# BIT[(length)]
114+
}
115+
116+
function testChars(){
117+
118+
# CHAR[(length)] [BINARY] [CHARACTER SET charset_name] [COLLATE collation_name]
119+
# VARCHAR(length) [BINARY] [CHARACTER SET charset_name] [COLLATE collation_name]
120+
# BINARY[(length)]
121+
# VARBINARY(length)
122+
}
123+
124+
function testTexts(){
125+
126+
# TINYTEXT [BINARY] [CHARACTER SET charset_name] [COLLATE collation_name]
127+
# TEXT [BINARY] [CHARACTER SET charset_name] [COLLATE collation_name]
128+
# MEDIUMTEXT [BINARY] [CHARACTER SET charset_name] [COLLATE collation_name]
129+
# LONGTEXT [BINARY] [CHARACTER SET charset_name] [COLLATE collation_name]
130+
}
131+
132+
function testSets(){
133+
134+
# ENUM(value1,value2,value3,...) [CHARACTER SET charset_name] [COLLATE collation_name]
135+
# SET(value1,value2,value3,...) [CHARACTER SET charset_name] [COLLATE collation_name]
136+
}
137+
138+
139+
function testSpatials(){
140+
141+
# GEOMETRY
142+
# POINT
143+
# LINESTRING
144+
# POLYGON
145+
# MULTIPOINT
146+
# MULTILINESTRING
147+
# MULTIPOLYGON
148+
# GEOMETRYCOLLECTION
149+
}
150+
151+
function testFieldOptions(){
152+
153+
# TODO
154+
155+
# data_type
156+
# [NOT NULL | NULL]
157+
# [DEFAULT default_value]
158+
# [AUTO_INCREMENT]
159+
# [UNIQUE [KEY] | [PRIMARY] KEY]
160+
# [COMMENT 'string']
161+
# [COLUMN_FORMAT {FIXED|DYNAMIC|DEFAULT}]
162+
# [STORAGE {DISK|MEMORY|DEFAULT}]
163+
# [reference_definition]
164+
}
165+
166+
function testVirtualOptions(){
167+
168+
# TODO
169+
170+
# data_type [GENERATED ALWAYS] AS (expression)
171+
# [VIRTUAL | STORED]
172+
# [UNIQUE [KEY]] | [[PRIMARY] KEY]
173+
# [COMMENT comment]
174+
# [NOT NULL | NULL]
175+
}
176+
177+
178+
function get_first_table($str){
179+
$obj = new iamcal\SQLParser();
180+
$obj->parse($str);
181+
182+
$tables = array_keys($obj->tables);
183+
$first_key = $tables[0];
184+
185+
return $obj->tables[$first_key];
186+
}
187+
}

0 commit comments

Comments
 (0)