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