AtteanX-Query-Cache

 view release on metacpan or  search on metacpan

t/simple-sparql-tpf-planner.t  view on Meta::CPAN

=head1 AUTHOR

Kjetil Kjernsmo E<lt>kjetilk@cpan.orgE<gt>.

=head1 COPYRIGHT AND LICENCE

This software is copyright (c) 2015, 2016 by Kjetil Kjernsmo.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.


=cut

use v5.14;
use autodie;
use utf8;
use Test::Modern;

use CHI;

use Attean;
use Attean::RDF;
use AtteanX::QueryPlanner::Cache::LDF;
use AtteanX::Store::Memory;
use Carp::Always;
use Data::Dumper;
use AtteanX::Store::SPARQL;
use AtteanX::Parser::SPARQL;
use AtteanX::Store::LDF;
use AtteanX::Model::SPARQLCache::LDF;
use Log::Any::Adapter;
use Redis;
use Test::RedisServer;


Log::Any::Adapter->set($ENV{LOG_ADAPTER}) if ($ENV{LOG_ADAPTER});

my $cache = CHI->new( driver => 'Memory', global => 1 );

my $p	= AtteanX::QueryPlanner::Cache::LDF->new;
isa_ok($p, 'Attean::QueryPlanner');
isa_ok($p, 'AtteanX::QueryPlanner::Cache::LDF');
does_ok($p, 'Attean::API::CostPlanner');

my $redis_server = Test::RedisServer->new;

my $redis1 = Redis->new( $redis_server->connect_info );
is $redis1->ping, 'PONG', 'Redis Pubsub ping pong ok';

package TestLDFCreateStore {
        use Moo;
        with 'Test::Attean::Store::LDF::Role::CreateStore';
};

my $test = TestLDFCreateStore->new;


{

	my $sparqlstore	= Attean->get_store('SPARQL')->new('endpoint_url' => iri('http://test.invalid/sparql'));
	isa_ok($sparqlstore, 'AtteanX::Store::SPARQL');

	my $graph = iri('http://test.invalid/graph');
	my $t		= triplepattern(variable('s'), iri('http://example.org/m/p'), literal('1'));
	my $u		= triplepattern(variable('s'), iri('http://example.org/m/p'), variable('o'));
	my $v		= triplepattern(variable('s'), iri('http://example.org/m/q'), blank('xyz'));
	my $w		= triplepattern(variable('a'), iri('http://example.org/m/b'), iri('http://example.org/m/c'));
	my $x		= triplepattern(variable('s'), iri('http://example.org/m/q'), iri('http://example.org/m/a'));
	my $y		= triplepattern(variable('o'), iri('http://example.org/m/b'), literal('2'));
	my $z		= triplepattern(variable('a'), iri('http://example.org/m/c'), variable('s'));
	my $s		= triplepattern(iri('http://example.org/m/a'), variable('p'), variable('o'));

	my $ldfstore	= $test->create_store(triples => [
																	 triple(iri('http://example.org/foo'), iri('http://example.org/m/p'), literal('1')),
																	 triple(iri('http://example.org/foo'), iri('http://example.org/m/p'), iri('http://example.org/bar')),
																	 triple(iri('http://example.org/m/a'), iri('http://example.org/m/p'), iri('http://example.org/bar')),
																	 triple(iri('http://example.org/bar'), iri('http://example.org/m/p'), literal('1')),
																	 triple(iri('http://example.org/bar'), iri('http://example.org/m/p'), literal('o')),
																	 triple(iri('http://example.org/bar'), iri('http://example.org/m/p'), literal('dahut')),
																	 triple(iri('http://example.com/foo'), iri('http://example.org/m/p'), literal('dahut')),
																	 triple(iri('http://example.com/foo'), iri('http://example.org/m/p'), iri('http://example.org/baz')),
																	 triple(iri('http://example.com/foo'), iri('http://example.org/m/p'), iri('http://example.org/foobar')),
																	 triple(iri('http://example.com/bar'), iri('http://example.org/m/p'), literal('dahut')),
																	 triple(iri('http://example.org/dahut'), iri('http://example.org/m/dahut'), literal('1')),
																	 triple(iri('http://example.org/dahut'), iri('http://example.org/m/dahut'), literal('Foobar')),
																	 triple(iri('http://example.org/dahut'), iri('http://example.org/m/q'), literal('xyz')),
																	 triple(iri('http://example.com/dahut'), iri('http://example.org/m/b'), iri('http://example.org/m/c')),
																	 triple(iri('http://example.com/dahut'), iri('http://example.org/m/b'), literal('2')),
																	 triple(iri('http://example.org/m/a'), iri('http://example.org/m/q'), iri('http://example.org/baz')),
																	 triple(iri('http://example.org/m/a'), iri('http://example.org/m/q'), iri('http://example.org/foobar')),
																	 triple(iri('http://example.org/m/a'), iri('http://example.org/m/c'), iri('http://example.org/foo')),
																	 triple(iri('http://example.org/m/a'), iri('http://example.org/m/p'), iri('http://example.org/m/o')),
																	]);


	isa_ok($ldfstore, 'AtteanX::Store::LDF');
	my $model	= AtteanX::Model::SPARQLCache::LDF->new( store => $sparqlstore,
																		  ldf_store => $ldfstore,
																		  cache => $cache,
																		  publisher => $redis1);
	isa_ok($model, 'AtteanX::Model::SPARQLCache::LDF');
	isa_ok($model, 'AtteanX::Model::SPARQLCache');
	isa_ok($model, 'AtteanX::Model::SPARQL');

	subtest 'Empty BGP, to test basics' => sub {
		# plan skip_all => 'it works';
		note("An empty BGP should produce the join identity table plan");
		my $bgp		= Attean::Algebra::BGP->new(triples => []);
		my $plan	= $p->plan_for_algebra($bgp, $model, [$graph]);
		does_ok($plan, 'Attean::API::Plan', 'Empty BGP');
		isa_ok($plan, 'Attean::Plan::Table');
		my $rows	= $plan->rows;
		is(scalar(@$rows), 1);
	};


	subtest '1-triple BGP single variable, with cache, not cached' => sub {
		note("A 1-triple BGP should produce a single Attean::Plan::Iterator plan object");
		$cache->set('?v001 <http://example.org/m/p> "1" .', ['<http://example.org/foo>', '<http://example.org/bar>']);
		$cache->set('?v001 <http://example.org/m/p> "dahut" .', ['<http://example.com/foo>', '<http://example.com/bar>']);



( run in 0.999 second using v1.01-cache-2.11-cpan-39bf76dae61 )