Alias properly checks ->{attrs}{alias}
[dbsrgits/DBIx-Class-Historic.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
d1b1377b 19my @json_backends = qw/XS JSON DWIW Syck/;
20my $tests_per_run = 5;
d8d6276a 21
d1b1377b 22plan tests => $tests_per_run * @json_backends;
1405206e 23
d1b1377b 24use JSON::Any;
25for my $js (@json_backends) {
1405206e 26
d1b1377b 27 eval {JSON::Any->import ($js) };
28 SKIP: {
2f3f9d21 29 skip ("Json backend $js is not available, skip testing", $tests_per_run) if $@;
0ffbc9ec 30
d1b1377b 31 $ENV{JSON_ANY_ORDER} = $js;
32 eval { test_dbicadmin () };
33 diag $@ if $@;
34 }
35}
36
37sub test_dbicadmin {
38 my $schema = DBICTest->init_schema( sqlite_use_file => 1 ); # reinit a fresh db for every run
d8d6276a 39
d1b1377b 40 my $employees = $schema->resultset('Employee');
d8d6276a 41
e1e87a42 42 system( _prepare_system_args( qw|--op=insert --set={"name":"Matt"}| ) );
43 ok( ($employees->count()==1), "$ENV{JSON_ANY_ORDER}: insert count" );
d8d6276a 44
d1b1377b 45 my $employee = $employees->find(1);
e1e87a42 46 ok( ($employee->name() eq 'Matt'), "$ENV{JSON_ANY_ORDER}: insert valid" );
d8d6276a 47
e1e87a42 48 system( _prepare_system_args( qw|--op=update --set={"name":"Trout"}| ) );
d1b1377b 49 $employee = $employees->find(1);
e1e87a42 50 ok( ($employee->name() eq 'Trout'), "$ENV{JSON_ANY_ORDER}: update" );
1405206e 51
e1e87a42 52 system( _prepare_system_args( qw|--op=insert --set={"name":"Aran"}| ) );
70350518 53
1f2e0cb9 54 SKIP: {
55 skip ("MSWin32 doesn't support -| either", 1) if $^O eq 'MSWin32';
56
57 open(my $fh, "-|", _prepare_system_args( qw|--op=select --attrs={"order_by":"name"}| ) ) or die $!;
58 my $data = do { local $/; <$fh> };
59 close($fh);
60 ok( ($data=~/Aran.*Trout/s), "$ENV{JSON_ANY_ORDER}: select with attrs" );
61 }
d8d6276a 62
e1e87a42 63 system( _prepare_system_args( qw|--op=delete --where={"name":"Trout"}| ) );
64 ok( ($employees->count()==1), "$ENV{JSON_ANY_ORDER}: delete" );
65}
66
67# Why do we need this crap? Apparently MSWin32 can not pass through quotes properly
68# (sometimes it will and sometimes not, depending on what compiler was used to build
69# perl). So we go the extra mile to escape all the quotes. We can't also use ' instead
70# of ", because JSON::XS (proudly) does not support "malformed JSON" as the author
71# calls it. Bleh.
72#
73sub _prepare_system_args {
74 my $perl = $^X;
75 my @args = (
76 qw|script/dbicadmin --quiet --schema=DBICTest::Schema --class=Employee --tlibs|,
77 q|--connect=["dbi:SQLite:dbname=t/var/DBIxClass.db","","",{"AutoCommit":1}]|,
78 qw|--force --tlibs|,
79 @_,
80 );
81
82 if ( $^O eq 'MSWin32' ) {
83 $perl = qq|"$perl"|; # execution will fail if $^X contains paths
84 for (@args) {
85 $_ =~ s/"/\\"/g;
86 }
87 }
88
89 return ($perl, @args);
d1b1377b 90}