File: Catmandu-Fix-Bind-hashmap.t

package info (click to toggle)
libcatmandu-perl 1.2024-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,552 kB
  • sloc: perl: 17,037; makefile: 24; sh: 1
file content (190 lines) | stat: -rw-r--r-- 3,703 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/env perl
use lib 't/lib';
use strict;
use warnings;
use Test::More;
use Test::Exception;
use Catmandu::Fix;
use Catmandu::Importer::Mock;
use Cpanel::JSON::XS qw(decode_json);
use Catmandu::Util   qw(:is);
use Capture::Tiny ':all';

my $pkg;

BEGIN {
    $pkg = 'Catmandu::Fix::Bind::hashmap';
    use_ok $pkg;
}
require_ok $pkg;

my $fixes = <<EOF;
do hashmap()
  add_field(foo,bar)
end
EOF

my $fixer = Catmandu::Fix->new(fixes => [$fixes]);

ok $fixer, 'create fixer';

is_deeply $fixer->fix({}), {foo => 'bar'}, 'testing add_field';

$fixes = <<EOF;
do hashmap()
end
EOF

$fixer = Catmandu::Fix->new(fixes => [$fixes]);

is_deeply $fixer->fix({foo => 'bar'}), {foo => 'bar'},
    'testing zero fix functions';

$fixes = <<EOF;
do hashmap()
  unless exists(foo)
    add_field(foo,bar)
  end
end
EOF

$fixer = Catmandu::Fix->new(fixes => [$fixes]);

is_deeply $fixer->fix({}), {foo => 'bar'}, 'testing unless';

$fixes = <<EOF;
do hashmap()
  if exists(foo)
    add_field(foo2,bar)
  end
end
EOF

$fixer = Catmandu::Fix->new(fixes => [$fixes]);

is_deeply $fixer->fix({foo => 'bar'}), {foo => 'bar', foo2 => 'bar'},
    'testing if';

$fixes = <<EOF;
do hashmap()
  select exists(foo)
end
EOF

$fixer = Catmandu::Fix->new(fixes => [$fixes]);

is_deeply $fixer->fix({foo => 'bar'}), {foo => 'bar'}, 'testing select';

$fixes = <<EOF;
do hashmap()
 do hashmap()
  do hashmap()
   add_field(foo,bar)
  end
 end
end
EOF

$fixer = Catmandu::Fix->new(fixes => [$fixes]);

is_deeply $fixer->fix({foo => 'bar'}), {foo => 'bar'}, 'testing nesting';

$fixes = <<EOF;
add_field(before,ok)
do hashmap()
   add_field(inside,ok)
end
add_field(after,ok)
EOF

$fixer = Catmandu::Fix->new(fixes => [$fixes]);

is_deeply $fixer->fix({foo => 'bar'}),
    {foo => 'bar', before => 'ok', inside => 'ok', after => 'ok'},
    'before/after testing';

# Specific tests
{
    my ($stdout, $stderr, $exit) = capture {
        $fixes = <<EOF;
  do hashmap(exporter: CSV, join: ',')
   do identity()
    copy_field(isbn,key)
    copy_field(_id,value)
   end
  end
EOF
        $fixer = Catmandu::Fix->new(fixes => [$fixes]);
        $fixer->fix(
            [
                {_id => 1, isbn => '1234567890'},
                {_id => 2, isbn => '1234567890'},
                {_id => 3, isbn => '0987654321'},
            ]
        );
        undef($fixer);
    };

    my $exp = <<EOF;
_id,value
0987654321,3
1234567890,"1,2"
EOF

    is $stdout, $exp, 'grouping isbn join';
}

{
    my ($stdout, $stderr, $exit) = capture {
        $fixes = <<EOF;
  do hashmap(exporter: JSON, uniq: 1)
    copy_field(isbn,key)
    copy_field(_id,value)
  end
EOF
        $fixer = Catmandu::Fix->new(fixes => [$fixes]);
        $fixer->fix(
            [
                {_id => 1, isbn => '1234567890'},
                {_id => 2, isbn => '1234567890'},
                {_id => 3, isbn => '0987654321'},
            ]
        );
        undef($fixer);
    };

    my $exp
        = '[{"_id":"0987654321","value":["3"]},{"_id":"1234567890","value":["1","2"]}]';

    is_deeply decode_json($stdout), decode_json($exp), 'grouping isbn uniq';
}

{
    my ($stdout, $stderr, $exit) = capture {
        $fixes = <<EOF;
  do hashmap(exporter: CSV, count: 1)
    copy_field(isbn,key)
    copy_field(_id,value)
  end
EOF
        $fixer = Catmandu::Fix->new(fixes => [$fixes]);
        $fixer->fix(
            [
                {_id => 1, isbn => '1234567890'},
                {_id => 2, isbn => '1234567890'},
                {_id => 3, isbn => '0987654321'},
            ]
        );
        undef($fixer);
    };

    my $exp = <<EOF;
_id,value
1234567890,2
0987654321,1
EOF

    is $stdout, $exp, 'grouping isbn count';
}

done_testing;