Skip to content

Commit

Permalink
indexing further tested
Browse files Browse the repository at this point in the history
  • Loading branch information
esokullu committed Jan 8, 2018
1 parent 16f86fa commit 1248fbd
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 20 deletions.
13 changes: 7 additions & 6 deletions src/Pho/Kernel/Init.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,14 @@ protected function seedRoot(? Foundation\AbstractActor $founder = null): void
}
else {
$this->logger()->info("Creating a new graph from scratch");
if(!is_null($founder)) {
$founder->persist();
}
else {
throw new Exceptions\FounderMustBeSetException();
}
$this["founder"] = $this->share(function($c) use ($founder) {
if(!is_null($founder)) {
$founder->persist();
return $founder;
}
$founder_class = $c["config"]->default_objects->founder;
return new $founder_class($c); // will turn into admin by Network
return $founder;
});
$this["graph"] = $this->share(function($c) {
$graph_class = $c["config"]->default_objects->graph;
Expand Down
59 changes: 54 additions & 5 deletions src/Pho/Kernel/Services/Index/IndexInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,68 @@
interface IndexInterface
{
/**
* Direct access to the index service to query data.
* Searches through the index with given key and its value.
*
* @param string $query Cypher query
* @param array $param Query params. Optional.
*
* @return mixed Result set
*/
public function query(string $query, array $params = []); //: mixed;

/**
* Direct access to the the index client
*
* @param string $query Cypher query.
* @param array $params Query parameters.
* This class does also provide direct read-only access to the
* client, for debugging purposes.
*
* @return mixed
* @return mixed Client in its native form
*/
public function query(string $query, array $params = []); //: void;
public function client(); //: mixed

/**
* Indexes an entity
*
* Given in array form, indexes the entity (be it an
* edge or node) making it searchable.
*
* @param array $entity In array form.
*
* @return void
*
* @throws Exception if the entity header is not recognized.
*/
public function index(array $entity): void;

/**
* Notifies the index that a node was deleted
*
* Removes the entity from the index, cleaning
* up resources and making index results more efficient.
*
* @param string $id ID in string format.
*
* @return void
*/
public function nodeDeleted(string $id): void;

/**
* Notifies the index that an edge was deleted
*
* Removes the entity from the index, cleaning
* up resources and making index results more efficient.
*
* @param string $id ID in string format.
*
* @return void
*/
public function edgeDeleted(string $id): void;

/**
* Cleans up the index database
*
* @return void
*/
public function flush(): void;

}
5 changes: 5 additions & 0 deletions src/Pho/Kernel/Traits/Node/PersistentTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ protected function persistable(): bool
return (static::T_PERSISTENT && $this->kernel->live());
}

/**
* Persists the node in the database.
*
* @return void
*/
public function persist(): void
{
if(!$this->persistable())
Expand Down
18 changes: 16 additions & 2 deletions tests/Pho/Kernel/BootTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Pho\Kernel;

use Pho\Kernel\Standards\Space;

class BootTest extends TestCase
{

Expand All @@ -23,10 +25,21 @@ public function testSimple() {
$this->assertInstanceOf(Kernel::class, $this->kernel);
}

/**
* @expectedException \Pho\Kernel\Exceptions\FounderMustBeSetException
*
* @return void
*/
public function testFirstBootWithNoFounder() {
$this->redis->flushdb();
$this->kernel->boot();
}

public function testBoot() {
$this->redis->flushdb();
$this->assertFalse($this->kernel->live());
$this->kernel->boot();
$founder = new \PhoNetworksAutogenerated\User($this->kernel, new Space($this->kernel), "123456");
$this->kernel->boot($founder);
$redis_configs = $this->redis->keys("configs:*");
$this->assertContains("configs:graph_id", $redis_configs);
$graph_recreated = $this->kernel->gs()->node($this->redis->get("configs:graph_id"));
Expand All @@ -38,7 +51,8 @@ public function testBoot() {

public function testReboot() {
$this->redis->flushdb();
$this->kernel->boot();
$founder = new \PhoNetworksAutogenerated\User($this->kernel, new Space($this->kernel), "123456");
$this->kernel->boot($founder);
$founder_id = $this->kernel->founder()->id()->toString();
$graph_id = $this->kernel->graph()->id()->toString();
$founder_context = $this->kernel->founder()->context()->id()->toString();
Expand Down
2 changes: 1 addition & 1 deletion tests/Pho/Kernel/DefaultObjectsAreStandardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function testSimple() {
$this->kernel->logger()->info("DB flushed and restarted");
$this->assertInstanceOf(Kernel::class, $this->kernel);
$this->kernel->logger()->info("instance check for kernel completed.");
$this->assertInstanceOf(Standards\Founder::class, $this->kernel->founder());
$this->assertInstanceOf(\PhoNetworksAutogenerated\User::class, $this->kernel->founder());
$this->kernel->logger()->info("instance check for founder completed.");
$u = new \PhoNetworksAutogenerated\User($this->kernel, $this->graph, "123456");
$this->kernel->logger()->info("The User object is created");
Expand Down
51 changes: 45 additions & 6 deletions tests/Pho/Kernel/Services/Index/Neo4jTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,31 @@ class Neo4jTest extends TestCase

private $client;

protected function _countNumberOfNodes(): int
{
$res = $this->kernel->index()->query("MATCH(n) RETURN n");
return count($res->results());
}

protected function _countNumberOfEdges(): int
{
$res = $this->kernel->index()->query("MATCH ()-[r]->() RETURN r");
return count($res->results());
}

public function testKernelService() {
$this->assertInstanceOf(\Pho\Kernel\Services\Index\Adapters\Neo4j::class, $this->kernel->index());
}

public function testBasic() {
$this->flushDBandRestart();
//eval(\Psy\sh());
$u = new \PhoNetworksAutogenerated\User($this->kernel, $this->graph, "123456");
$about = "for testing purposes only";
$u->setAbout($about);
$res = $this->kernel->index()->query("MATCH(n {About: {about}}) RETURN n", ["about" => $about]);
$this->assertEquals($res->firstRecord()->values()[0]->value("Birthday"), $u->getBirthday());
//$this->assertEquals($res->firstRecord()->values()[0]->value("Birthday"), $u->getBirthday());
$this->assertEquals($res->results()[0]["Birthday"], $u->getBirthday());
$this->created[] = $u->id();
}

Expand All @@ -38,15 +52,40 @@ public function testWithEdge() {
$content = "my first post";
$post = $u->post($content);
$res = $this->kernel->index()->query("MATCH(n {Status: {content}}) RETURN n", ["content" => $content]);
$this->assertEquals($res->firstRecord()->values()[0]->value("CreateTime"), $post->getCreateTime());
$this->assertEquals($res->results()[0]["CreateTime"], $post->getCreateTime());
$res = $this->kernel->index()->query("MATCH(t {udid: {tail}})-[e]->(h {udid: {head}}) RETURN e", ["tail" => (string) $u->id(), "head" => (string) $post->id()]);
$this->assertEquals($res->firstRecord()->values()[0]->value("udid"), (string) $post->edges()->in()->current()->id());
$this->assertEquals($res->results()[0]["udid"], (string) $post->edges()->in()->current()->id());
$this->created[] = $u->id();
$this->created[] = $post->id();
$this->created[] = $post->edges()->in()->current()->id();
}




public function testFromScratch() {
$this->flushDBandRestart();
$initial_number_of_nodes = 3; // virtualgraph, graph, founder
$initial_number_of_edges = 0;
$this->assertEquals($initial_number_of_nodes, $this->_countNumberOfNodes());
$this->assertEquals($initial_number_of_edges, $this->_countNumberOfEdges());
$user1 = new \PhoNetworksAutogenerated\User($this->kernel, $this->graph, "123456");
$user2 = new \PhoNetworksAutogenerated\User($this->kernel, $this->graph, "123456");
$user3 = new \PhoNetworksAutogenerated\User($this->kernel, $this->graph, "123456");
$this->assertEquals(($initial_number_of_nodes + 3), $this->_countNumberOfNodes()); // including the Graph itself // where is the founder?
$user1->follow($user2);
$user2->follow($user1);
$user2->follow($user3);
$this->assertEquals(($initial_number_of_edges + 3), $this->_countNumberOfEdges());
$this->kernel->founder()->follow($user1);
$this->assertEquals(($initial_number_of_edges + 4), $this->_countNumberOfEdges());
$content = "my first post";
$post = $user1->post($content);
$this->assertEquals(($initial_number_of_nodes + 4), $this->_countNumberOfNodes());
$this->assertEquals(($initial_number_of_edges + 5), $this->_countNumberOfEdges());
$this->kernel->founder()->like($post);
$user3->like($post);
$this->assertEquals(($initial_number_of_nodes + 4), $this->_countNumberOfNodes());
$this->assertEquals(($initial_number_of_edges + 7), $this->_countNumberOfEdges());
//eval(\Psy\sh());


}
}
3 changes: 3 additions & 0 deletions tests/Pho/Kernel/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
include("tests/assets/compiled/UserOut/Consume.php");

use Pho\Kernel\Kernel;
use Pho\Kernel\Standards\Space;

class TestCase extends \PHPUnit\Framework\TestCase
{
Expand Down Expand Up @@ -49,6 +50,8 @@ protected function startKernel($founder=null): void
{
$this->configs = $this->getKernelConfig();
$this->kernel = new Kernel($this->configs);
if(is_null($founder))
$founder = new \PhoNetworksAutogenerated\User($this->kernel, new Space($this->kernel), "123456");
$this->kernel->boot($founder);
$this->graph = $this->kernel->graph();
$this->kernel->logger()->info("Test started for: %s", get_class($this));
Expand Down

0 comments on commit 1248fbd

Please sign in to comment.