Fix dumb omission in t/89dbicadmin.t
[dbsrgits/DBIx-Class.git] / t / 89dbicadmin.t
1 # vim: filetype=perl
2 use strict;
3 use warnings;  
4
5 use Test::More;
6 use lib qw(t/lib);
7 use DBICTest;
8
9
10 eval 'require JSON::Any';
11 plan skip_all => 'Install JSON::Any to run this test' if ($@);
12
13 eval 'require Text::CSV_XS';
14 if ($@) {
15     eval 'require Text::CSV_PP';
16     plan skip_all => 'Install Text::CSV_XS or Text::CSV_PP to run this test' if ($@);
17 }
18
19 my @json_backends = qw/XS JSON DWIW Syck/;
20 my $tests_per_run = 5;
21
22 plan tests => $tests_per_run * @json_backends;
23
24 for my $js (@json_backends) {
25
26     eval {JSON::Any->import ($js) };
27     SKIP: {
28         skip ("Json backend $js is not available, skip testing", $tests_per_run) if $@;
29
30         $ENV{JSON_ANY_ORDER} = $js;
31         eval { test_dbicadmin () };
32         diag $@ if $@;
33     }
34 }
35
36 sub test_dbicadmin {
37     my $schema = DBICTest->init_schema( sqlite_use_file => 1 );  # reinit a fresh db for every run
38
39     my $employees = $schema->resultset('Employee');
40
41     system( _prepare_system_args( qw|--op=insert --set={"name":"Matt"}| ) );
42     ok( ($employees->count()==1), "$ENV{JSON_ANY_ORDER}: insert count" );
43
44     my $employee = $employees->find(1);
45     ok( ($employee->name() eq 'Matt'), "$ENV{JSON_ANY_ORDER}: insert valid" );
46
47     system( _prepare_system_args( qw|--op=update --set={"name":"Trout"}| ) );
48     $employee = $employees->find(1);
49     ok( ($employee->name() eq 'Trout'), "$ENV{JSON_ANY_ORDER}: update" );
50
51     system( _prepare_system_args( qw|--op=insert --set={"name":"Aran"}| ) );
52
53     SKIP: {
54         skip ("MSWin32 doesn't support -| either", 1) if $^O eq 'MSWin32';
55
56         open(my $fh, "-|",  _prepare_system_args( qw|--op=select --attrs={"order_by":"name"}| ) ) or die $!;
57         my $data = do { local $/; <$fh> };
58         close($fh);
59         ok( ($data=~/Aran.*Trout/s), "$ENV{JSON_ANY_ORDER}: select with attrs" );
60     }
61
62     system( _prepare_system_args( qw|--op=delete --where={"name":"Trout"}| ) );
63     ok( ($employees->count()==1), "$ENV{JSON_ANY_ORDER}: delete" );
64 }
65
66 # Why do we need this crap? Apparently MSWin32 can not pass through quotes properly
67 # (sometimes it will and sometimes not, depending on what compiler was used to build
68 # perl). So we go the extra mile to escape all the quotes. We can't also use ' instead
69 # of ", because JSON::XS (proudly) does not support "malformed JSON" as the author
70 # calls it. Bleh.
71 #
72 sub _prepare_system_args {
73     my $perl = $^X;
74     my @args = (
75         qw|script/dbicadmin --quiet --schema=DBICTest::Schema --class=Employee --tlibs|,
76         q|--connect=["dbi:SQLite:dbname=t/var/DBIxClass.db","","",{"AutoCommit":1}]|,
77         qw|--force --tlibs|,
78         @_,
79     );
80
81     if ( $^O eq 'MSWin32' ) {
82         $perl = qq|"$perl"|;    # execution will fail if $^X contains paths
83         for (@args) {
84             $_ =~ s/"/\\"/g;
85         }
86     }
87
88     return ($perl, @args);
89 }