Files
DomFramework/Tests/DbjsonTest.php

420 lines
14 KiB
PHP

<?php
/**
* DomFramework - Tests
* @package domframework
* @author Dominique Fournier <dominique@fournier38.fr>
* @license BSD
*/
namespace Domframework\Tests;
use Domframework\Dbjson;
/**
* Test the Dbjson database
*/
class DbjsonTest extends \PHPUnit_Framework_TestCase
{
public function testInsertOne1()
{
// Document #0
define("dbfile", "/tmp/dbjson-" . time());
$dbjson = new Dbjson("dbjson://" . dbfile);
$res = $dbjson->insertOne(
"collection",
["key1" => "val1", "key2" => "val2"]
);
$this->assertSame($res, 1);
}
public function testInsertOne2()
{
// Document #1
$dbjson = new Dbjson("dbjson://" . dbfile);
$res = $dbjson->insertOne(
"collection",
["key1" => "val1", "key2" => "val2"]
);
$this->assertSame($res, 1);
}
public function testInsertMany1()
{
// Error : Invalid array provided (not array of array)
$this->setExpectedException("Exception");
$dbjson = new Dbjson("dbjson://" . dbfile);
$res = $dbjson->insertMany(
"collection",
["key1" => "val1", "key2" => "val2"]
);
}
public function testInsertMany2()
{
// Document #2 and #3
$dbjson = new Dbjson("dbjson://" . dbfile);
$res = $dbjson->insertMany(
"collection",
[["key1" => "val3", "key2" => "val2"],
["key1" => "val3", "key2" => "val4"]]
);
$this->assertSame($res, 2);
}
public function testFilter1()
{
// Return all the keys (filter = array ())
$dbjson = new Dbjson("dbjson://" . dbfile);
$res = $dbjson->filter("collection", []);
$this->assertSame(array_keys($res), [0,1,2,3]);
}
public function testFilter2()
{
// Return the keys where filter = array ("key2"=>"val2"))
$dbjson = new Dbjson("dbjson://" . dbfile);
$res = $dbjson->filter("collection", ["key2" => "val2"]);
$this->assertSame(array_keys($res), [0,1,2]);
}
public function testFilter3()
{
// Return the keys where filter = array ("key1"=>"val3","key2"=>"val2"))
$dbjson = new Dbjson("dbjson://" . dbfile);
$res = $dbjson->filter("collection", ["key1" => "val3",
"key2" => "val2"]);
$this->assertSame(count($res), 1);
}
public function testFilter4()
{
// Return the keys where filter = array ("key1"=>array ("val3", "=="),
// "key2"=>array ("val2", "=="))
$dbjson = new Dbjson("dbjson://" . dbfile);
$res = $dbjson->filter("collection", ["key1" => ["val3", "=="],
"key2" => ["val2", "=="]]);
$this->assertSame(count($res), 1);
}
public function testFilter5()
{
// Return the keys where filter = array ("key1"=>array ("val3", "<="),
// "key2"=>array ("val2", "=="))
$dbjson = new Dbjson("dbjson://" . dbfile);
$res = $dbjson->filter("collection", ["key1" => ["val3", "<="],
"key2" => ["val2", "=="]]);
$this->assertSame(array_keys($res), [0,1,2]);
}
public function testFilter6()
{
// Return the keys where filter = array ("key1"=>array ("val3", "<="),
// "key2"=>array ("val2", ">"))
$dbjson = new Dbjson("dbjson://" . dbfile);
$res = $dbjson->filter("collection", ["key1" => ["val3", "<="],
"key2" => ["val2", ">"]]);
$this->assertSame(count($res), 1);
}
public function testFind1()
{
$dbjson = new Dbjson("dbjson://" . dbfile);
$res = $dbjson->find("collection", ["key1" => ["val3", "<="],
"key2" => ["val2", ">"]]);
$res = array_values($res);
// ["_id"] is random : skip the test
unset($res[0]["_id"]);
$this->assertSame($res, [0 => ["key1" => "val3",
"key2" => "val4"]]);
}
public function testFind2()
{
$dbjson = new Dbjson("dbjson://" . dbfile);
$res = $dbjson->find(
"collection",
["key1" => ["val3", "<="],
"key2" => ["val2", ">"]],
"*"
);
$res = array_values($res);
// ["_id"] is random : skip the test
unset($res[0]["_id"]);
$this->assertSame($res, [0 => ["key1" => "val3",
"key2" => "val4"]]);
}
public function testFind3()
{
// Exception : fields not an array
$this->setExpectedException("Exception");
$dbjson = new Dbjson("dbjson://" . dbfile);
$res = $dbjson->find(
"collection",
["key1" => ["val3", "<="],
"key2" => ["val2", ">"]],
"key1"
);
}
public function testFind4()
{
$dbjson = new Dbjson("dbjson://" . dbfile);
$res = $dbjson->find(
"collection",
["key1" => ["val3", "<="],
"key2" => ["val2", ">"]],
["key1"]
);
// ["_id"] is random : skip the test
$res = array_values($res);
unset($res[0]["_id"]);
$this->assertSame($res, [0 => ["key1" => "val3"]]);
}
public function testFind5()
{
$dbjson = new Dbjson("dbjson://" . dbfile);
$res = $dbjson->find(
"collection",
["key1" => ["val3", "<="],
"key2" => ["val2", ">"]],
["key1", "key2"]
);
$res = array_values($res);
// ["_id"] is random : skip the test
unset($res[0]["_id"]);
$this->assertSame($res, [0 => ["key1" => "val3",
"key2" => "val4"]]);
}
public function testFind6()
{
$dbjson = new Dbjson("dbjson://" . dbfile);
$res = $dbjson->find(
"collection",
["key1" => ["val3", "<="],
"key2" => ["val2", "=="]],
["key1", "key2"]
);
$res = array_values($res);
// ["_id"] is random : skip the test
unset($res[0]["_id"]);
unset($res[1]["_id"]);
unset($res[2]["_id"]);
$this->assertSame($res, [0 => ["key1" => "val1",
"key2" => "val2"],
1 => ["key1" => "val1",
"key2" => "val2"],
2 => ["key1" => "val3",
"key2" => "val2"]]);
}
public function testFind7()
{
$dbjson = new Dbjson("dbjson://" . dbfile);
$res = $dbjson->find(
"collection",
["key1" => ["val3", "<="],
"key2" => ["val2", "=="]],
["key2"]
);
$res = array_values($res);
// ["_id"] is random : skip the test
unset($res[0]["_id"]);
unset($res[1]["_id"]);
unset($res[2]["_id"]);
$this->assertSame($res, [0 => ["key2" => "val2"],
1 => ["key2" => "val2"],
2 => ["key2" => "val2"]]);
}
public function testFind8()
{
$dbjson = new Dbjson("dbjson://" . dbfile);
$res = $dbjson->find(
"collection",
["key1" => ["val3", "<="],
"key2" => ["val2", "=="]],
["key2"],
1
);
$res = array_values($res);
// ["_id"] is random : skip the test
unset($res[0]["_id"]);
$this->assertSame($res, [0 => ["key2" => "val2"]]);
}
public function testDeleteOne1()
{
$dbjson = new Dbjson("dbjson://" . dbfile);
$res = $dbjson->deleteOne(
"collection",
["key1" => ["val3", "<="],
"key2" => ["val2",
"=="]]
);
$this->assertSame($res, 1);
}
public function testFind9()
{
$dbjson = new Dbjson("dbjson://" . dbfile);
$res = $dbjson->find(
"collection",
["key1" => ["val3", "<="],
"key2" => ["val2", "=="]],
["key2"]
);
$res = array_values($res);
// ["_id"] is random : skip the test
unset($res[0]["_id"]);
unset($res[1]["_id"]);
$this->assertSame($res, [0 => ["key2" => "val2"],
1 => ["key2" => "val2"]]);
}
public function testDeleteMany1()
{
$dbjson = new Dbjson("dbjson://" . dbfile);
$res = $dbjson->deleteMany(
"collection",
["key1" => ["val3", "<="],
"key2" => ["val2",
"=="]]
);
$this->assertSame($res, 2);
}
public function testFind10()
{
$dbjson = new Dbjson("dbjson://" . dbfile);
$res = $dbjson->find(
"collection",
["key1" => ["val3", "<="],
"key2" => ["val2", "=="]],
["key2"]
);
// ["_id"] is random : skip the test
unset($res[1]["_id"]);
unset($res[2]["_id"]);
$this->assertSame($res, []);
}
public function testFind11()
{
$dbjson = new Dbjson("dbjson://" . dbfile);
$res = array_values($dbjson->find("collection"));
// ["_id"] is random : skip the test
unset($res[0]["_id"]);
$this->assertSame($res, [0 => ["key1" => "val3", "key2" => "val4"]]);
}
public function testReplace1()
{
$dbjson = new Dbjson("dbjson://" . dbfile);
$res = $dbjson->replace("collection", [], ["key2" => "val5"]);
$this->assertSame($res, 1);
}
public function testFind12()
{
$dbjson = new Dbjson("dbjson://" . dbfile);
$res = array_values($dbjson->find("collection"));
// ["_id"] is random : skip the test
unset($res[0]["_id"]);
$this->assertSame($res, [0 => ["key2" => "val5"]]);
}
public function testUpdate1()
{
$dbjson = new Dbjson("dbjson://" . dbfile);
$res = $dbjson->update("collection", [], ["key2" => "val6",
"key5" => "val5"]);
$this->assertSame($res, 1);
}
public function testFind13()
{
$dbjson = new Dbjson("dbjson://" . dbfile);
$res = array_values($dbjson->find("collection"));
// ["_id"] is random : skip the test
unset($res[0]["_id"]);
$this->assertSame($res, [0 => ["key2" => "val6",
"key5" => "val5"]]);
}
public function testInsertOne3()
{
// Document #4
$dbjson = new Dbjson("dbjson://" . dbfile);
$res = $dbjson->insertOne(
"collection",
["key1" => "val1", "key2" => "val2"]
);
$this->assertSame($res, 1);
}
public function testFind14()
{
$dbjson = new Dbjson("dbjson://" . dbfile);
$res = array_values($dbjson->find("collection"));
// ["_id"] is random : skip the test
unset($res[0]["_id"]);
unset($res[1]["_id"]);
$this->assertSame($res, [0 => ["key2" => "val6",
"key5" => "val5"],
1 => ["key1" => "val1",
"key2" => "val2"]]);
}
public function testUpdate2()
{
$dbjson = new Dbjson("dbjson://" . dbfile);
$res = $dbjson->update("collection", [], ["key2" => "val7",
"key5" => "val8"]);
$this->assertSame($res, 2);
}
public function testFind15()
{
$dbjson = new Dbjson("dbjson://" . dbfile);
$res = array_values($dbjson->find("collection"));
// ["_id"] is random : skip the test
unset($res[0]["_id"]);
unset($res[1]["_id"]);
$this->assertSame($res, [0 => ["key2" => "val7",
"key5" => "val8"],
1 => ["key1" => "val1",
"key2" => "val7",
"key5" => "val8"]]);
}
public function testUpdate3()
{
$dbjson = new Dbjson("dbjson://" . dbfile);
$res = $dbjson->update(
"collection",
[],
["key2" => "val9",
"key5" => "val7",
"_unset" => ["key2"]]
);
$this->assertSame($res, 2);
}
public function testFind16()
{
$dbjson = new Dbjson("dbjson://" . dbfile);
$res = array_values($dbjson->find("collection"));
// ["_id"] is random : skip the test
unset($res[0]["_id"]);
unset($res[1]["_id"]);
$this->assertSame($res, [0 => ["key5" => "val7"],
1 => ["key1" => "val1",
"key5" => "val7"]]);
}
// Concurrency tests
public function testConcurrency1()
{
$dbjson1 = new Dbjson("dbjson://" . dbfile);
$dbjson2 = new Dbjson("dbjson://" . dbfile);
$dbjson1->insertOne(
"collection",
["key1" => "val1", "key2" => "val2"]
);
$res = array_values($dbjson2->find("collection"));
// ["_id"] is random : skip the test
unset($res[0]["_id"]);
unset($res[1]["_id"]);
unset($res[2]["_id"]);
$this->assertSame($res, [0 => ["key5" => "val7"],
1 => ["key1" => "val1",
"key5" => "val7"],
2 => ["key1" => "val1",
"key2" => "val2"]]);
}
}