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