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/; |
d1b1377b |
19 | my $tests_per_run = 5; |
d8d6276a |
20 | |
d1b1377b |
21 | plan tests => $tests_per_run * @json_backends; |
1405206e |
22 | |
d1b1377b |
23 | for my $js (@json_backends) { |
1405206e |
24 | |
d1b1377b |
25 | eval {JSON::Any->import ($js) }; |
26 | SKIP: { |
2f3f9d21 |
27 | skip ("Json backend $js is not available, skip testing", $tests_per_run) if $@; |
0ffbc9ec |
28 | |
d1b1377b |
29 | $ENV{JSON_ANY_ORDER} = $js; |
30 | eval { test_dbicadmin () }; |
31 | diag $@ if $@; |
32 | } |
33 | } |
34 | |
35 | sub test_dbicadmin { |
36 | my $schema = DBICTest->init_schema( sqlite_use_file => 1 ); # reinit a fresh db for every run |
d8d6276a |
37 | |
d1b1377b |
38 | my $employees = $schema->resultset('Employee'); |
d8d6276a |
39 | |
c57f1cf7 |
40 | system( _prepare_system_args( qw|--op=insert --set={"name":"Matt"}| ) ); |
e1e87a42 |
41 | ok( ($employees->count()==1), "$ENV{JSON_ANY_ORDER}: insert count" ); |
d8d6276a |
42 | |
d1b1377b |
43 | my $employee = $employees->find(1); |
e1e87a42 |
44 | ok( ($employee->name() eq 'Matt'), "$ENV{JSON_ANY_ORDER}: insert valid" ); |
d8d6276a |
45 | |
c57f1cf7 |
46 | system( _prepare_system_args( qw|--op=update --set={"name":"Trout"}| ) ); |
d1b1377b |
47 | $employee = $employees->find(1); |
e1e87a42 |
48 | ok( ($employee->name() eq 'Trout'), "$ENV{JSON_ANY_ORDER}: update" ); |
1405206e |
49 | |
c57f1cf7 |
50 | system( _prepare_system_args( qw|--op=insert --set={"name":"Aran"}| ) ); |
70350518 |
51 | |
1f2e0cb9 |
52 | SKIP: { |
53 | skip ("MSWin32 doesn't support -| either", 1) if $^O eq 'MSWin32'; |
54 | |
c57f1cf7 |
55 | open(my $fh, "-|", _prepare_system_args( qw|--op=select --attrs={"order_by":"name"}| ) ) or die $!; |
1f2e0cb9 |
56 | my $data = do { local $/; <$fh> }; |
57 | close($fh); |
fd27648a |
58 | if (!ok( ($data=~/Aran.*Trout/s), "$ENV{JSON_ANY_ORDER}: select with attrs" )) { |
dafa32f9 |
59 | diag ("data from select is $data") |
60 | }; |
1f2e0cb9 |
61 | } |
d8d6276a |
62 | |
c57f1cf7 |
63 | system( _prepare_system_args( qw|--op=delete --where={"name":"Trout"}| ) ); |
e1e87a42 |
64 | ok( ($employees->count()==1), "$ENV{JSON_ANY_ORDER}: delete" ); |
65 | } |
66 | |
67 | # Why do we need this crap? Apparently MSWin32 can not pass through quotes properly |
68 | # (sometimes it will and sometimes not, depending on what compiler was used to build |
69 | # perl). So we go the extra mile to escape all the quotes. We can't also use ' instead |
70 | # of ", because JSON::XS (proudly) does not support "malformed JSON" as the author |
71 | # calls it. Bleh. |
72 | # |
73 | sub _prepare_system_args { |
74 | my $perl = $^X; |
dafa32f9 |
75 | |
e1e87a42 |
76 | my @args = ( |
dafa32f9 |
77 | qw|script/dbicadmin --quiet --schema=DBICTest::Schema --class=Employee|, |
e1e87a42 |
78 | q|--connect=["dbi:SQLite:dbname=t/var/DBIxClass.db","","",{"AutoCommit":1}]|, |
dafa32f9 |
79 | qw|--force|, |
e1e87a42 |
80 | @_, |
81 | ); |
82 | |
83 | if ( $^O eq 'MSWin32' ) { |
84 | $perl = qq|"$perl"|; # execution will fail if $^X contains paths |
85 | for (@args) { |
86 | $_ =~ s/"/\\"/g; |
87 | } |
88 | } |
89 | |
90 | return ($perl, @args); |
d1b1377b |
91 | } |