2013-11-12 17:36:14 +00:00
|
|
|
<?php
|
|
|
|
use crodas\InfluxPHP\Client;
|
2013-11-12 18:16:18 +00:00
|
|
|
use crodas\InfluxPHP\DB;
|
2013-11-12 17:36:14 +00:00
|
|
|
|
2014-09-04 16:28:50 +00:00
|
|
|
class DBTest extends \PHPUnit_Framework_TestCase
|
2013-11-12 17:36:14 +00:00
|
|
|
{
|
|
|
|
public function testCreate()
|
|
|
|
{
|
|
|
|
$client = new Client;
|
|
|
|
return $client->createDatabase("test_foobar");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException RuntimeException
|
|
|
|
*/
|
|
|
|
public function testCreateException()
|
|
|
|
{
|
|
|
|
$client = new Client;
|
|
|
|
return $client->createDatabase("test_foobar");
|
|
|
|
}
|
2013-11-12 18:16:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @dependsOn testCreateException
|
|
|
|
*/
|
|
|
|
public function testDelete()
|
|
|
|
{
|
|
|
|
$client = new Client;
|
|
|
|
return $client->deleteDatabase("test_foobar");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dependsOn testDelete
|
|
|
|
* @expectedException RuntimeException
|
|
|
|
*/
|
|
|
|
public function testDeleteException()
|
|
|
|
{
|
|
|
|
$client = new Client;
|
|
|
|
return $client->deleteDatabase("test_foobar");
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDBObject()
|
|
|
|
{
|
|
|
|
$client = new Client;
|
|
|
|
$client->createDatabase("test_xxx");
|
|
|
|
$this->assertTrue($client->test_xxx instanceof DB);
|
|
|
|
$this->assertTrue($client->getDatabase("test_xxx") instanceof DB);
|
|
|
|
$client->test_xxx->drop();
|
|
|
|
}
|
|
|
|
|
2013-11-15 03:17:00 +00:00
|
|
|
public function testTimePrecision()
|
|
|
|
{
|
|
|
|
$client = new Client;
|
|
|
|
$this->assertEquals('s', $client->getTimePrecision());
|
|
|
|
$db = $client->createDatabase("test_yyyy");
|
|
|
|
$this->assertEquals('s', $db->getTimePrecision());
|
|
|
|
|
|
|
|
|
|
|
|
$client->setTimePrecision('m');
|
|
|
|
$this->assertEquals('m', $client->getTimePrecision());
|
|
|
|
$this->assertEquals('m', $db->getTimePrecision());
|
|
|
|
|
|
|
|
$db1 = $client->createDatabase("test_yyyx");
|
|
|
|
$this->assertEquals('m', $db->getTimePrecision());
|
|
|
|
}
|
|
|
|
|
2013-12-02 11:09:33 +00:00
|
|
|
/**
|
|
|
|
* @expectedException InvalidArgumentException
|
2013-11-15 03:17:00 +00:00
|
|
|
*/
|
|
|
|
public function testInvalidTimePrecision()
|
|
|
|
{
|
|
|
|
$client = new Client;
|
2014-09-04 16:33:45 +00:00
|
|
|
$client->SetTimePrecision(array());
|
2013-11-15 03:17:00 +00:00
|
|
|
}
|
|
|
|
|
2013-11-12 18:16:18 +00:00
|
|
|
public function testQuery()
|
|
|
|
{
|
|
|
|
$client = new Client;
|
2013-11-13 07:51:45 +00:00
|
|
|
$db = $client->createDatabase("test_xxx");
|
2013-11-12 18:16:18 +00:00
|
|
|
$db->createUser("root", "root");
|
|
|
|
|
2014-09-04 16:33:45 +00:00
|
|
|
$db->insert("foobar", array('type' => '/foobar', 'karma' => 10));
|
|
|
|
$db->insert("foobar", array('type' => '/foobar', 'karma' => 20));
|
|
|
|
$db->insert("foobar", array('type' => '/barfoo', 'karma' => 30));
|
2013-11-12 18:16:18 +00:00
|
|
|
|
2013-11-13 07:54:31 +00:00
|
|
|
$this->assertEquals($db->first("SELECT max(karma) FROM foobar")->max, 30);
|
|
|
|
$this->assertEquals($db->first("SELECT min(karma) FROM foobar")->min, 10);
|
|
|
|
$this->assertEquals($db->first("SELECT mean(karma) FROM foobar")->mean, 20);
|
|
|
|
|
|
|
|
foreach ($db->query("SELECT mean(karma) FROM foobar GROUP BY type") as $row) {
|
2013-11-15 03:26:02 +00:00
|
|
|
$this->assertTrue(is_int($row->time));
|
2013-11-13 07:54:31 +00:00
|
|
|
if ($row->type == "/foobar") {
|
|
|
|
$this->assertEquals(15, $row->mean);
|
|
|
|
} else {
|
|
|
|
$this->assertEquals(30, $row->mean);
|
|
|
|
}
|
|
|
|
}
|
2013-11-15 03:26:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @dependsOn testQuery */
|
|
|
|
function testDifferentTimePeriod()
|
|
|
|
{
|
|
|
|
$client = new Client;
|
|
|
|
$db = $client->test_xxx;
|
|
|
|
|
|
|
|
$client->setTimePrecision('u');
|
2014-09-04 16:28:50 +00:00
|
|
|
foreach ($db->query("SELECT mean(karma) FROM foobar GROUP BY type, time(1h)") as $row) {
|
2013-11-15 03:26:02 +00:00
|
|
|
$this->assertTrue($row->time > time()*1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
$client->setTimePrecision('m');
|
2014-09-04 16:28:50 +00:00
|
|
|
foreach ($db->query("SELECT mean(karma) FROM foobar GROUP BY type, time(1h)") as $row) {
|
2013-11-15 03:26:02 +00:00
|
|
|
$this->assertTrue($row->time < time()*10000);
|
|
|
|
}
|
|
|
|
|
|
|
|
$client->setTimePrecision('s');
|
2014-09-04 16:28:50 +00:00
|
|
|
foreach ($db->query("SELECT mean(karma) FROM foobar GROUP BY type, time(1h)") as $row) {
|
2013-11-15 03:26:02 +00:00
|
|
|
$this->assertTrue($row->time < time()+20);
|
|
|
|
}
|
2013-11-12 18:16:18 +00:00
|
|
|
|
|
|
|
$db->drop();
|
2013-12-02 11:09:33 +00:00
|
|
|
}
|
2013-11-12 17:36:14 +00:00
|
|
|
}
|