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