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