More cleanup
[dbsrgits/DBIx-Class.git] / t / 89dbicadmin.t
CommitLineData
a705b175 1# vim: filetype=perl
70350518 2use strict;
dafa32f9 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 {
dafa32f9 44# $ENV{PERL5LIB} = join ':', @INC;
45
d1b1377b 46 my $schema = DBICTest->init_schema( sqlite_use_file => 1 ); # reinit a fresh db for every run
d8d6276a 47
d1b1377b 48 my $employees = $schema->resultset('Employee');
d8d6276a 49
c57f1cf7 50 system( _prepare_system_args( qw|--op=insert --set={"name":"Matt"}| ) );
e1e87a42 51 ok( ($employees->count()==1), "$ENV{JSON_ANY_ORDER}: insert count" );
d8d6276a 52
d1b1377b 53 my $employee = $employees->find(1);
e1e87a42 54 ok( ($employee->name() eq 'Matt'), "$ENV{JSON_ANY_ORDER}: insert valid" );
d8d6276a 55
c57f1cf7 56 system( _prepare_system_args( qw|--op=update --set={"name":"Trout"}| ) );
d1b1377b 57 $employee = $employees->find(1);
e1e87a42 58 ok( ($employee->name() eq 'Trout'), "$ENV{JSON_ANY_ORDER}: update" );
1405206e 59
c57f1cf7 60 system( _prepare_system_args( qw|--op=insert --set={"name":"Aran"}| ) );
70350518 61
1f2e0cb9 62 SKIP: {
63 skip ("MSWin32 doesn't support -| either", 1) if $^O eq 'MSWin32';
64
c57f1cf7 65 open(my $fh, "-|", _prepare_system_args( qw|--op=select --attrs={"order_by":"name"}| ) ) or die $!;
1f2e0cb9 66 my $data = do { local $/; <$fh> };
67 close($fh);
fd27648a 68 if (!ok( ($data=~/Aran.*Trout/s), "$ENV{JSON_ANY_ORDER}: select with attrs" )) {
dafa32f9 69 diag ("data from select is $data")
70 };
1f2e0cb9 71 }
d8d6276a 72
c57f1cf7 73 system( _prepare_system_args( qw|--op=delete --where={"name":"Trout"}| ) );
e1e87a42 74 ok( ($employees->count()==1), "$ENV{JSON_ANY_ORDER}: delete" );
75}
76
77# Why do we need this crap? Apparently MSWin32 can not pass through quotes properly
78# (sometimes it will and sometimes not, depending on what compiler was used to build
79# perl). So we go the extra mile to escape all the quotes. We can't also use ' instead
80# of ", because JSON::XS (proudly) does not support "malformed JSON" as the author
81# calls it. Bleh.
82#
83sub _prepare_system_args {
84 my $perl = $^X;
dafa32f9 85
e1e87a42 86 my @args = (
dafa32f9 87 qw|script/dbicadmin --quiet --schema=DBICTest::Schema --class=Employee|,
e1e87a42 88 q|--connect=["dbi:SQLite:dbname=t/var/DBIxClass.db","","",{"AutoCommit":1}]|,
dafa32f9 89 qw|--force|,
e1e87a42 90 @_,
91 );
92
93 if ( $^O eq 'MSWin32' ) {
94 $perl = qq|"$perl"|; # execution will fail if $^X contains paths
95 for (@args) {
96 $_ =~ s/"/\\"/g;
97 }
98 }
99
100 return ($perl, @args);
d1b1377b 101}