X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F89dbicadmin.t;h=1729d2df29c8c43328fbe9d65c652eb2dd81886e;hb=26283ee38f220f6c6bae720ea5a189c9c0f47f6f;hp=b62d62231e93d650e936d50cfa4aef886ec6d072;hpb=35ce997716cc332d367bea6654da4d982671fb10;p=dbsrgits%2FDBIx-Class.git diff --git a/t/89dbicadmin.t b/t/89dbicadmin.t index b62d622..1729d2d 100644 --- a/t/89dbicadmin.t +++ b/t/89dbicadmin.t @@ -6,10 +6,9 @@ use Test::More; use lib qw(t/lib); use DBICTest; -my $schema = DBICTest->init_schema(); -eval 'require JSON'; -plan skip_all => 'Install JSON to run this test' if ($@); +eval 'require JSON::Any'; +plan skip_all => 'Install JSON::Any to run this test' if ($@); eval 'require Text::CSV_XS'; if ($@) { @@ -17,28 +16,74 @@ if ($@) { plan skip_all => 'Install Text::CSV_XS or Text::CSV_PP to run this test' if ($@); } -plan tests => 5; +my @json_backends = qw/XS JSON DWIW/; +my $tests_per_run = 5; -# double quotes round the arguments and single-quote within to make sure the -# tests run on windows as well +plan tests => $tests_per_run * @json_backends; -my $employees = $schema->resultset('Employee'); -my $cmd = qq|$^X script/dbicadmin --schema=DBICTest::Schema --class=Employee --tlibs --connect="['dbi:SQLite:dbname=t/var/DBIxClass.db','','']" --force --tlibs|; +for my $js (@json_backends) { -`$cmd --op=insert --set="{name:'Matt'}"`; -ok( ($employees->count()==1), 'insert count' ); + eval {JSON::Any->import ($js) }; + SKIP: { + skip ("Json backend $js is not available, skip testing", $tests_per_run) if $@; -my $employee = $employees->find(1); -ok( ($employee->name() eq 'Matt'), 'insert valid' ); + $ENV{JSON_ANY_ORDER} = $js; + eval { test_dbicadmin () }; + diag $@ if $@; + } +} + +sub test_dbicadmin { + my $schema = DBICTest->init_schema( sqlite_use_file => 1 ); # reinit a fresh db for every run + + my $employees = $schema->resultset('Employee'); + + system( _prepare_system_args( qw|--op=insert --set={"name":"Matt"}| ) ); + ok( ($employees->count()==1), "$ENV{JSON_ANY_ORDER}: insert count" ); + + my $employee = $employees->find(1); + ok( ($employee->name() eq 'Matt'), "$ENV{JSON_ANY_ORDER}: insert valid" ); + + system( _prepare_system_args( qw|--op=update --set={"name":"Trout"}| ) ); + $employee = $employees->find(1); + ok( ($employee->name() eq 'Trout'), "$ENV{JSON_ANY_ORDER}: update" ); -`$cmd --op=update --set="{name:'Trout'}"`; -$employee = $employees->find(1); -ok( ($employee->name() eq 'Trout'), 'update' ); + system( _prepare_system_args( qw|--op=insert --set={"name":"Aran"}| ) ); -`$cmd --op=insert --set="{name:'Aran'}"`; -my $data = `$cmd --op=select --attrs="{order_by:'name'}"`; -ok( ($data=~/Aran.*Trout/s), 'select with attrs' ); + SKIP: { + skip ("MSWin32 doesn't support -| either", 1) if $^O eq 'MSWin32'; -`$cmd --op=delete --where="{name:'Trout'}"`; -ok( ($employees->count()==1), 'delete' ); + open(my $fh, "-|", _prepare_system_args( qw|--op=select --attrs={"order_by":"name"}| ) ) or die $!; + my $data = do { local $/; <$fh> }; + close($fh); + ok( ($data=~/Aran.*Trout/s), "$ENV{JSON_ANY_ORDER}: select with attrs" ); + } + system( _prepare_system_args( qw|--op=delete --where={"name":"Trout"}| ) ); + ok( ($employees->count()==1), "$ENV{JSON_ANY_ORDER}: delete" ); +} + +# Why do we need this crap? Apparently MSWin32 can not pass through quotes properly +# (sometimes it will and sometimes not, depending on what compiler was used to build +# perl). So we go the extra mile to escape all the quotes. We can't also use ' instead +# of ", because JSON::XS (proudly) does not support "malformed JSON" as the author +# calls it. Bleh. +# +sub _prepare_system_args { + my $perl = $^X; + my @args = ( + qw|script/dbicadmin --quiet --schema=DBICTest::Schema --class=Employee --tlibs|, + q|--connect=["dbi:SQLite:dbname=t/var/DBIxClass.db","","",{"AutoCommit":1}]|, + qw|--force --tlibs|, + @_, + ); + + if ( $^O eq 'MSWin32' ) { + $perl = qq|"$perl"|; # execution will fail if $^X contains paths + for (@args) { + $_ =~ s/"/\\"/g; + } + } + + return ($perl, @args); +}