3 defined('AK_TEST_DATABASE_ON') ?
null : define('AK_TEST_DATABASE_ON', true);
4 require_once(dirname(__FILE__
).'/../../fixtures/config/config.php');
6 require_once(AK_LIB_DIR
.DS
.'AkInstaller.php');
8 class Test_of_AkInstaller
extends AkUnitTest
14 $this->Installer
= new AkInstaller();
19 global $ADODB_FETCH_MODE;
20 $ADODB_FETCH_MODE = $this->_original_ADODB_FETCH_MODE
;
23 function Test_setup_expected_returns()
25 $db_type = $this->Installer
->db
->type();
29 $this->expected_for_creating_table
= array(
30 'ID' => array('type'=>'int4','not_null'=>1,'default_value'=>"nextval('test_pages_id_seq'::regclass)",'primary_key' => 1),
31 'TITLE' => array('max_length'=>255,'has_default'=>null,'type'=>'varchar'),
32 'BODY' => array('max_length'=>-1,'has_default'=>null,'type'=>'text'),
33 'PARENT_ID' => array('max_length'=>4,'has_default'=>1,'default_value'=>0,'not_null'=>1,'type'=>'int4'),
34 'CREATED_AT' => array('type'=>'timestamp', 'max_length'=> 8,'has_default'=>null),
35 'UPDATED_AT' => array('type'=>'timestamp', 'max_length'=> 8,'has_default'=>null),
38 $this->expected_for_default_types
= array(
39 'ID' => array('type'=>'int4','not_null'=>1, 'default_value' => "nextval('test_defaults_id_seq'::regclass)",'primary_key'=>1),
40 'NAME' => array('type'=>'varchar', 'max_length'=>255, 'has_default'=>null),
41 'SCREEN_NAME' => array('type'=>'varchar', 'max_length'=>255, 'has_default'=>null),
42 'DESCRIPTION' => array('max_length'=>-1,'has_default'=>null),
43 'EN_URL' => array('max_length'=>255,'has_default'=>null),
44 'ES_URL' => array('max_length'=>255,'has_default'=>null),
45 'OWNER_ID' => array('type'=>'int4', 'max_length'=>4),
46 'MODIFIED_AT' => array('type'=>'timestamp'),
47 'CREATED_ON' => array('type'=>'date'),
48 'IS_FEATURED' => array('max_length'=>1,'type'=>'bool'),
49 'POSITION' => array('max_length'=>4),
50 'LOCK_VERSION' => array('type'=>'int4', 'max_length'=>4,'default_value' => 1),
51 'EDIT_COUNT' => array('type'=>'int4', 'max_length'=>4,'default_value' => 0),
58 $this->expected_for_creating_table
= array(
59 'ID' => array('type'=>'INTEGER', 'max_length'=>-1,'not_null'=>1,'auto_increment'=>1),
60 'TITLE' => array('max_length'=>255,'not_null'=>null),
61 'BODY' => array('max_length'=>-1,'not_null'=>null,'type'=>'TEXT'),
62 'PARENT_ID' => array('has_default'=>1,'default_value'=>0,'not_null'=>1),
63 'CREATED_AT' => array('type'=>'DATE'),
65 $this->expected_for_default_types
= array(
66 'ID' => array('type'=>'INTEGER', 'max_length'=>-1,'not_null'=>1,'auto_increment'=>1,'primary_key'=>1),
67 'NAME' => array('max_length'=>255,'not_null'=>null),
68 'SCREEN_NAME' => array('max_length'=>255,'not_null'=>null),
69 'DESCRIPTION' => array('max_length'=>-1,'not_null'=>null),
70 'EN_URL' => array('max_length'=>255,'not_null'=>null),
71 'ES_URL' => array('max_length'=>255,'not_null'=>null),
72 'OWNER_ID' => array('type'=>'INTEGER', 'max_length'=>-1),
73 'MODIFIED_AT' => array('type'=>'DATE'),
74 'CREATED_ON' => array('type'=>'DATE'),
75 'IS_FEATURED' => array('max_length'=>-1,'type'=>'BOOLEAN'),
76 'POSITION' => array('max_length'=>-1),
77 'LOCK_VERSION' => array('type'=>'INTEGER', 'max_length'=>-1, 'default_value' => 1),
78 'EDIT_COUNT' => array('type'=>'INTEGER', 'max_length'=>-1,'default_value' => 0),
84 $this->expected_for_creating_table
= array(
85 'ID' => array('type'=>'int', 'max_length'=>11,'not_null'=>1,'auto_increment'=>1),
86 'TITLE' => array('max_length'=>255,'not_null'=>null),
87 'BODY' => array('max_length'=>-1,'not_null'=>null),
88 'PARENT_ID' => array('has_default'=>1,'default_value'=>0,'not_null'=>1),
89 'CREATED_AT' => array('type'=>'datetime'),
91 $this->expected_for_default_types
= array(
92 'ID' => array('type'=>'int', 'max_length'=>11,'not_null'=>1,'auto_increment'=>1,'primary_key'=>1),
93 'NAME' => array('max_length'=>255,'not_null'=>null),
94 'SCREEN_NAME' => array('max_length'=>255,'not_null'=>null),
95 'DESCRIPTION' => array('max_length'=>-1,'not_null'=>null),
96 'EN_URL' => array('max_length'=>255,'not_null'=>null),
97 'ES_URL' => array('max_length'=>255,'not_null'=>null),
98 'OWNER_ID' => array('type'=>'int', 'max_length'=>11),
99 'MODIFIED_AT' => array('type'=>'datetime'),
100 'CREATED_ON' => array('type'=>'date'),
101 'IS_FEATURED' => array('max_length'=>1),
102 'POSITION' => array('max_length'=>11),
103 'LOCK_VERSION' => array('type'=>'int', 'max_length'=>11,'default_value' => 1),
104 'EDIT_COUNT' => array('type'=>'int', 'max_length'=>11,'default_value' => 0),
111 function Test_for_creating_table()
113 $this->Installer
->createTable('test_pages', "
114 id integer not null auto_increment primary_key,
117 parent_id integer not null default '0',
122 $from_datadict = $this->Installer
->db
->getColumnDetails('test_pages');
124 foreach ($this->expected_for_creating_table
as $column=>$details){
125 foreach ($details as $index=>$value) {
126 $this->assertEqual($this->expected_for_creating_table
[$column][$index], $from_datadict[$column]->$index);
130 $this->Installer
->createTable('test_categories', "
131 id integer not null auto_increment primary_key,
134 parent_id integer not null default '0'
135 ", array('timestamp'=>false));
138 $this->Installer
->createTable('test_categories_pages', "
139 category_id integer not null,
140 page_id integer not null
141 ", array('timestamp'=>false));
143 $this->Installer
->createTable('test_nice_urls', "
144 id integer not null auto_increment primary_key,
146 context_id integer not null,
147 context string(255) not null default 'page'
148 ", array('timestamp'=>false));
151 $this->Installer
->dropTables('test_pages','test_categories');
152 $this->Installer
->dropTables('test_categories_pages,test_nice_urls');
156 function Test_of_createTable()
158 $this->Installer
= new AkInstaller();
160 $this->assertFalse($this->Installer
->tableExists('test_1'));
162 $this->Installer
->createTable('test_1','id int auto key,body string(32) index,author string(32)');
163 $this->assertTrue($this->Installer
->tableExists('test_1'));
165 $avail_indices = $this->Installer
->db
->getIndexes('test_1');
166 $this->assertTrue(isset($avail_indices['idx_test_1_body']));
167 $this->assertFalse($avail_indices['idx_test_1_body']['unique']);
168 $this->assertTrue($avail_indices['idx_test_1_body']['columns'][0]=='body');
170 $this->Installer
->dropTable('test_1');
172 $this->Installer
->createTable('test_1','id int auto key,body string(32) index');
173 $this->assertTrue($this->Installer
->tableExists('test_1'));
175 $avail_indices = $this->Installer
->db
->getIndexes('test_1');
176 $this->assertTrue(isset($avail_indices['idx_test_1_body']));
177 $this->assertFalse($avail_indices['idx_test_1_body']['unique']);
178 $this->assertTrue($avail_indices['idx_test_1_body']['columns'][0]=='body');
180 $this->Installer
->dropTable('test_1');
181 $this->assertFalse($this->Installer
->tableExists('test_1'));
183 $this->Installer
->createTable('test_1','id int auto key,body string(32) unique');
184 $this->assertTrue($this->Installer
->tableExists('test_1'));
186 $avail_indices = $this->Installer
->db
->getIndexes('test_1');
187 $this->assertTrue(isset($avail_indices['idx_test_1_body UNIQUE']));
188 $this->assertTrue($avail_indices['idx_test_1_body UNIQUE']['unique']);
189 $this->assertTrue($avail_indices['idx_test_1_body UNIQUE']['columns'][0]=='body');
191 $this->Installer
->dropTable('test_1');
192 $this->assertFalse($this->Installer
->tableExists('test_1'));
194 $this->Installer
->createTable('test_1','id int auto key,body string(32) full_text',array('mysql'=>'Type=MyISAM'));
195 $this->assertTrue($this->Installer
->tableExists('test_1'));
196 $this->Installer
->dropTable('test_1');
200 function Test_of_add_and_remove_Indices()
202 $this->Installer
= new AkInstaller();
204 $this->Installer
->createTable('test_1','id int auto key,free string(32),beer string(23)');
206 $avail_indices = $this->Installer
->db
->getIndexes('test_1');
208 $this->assertFalse($this->_hasIndexes($avail_indices));
210 $this->Installer
->addIndex('test_1','beer, free UNIQUE');
211 $avail_indices = $this->Installer
->db
->getIndexes('test_1');
212 $this->assertTrue($this->_hasIndexes($avail_indices));
213 $this->assertTrue($this->_hasIndexes($avail_indices, 'idx_test_1_beer, free UNIQUE'));
214 $this->assertTrue($avail_indices['idx_test_1_beer, free UNIQUE']['unique']);
216 $this->Installer
->removeIndex('test_1','beer, free UNIQUE');
217 $avail_indices = $this->Installer
->db
->getIndexes('test_1');
218 $this->assertFalse(isset($avail_indices['idx_test_1_beer, free UNIQUE']));
220 $this->Installer
->addIndex('test_1','beer, free UNIQUE','freebeer');
221 $avail_indices = $this->Installer
->db
->getIndexes('test_1');
222 $this->assertTrue(isset($avail_indices['freebeer']));
223 $this->assertTrue($avail_indices['freebeer']['unique']);
224 $this->assertTrue($avail_indices['freebeer']['columns'][0]=='beer');
225 $this->assertTrue($avail_indices['freebeer']['columns'][1]=='free');
227 $this->Installer
->dropIndex('test_1','freebeer');
228 $avail_indices = $this->Installer
->db
->getIndexes('test_1');
229 $this->assertFalse($this->_hasIndexes($avail_indices));
231 $this->Installer
->dropTable('test_1');
235 function Test_of_default_types()
237 $this->Installer
= new AkInstaller();
238 $this->Installer
->createTable('test_defaults','id,name,screen_name string,description,*url,owner_id,modified_at,created_on,is_featured,position,lock_version,edit_count');
239 $from_datadict = $this->Installer
->db
->getColumnDetails('test_defaults');
241 foreach ($this->expected_for_default_types
as $column=>$details){
242 foreach ($details as $index=>$value) {
243 $this->assertEqual($this->expected_for_default_types
[$column][$index], $from_datadict[$column]->$index, "Column '$column', attribute $index = '{$from_datadict[$column]->$index}' instead of expected '{$this->expected_for_default_types[$column][$index]}'");
249 function _hasIndexes($meta_details, $search = false)
252 if(!empty($meta_details) && is_array($meta_details)){
253 foreach ($meta_details as $k=>$meta_detail){
254 if(empty($search) && strstr($k,'idx')){
256 }elseif (strstr($k,$search)){
265 function test_should_create_default_values_correctly()
267 $this->installAndIncludeModels(array('Thumbnail'));
268 $Thumbnail =& new Thumbnail();
269 $this->assertEqual($Thumbnail->get('owner'), 'Picture');
272 // see AkDbAdapter_schema for the tests; this is only a wrapper
273 function test_should_rename_columns()
275 $this->Installer
->renameColumn('test_defaults','screen_name','real_name');
276 if ($this->Installer
->db
->type()=='sqlite') {
277 $this->assertError();
279 $this->Installer
->dropTable('test_defaults');
284 ak_test('Test_of_AkInstaller', true);