]> Dogcows Code - chaz/graphql-client/blob - t/cli.t
Release GraphQL-Client 0.605
[chaz/graphql-client] / t / cli.t
1 #!/usr/bin/env perl
2
3 use warnings;
4 use strict;
5
6 use Test::Exception;
7 use Test::More;
8
9 use GraphQL::Client::CLI;
10
11 delete $ENV{GRAPHQL_CLIENT_OPTIONS};
12
13 subtest 'get_options' => sub {
14 my $expected = {
15 format => 'json:pretty',
16 filter => undef,
17 help => undef,
18 manual => undef,
19 operation_name => undef,
20 outfile => undef,
21 query => 'bar',
22 transport => undef,
23 unpack => 0,
24 url => 'foo',
25 variables => undef,
26 version => undef,
27 };
28
29 my $r = GraphQL::Client::CLI->_get_options(qw{--url foo --query bar});
30 is_deeply($r, $expected, '--url and --query set options') or diag explain $r;
31
32 $r = GraphQL::Client::CLI->_get_options(qw{foo --query bar});
33 is_deeply($r, $expected, '--url is optional') or diag explain $r;
34
35 $r = GraphQL::Client::CLI->_get_options(qw{foo bar});
36 is_deeply($r, $expected, '--query is also optional') or diag explain $r;
37
38 {
39 local $ENV{GRAPHQL_CLIENT_OPTIONS} = '--url asdf --query "baz qux" --unpack';
40 local $expected->{query} = 'baz qux';
41 local $expected->{unpack} = 1;
42 $r = GraphQL::Client::CLI->_get_options(qw{--url foo});
43 is_deeply($r, $expected, 'options can come from GRAPHQL_CLIENT_OPTIONS') or diag explain $r;
44 }
45 };
46
47 subtest 'get_options_transport' => sub {
48 my $expected = {
49 format => 'json:pretty',
50 filter => undef,
51 help => undef,
52 manual => undef,
53 operation_name => undef,
54 outfile => undef,
55 query => 'bar',
56 transport => { headers => {'X-Test' => 'value', 'X-Test-2' => 'val2' } },
57 unpack => 0,
58 url => 'foo',
59 variables => undef,
60 version => undef,
61 };
62
63 my $r = GraphQL::Client::CLI->_get_options(qw{--url foo --query bar --transport headers.X-Test=value --transport headers.X-Test-2=val2});
64 is_deeply($r, $expected, '--url, --query set option and correctly expanded transport options') or diag explain $r;
65 };
66
67
68
69
70 subtest 'expand_vars' => sub {
71 my $r = GraphQL::Client::CLI::_expand_vars({
72 'foo.bar' => 'baz',
73 'foo.qux.muf' => 42,
74 'arr1[1].tut' => 'whatever',
75 'arr2[1][0].meh'=> 3.1415,
76 });
77 is_deeply($r, {
78 foo => {
79 bar => 'baz',
80 qux => {
81 muf => 42,
82 },
83 },
84 arr1 => [
85 undef,
86 {
87 tut => 'whatever',
88 }
89 ],
90 arr2 => [
91 undef,
92 [
93 {
94 meh => 3.1415,
95 },
96 ],
97 ],
98 }, 'expand all the vars') or diag explain $r;
99
100 throws_ok {
101 GraphQL::Client::CLI::_expand_vars({
102 'foo[0]' => 'baz',
103 'foo.bar' => 'muf',
104 });
105 } qr/^Conflicting keys/, 'throw if conflict between hash and array';
106
107 throws_ok {
108 GraphQL::Client::CLI::_expand_vars({
109 'foo' => 'baz',
110 'foo.bar' => 'muf',
111 });
112 } qr/^Conflicting keys/, 'throw if conflict between hash and scalar';
113 };
114
115 done_testing;
This page took 0.045127 seconds and 5 git commands to generate.