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