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