Commit | Line | Data |
a705b175 |
1 | # vim: filetype=perl |
70350518 |
2 | use strict; |
dafa32f9 |
3 | use warnings; |
d8d6276a |
4 | |
70350518 |
5 | use Test::More; |
73fbf6bd |
6 | use Config; |
70350518 |
7 | use lib qw(t/lib); |
8 | use DBICTest; |
d8d6276a |
9 | |
f305b8f0 |
10 | BEGIN { |
a0361822 |
11 | require DBIx::Class; |
12 | plan skip_all => 'Test needs ' . |
13 | DBIx::Class::Optional::Dependencies->req_missing_for('test_admin_script') |
14 | unless DBIx::Class::Optional::Dependencies->req_ok_for('test_admin_script'); |
f305b8f0 |
15 | } |
ebcd0e4f |
16 | |
f3ec358e |
17 | $ENV{PATH} = ''; |
18 | $ENV{PERL5LIB} = join ($Config{path_sep}, @INC); |
19 | |
534210b5 |
20 | my @json_backends = qw/XS JSON DWIW/; |
099f10d1 |
21 | |
22 | # test the script is setting @INC properly |
7b71391b |
23 | test_exec (qw|-It/lib/testinclude --schema=DBICTestAdminInc --connect=[] --insert|); |
1c4391f3 |
24 | cmp_ok ( $? >> 8, '==', 70, 'Correct exit code from connecting a custom INC schema' ); |
099f10d1 |
25 | |
7b71391b |
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 | |
a0361822 |
37 | TODO: { |
38 | local $TODO = 'these tests need to be fixed for Win32' if $^O eq 'MSWin32'; |
1405206e |
39 | |
a0361822 |
40 | for my $js (@json_backends) { |
0ffbc9ec |
41 | |
a0361822 |
42 | eval {JSON::Any->import ($js) }; |
43 | SKIP: { |
44 | skip ("JSON backend $js is not available, skip testing", 1) if $@; |
45 | |
46 | $ENV{JSON_ANY_ORDER} = $js; |
47 | eval { test_dbicadmin () }; |
48 | diag $@ if $@; |
49 | } |
50 | } |
d1b1377b |
51 | } |
52 | |
7b71391b |
53 | done_testing(); |
54 | |
d1b1377b |
55 | sub test_dbicadmin { |
56 | my $schema = DBICTest->init_schema( sqlite_use_file => 1 ); # reinit a fresh db for every run |
d8d6276a |
57 | |
d1b1377b |
58 | my $employees = $schema->resultset('Employee'); |
d8d6276a |
59 | |
20c48bc4 |
60 | test_exec( default_args(), qw|--op=insert --set={"name":"Matt"}| ); |
e1e87a42 |
61 | ok( ($employees->count()==1), "$ENV{JSON_ANY_ORDER}: insert count" ); |
d8d6276a |
62 | |
d1b1377b |
63 | my $employee = $employees->find(1); |
e1e87a42 |
64 | ok( ($employee->name() eq 'Matt'), "$ENV{JSON_ANY_ORDER}: insert valid" ); |
d8d6276a |
65 | |
20c48bc4 |
66 | test_exec( default_args(), qw|--op=update --set={"name":"Trout"}| ); |
d1b1377b |
67 | $employee = $employees->find(1); |
e1e87a42 |
68 | ok( ($employee->name() eq 'Trout'), "$ENV{JSON_ANY_ORDER}: update" ); |
1405206e |
69 | |
20c48bc4 |
70 | test_exec( default_args(), qw|--op=insert --set={"name":"Aran"}| ); |
70350518 |
71 | |
1f2e0cb9 |
72 | SKIP: { |
73 | skip ("MSWin32 doesn't support -| either", 1) if $^O eq 'MSWin32'; |
74 | |
f3ec358e |
75 | my ($perl) = $^X =~ /(.*)/; |
76 | |
85143769 |
77 | open(my $fh, "-|", ( $perl, '-MDBICTest::RunMode', 'script/dbicadmin', default_args(), qw|--op=select --attrs={"order_by":"name"}| ) ) or die $!; |
1f2e0cb9 |
78 | my $data = do { local $/; <$fh> }; |
79 | close($fh); |
fd27648a |
80 | if (!ok( ($data=~/Aran.*Trout/s), "$ENV{JSON_ANY_ORDER}: select with attrs" )) { |
dafa32f9 |
81 | diag ("data from select is $data") |
82 | }; |
1f2e0cb9 |
83 | } |
d8d6276a |
84 | |
20c48bc4 |
85 | test_exec( default_args(), qw|--op=delete --where={"name":"Trout"}| ); |
e1e87a42 |
86 | ok( ($employees->count()==1), "$ENV{JSON_ANY_ORDER}: delete" ); |
87 | } |
88 | |
20c48bc4 |
89 | sub default_args { |
8d6b1478 |
90 | my $dbname = DBICTest->_sqlite_dbfilename; |
20c48bc4 |
91 | return ( |
92 | qw|--quiet --schema=DBICTest::Schema --class=Employee|, |
8d6b1478 |
93 | qq|--connect=["dbi:SQLite:dbname=$dbname","","",{"AutoCommit":1}]|, |
099f10d1 |
94 | qw|--force -I testincludenoniterference|, |
20c48bc4 |
95 | ); |
96 | } |
97 | |
20c48bc4 |
98 | sub test_exec { |
f3ec358e |
99 | my ($perl) = $^X =~ /(.*)/; |
20c48bc4 |
100 | |
a0361822 |
101 | my @args = ($perl, '-MDBICTest::RunMode', 'script/dbicadmin', @_); |
20c48bc4 |
102 | |
a0361822 |
103 | if ($^O eq 'MSWin32') { |
104 | require Win32::ShellQuote; # included in test optdeps |
105 | @args = Win32::ShellQuote::quote_system_list(@args); |
20c48bc4 |
106 | } |
e1e87a42 |
107 | |
a0361822 |
108 | system @args; |
d1b1377b |
109 | } |