Real detabify
[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     my $schema = DBICTest->init_schema( sqlite_use_file => 1 );  # reinit a fresh db for every run
45
46     my $employees = $schema->resultset('Employee');
47
48     system( _prepare_system_args( qw|--op=insert --set={"name":"Matt"}| ) );
49     ok( ($employees->count()==1), "$ENV{JSON_ANY_ORDER}: insert count" );
50
51     my $employee = $employees->find(1);
52     ok( ($employee->name() eq 'Matt'), "$ENV{JSON_ANY_ORDER}: insert valid" );
53
54     system( _prepare_system_args( qw|--op=update --set={"name":"Trout"}| ) );
55     $employee = $employees->find(1);
56     ok( ($employee->name() eq 'Trout'), "$ENV{JSON_ANY_ORDER}: update" );
57
58     system( _prepare_system_args( qw|--op=insert --set={"name":"Aran"}| ) );
59
60     SKIP: {
61         skip ("MSWin32 doesn't support -| either", 1) if $^O eq 'MSWin32';
62
63         open(my $fh, "-|",  _prepare_system_args( qw|--op=select --attrs={"order_by":"name"}| ) ) or die $!;
64         my $data = do { local $/; <$fh> };
65         close($fh);
66         if (!ok( ($data=~/Aran.*Trout/s), "$ENV{JSON_ANY_ORDER}: select with attrs" )) {
67                         diag ("data from select is $data")
68                 };
69     }
70
71     system( _prepare_system_args( qw|--op=delete --where={"name":"Trout"}| ) );
72     ok( ($employees->count()==1), "$ENV{JSON_ANY_ORDER}: delete" );
73 }
74
75 # Why do we need this crap? Apparently MSWin32 can not pass through quotes properly
76 # (sometimes it will and sometimes not, depending on what compiler was used to build
77 # perl). So we go the extra mile to escape all the quotes. We can't also use ' instead
78 # of ", because JSON::XS (proudly) does not support "malformed JSON" as the author
79 # calls it. Bleh.
80 #
81 sub _prepare_system_args {
82     my $perl = $^X;
83     my @args = (
84         qw|script/dbicadmin --quiet --schema=DBICTest::Schema --class=Employee --tlibs|,
85         q|--connect=["dbi:SQLite:dbname=t/var/DBIxClass.db","","",{"AutoCommit":1}]|,
86         qw|--force --tlibs|,
87         @_,
88     );
89
90     if ( $^O eq 'MSWin32' ) {
91         $perl = qq|"$perl"|;    # execution will fail if $^X contains paths
92         for (@args) {
93             $_ =~ s/"/\\"/g;
94         }
95     }
96
97     return ($perl, @args);
98 }