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