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