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