DatabaseTest.php
4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
/**
* @package framework
* @subpackage Testing
*/
class DatabaseTest extends SapphireTest {
protected $extraDataObjects = array(
'DatabaseTest_MyObject',
);
protected $usesDatabase = true;
public function testDontRequireField() {
$conn = DB::getConn();
$this->assertArrayHasKey(
'MyField',
$conn->fieldList('DatabaseTest_MyObject')
);
$conn->dontRequireField('DatabaseTest_MyObject', 'MyField');
$this->assertArrayHasKey(
'_obsolete_MyField',
$conn->fieldList('DatabaseTest_MyObject'),
'Field is renamed to _obsolete_<fieldname> through dontRequireField()'
);
$this->resetDBSchema(true);
}
public function testRenameField() {
$conn = DB::getConn();
$conn->clearCachedFieldlist();
$conn->renameField('DatabaseTest_MyObject', 'MyField', 'MyRenamedField');
$this->assertArrayHasKey(
'MyRenamedField',
$conn->fieldList('DatabaseTest_MyObject'),
'New fieldname is set through renameField()'
);
$this->assertArrayNotHasKey(
'MyField',
$conn->fieldList('DatabaseTest_MyObject'),
'Old fieldname isnt preserved through renameField()'
);
$this->resetDBSchema(true);
}
public function testMySQLCreateTableOptions() {
if(!(DB::getConn() instanceof MySQLDatabase)) {
$this->markTestSkipped('MySQL only');
}
$ret = DB::query(sprintf(
'SHOW TABLE STATUS WHERE "Name" = \'%s\'',
'DatabaseTest_MyObject'
))->first();
$this->assertEquals($ret['Engine'],'InnoDB',
"MySQLDatabase tables can be changed to InnoDB through DataObject::\$create_table_options"
);
}
function testIsSchemaUpdating() {
$db = DB::getConn();
$this->assertFalse($db->isSchemaUpdating(), 'Before the transaction the flag is false.');
$db->beginSchemaUpdate();
$this->assertTrue($db->isSchemaUpdating(), 'During the transaction the flag is true.');
$db->endSchemaUpdate();
$this->assertFalse($db->isSchemaUpdating(), 'After the transaction the flag is false.');
$db->beginSchemaUpdate();
$db->cancelSchemaUpdate();
$this->assertFalse($db->doesSchemaNeedUpdating(), 'After cancelling the transaction the flag is false');
}
public function testSchemaUpdateChecking() {
$db = DB::getConn();
// Initially, no schema changes necessary
$db->beginSchemaUpdate();
$this->assertFalse($db->doesSchemaNeedUpdating());
// If we make a change, then the schema will need updating
$db->transCreateTable("TestTable");
$this->assertTrue($db->doesSchemaNeedUpdating());
// If we make cancel the change, then schema updates are no longer necessary
$db->cancelSchemaUpdate();
$this->assertFalse($db->doesSchemaNeedUpdating());
}
public function testHasTable() {
$this->assertTrue(DB::getConn()->hasTable('DatabaseTest_MyObject'));
$this->assertFalse(DB::getConn()->hasTable('asdfasdfasdf'));
}
public function testGetAndReleaseLock() {
$db = DB::getConn();
if(!$db->supportsLocks()) {
return $this->markTestSkipped('Tested database doesn\'t support application locks');
}
$this->assertTrue($db->getLock('DatabaseTest'),
'Can aquire lock');
// $this->assertFalse($db->getLock('DatabaseTest'), 'Can\'t repeatedly aquire the same lock');
$this->assertTrue($db->getLock('DatabaseTest'),
'The same lock can be aquired multiple times in the same connection');
$this->assertTrue($db->getLock('DatabaseTestOtherLock'),
'Can aquire different lock');
$db->releaseLock('DatabaseTestOtherLock');
// Release potentially stacked locks from previous getLock() invocations
$db->releaseLock('DatabaseTest');
$db->releaseLock('DatabaseTest');
$this->assertTrue($db->getLock('DatabaseTest'),
'Can aquire lock after releasing it');
$db->releaseLock('DatabaseTest');
}
public function testCanLock() {
$db = DB::getConn();
if(!$db->supportsLocks()) {
return $this->markTestSkipped('Database doesn\'t support locks');
}
if($db instanceof MSSQLDatabase) {
return $this->markTestSkipped('MSSQLDatabase doesn\'t support inspecting locks');
}
$this->assertTrue($db->canLock('DatabaseTest'), 'Can lock before first aquiring one');
$db->getLock('DatabaseTest');
$this->assertFalse($db->canLock('DatabaseTest'), 'Can\'t lock after aquiring one');
$db->releaseLock('DatabaseTest');
$this->assertTrue($db->canLock('DatabaseTest'), 'Can lock again after releasing it');
}
}
class DatabaseTest_MyObject extends DataObject implements TestOnly {
private static $create_table_options = array('MySQLDatabase' => 'ENGINE=InnoDB');
private static $db = array(
'MyField' => 'Varchar'
);
}