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