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