Commit | Line | Data |
a705b175 |
1 | # vim: filetype=perl |
70350518 |
2 | use strict; |
dafa32f9 |
3 | use warnings; |
d8d6276a |
4 | |
70350518 |
5 | use Test::More; |
73fbf6bd |
6 | use Config; |
70350518 |
7 | use lib qw(t/lib); |
73fbf6bd |
8 | $ENV{PERL5LIB} = join ($Config{path_sep}, @INC); |
70350518 |
9 | use DBICTest; |
d8d6276a |
10 | |
d8d6276a |
11 | |
f305b8f0 |
12 | BEGIN { |
ebcd0e4f |
13 | require DBIx::Class; |
a4a02f15 |
14 | plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for('admin_script') |
15 | unless DBIx::Class::Optional::Dependencies->req_ok_for('admin_script'); |
f305b8f0 |
16 | } |
ebcd0e4f |
17 | |
534210b5 |
18 | my @json_backends = qw/XS JSON DWIW/; |
20c48bc4 |
19 | my $tests_per_run = 5; |
20c48bc4 |
20 | plan tests => ($tests_per_run * @json_backends) + 1; |
1405206e |
21 | |
099f10d1 |
22 | |
23 | # test the script is setting @INC properly |
1c4391f3 |
24 | test_exec (qw| -It/lib/testinclude --schema=DBICTestAdminInc --insert --connect=[] |); |
25 | cmp_ok ( $? >> 8, '==', 70, 'Correct exit code from connecting a custom INC schema' ); |
099f10d1 |
26 | |
d1b1377b |
27 | for my $js (@json_backends) { |
1405206e |
28 | |
d1b1377b |
29 | eval {JSON::Any->import ($js) }; |
30 | SKIP: { |
f9c86f87 |
31 | skip ("JSON backend $js is not available, skip testing", $tests_per_run) if $@; |
0ffbc9ec |
32 | |
d1b1377b |
33 | $ENV{JSON_ANY_ORDER} = $js; |
34 | eval { test_dbicadmin () }; |
35 | diag $@ if $@; |
36 | } |
37 | } |
38 | |
39 | sub test_dbicadmin { |
40 | my $schema = DBICTest->init_schema( sqlite_use_file => 1 ); # reinit a fresh db for every run |
d8d6276a |
41 | |
d1b1377b |
42 | my $employees = $schema->resultset('Employee'); |
d8d6276a |
43 | |
20c48bc4 |
44 | test_exec( default_args(), qw|--op=insert --set={"name":"Matt"}| ); |
e1e87a42 |
45 | ok( ($employees->count()==1), "$ENV{JSON_ANY_ORDER}: insert count" ); |
d8d6276a |
46 | |
d1b1377b |
47 | my $employee = $employees->find(1); |
e1e87a42 |
48 | ok( ($employee->name() eq 'Matt'), "$ENV{JSON_ANY_ORDER}: insert valid" ); |
d8d6276a |
49 | |
20c48bc4 |
50 | test_exec( default_args(), qw|--op=update --set={"name":"Trout"}| ); |
d1b1377b |
51 | $employee = $employees->find(1); |
e1e87a42 |
52 | ok( ($employee->name() eq 'Trout'), "$ENV{JSON_ANY_ORDER}: update" ); |
1405206e |
53 | |
20c48bc4 |
54 | test_exec( default_args(), qw|--op=insert --set={"name":"Aran"}| ); |
70350518 |
55 | |
1f2e0cb9 |
56 | SKIP: { |
57 | skip ("MSWin32 doesn't support -| either", 1) if $^O eq 'MSWin32'; |
58 | |
f9c86f87 |
59 | open(my $fh, "-|", ( $^X, 'script/dbicadmin', default_args(), qw|--op=select --attrs={"order_by":"name"}| ) ) or die $!; |
1f2e0cb9 |
60 | my $data = do { local $/; <$fh> }; |
61 | close($fh); |
fd27648a |
62 | if (!ok( ($data=~/Aran.*Trout/s), "$ENV{JSON_ANY_ORDER}: select with attrs" )) { |
dafa32f9 |
63 | diag ("data from select is $data") |
64 | }; |
1f2e0cb9 |
65 | } |
d8d6276a |
66 | |
20c48bc4 |
67 | test_exec( default_args(), qw|--op=delete --where={"name":"Trout"}| ); |
e1e87a42 |
68 | ok( ($employees->count()==1), "$ENV{JSON_ANY_ORDER}: delete" ); |
69 | } |
70 | |
20c48bc4 |
71 | sub default_args { |
72 | return ( |
73 | qw|--quiet --schema=DBICTest::Schema --class=Employee|, |
74 | q|--connect=["dbi:SQLite:dbname=t/var/DBIxClass.db","","",{"AutoCommit":1}]|, |
099f10d1 |
75 | qw|--force -I testincludenoniterference|, |
20c48bc4 |
76 | ); |
77 | } |
78 | |
e1e87a42 |
79 | # Why do we need this crap? Apparently MSWin32 can not pass through quotes properly |
80 | # (sometimes it will and sometimes not, depending on what compiler was used to build |
81 | # perl). So we go the extra mile to escape all the quotes. We can't also use ' instead |
82 | # of ", because JSON::XS (proudly) does not support "malformed JSON" as the author |
83 | # calls it. Bleh. |
84 | # |
20c48bc4 |
85 | sub test_exec { |
86 | my $perl = $^X; |
87 | |
88 | my @args = ('script/dbicadmin', @_); |
89 | |
90 | if ( $^O eq 'MSWin32' ) { |
91 | $perl = qq|"$perl"|; # execution will fail if $^X contains paths |
92 | for (@args) { |
93 | $_ =~ s/"/\\"/g; |
e1e87a42 |
94 | } |
20c48bc4 |
95 | } |
e1e87a42 |
96 | |
20c48bc4 |
97 | system ($perl, @args); |
d1b1377b |
98 | } |