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