dep check to end of module
[dbsrgits/DBIx-Class.git] / t / 89dbicadmin.t
CommitLineData
d8d6276a 1# vim: filetype=perl
70350518 2use strict;
3use warnings;
d8d6276a 4
70350518 5use Test::More;
6use lib qw(t/lib);
7use DBICTest;
d8d6276a 8
d8d6276a 9
e4c66bc8 10eval 'require JSON::Any';
11plan skip_all => 'Install JSON::Any to run this test' if ($@);
d8d6276a 12
70350518 13eval 'require Text::CSV_XS';
14if ($@) {
15 eval 'require Text::CSV_PP';
16 plan skip_all => 'Install Text::CSV_XS or Text::CSV_PP to run this test' if ($@);
17}
d8d6276a 18
534210b5 19my @json_backends = qw/XS JSON DWIW/;
d1b1377b 20my $tests_per_run = 5;
d8d6276a 21
d1b1377b 22plan tests => $tests_per_run * @json_backends;
1405206e 23
d1b1377b 24for my $js (@json_backends) {
1405206e 25
d1b1377b 26 eval {JSON::Any->import ($js) };
27 SKIP: {
2f3f9d21 28 skip ("Json backend $js is not available, skip testing", $tests_per_run) if $@;
0ffbc9ec 29
d1b1377b 30 $ENV{JSON_ANY_ORDER} = $js;
31 eval { test_dbicadmin () };
32 diag $@ if $@;
33 }
34}
35
36sub test_dbicadmin {
37 my $schema = DBICTest->init_schema( sqlite_use_file => 1 ); # reinit a fresh db for every run
d8d6276a 38
d1b1377b 39 my $employees = $schema->resultset('Employee');
d8d6276a 40
c57f1cf7 41 system( _prepare_system_args( qw|--op=insert --set={"name":"Matt"}| ) );
e1e87a42 42 ok( ($employees->count()==1), "$ENV{JSON_ANY_ORDER}: insert count" );
d8d6276a 43
d1b1377b 44 my $employee = $employees->find(1);
e1e87a42 45 ok( ($employee->name() eq 'Matt'), "$ENV{JSON_ANY_ORDER}: insert valid" );
d8d6276a 46
c57f1cf7 47 system( _prepare_system_args( qw|--op=update --set={"name":"Trout"}| ) );
d1b1377b 48 $employee = $employees->find(1);
e1e87a42 49 ok( ($employee->name() eq 'Trout'), "$ENV{JSON_ANY_ORDER}: update" );
1405206e 50
c57f1cf7 51 system( _prepare_system_args( qw|--op=insert --set={"name":"Aran"}| ) );
70350518 52
1f2e0cb9 53 SKIP: {
54 skip ("MSWin32 doesn't support -| either", 1) if $^O eq 'MSWin32';
55
c57f1cf7 56 open(my $fh, "-|", _prepare_system_args( qw|--op=select --attrs={"order_by":"name"}| ) ) or die $!;
1f2e0cb9 57 my $data = do { local $/; <$fh> };
58 close($fh);
fd27648a 59 if (!ok( ($data=~/Aran.*Trout/s), "$ENV{JSON_ANY_ORDER}: select with attrs" )) {
60 diag ("data from select is $data")
61 };
1f2e0cb9 62 }
d8d6276a 63
c57f1cf7 64 system( _prepare_system_args( qw|--op=delete --where={"name":"Trout"}| ) );
e1e87a42 65 ok( ($employees->count()==1), "$ENV{JSON_ANY_ORDER}: delete" );
66}
67
68# Why do we need this crap? Apparently MSWin32 can not pass through quotes properly
69# (sometimes it will and sometimes not, depending on what compiler was used to build
70# perl). So we go the extra mile to escape all the quotes. We can't also use ' instead
71# of ", because JSON::XS (proudly) does not support "malformed JSON" as the author
72# calls it. Bleh.
73#
74sub _prepare_system_args {
75 my $perl = $^X;
76 my @args = (
77 qw|script/dbicadmin --quiet --schema=DBICTest::Schema --class=Employee --tlibs|,
78 q|--connect=["dbi:SQLite:dbname=t/var/DBIxClass.db","","",{"AutoCommit":1}]|,
79 qw|--force --tlibs|,
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}