Better handling of bogus tmpdir returned by File::Spec on MSWin32
[dbsrgits/DBIx-Class.git] / t / admin / 10script.t
CommitLineData
a705b175 1# vim: filetype=perl
70350518 2use strict;
dafa32f9 3use warnings;
d8d6276a 4
70350518 5use Test::More;
73fbf6bd 6use Config;
70350518 7use lib qw(t/lib);
8use DBICTest;
d8d6276a 9
f305b8f0 10BEGIN {
ebcd0e4f 11 require DBIx::Class;
a4a02f15 12 plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for('admin_script')
13 unless DBIx::Class::Optional::Dependencies->req_ok_for('admin_script');
f305b8f0 14}
ebcd0e4f 15
f3ec358e 16$ENV{PATH} = '';
17$ENV{PERL5LIB} = join ($Config{path_sep}, @INC);
18
534210b5 19my @json_backends = qw/XS JSON DWIW/;
099f10d1 20
21# test the script is setting @INC properly
7b71391b 22test_exec (qw|-It/lib/testinclude --schema=DBICTestAdminInc --connect=[] --insert|);
1c4391f3 23cmp_ok ( $? >> 8, '==', 70, 'Correct exit code from connecting a custom INC schema' );
099f10d1 24
7b71391b 25# test that config works properly
26{
27 no warnings 'qw';
28 test_exec(qw|-It/lib/testinclude --schema=DBICTestConfig --create --connect=["klaatu","barada","nikto"]|);
29 cmp_ok( $? >> 8, '==', 71, 'Correct schema loaded via config' ) || exit;
30}
31
32# test that config-file works properly
33test_exec(qw|-It/lib/testinclude --schema=DBICTestConfig --config=t/lib/admincfgtest.json --config-stanza=Model::Gort --deploy|);
34cmp_ok ($? >> 8, '==', 71, 'Correct schema loaded via testconfig');
35
d1b1377b 36for my $js (@json_backends) {
1405206e 37
d1b1377b 38 eval {JSON::Any->import ($js) };
39 SKIP: {
7b71391b 40 skip ("JSON backend $js is not available, skip testing", 1) if $@;
0ffbc9ec 41
d1b1377b 42 $ENV{JSON_ANY_ORDER} = $js;
43 eval { test_dbicadmin () };
44 diag $@ if $@;
45 }
46}
47
7b71391b 48done_testing();
49
d1b1377b 50sub test_dbicadmin {
51 my $schema = DBICTest->init_schema( sqlite_use_file => 1 ); # reinit a fresh db for every run
d8d6276a 52
d1b1377b 53 my $employees = $schema->resultset('Employee');
d8d6276a 54
20c48bc4 55 test_exec( default_args(), qw|--op=insert --set={"name":"Matt"}| );
e1e87a42 56 ok( ($employees->count()==1), "$ENV{JSON_ANY_ORDER}: insert count" );
d8d6276a 57
d1b1377b 58 my $employee = $employees->find(1);
e1e87a42 59 ok( ($employee->name() eq 'Matt'), "$ENV{JSON_ANY_ORDER}: insert valid" );
d8d6276a 60
20c48bc4 61 test_exec( default_args(), qw|--op=update --set={"name":"Trout"}| );
d1b1377b 62 $employee = $employees->find(1);
e1e87a42 63 ok( ($employee->name() eq 'Trout'), "$ENV{JSON_ANY_ORDER}: update" );
1405206e 64
20c48bc4 65 test_exec( default_args(), qw|--op=insert --set={"name":"Aran"}| );
70350518 66
1f2e0cb9 67 SKIP: {
68 skip ("MSWin32 doesn't support -| either", 1) if $^O eq 'MSWin32';
69
f3ec358e 70 my ($perl) = $^X =~ /(.*)/;
71
72 open(my $fh, "-|", ( $perl, 'script/dbicadmin', default_args(), qw|--op=select --attrs={"order_by":"name"}| ) ) or die $!;
1f2e0cb9 73 my $data = do { local $/; <$fh> };
74 close($fh);
fd27648a 75 if (!ok( ($data=~/Aran.*Trout/s), "$ENV{JSON_ANY_ORDER}: select with attrs" )) {
dafa32f9 76 diag ("data from select is $data")
77 };
1f2e0cb9 78 }
d8d6276a 79
20c48bc4 80 test_exec( default_args(), qw|--op=delete --where={"name":"Trout"}| );
e1e87a42 81 ok( ($employees->count()==1), "$ENV{JSON_ANY_ORDER}: delete" );
82}
83
20c48bc4 84sub default_args {
8d6b1478 85 my $dbname = DBICTest->_sqlite_dbfilename;
20c48bc4 86 return (
87 qw|--quiet --schema=DBICTest::Schema --class=Employee|,
8d6b1478 88 qq|--connect=["dbi:SQLite:dbname=$dbname","","",{"AutoCommit":1}]|,
099f10d1 89 qw|--force -I testincludenoniterference|,
20c48bc4 90 );
91}
92
e1e87a42 93# Why do we need this crap? Apparently MSWin32 can not pass through quotes properly
94# (sometimes it will and sometimes not, depending on what compiler was used to build
95# perl). So we go the extra mile to escape all the quotes. We can't also use ' instead
96# of ", because JSON::XS (proudly) does not support "malformed JSON" as the author
97# calls it. Bleh.
98#
20c48bc4 99sub test_exec {
f3ec358e 100 my ($perl) = $^X =~ /(.*)/;
20c48bc4 101
102 my @args = ('script/dbicadmin', @_);
103
104 if ( $^O eq 'MSWin32' ) {
105 $perl = qq|"$perl"|; # execution will fail if $^X contains paths
106 for (@args) {
107 $_ =~ s/"/\\"/g;
e1e87a42 108 }
20c48bc4 109 }
e1e87a42 110
20c48bc4 111 system ($perl, @args);
d1b1377b 112}