Revert skip from a0361822 and fix tests to work correctly
[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;
16120b22 7use File::Spec;
70350518 8use lib qw(t/lib);
9use DBICTest;
d8d6276a 10
f305b8f0 11BEGIN {
a0361822 12 require DBIx::Class;
13 plan skip_all => 'Test needs ' .
14 DBIx::Class::Optional::Dependencies->req_missing_for('test_admin_script')
15 unless DBIx::Class::Optional::Dependencies->req_ok_for('test_admin_script');
16120b22 16
17 # just in case the user env has stuff in it
18 delete $ENV{JSON_ANY_ORDER};
f305b8f0 19}
ebcd0e4f 20
16120b22 21use JSON::Any;
22
f3ec358e 23$ENV{PATH} = '';
24$ENV{PERL5LIB} = join ($Config{path_sep}, @INC);
25
534210b5 26my @json_backends = qw/XS JSON DWIW/;
099f10d1 27
28# test the script is setting @INC properly
7b71391b 29test_exec (qw|-It/lib/testinclude --schema=DBICTestAdminInc --connect=[] --insert|);
1c4391f3 30cmp_ok ( $? >> 8, '==', 70, 'Correct exit code from connecting a custom INC schema' );
099f10d1 31
7b71391b 32# test that config works properly
33{
34 no warnings 'qw';
35 test_exec(qw|-It/lib/testinclude --schema=DBICTestConfig --create --connect=["klaatu","barada","nikto"]|);
36 cmp_ok( $? >> 8, '==', 71, 'Correct schema loaded via config' ) || exit;
37}
38
39# test that config-file works properly
40test_exec(qw|-It/lib/testinclude --schema=DBICTestConfig --config=t/lib/admincfgtest.json --config-stanza=Model::Gort --deploy|);
41cmp_ok ($? >> 8, '==', 71, 'Correct schema loaded via testconfig');
42
16120b22 43for my $js (@json_backends) {
1405206e 44
16120b22 45 eval {JSON::Any->import ($js) };
46 SKIP: {
47 skip ("JSON backend $js is not available, skip testing", 1) if $@;
a0361822 48
16120b22 49 local $ENV{JSON_ANY_ORDER} = $js;
50 eval { test_dbicadmin () };
51 diag $@ if $@;
52 }
d1b1377b 53}
54
7b71391b 55done_testing();
56
d1b1377b 57sub test_dbicadmin {
58 my $schema = DBICTest->init_schema( sqlite_use_file => 1 ); # reinit a fresh db for every run
d8d6276a 59
d1b1377b 60 my $employees = $schema->resultset('Employee');
d8d6276a 61
20c48bc4 62 test_exec( default_args(), qw|--op=insert --set={"name":"Matt"}| );
e1e87a42 63 ok( ($employees->count()==1), "$ENV{JSON_ANY_ORDER}: insert count" );
d8d6276a 64
d1b1377b 65 my $employee = $employees->find(1);
e1e87a42 66 ok( ($employee->name() eq 'Matt'), "$ENV{JSON_ANY_ORDER}: insert valid" );
d8d6276a 67
20c48bc4 68 test_exec( default_args(), qw|--op=update --set={"name":"Trout"}| );
d1b1377b 69 $employee = $employees->find(1);
e1e87a42 70 ok( ($employee->name() eq 'Trout'), "$ENV{JSON_ANY_ORDER}: update" );
1405206e 71
20c48bc4 72 test_exec( default_args(), qw|--op=insert --set={"name":"Aran"}| );
70350518 73
1f2e0cb9 74 SKIP: {
16120b22 75 skip ("MSWin32 doesn't support -|", 1) if $^O eq 'MSWin32';
1f2e0cb9 76
f3ec358e 77 my ($perl) = $^X =~ /(.*)/;
78
85143769 79 open(my $fh, "-|", ( $perl, '-MDBICTest::RunMode', 'script/dbicadmin', default_args(), qw|--op=select --attrs={"order_by":"name"}| ) ) or die $!;
1f2e0cb9 80 my $data = do { local $/; <$fh> };
81 close($fh);
fd27648a 82 if (!ok( ($data=~/Aran.*Trout/s), "$ENV{JSON_ANY_ORDER}: select with attrs" )) {
dafa32f9 83 diag ("data from select is $data")
84 };
1f2e0cb9 85 }
d8d6276a 86
20c48bc4 87 test_exec( default_args(), qw|--op=delete --where={"name":"Trout"}| );
e1e87a42 88 ok( ($employees->count()==1), "$ENV{JSON_ANY_ORDER}: delete" );
89}
90
20c48bc4 91sub default_args {
16120b22 92 my $dsn = JSON::Any->encode([
93 'dbi:SQLite:dbname=' . DBICTest->_sqlite_dbfilename,
94 '',
95 '',
96 { AutoCommit => 1 },
97 ]);
98
20c48bc4 99 return (
100 qw|--quiet --schema=DBICTest::Schema --class=Employee|,
16120b22 101 qq|--connect=$dsn|,
099f10d1 102 qw|--force -I testincludenoniterference|,
20c48bc4 103 );
104}
105
20c48bc4 106sub test_exec {
f3ec358e 107 my ($perl) = $^X =~ /(.*)/;
20c48bc4 108
16120b22 109 my @args = ($perl, '-MDBICTest::RunMode', File::Spec->catfile(qw(script dbicadmin)), @_);
20c48bc4 110
a0361822 111 if ($^O eq 'MSWin32') {
112 require Win32::ShellQuote; # included in test optdeps
113 @args = Win32::ShellQuote::quote_system_list(@args);
20c48bc4 114 }
e1e87a42 115
a0361822 116 system @args;
d1b1377b 117}