8 $ENV{PERL5LIB} = join ($Config{path_sep}, @INC);
14 plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for('admin_script')
15 unless DBIx::Class::Optional::Dependencies->req_ok_for('admin_script');
18 my @json_backends = qw/XS JSON DWIW/;
20 # test the script is setting @INC properly
21 test_exec (qw|-It/lib/testinclude --schema=DBICTestAdminInc --connect=[] --insert|);
22 cmp_ok ( $? >> 8, '==', 70, 'Correct exit code from connecting a custom INC schema' );
24 # test that config works properly
27 test_exec(qw|-It/lib/testinclude --schema=DBICTestConfig --create --connect=["klaatu","barada","nikto"]|);
28 cmp_ok( $? >> 8, '==', 71, 'Correct schema loaded via config' ) || exit;
31 # test that config-file works properly
32 test_exec(qw|-It/lib/testinclude --schema=DBICTestConfig --config=t/lib/admincfgtest.json --config-stanza=Model::Gort --deploy|);
33 cmp_ok ($? >> 8, '==', 71, 'Correct schema loaded via testconfig');
35 for my $js (@json_backends) {
37 eval {JSON::Any->import ($js) };
39 skip ("JSON backend $js is not available, skip testing", 1) if $@;
41 $ENV{JSON_ANY_ORDER} = $js;
42 eval { test_dbicadmin () };
50 my $schema = DBICTest->init_schema( sqlite_use_file => 1 ); # reinit a fresh db for every run
52 my $employees = $schema->resultset('Employee');
54 test_exec( default_args(), qw|--op=insert --set={"name":"Matt"}| );
55 ok( ($employees->count()==1), "$ENV{JSON_ANY_ORDER}: insert count" );
57 my $employee = $employees->find(1);
58 ok( ($employee->name() eq 'Matt'), "$ENV{JSON_ANY_ORDER}: insert valid" );
60 test_exec( default_args(), qw|--op=update --set={"name":"Trout"}| );
61 $employee = $employees->find(1);
62 ok( ($employee->name() eq 'Trout'), "$ENV{JSON_ANY_ORDER}: update" );
64 test_exec( default_args(), qw|--op=insert --set={"name":"Aran"}| );
67 skip ("MSWin32 doesn't support -| either", 1) if $^O eq 'MSWin32';
69 open(my $fh, "-|", ( $^X, 'script/dbicadmin', default_args(), qw|--op=select --attrs={"order_by":"name"}| ) ) or die $!;
70 my $data = do { local $/; <$fh> };
72 if (!ok( ($data=~/Aran.*Trout/s), "$ENV{JSON_ANY_ORDER}: select with attrs" )) {
73 diag ("data from select is $data")
77 test_exec( default_args(), qw|--op=delete --where={"name":"Trout"}| );
78 ok( ($employees->count()==1), "$ENV{JSON_ANY_ORDER}: delete" );
83 qw|--quiet --schema=DBICTest::Schema --class=Employee|,
84 q|--connect=["dbi:SQLite:dbname=t/var/DBIxClass.db","","",{"AutoCommit":1}]|,
85 qw|--force -I testincludenoniterference|,
89 # Why do we need this crap? Apparently MSWin32 can not pass through quotes properly
90 # (sometimes it will and sometimes not, depending on what compiler was used to build
91 # perl). So we go the extra mile to escape all the quotes. We can't also use ' instead
92 # of ", because JSON::XS (proudly) does not support "malformed JSON" as the author
98 my @args = ('script/dbicadmin', @_);
100 if ( $^O eq 'MSWin32' ) {
101 $perl = qq|"$perl"|; # execution will fail if $^X contains paths
107 system ($perl, @args);