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 { |
13 | eval "require DBIx::Class::Admin"; |
14 | plan skip_all => "Deps not installed: $@" if $@; |
d8d6276a |
15 | |
f305b8f0 |
16 | eval "require Getopt::Long::Descriptive"; |
17 | plan skip_all => 'Install Getopt::Long::Descriptive to run this test' if ($@); |
18 | |
19 | eval 'require JSON::Any'; |
20 | plan skip_all => 'Install JSON::Any to run this test' if ($@); |
d8d6276a |
21 | |
f305b8f0 |
22 | eval 'require Text::CSV_XS'; |
23 | if ($@) { |
24 | eval 'require Text::CSV_PP'; |
25 | plan skip_all => 'Install Text::CSV_XS or Text::CSV_PP to run this test' if ($@); |
26 | } |
27 | } |
534210b5 |
28 | my @json_backends = qw/XS JSON DWIW/; |
d1b1377b |
29 | my $tests_per_run = 5; |
d8d6276a |
30 | |
d1b1377b |
31 | plan tests => $tests_per_run * @json_backends; |
1405206e |
32 | |
d1b1377b |
33 | for my $js (@json_backends) { |
1405206e |
34 | |
d1b1377b |
35 | eval {JSON::Any->import ($js) }; |
36 | SKIP: { |
2f3f9d21 |
37 | skip ("Json backend $js is not available, skip testing", $tests_per_run) if $@; |
0ffbc9ec |
38 | |
d1b1377b |
39 | $ENV{JSON_ANY_ORDER} = $js; |
40 | eval { test_dbicadmin () }; |
41 | diag $@ if $@; |
42 | } |
43 | } |
44 | |
45 | sub test_dbicadmin { |
dafa32f9 |
46 | # $ENV{PERL5LIB} = join ':', @INC; |
47 | |
d1b1377b |
48 | my $schema = DBICTest->init_schema( sqlite_use_file => 1 ); # reinit a fresh db for every run |
d8d6276a |
49 | |
d1b1377b |
50 | my $employees = $schema->resultset('Employee'); |
d8d6276a |
51 | |
c57f1cf7 |
52 | system( _prepare_system_args( qw|--op=insert --set={"name":"Matt"}| ) ); |
e1e87a42 |
53 | ok( ($employees->count()==1), "$ENV{JSON_ANY_ORDER}: insert count" ); |
d8d6276a |
54 | |
d1b1377b |
55 | my $employee = $employees->find(1); |
e1e87a42 |
56 | ok( ($employee->name() eq 'Matt'), "$ENV{JSON_ANY_ORDER}: insert valid" ); |
d8d6276a |
57 | |
c57f1cf7 |
58 | system( _prepare_system_args( qw|--op=update --set={"name":"Trout"}| ) ); |
d1b1377b |
59 | $employee = $employees->find(1); |
e1e87a42 |
60 | ok( ($employee->name() eq 'Trout'), "$ENV{JSON_ANY_ORDER}: update" ); |
1405206e |
61 | |
c57f1cf7 |
62 | system( _prepare_system_args( qw|--op=insert --set={"name":"Aran"}| ) ); |
70350518 |
63 | |
1f2e0cb9 |
64 | SKIP: { |
65 | skip ("MSWin32 doesn't support -| either", 1) if $^O eq 'MSWin32'; |
66 | |
c57f1cf7 |
67 | open(my $fh, "-|", _prepare_system_args( qw|--op=select --attrs={"order_by":"name"}| ) ) or die $!; |
1f2e0cb9 |
68 | my $data = do { local $/; <$fh> }; |
69 | close($fh); |
fd27648a |
70 | if (!ok( ($data=~/Aran.*Trout/s), "$ENV{JSON_ANY_ORDER}: select with attrs" )) { |
dafa32f9 |
71 | diag ("data from select is $data") |
72 | }; |
1f2e0cb9 |
73 | } |
d8d6276a |
74 | |
c57f1cf7 |
75 | system( _prepare_system_args( qw|--op=delete --where={"name":"Trout"}| ) ); |
e1e87a42 |
76 | ok( ($employees->count()==1), "$ENV{JSON_ANY_ORDER}: delete" ); |
77 | } |
78 | |
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 | # |
85 | sub _prepare_system_args { |
86 | my $perl = $^X; |
dafa32f9 |
87 | |
e1e87a42 |
88 | my @args = ( |
dafa32f9 |
89 | qw|script/dbicadmin --quiet --schema=DBICTest::Schema --class=Employee|, |
e1e87a42 |
90 | q|--connect=["dbi:SQLite:dbname=t/var/DBIxClass.db","","",{"AutoCommit":1}]|, |
dafa32f9 |
91 | qw|--force|, |
e1e87a42 |
92 | @_, |
93 | ); |
94 | |
95 | if ( $^O eq 'MSWin32' ) { |
96 | $perl = qq|"$perl"|; # execution will fail if $^X contains paths |
97 | for (@args) { |
98 | $_ =~ s/"/\\"/g; |
99 | } |
100 | } |
101 | |
102 | return ($perl, @args); |
d1b1377b |
103 | } |