Add DBJSON support. DBJSON is a NoSQL database, writing the data in one file. There is no optimizations, so it is not quick, but it works on all the PHP sites
git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@2561 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
351
Tests/dbjsonTest.php
Normal file
351
Tests/dbjsonTest.php
Normal file
@@ -0,0 +1,351 @@
|
||||
<?php
|
||||
|
||||
require_once ("dbjson.php");
|
||||
|
||||
/** Test the dbjson database */
|
||||
class test_dbjson extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function test_insertOne1 ()
|
||||
{
|
||||
// Document #0
|
||||
define ("dbfile", "/tmp/dbjson-".time());
|
||||
$dbjson = new dbjson ("dbjson://".dbfile);
|
||||
$res = $dbjson->insertOne ("collection",
|
||||
array ("key1"=>"val1", "key2"=>"val2"));
|
||||
$this->assertSame ($res, 1);
|
||||
}
|
||||
|
||||
public function test_insertOne2 ()
|
||||
{
|
||||
// Document #1
|
||||
$dbjson = new dbjson ("dbjson://".dbfile);
|
||||
$res = $dbjson->insertOne ("collection",
|
||||
array ("key1"=>"val1", "key2"=>"val2"));
|
||||
$this->assertSame ($res, 1);
|
||||
}
|
||||
|
||||
public function test_insertMany1 ()
|
||||
{
|
||||
// Error : Invalid array provided (not array of array)
|
||||
$this->setExpectedException ("Exception");
|
||||
$dbjson = new dbjson ("dbjson://".dbfile);
|
||||
$res = $dbjson->insertMany ("collection",
|
||||
array ("key1"=>"val1", "key2"=>"val2"));
|
||||
}
|
||||
|
||||
public function test_insertMany2 ()
|
||||
{
|
||||
// Document #2 and #3
|
||||
$dbjson = new dbjson ("dbjson://".dbfile);
|
||||
$res = $dbjson->insertMany ("collection",
|
||||
array (array ("key1"=>"val3", "key2"=>"val2"),
|
||||
array ("key1"=>"val3", "key2"=>"val4")));
|
||||
$this->assertSame ($res, 2);
|
||||
}
|
||||
|
||||
public function test_filter1 ()
|
||||
{
|
||||
// Return all the keys (filter = array ())
|
||||
$dbjson = new dbjson ("dbjson://".dbfile);
|
||||
$res = $dbjson->filter ("collection", array ());
|
||||
$this->assertSame ($res, array (0,1,2,3));
|
||||
}
|
||||
public function test_filter2 ()
|
||||
{
|
||||
// Return the keys where filter = array ("key2"=>"val2"))
|
||||
$dbjson = new dbjson ("dbjson://".dbfile);
|
||||
$res = $dbjson->filter ("collection", array ("key2"=>"val2"));
|
||||
$this->assertSame ($res, array (0,1,2));
|
||||
}
|
||||
public function test_filter3 ()
|
||||
{
|
||||
// Return the keys where filter = array ("key1"=>"val3","key2"=>"val2"))
|
||||
$dbjson = new dbjson ("dbjson://".dbfile);
|
||||
$res = $dbjson->filter ("collection", array ("key1"=>"val3",
|
||||
"key2"=>"val2"));
|
||||
$this->assertSame ($res, array (2));
|
||||
}
|
||||
public function test_filter4 ()
|
||||
{
|
||||
// Return the keys where filter = array ("key1"=>array ("val3", "=="),
|
||||
// "key2"=>array ("val2", "=="))
|
||||
$dbjson = new dbjson ("dbjson://".dbfile);
|
||||
$res = $dbjson->filter ("collection",array ("key1"=>array ("val3", "=="),
|
||||
"key2"=>array ("val2", "==")));
|
||||
$this->assertSame ($res, array (2));
|
||||
}
|
||||
public function test_filter5 ()
|
||||
{
|
||||
// Return the keys where filter = array ("key1"=>array ("val3", "<="),
|
||||
// "key2"=>array ("val2", "=="))
|
||||
$dbjson = new dbjson ("dbjson://".dbfile);
|
||||
$res = $dbjson->filter ("collection", array ("key1"=>array ("val3", "<="),
|
||||
"key2"=>array ("val2", "==")));
|
||||
$this->assertSame ($res, array (0,1,2));
|
||||
}
|
||||
public function test_filter6 ()
|
||||
{
|
||||
// Return the keys where filter = array ("key1"=>array ("val3", "<="),
|
||||
// "key2"=>array ("val2", ">"))
|
||||
$dbjson = new dbjson ("dbjson://".dbfile);
|
||||
$res = $dbjson->filter ("collection", array ("key1"=>array ("val3", "<="),
|
||||
"key2"=>array ("val2", ">")));
|
||||
$this->assertSame ($res, array (3));
|
||||
}
|
||||
|
||||
public function test_find1 ()
|
||||
{
|
||||
$dbjson = new dbjson ("dbjson://".dbfile);
|
||||
$res = $dbjson->find ("collection", array ("key1"=>array ("val3", "<="),
|
||||
"key2"=>array ("val2", ">")));
|
||||
// ["_id"] is random : skip the test
|
||||
unset ($res[3]["_id"]);
|
||||
$this->assertSame ($res, array (3=>array ("key1"=>"val3",
|
||||
"key2"=>"val4")));
|
||||
}
|
||||
public function test_find2 ()
|
||||
{
|
||||
$dbjson = new dbjson ("dbjson://".dbfile);
|
||||
$res = $dbjson->find ("collection", array ("key1"=>array ("val3", "<="),
|
||||
"key2"=>array ("val2", ">")),
|
||||
"*");
|
||||
// ["_id"] is random : skip the test
|
||||
unset ($res[3]["_id"]);
|
||||
$this->assertSame ($res, array (3=>array ("key1"=>"val3",
|
||||
"key2"=>"val4")));
|
||||
}
|
||||
public function test_find3 ()
|
||||
{
|
||||
// Exception : fields not an array
|
||||
$this->setExpectedException ("Exception");
|
||||
$dbjson = new dbjson ("dbjson://".dbfile);
|
||||
$res = $dbjson->find ("collection", array ("key1"=>array ("val3", "<="),
|
||||
"key2"=>array ("val2", ">")),
|
||||
"key1");
|
||||
}
|
||||
public function test_find4 ()
|
||||
{
|
||||
$dbjson = new dbjson ("dbjson://".dbfile);
|
||||
$res = $dbjson->find ("collection", array ("key1"=>array ("val3", "<="),
|
||||
"key2"=>array ("val2", ">")),
|
||||
array ("key1"));
|
||||
// ["_id"] is random : skip the test
|
||||
unset ($res[3]["_id"]);
|
||||
$this->assertSame ($res, array (3=>array ("key1"=>"val3")));
|
||||
}
|
||||
public function test_find5 ()
|
||||
{
|
||||
$dbjson = new dbjson ("dbjson://".dbfile);
|
||||
$res = $dbjson->find ("collection", array ("key1"=>array ("val3", "<="),
|
||||
"key2"=>array ("val2", ">")),
|
||||
array ("key1", "key2"));
|
||||
// ["_id"] is random : skip the test
|
||||
unset ($res[3]["_id"]);
|
||||
$this->assertSame ($res, array (3=>array ("key1"=>"val3",
|
||||
"key2"=>"val4")));
|
||||
}
|
||||
public function test_find6 ()
|
||||
{
|
||||
$dbjson = new dbjson ("dbjson://".dbfile);
|
||||
$res = $dbjson->find ("collection", array ("key1"=>array ("val3", "<="),
|
||||
"key2"=>array ("val2", "==")),
|
||||
array ("key1", "key2"));
|
||||
// ["_id"] is random : skip the test
|
||||
unset ($res[0]["_id"]);
|
||||
unset ($res[1]["_id"]);
|
||||
unset ($res[2]["_id"]);
|
||||
$this->assertSame ($res, array (0=>array ("key1"=>"val1",
|
||||
"key2"=>"val2"),
|
||||
1=>array ("key1"=>"val1",
|
||||
"key2"=>"val2"),
|
||||
2=>array ("key1"=>"val3",
|
||||
"key2"=>"val2")));
|
||||
}
|
||||
public function test_find7 ()
|
||||
{
|
||||
$dbjson = new dbjson ("dbjson://".dbfile);
|
||||
$res = $dbjson->find ("collection", array ("key1"=>array ("val3", "<="),
|
||||
"key2"=>array ("val2", "==")),
|
||||
array ("key2"));
|
||||
// ["_id"] is random : skip the test
|
||||
unset ($res[0]["_id"]);
|
||||
unset ($res[1]["_id"]);
|
||||
unset ($res[2]["_id"]);
|
||||
$this->assertSame ($res, array (0=>array ("key2"=>"val2"),
|
||||
1=>array ("key2"=>"val2"),
|
||||
2=>array ("key2"=>"val2")));
|
||||
}
|
||||
public function test_find8 ()
|
||||
{
|
||||
$dbjson = new dbjson ("dbjson://".dbfile);
|
||||
$res = $dbjson->find ("collection", array ("key1"=>array ("val3", "<="),
|
||||
"key2"=>array ("val2", "==")),
|
||||
array ("key2"), 1);
|
||||
// ["_id"] is random : skip the test
|
||||
unset ($res[0]["_id"]);
|
||||
$this->assertSame ($res, array (0=>array ("key2"=>"val2")));
|
||||
}
|
||||
|
||||
public function test_deleteOne1 ()
|
||||
{
|
||||
$dbjson = new dbjson ("dbjson://".dbfile);
|
||||
$res = $dbjson->deleteOne ("collection",
|
||||
array ("key1"=>array ("val3", "<="),
|
||||
"key2"=>array ("val2", "==")));
|
||||
$this->assertSame ($res, 1);
|
||||
}
|
||||
public function test_find9 ()
|
||||
{
|
||||
$dbjson = new dbjson ("dbjson://".dbfile);
|
||||
$res = $dbjson->find ("collection", array ("key1"=>array ("val3", "<="),
|
||||
"key2"=>array ("val2", "==")),
|
||||
array ("key2"));
|
||||
// ["_id"] is random : skip the test
|
||||
unset ($res[1]["_id"]);
|
||||
unset ($res[2]["_id"]);
|
||||
$this->assertSame ($res, array (1=>array ("key2"=>"val2"),
|
||||
2=>array ("key2"=>"val2")));
|
||||
}
|
||||
|
||||
public function test_deleteMany1 ()
|
||||
{
|
||||
$dbjson = new dbjson ("dbjson://".dbfile);
|
||||
$res = $dbjson->deleteMany ("collection",
|
||||
array ("key1"=>array ("val3", "<="),
|
||||
"key2"=>array ("val2", "==")));
|
||||
$this->assertSame ($res, 2);
|
||||
}
|
||||
public function test_find10 ()
|
||||
{
|
||||
$dbjson = new dbjson ("dbjson://".dbfile);
|
||||
$res = $dbjson->find ("collection", array ("key1"=>array ("val3", "<="),
|
||||
"key2"=>array ("val2", "==")),
|
||||
array ("key2"));
|
||||
// ["_id"] is random : skip the test
|
||||
unset ($res[1]["_id"]);
|
||||
unset ($res[2]["_id"]);
|
||||
$this->assertSame ($res, array ());
|
||||
}
|
||||
|
||||
public function test_find11 ()
|
||||
{
|
||||
$dbjson = new dbjson ("dbjson://".dbfile);
|
||||
$res = $dbjson->find ("collection");
|
||||
// ["_id"] is random : skip the test
|
||||
unset ($res[3]["_id"]);
|
||||
$this->assertSame ($res, array (3=>array ("key1"=>"val3", "key2"=>"val4")));
|
||||
}
|
||||
|
||||
public function test_replace1 ()
|
||||
{
|
||||
$dbjson = new dbjson ("dbjson://".dbfile);
|
||||
$res = $dbjson->replace ("collection", array (), array ("key2"=>"val5"));
|
||||
$this->assertSame ($res, 1);
|
||||
}
|
||||
public function test_find12 ()
|
||||
{
|
||||
$dbjson = new dbjson ("dbjson://".dbfile);
|
||||
$res = $dbjson->find ("collection");
|
||||
// ["_id"] is random : skip the test
|
||||
unset ($res[3]["_id"]);
|
||||
$this->assertSame ($res, array (3=>array ("key2"=>"val5")));
|
||||
}
|
||||
|
||||
public function test_update1 ()
|
||||
{
|
||||
$dbjson = new dbjson ("dbjson://".dbfile);
|
||||
$res = $dbjson->update ("collection", array (), array ("key2"=>"val6",
|
||||
"key5"=>"val5"));
|
||||
$this->assertSame ($res, 1);
|
||||
}
|
||||
public function test_find13 ()
|
||||
{
|
||||
$dbjson = new dbjson ("dbjson://".dbfile);
|
||||
$res = $dbjson->find ("collection");
|
||||
// ["_id"] is random : skip the test
|
||||
unset ($res[3]["_id"]);
|
||||
$this->assertSame ($res, array (3=>array ("key2"=>"val6",
|
||||
"key5"=>"val5")));
|
||||
}
|
||||
public function test_insertOne3 ()
|
||||
{
|
||||
// Document #4
|
||||
$dbjson = new dbjson ("dbjson://".dbfile);
|
||||
$res = $dbjson->insertOne ("collection",
|
||||
array ("key1"=>"val1", "key2"=>"val2"));
|
||||
$this->assertSame ($res, 1);
|
||||
}
|
||||
public function test_find14 ()
|
||||
{
|
||||
$dbjson = new dbjson ("dbjson://".dbfile);
|
||||
$res = $dbjson->find ("collection");
|
||||
// ["_id"] is random : skip the test
|
||||
unset ($res[3]["_id"]);
|
||||
unset ($res[4]["_id"]);
|
||||
$this->assertSame ($res, array (3=>array ("key2"=>"val6",
|
||||
"key5"=>"val5"),
|
||||
4=>array ("key1"=>"val1",
|
||||
"key2"=>"val2")));
|
||||
}
|
||||
|
||||
public function test_update2 ()
|
||||
{
|
||||
$dbjson = new dbjson ("dbjson://".dbfile);
|
||||
$res = $dbjson->update ("collection", array (), array ("key2"=>"val7",
|
||||
"key5"=>"val8"));
|
||||
$this->assertSame ($res, 2);
|
||||
}
|
||||
public function test_find15 ()
|
||||
{
|
||||
$dbjson = new dbjson ("dbjson://".dbfile);
|
||||
$res = $dbjson->find ("collection");
|
||||
// ["_id"] is random : skip the test
|
||||
unset ($res[3]["_id"]);
|
||||
unset ($res[4]["_id"]);
|
||||
$this->assertSame ($res, array (3=>array ("key2"=>"val7",
|
||||
"key5"=>"val8"),
|
||||
4=>array ("key1"=>"val1",
|
||||
"key2"=>"val7",
|
||||
"key5"=>"val8")));
|
||||
}
|
||||
|
||||
public function test_update3 ()
|
||||
{
|
||||
$dbjson = new dbjson ("dbjson://".dbfile);
|
||||
$res = $dbjson->update ("collection", array (),
|
||||
array ("key2"=>"val9",
|
||||
"key5"=>"val7",
|
||||
"_unset"=>array ("key2")));
|
||||
$this->assertSame ($res, 2);
|
||||
}
|
||||
public function test_find16 ()
|
||||
{
|
||||
$dbjson = new dbjson ("dbjson://".dbfile);
|
||||
$res = $dbjson->find ("collection");
|
||||
// ["_id"] is random : skip the test
|
||||
unset ($res[3]["_id"]);
|
||||
unset ($res[4]["_id"]);
|
||||
$this->assertSame ($res, array (3=>array ("key5"=>"val7"),
|
||||
4=>array ("key1"=>"val1",
|
||||
"key5"=>"val7")));
|
||||
}
|
||||
|
||||
// Concurrency tests
|
||||
public function test_concurrency1 ()
|
||||
{
|
||||
$dbjson1 = new dbjson ("dbjson://".dbfile);
|
||||
$dbjson2 = new dbjson ("dbjson://".dbfile);
|
||||
$dbjson1->insertOne ("collection",
|
||||
array ("key1"=>"val1", "key2"=>"val2"));
|
||||
$res = $dbjson2->find ("collection");
|
||||
// ["_id"] is random : skip the test
|
||||
unset ($res[3]["_id"]);
|
||||
unset ($res[4]["_id"]);
|
||||
unset ($res[5]["_id"]);
|
||||
$this->assertSame ($res, array (3=>array ("key5"=>"val7"),
|
||||
4=>array ("key1"=>"val1",
|
||||
"key5"=>"val7"),
|
||||
5=>array ("key1"=>"val1",
|
||||
"key2"=>"val2")));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user