plan
skip_all
=>
'set TEST_ONLINE to enable this test'
unless
$ENV
{TEST_ONLINE};
my
$conn
= r->
connect
->repl;
r->db(
'test'
)->drop->run;
r->db(
'test'
)->create->run;
r->db(
'test'
)->table(
'marvel'
)->create(
primary_key
=>
'superhero'
)->run;
r->table(
'marvel'
)->insert(
[
{
user_id
=> 1,
superhero
=>
'Iron Man'
,
superpower
=>
'Arc Reactor'
,
active
=> 1,
age
=> 35,
victories
=> 2,
battles
=> 3,
villainDefeated
=>
'Mandarin'
,
outfits
=> 12,
gadget
=>
'Shoulder-mounted rocket launcher'
},
{
user_id
=> 8,
superhero
=>
'Wolverine'
,
superpower
=>
'Adamantium'
,
age
=> 40,
victories
=> 12,
battles
=> 3,
villainDefeated
=>
'Sabretooth'
,
outfits
=> 2,
},
{
user_id
=> 9,
superhero
=>
'Spider-Man'
,
superpower
=>
'Spidy Sense'
,
age
=> 20,
victories
=> 24,
battles
=> 3,
villainDefeated
=>
'Green Goblin'
,
gadget
=>
'Web-slinger'
}
]
)->run;
r->db(
'test'
)->table(
'villains'
)->create(
primary_key
=>
'name'
)->run;
r->table(
'villains'
)->insert(
[
{
name
=>
'Mandarin'
, },
{
name
=>
'Sabretooth'
, },
{
name
=>
'Green Goblin'
}
]
)->run;
my
$res
;
$res
= r->table(
'marvel'
)->get_all( r->args( [
'Spider-Man'
,
'Wolverine'
] ),
{
index
=>
'superhero'
} )->run;
is
$res
->type, 2,
'Correct response type'
;
is_deeply [
sort
map
{
$_
->{superhero} } @{
$res
->response } ],
[
'Spider-Man'
,
'Wolverine'
],
'Correct response'
;
my
$res
= r->
do
(
r->table(
'marvel'
)->get(
'Iron Man'
),
sub
($) {
my
$ironman
=
shift
;
$ironman
->bracket(
'superpower'
);
}
)->run;
is
$res
->type, 1,
'Correct response type'
;
is
$res
->response,
'Arc Reactor'
,
'Correct response'
;
r->table(
'marvel'
)->
map
(
r->branch(
sub
{
shift
->bracket(
'victories'
)->gt(1); },
sub
{
shift
->bracket(
'superhero'
)->add(
' is a superhero'
); },
sub
{
shift
->bracket(
'superhero'
)->add(
' is a hero'
); }
)
)->run;
$res
= r->table(
'marvel'
)->for_each(
sub
{
my
$hero
=
shift
;
return
r->table(
'villains'
)->get(
$hero
->bracket(
'villainDefeated'
) )
->
delete
;
}
)->run;
is
$res
->type, 1,
'Correct response type'
;
is
$res
->response->{deleted}, 3,
'Correct response'
;
$res
= r->table(
'marvel'
)->get(
'Iron Man'
)->
do
(
sub
{
my
$ironman
=
shift
;
r->branch(
$ironman
->bracket(
'victories'
)->lt(
$ironman
->bracket(
'battles'
) ),
r->error(
'impossible code path'
),
$ironman
);
}
)->run;
is
$res
->type, 18,
'Correct response type'
;
is
$res
->response->[0],
'impossible code path'
,
'Correct response'
;
$res
= r->table(
'marvel'
)->
map
(
sub
{
my
$stuff
=
shift
;
$stuff
->bracket(
'outfits'
)->
default
(0)
->add(
$stuff
->bracket(
'active'
)->
default
(0) );
}
)->run;
is
$res
->type, 2,
'Correct response type'
;
is_deeply [
sort
{
$a
<=>
$b
} @{
$res
->response } ], [
'0'
,
'2'
,
'13'
],
'Correct response'
;
$res
= r->table(
'marvel'
)->
map
(r->row->bracket(
'gadget'
)->
default
)->run;
is
$res
->type, 2,
'Correct response type'
;
is_deeply [
sort
@{
$res
->response } ], [
undef
,
'Shoulder-mounted rocket launcher'
,
'Web-slinger'
],
'Correct response'
;
$res
= r->expr( {
'a'
=>
'b'
} )->merge( {
'b'
=> [ 1, 2, 3 ] } )->run(
$conn
);
is
$res
->type, 1,
'Correct response type'
;
is_deeply
$res
->response, {
'a'
=>
'b'
,
'b'
=> [
'1'
,
'2'
,
'3'
] },
'Correct response'
;
$res
= r->js(
"'str1' + 'str2'"
)->run;
is
$res
->type, 1,
'Correct response type'
;
is
$res
->response,
'str1str2'
,
'Correct response'
;
$res
= r->table(
'marvel'
)
->filter( r->js(
'(function (row) { return row.age > 35; })'
) )->run(
$conn
);
is
$res
->type, 2,
'Correct response type'
;
is
$res
->response->[0]->{superhero},
'Wolverine'
,
'Correct response type'
;
$res
= r->js(
'while(true) {}'
, 1.3 )->run(
$conn
);
is
$res
->type, 18,
'Correct response type'
;
is
$res
->response->[0],
'JavaScript query `while(true) {}` timed out after 1.300 seconds.'
,
'Correct response'
;
$res
= r->table(
'marvel'
)->coerce_to(
'array'
)->run;
is
$res
->type, 1,
'Correct response type'
;
isa_ok
$res
->response,
'ARRAY'
,
'Correct response'
;
$res
= r->expr( [ [
'name'
,
'Iron Man'
], [
'victories'
, 2000 ] ] )
->coerce_to(
'object'
)->run(
$conn
);
is
$res
->type, 1,
'Correct response type'
;
isa_ok
$res
->response,
'HASH'
,
'Correct response'
;
$res
= r->expr(1)->coerce_to(
'string'
)->run(
$conn
);
is
$res
->type, 1,
'Correct response type'
;
is
$res
->response,
'1'
,
'Correct response'
;
$res
= r->expr(
"foo"
)->type_of->run(
$conn
);
is
$res
->type, 1,
'Correct response type'
;
is
$res
->response,
'STRING'
,
'Correct response'
;
$res
= r->table(
'marvel'
)->info->run(
$conn
);
is
$res
->type, 1,
'Correct response type'
;
$res
->response->{db}->{id} =
''
;
$res
->response->{id} =
''
;
$res
->response->{doc_count_estimates} = [6];
is_deeply
$res
->response,
{
primary_key
=>
'superhero'
,
db
=> {
name
=>
'test'
,
type
=>
'DB'
,
id
=>
''
},
name
=>
'marvel'
,
type
=>
'TABLE'
,
id
=>
''
,
indexes
=> [],
doc_count_estimates
=> [6]
},
'Correct response'
;
$res
= r->json(
"[1,2,3]"
)->run(
$conn
);
is
$res
->type, 1,
'Correct response type'
;
is_deeply
$res
->response, [
'1'
,
'2'
,
'3'
],
'Correct response'
;
is
$res
->type, 1,
'Correct response type'
;
like
$res
->response->{headers}->{
'User-Agent'
},
qr/RethinkDB\/
\d+\.\d+\.\d+/,
'Correct response'
;
r->db(
'test'
)->table_create(
'posts'
)->run(
$conn
);
$res
is
$res
->type, 1,
'Correct response type'
;
is
$res
->response->{inserted}, 1,
'Correct response'
;
my
$data
= {
player
=>
'Bob'
,
game
=>
'tic tac toe'
};
$res
->run(
$conn
);
is
$res
->type, 1,
'Correct response type'
;
is_deeply
$res
->response->{form},
$data
,
'Correct response'
;
$res
= r->uuid->run;
is
$res
->type, 1,
'Correct response type'
;
like
$res
->response,
qr/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/
,
'Correct response'
;
r->db(
'test'
)->drop->run;
done_testing();