Skip to content

Commit 8cde164

Browse files
committed
tests for indexes
1 parent 2d64c08 commit 8cde164

1 file changed

Lines changed: 110 additions & 0 deletions

File tree

tests/IndexTest.php

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
use PHPUnit\Framework\TestCase;
3+
4+
final class IndexTest extends TestCase{
5+
6+
function testBasicIndex(){
7+
8+
$tbl = $this->get_first_table("CREATE TABLE foo (bar INT, INDEX(bar))");
9+
$this->assertEquals(count($tbl['indexes']), 1);
10+
$this->assertEquals($tbl['indexes'][0]['type'], "INDEX");
11+
$this->assertEquals(count($tbl['indexes'][0]['cols']), 1);
12+
$this->assertEquals($tbl['indexes'][0]['cols'][0]['name'], "bar");
13+
}
14+
15+
function testIndexTypes(){
16+
17+
$tbl = $this->get_first_table("CREATE TABLE foo (bar INT, INDEX(bar))");
18+
$this->assertEquals($tbl['indexes'][0]['type'], "INDEX");
19+
20+
$tbl = $this->get_first_table("CREATE TABLE foo (bar INT, KEY (bar))");
21+
$this->assertEquals($tbl['indexes'][0]['type'], "INDEX");
22+
23+
$tbl = $this->get_first_table("CREATE TABLE foo (bar INT, UNIQUE(bar))");
24+
$this->assertEquals($tbl['indexes'][0]['type'], "UNIQUE");
25+
26+
$tbl = $this->get_first_table("CREATE TABLE foo (bar INT, UNIQUE INDEX(bar))");
27+
$this->assertEquals($tbl['indexes'][0]['type'], "UNIQUE");
28+
29+
$tbl = $this->get_first_table("CREATE TABLE foo (bar INT, UNIQUE KEY (bar))");
30+
$this->assertEquals($tbl['indexes'][0]['type'], "UNIQUE");
31+
32+
$tbl = $this->get_first_table("CREATE TABLE foo (bar INT, PRIMARY KEY(bar))");
33+
$this->assertEquals($tbl['indexes'][0]['type'], "PRIMARY");
34+
35+
$tbl = $this->get_first_table("CREATE TABLE foo (bar INT, FULLTEXT(bar))");
36+
$this->assertEquals($tbl['indexes'][0]['type'], "FULLTEXT");
37+
38+
$tbl = $this->get_first_table("CREATE TABLE foo (bar INT, FULLTEXT INDEX(bar))");
39+
$this->assertEquals($tbl['indexes'][0]['type'], "FULLTEXT");
40+
41+
$tbl = $this->get_first_table("CREATE TABLE foo (bar INT, FULLTEXT KEY(bar))");
42+
$this->assertEquals($tbl['indexes'][0]['type'], "FULLTEXT");
43+
44+
$tbl = $this->get_first_table("CREATE TABLE foo (bar INT, SPATIAL(bar))");
45+
$this->assertEquals($tbl['indexes'][0]['type'], "SPATIAL");
46+
47+
$tbl = $this->get_first_table("CREATE TABLE foo (bar INT, SPATIAL INDEX(bar))");
48+
$this->assertEquals($tbl['indexes'][0]['type'], "SPATIAL");
49+
50+
$tbl = $this->get_first_table("CREATE TABLE foo (bar INT, SPATIAL KEY(bar))");
51+
$this->assertEquals($tbl['indexes'][0]['type'], "SPATIAL");
52+
}
53+
54+
function testConstraints(){
55+
56+
# TODO
57+
}
58+
59+
function testIndexNames(){
60+
61+
$tbl = $this->get_first_table("CREATE TABLE foo (bar INT, INDEX(bar))");
62+
$this->assertEquals(array_key_exists('name', $tbl['indexes'][0]), false);
63+
64+
$tbl = $this->get_first_table("CREATE TABLE foo (bar INT, INDEX baz(bar))");
65+
$this->assertEquals($tbl['indexes'][0]['name'], "baz");
66+
67+
$tbl = $this->get_first_table("CREATE TABLE foo (bar INT, FULLTEXT KEY `baz` (bar))");
68+
$this->assertEquals($tbl['indexes'][0]['name'], "baz");
69+
}
70+
71+
72+
function testIndexModes(){
73+
74+
$tbl = $this->get_first_table("CREATE TABLE foo (bar INT, INDEX(bar))");
75+
$this->assertEquals(array_key_exists('mode', $tbl['indexes'][0]), false);
76+
77+
$tbl = $this->get_first_table("CREATE TABLE foo (bar INT, INDEX USING BTREE(bar))");
78+
$this->assertEquals($tbl['indexes'][0]['mode'], "BTREE");
79+
80+
$tbl = $this->get_first_table("CREATE TABLE foo (bar INT, INDEX USING HASH (bar))");
81+
$this->assertEquals($tbl['indexes'][0]['mode'], "HASH");
82+
83+
$tbl = $this->get_first_table("CREATE TABLE foo (bar INT, PRIMARY KEY USING HASH (bar))");
84+
$this->assertEquals($tbl['indexes'][0]['mode'], "HASH");
85+
86+
# since fulltext and spatial indexes don't have a type/mode, this is invalid
87+
$tbl = $this->get_first_table("CREATE TABLE foo (bar INT, FULLTEXT USING HASH(bar))");
88+
$this->assertEquals(array_key_exists('mode', $tbl['indexes'][0]), false);
89+
}
90+
91+
function testIndexCols(){
92+
93+
# TODO
94+
}
95+
96+
function testIndexOptions(){
97+
98+
# TODO
99+
}
100+
101+
function get_first_table($str){
102+
$obj = new iamcal\SQLParser();
103+
$obj->parse($str);
104+
105+
$tables = array_keys($obj->tables);
106+
$first_key = $tables[0];
107+
108+
return $obj->tables[$first_key];
109+
}
110+
}

0 commit comments

Comments
 (0)