File: utilities.t

package info (click to toggle)
libdbix-class-helpers-perl 2.023007-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 792 kB
  • ctags: 294
  • sloc: perl: 3,872; sql: 161; makefile: 7
file content (161 lines) | stat: -rw-r--r-- 4,291 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!perl

use strict;
use warnings;

use lib 't/lib';
use Test::More;
use Test::Exception;
use Test::Deep;

use DBIx::Class::Helpers::Util ':all';

my ($ns, $class) = get_namespace_parts('Project::Schema::Result::Child');
is $ns, 'Project::Schema::Result',
   'namespace part of get_namespace_parts works';
is $class, 'Child', 'result part of get_namespace_parts works';

($ns, $class) = get_namespace_parts('Project::Schema::Result::HouseHold::Child');
is $ns, 'Project::Schema::Result',
   'namespace part of get_namespace_parts works';
is $class, 'HouseHold::Child', 'result part of get_namespace_parts works';

subtest is_load_namespaces => sub {
   ok is_load_namespaces('P::Result::Foo'),
      'is_load_namespaces works when correct';
   ok !is_load_namespaces('P::Foo'),
      'is_load_namespaces works when incorrect';
   ok is_load_namespaces('P::Result::Foo::Bar'),
         'is_load_namespaces works with two levels namespace';
};

subtest is_not_load_namespaces => sub {
   ok is_not_load_namespaces('P::Foo'),
      'is_not_load_namespaces works correct';
   ok !is_not_load_namespaces('P::Result::Foo'),
      'is_not_load_namespaces works when incorrect';
};

subtest assert_similar_namespaces => sub {
   lives_ok { assert_similar_namespaces('P::Foo', 'L::Bar') }
      'assert_similar_namespaces works when both non-namespace';
   lives_ok { assert_similar_namespaces('P::Result::Foo', 'L::Result::Bar') }
      'assert_similar_namespaces works when both namespace';
   dies_ok { assert_similar_namespaces('P::Foo', 'L::Result::Bar') }
      'assert_similar_namespaces works when right is namespace';
   dies_ok { assert_similar_namespaces('P::Result::Foo', 'L::Bar') }
      'assert_similar_namespaces works when left is namespace';
   lives_ok { assert_similar_namespaces('P::Result::Foo::Bar',  'L::Result::Foo::Bar')}
      'assert_similar_namespaces works with two levels of right namespace';
};

subtest order_by_vistor => sub {
   my $complex_order_by = [
      { -desc => [qw( foo bar )] },
      'baz',
      { -asc => 'biff' }
   ];

   cmp_deeply(
      order_by_visitor($complex_order_by, sub{shift}),
      $complex_order_by,
      'roundtrip'
   );

   cmp_deeply(
      order_by_visitor('frew', sub{'bar'}),
      'bar',
      'simplest ever'
   );

   cmp_deeply(
      order_by_visitor({ -asc => 'foo' }, sub{'bar'}),
      { -asc => 'bar' },
      'simple hash'
   );

   cmp_deeply(
      order_by_visitor([{ -asc => 'foo' }, 'bar'], sub{
         if ($_[0] eq 'foo') {
            return 'foot'
         } else {
            return $_[0]
         }
      }),
      [{ -asc => 'foot' }, 'bar'],
      'typical'
   );

};

subtest normalize_connect_info => sub {
   subtest 'form 1' => sub {
      cmp_deeply(
         normalize_connect_info('dbi:foo'),
         { dsn => 'dbi:foo' },
         'dsn',
      );

      cmp_deeply(
         normalize_connect_info('dbi:foo', 'user'),
         {
            dsn => 'dbi:foo',
            user => 'user',
         },
         'dsn, user',
      );
      cmp_deeply(
         normalize_connect_info('dbi:foo', 'user', 'pass'),
         {
            dsn => 'dbi:foo',
            user => 'user',
            password => 'pass',
         },
         'dsn, user, pass',
      );
      cmp_deeply(
         normalize_connect_info('dbi:foo', 'user', 'pass',
            { LongReadLen => 1 },
         ),
         {
            dsn => 'dbi:foo',
            user => 'user',
            password => 'pass',
            LongReadLen => 1,
         },
         'dsn, user, pass, dbi_opts',
      );
      cmp_deeply(
         normalize_connect_info('dbi:foo', 'user', 'pass',
            { LongReadLen => 1 },
            { quote_names => 1 },
         ),
         {
            dsn => 'dbi:foo',
            user => 'user',
            password => 'pass',
            LongReadLen => 1,
            quote_names => 1,
         },
         'all params',
      );
   };

   subtest 'form 2' => sub {
      my $s = sub {};
      cmp_deeply(
         normalize_connect_info($s),
         { dbh_maker => $s },
         'just sub',
      );

      cmp_deeply(
         normalize_connect_info($s, { quote_names => 1 }),
         { dbh_maker => $s, quote_names => 1 },
         'sub and options',
      );
   };

};

done_testing;