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