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