DatabaseTest.php
6.4 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
namespace InfluxDB\Test;
use InfluxDB\Client;
use InfluxDB\Database;
use InfluxDB\Driver\Guzzle;
use InfluxDB\Point;
use InfluxDB\ResultSet;
use PHPUnit_Framework_MockObject_MockObject;
use PHPUnit_Framework_TestCase;
class DatabaseTest extends AbstractTest
{
/**
* @var string
*/
protected $dataToInsert;
/**
* @var
*/
protected $mockResultSet;
public function setUp()
{
parent::setUp();
$this->resultData = file_get_contents(dirname(__FILE__) . '/json/result.example.json');
$this->mockClient->expects($this->any())
->method('listDatabases')
->will($this->returnValue(array('test123', 'test')));
$this->dataToInsert = file_get_contents(dirname(__FILE__) . '/json/input.example.json');
}
public function testGetters()
{
$this->assertInstanceOf('InfluxDB\Client', $this->database->getClient());
$this->assertInstanceOf('InfluxDB\Query\Builder', $this->database->getQueryBuilder());
}
/**
*
*/
public function testQueries()
{
$testResultSet = new ResultSet($this->resultData);
$this->assertEquals($this->database->query('SELECT * FROM test_metric'), $testResultSet);
$this->database->drop();
$this->assertEquals('DROP DATABASE "influx_test_db"', Client::$lastQuery);
}
public function testRetentionPolicyQueries()
{
$retentionPolicy = $this->getTestRetentionPolicy();
$this->assertEquals(
$this->getTestDatabase()->createRetentionPolicy($retentionPolicy),
new ResultSet($this->getEmptyResult())
);
$this->database->listRetentionPolicies();
$this->assertEquals('SHOW RETENTION POLICIES ON "influx_test_db"', Client::$lastQuery);
$this->database->alterRetentionPolicy($this->getTestRetentionPolicy());
$this->assertEquals(
'ALTER RETENTION POLICY "test" ON "influx_test_db" DURATION 1d REPLICATION 1 DEFAULT',
Client::$lastQuery
);
}
/**
* @expectedException \InvalidArgumentException
*/
public function testEmptyDatabaseName()
{
new Database(null, $this->mockClient);
}
public function testCreate()
{
// test create with retention policy
$this->database->create($this->getTestRetentionPolicy('influx_test_db'), true);
$this->assertEquals(
'CREATE RETENTION POLICY "influx_test_db" ON "influx_test_db" DURATION 1d REPLICATION 1 DEFAULT',
Client::$lastQuery
);
// test creating a database without create if not exists
$this->database->create(null, true);
$this->assertEquals('CREATE DATABASE IF NOT EXISTS "influx_test_db"', Client::$lastQuery);
// test creating a database without create if not exists
$this->database->create(null, false);
$this->assertEquals('CREATE DATABASE "influx_test_db"', Client::$lastQuery);
$this->mockClient->expects($this->any())
->method('query')
->will($this->returnCallback(function () {
throw new \Exception('test exception');
}));
// test an exception being handled correctly
$this->setExpectedException('\InfluxDB\Database\Exception');
$this->database->create($this->getTestRetentionPolicy('influx_test_db'), false);
}
public function testExists()
{
$database = new Database('test', $this->mockClient);
$this->assertEquals($database->exists(), true);
}
public function testNotExists()
{
$database = new Database('test_not_exists', $this->mockClient);
$this->assertEquals($database->exists(), false);
}
public function testWritePointsInASingleCall()
{
$point1 = new Point(
'cpu_load_short',
0.64,
array('host' => 'server01', 'region' => 'us-west'),
array('cpucount' => 10),
1435222310
);
$point2 = new Point(
'cpu_load_short',
0.84
);
$this->assertEquals(true, $this->database->writePoints(array($point1, $point2)));
$this->mockClient->expects($this->once())
->method('write')
->will($this->throwException(new \Exception('Test exception')));
$this->setExpectedException('InfluxDB\Exception');
$this->database->writePoints(array($point1, $point2));
}
public function testQueryBuilderOrderBy()
{
$this->assertEquals(
$this->database->getQueryBuilder()
->from('test_metric')
->orderBy('time', 'DESC')->getQuery(),
'SELECT * FROM "test_metric" ORDER BY time DESC');
$this->assertEquals(
$this->database->getQueryBuilder()
->from('test_metric')
->orderBy('time', 'DESC')
->orderBy('some_field', 'ASC')
->getQuery(),
'SELECT * FROM "test_metric" ORDER BY time DESC,some_field ASC');
}
/**
* @see https://github.com/influxdata/influxdb-php/pull/36
*/
public function testReservedNames()
{
$database = new Database('stats', $this->mockClient);
// test handling of reserved keywords in database name
$database->create();
$this->assertEquals('CREATE DATABASE IF NOT EXISTS "stats"', Client::$lastQuery);
$database->listRetentionPolicies();
$this->assertEquals('SHOW RETENTION POLICIES ON "stats"', Client::$lastQuery);
// test handling of reserved keywords in retention policy names
$database->create($this->getTestRetentionPolicy('default'));
$this->assertEquals(
'CREATE RETENTION POLICY "default" ON "stats" DURATION 1d REPLICATION 1 DEFAULT',
Client::$lastQuery
);
// test handling of reserved keywords in measurement names
$this->assertEquals($database->getQueryBuilder()->from('server')->getQuery(), 'SELECT * FROM "server"');
}
/**
* @param string $name
*
* @return Database
*/
protected function getTestDatabase($name = 'test')
{
return new Database($name, $this->getClientMock(true));
}
/**
* @param string $name
*
* @return Database\RetentionPolicy
*/
protected function getTestRetentionPolicy($name = 'test')
{
return new Database\RetentionPolicy($name, '1d', 1, true);
}
}