| 1 | use v5.14; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | use Attean::Algebra; | ||
| 5 | use Attean::Expression; | ||
| 6 | use Attean::SimpleQueryEvaluator; | ||
| 7 | |||
| 8 | package Test::Attean::MockClient 0.033 { | ||
| 9 | use Moo; | ||
| 10 | use Data::Dumper; | ||
| 11 | use Types::Standard qw(ConsumerOf InstanceOf Bool Object); | ||
| 12 | use Encode qw(encode encode_utf8 decode_utf8); | ||
| 13 | use namespace::clean; | ||
| 14 | |||
| 15 | has 'endpoint' => (is => 'rw'); | ||
| 16 | has 'default_graph' => (is => 'ro', isa => ConsumerOf['Attean::API::IRI'], required => 1); | ||
| 17 | has 'model' => (is => 'ro', isa => ConsumerOf['Attean::API::Model'], required => 1); | ||
| 18 | has 'silent' => (is => 'ro', isa => Bool, default => 0); | ||
| 19 | |||
| 20 | sub query { | ||
| 21 | my $self = shift; | ||
| 22 | my $sparql = shift; | ||
| 23 | my $model = $self->model; | ||
| 24 | my $silent = $self->silent; | ||
| 25 | my $bytes = encode_utf8($sparql); | ||
| 26 | my $s = AtteanX::Parser::SPARQL->new(); | ||
| 27 | my ($algebra) = $s->parse_list_from_bytes($bytes); | ||
| 28 | my $e = Attean::SimpleQueryEvaluator->new( model => $model, default_graph => $self->default_graph ); | ||
| 29 | my $results = eval { $e->evaluate($algebra, $self->default_graph) }; | ||
| 30 | if ($@ and not $silent) { | ||
| 31 | die $@; | ||
| 32 | } | ||
| 33 | return $results; | ||
| 34 | } | ||
| 35 | } | ||
| 36 | |||
| 37 | package Test::Attean::TestSimpleQueryEvaluator 0.033 { | ||
| 38 | use Moo; | ||
| 39 | use Types::Standard qw(HashRef); | ||
| 40 | |||
| 41 | extends 'Attean::SimpleQueryEvaluator'; | ||
| 42 | has 'endpoints' => (is => 'rw', isa => HashRef, default => sub { +{} }); | ||
| 43 | |||
| 44 | sub register_test_endpoint { | ||
| 45 | my $self = shift; | ||
| 46 | my $endpoint = shift; | ||
| 47 | my $data = shift; | ||
| 48 | $self->endpoints->{$endpoint} = $data; | ||
| 49 | } | ||
| 50 | |||
| 51 | sub new_service_client { | ||
| 52 | my $self = shift; | ||
| 53 | my $endpoint = shift; | ||
| 54 | my $silent = shift; | ||
| 55 | if (my $data = $self->endpoints->{$endpoint}) { | ||
| 56 | my ($model, $dg) = @$data; | ||
| 57 | return Test::Attean::MockClient->new(endpoint => $endpoint, model => $model, default_graph => $dg, silent => $silent); | ||
| 58 | } | ||
| 59 | return $self->SUPER::new_service_client($endpoint, $silent, @_); | ||
| 60 | } | ||
| 61 | } | ||
| 62 | |||
| 63 | 1; | ||
| 64 |