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