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/; |
099f10d1 |
19 | |
20 | # test the script is setting @INC properly |
7b71391b |
21 | test_exec (qw|-It/lib/testinclude --schema=DBICTestAdminInc --connect=[] --insert|); |
1c4391f3 |
22 | cmp_ok ( $? >> 8, '==', 70, 'Correct exit code from connecting a custom INC schema' ); |
099f10d1 |
23 | |
7b71391b |
24 | # test that config works properly |
25 | { |
26 | no warnings 'qw'; |
27 | test_exec(qw|-It/lib/testinclude --schema=DBICTestConfig --create --connect=["klaatu","barada","nikto"]|); |
28 | cmp_ok( $? >> 8, '==', 71, 'Correct schema loaded via config' ) || exit; |
29 | } |
30 | |
31 | # test that config-file works properly |
32 | test_exec(qw|-It/lib/testinclude --schema=DBICTestConfig --config=t/lib/admincfgtest.json --config-stanza=Model::Gort --deploy|); |
33 | cmp_ok ($? >> 8, '==', 71, 'Correct schema loaded via testconfig'); |
34 | |
d1b1377b |
35 | for my $js (@json_backends) { |
1405206e |
36 | |
d1b1377b |
37 | eval {JSON::Any->import ($js) }; |
38 | SKIP: { |
7b71391b |
39 | skip ("JSON backend $js is not available, skip testing", 1) if $@; |
0ffbc9ec |
40 | |
d1b1377b |
41 | $ENV{JSON_ANY_ORDER} = $js; |
42 | eval { test_dbicadmin () }; |
43 | diag $@ if $@; |
44 | } |
45 | } |
46 | |
7b71391b |
47 | done_testing(); |
48 | |
d1b1377b |
49 | sub test_dbicadmin { |
50 | my $schema = DBICTest->init_schema( sqlite_use_file => 1 ); # reinit a fresh db for every run |
d8d6276a |
51 | |
d1b1377b |
52 | my $employees = $schema->resultset('Employee'); |
d8d6276a |
53 | |
20c48bc4 |
54 | test_exec( default_args(), qw|--op=insert --set={"name":"Matt"}| ); |
e1e87a42 |
55 | ok( ($employees->count()==1), "$ENV{JSON_ANY_ORDER}: insert count" ); |
d8d6276a |
56 | |
d1b1377b |
57 | my $employee = $employees->find(1); |
e1e87a42 |
58 | ok( ($employee->name() eq 'Matt'), "$ENV{JSON_ANY_ORDER}: insert valid" ); |
d8d6276a |
59 | |
20c48bc4 |
60 | test_exec( default_args(), qw|--op=update --set={"name":"Trout"}| ); |
d1b1377b |
61 | $employee = $employees->find(1); |
e1e87a42 |
62 | ok( ($employee->name() eq 'Trout'), "$ENV{JSON_ANY_ORDER}: update" ); |
1405206e |
63 | |
20c48bc4 |
64 | test_exec( default_args(), qw|--op=insert --set={"name":"Aran"}| ); |
70350518 |
65 | |
1f2e0cb9 |
66 | SKIP: { |
67 | skip ("MSWin32 doesn't support -| either", 1) if $^O eq 'MSWin32'; |
68 | |
f9c86f87 |
69 | open(my $fh, "-|", ( $^X, 'script/dbicadmin', default_args(), qw|--op=select --attrs={"order_by":"name"}| ) ) or die $!; |
1f2e0cb9 |
70 | my $data = do { local $/; <$fh> }; |
71 | close($fh); |
fd27648a |
72 | if (!ok( ($data=~/Aran.*Trout/s), "$ENV{JSON_ANY_ORDER}: select with attrs" )) { |
dafa32f9 |
73 | diag ("data from select is $data") |
74 | }; |
1f2e0cb9 |
75 | } |
d8d6276a |
76 | |
20c48bc4 |
77 | test_exec( default_args(), qw|--op=delete --where={"name":"Trout"}| ); |
e1e87a42 |
78 | ok( ($employees->count()==1), "$ENV{JSON_ANY_ORDER}: delete" ); |
79 | } |
80 | |
20c48bc4 |
81 | sub default_args { |
82 | return ( |
83 | qw|--quiet --schema=DBICTest::Schema --class=Employee|, |
84 | q|--connect=["dbi:SQLite:dbname=t/var/DBIxClass.db","","",{"AutoCommit":1}]|, |
099f10d1 |
85 | qw|--force -I testincludenoniterference|, |
20c48bc4 |
86 | ); |
87 | } |
88 | |
e1e87a42 |
89 | # Why do we need this crap? Apparently MSWin32 can not pass through quotes properly |
90 | # (sometimes it will and sometimes not, depending on what compiler was used to build |
91 | # perl). So we go the extra mile to escape all the quotes. We can't also use ' instead |
92 | # of ", because JSON::XS (proudly) does not support "malformed JSON" as the author |
93 | # calls it. Bleh. |
94 | # |
20c48bc4 |
95 | sub test_exec { |
96 | my $perl = $^X; |
97 | |
98 | my @args = ('script/dbicadmin', @_); |
99 | |
100 | if ( $^O eq 'MSWin32' ) { |
101 | $perl = qq|"$perl"|; # execution will fail if $^X contains paths |
102 | for (@args) { |
103 | $_ =~ s/"/\\"/g; |
e1e87a42 |
104 | } |
20c48bc4 |
105 | } |
e1e87a42 |
106 | |
20c48bc4 |
107 | system ($perl, @args); |
d1b1377b |
108 | } |