1 use Test::More tests => 39;
6 use_ok 'SL::Helper::Csv';
9 $csv = SL::Helper::Csv->new(
11 header => [ 'description' ],
14 isa_ok $csv->_csv, 'Text::CSV_XS';
15 isa_ok $csv->_io, 'IO::File';
16 isa_ok $csv->parse, 'SL::Helper::Csv', 'parsing returns self';
17 is_deeply $csv->get_data, [ { description => 'Kaffee' } ], 'simple case works';
19 $csv->class('SL::DB::Part');
21 is $csv->get_objects->[0]->description, 'Kaffee', 'get_object works';
24 SL::Dispatcher::pre_startup_setup();
27 $::myconfig{numberformat} = '1.000,00';
28 $::myconfig{dateformat} = 'dd.mm.yyyy';
29 $::locale = Locale->new('de');
31 $csv = SL::Helper::Csv->new(
32 file => \"Kaffee;0.12;12,2;1,5234\n",
33 header => [ 'description', 'sellprice', 'lastcost_as_number', 'listprice' ],
34 dispatch => { listprice => 'listprice_as_number' },
35 class => 'SL::DB::Part',
39 is $csv->get_objects->[0]->sellprice, 0.12, 'numeric attr works';
40 is $csv->get_objects->[0]->lastcost, 12.2, 'attr helper works';
41 is $csv->get_objects->[0]->listprice, 1.5234, 'dispatch works';
46 $csv = SL::Helper::Csv->new(
48 description,sellprice,lastcost_as_number,listprice,
49 Kaffee,0.12,'12,2','1,5234'
53 dispatch => { listprice => 'listprice_as_number' },
54 class => 'SL::DB::Part',
57 is scalar @{ $csv->get_objects }, 1, 'auto header works';
58 is $csv->get_objects->[0]->description, 'Kaffee', 'get_object works on auto header';
63 $csv = SL::Helper::Csv->new(
65 ;;description;sellprice;lastcost_as_number;
66 #####;Puppy;Kaffee;0.12;12,2;1,5234
68 class => 'SL::DB::Part',
71 is scalar @{ $csv->get_objects }, 1, 'bozo header doesn\'t blow things up';
75 $csv = SL::Helper::Csv->new(
77 description;partnumber;sellprice;lastcost_as_number;
78 Kaffee;;0.12;12,2;1,5234
79 Beer;1123245;0.12;12,2;1,5234
81 class => 'SL::DB::Part',
84 is scalar @{ $csv->get_objects }, 2, 'multiple objects work';
85 is $csv->get_objects->[0]->description, 'Kaffee', 'first object';
86 is $csv->get_objects->[1]->partnumber, '1123245', 'second object';
90 $csv = SL::Helper::Csv->new(
92 description;partnumber;sellprice;lastcost_as_number;
94 Beer;1123245;0.12;1.5234
96 numberformat => '1,000.00',
97 class => 'SL::DB::Part',
100 is $csv->get_objects->[0]->lastcost, '1221.52', 'formatnumber';
104 $csv = SL::Helper::Csv->new(
106 "description;partnumber;sellprice;lastcost_as_number;
107 Kaffee;;0.12;1,221.52
108 Beer;1123245;0.12;1.5234
110 numberformat => '1,000.00',
111 class => 'SL::DB::Part',
113 is $csv->parse, undef, 'broken csv header won\'t get parsed';
117 $csv = SL::Helper::Csv->new(
119 description;partnumber;sellprice;lastcost_as_number;
120 "Kaf"fee";;0.12;1,221.52
121 Beer;1123245;0.12;1.5234
123 numberformat => '1,000.00',
124 class => 'SL::DB::Part',
126 is $csv->parse, undef, 'broken csv content won\'t get parsed';
127 is_deeply $csv->errors, [ '"Kaf"fee";;0.12;1,221.52'."\n", 2023, 'EIQ - QUO character not allowed', 5, 2 ], 'error';
128 isa_ok( ($csv->errors)[0], 'SL::Helper::Csv::Error', 'Errors get objectified');
132 $csv = SL::Helper::Csv->new(
134 description;partnumber;sellprice;lastcost_as_number;wiener;
135 Kaffee;;0.12;1,221.52;ja wiener
136 Beer;1123245;0.12;1.5234;nein kein wieder
138 numberformat => '1,000.00',
139 ignore_unknown_columns => 1,
140 class => 'SL::DB::Part',
143 is $csv->get_objects->[0]->lastcost, '1221.52', 'ignore_unkown_columns works';
147 $csv = SL::Helper::Csv->new(
149 description;partnumber;sellprice;lastcost_as_number;buchungsgruppe;
150 Kaffee;;0.12;1,221.52;Standard 7%
151 Beer;1123245;0.12;1.5234;16 %
153 numberformat => '1,000.00',
154 class => 'SL::DB::Part',
156 buchungsgruppe => "buchungsgruppen.description",
160 isa_ok $csv->get_objects->[0]->buchungsgruppe, 'SL::DB::Buchungsgruppe', 'deep dispatch auto vivify works';
161 is $csv->get_objects->[0]->buchungsgruppe->description, 'Standard 7%', '...and gets set correctly';
166 $csv = SL::Helper::Csv->new(
168 description;partnumber;sellprice;lastcost_as_number;make_1;model_1;
169 Kaffee;;0.12;1,221.52;213;Chair 0815
170 Beer;1123245;0.12;1.5234;
172 numberformat => '1,000.00',
173 class => 'SL::DB::Part',
175 make_1 => "makemodels.0.make",
176 model_1 => "makemodels.0.model",
180 my @mm = $csv->get_objects->[0]->makemodel;
181 is scalar @mm, 1, 'one-to-many dispatch';
182 is $csv->get_objects->[0]->makemodels->[0]->model, 'Chair 0815', '... and works';
187 $csv = SL::Helper::Csv->new(
189 description;partnumber;sellprice;lastcost_as_number;make_1;model_1;make_2;model_2;
190 Kaffee;;0.12;1,221.52;213;Chair 0815;523;Table 15
192 numberformat => '1,000.00',
193 class => 'SL::DB::Part',
195 make_1 => "makemodels.0.make",
196 model_1 => "makemodels.0.model",
197 make_2 => "makemodels.1.make",
198 model_2 => "makemodels.1.model",
203 print Dumper($csv->errors);
205 my @mm = $csv->get_objects->[0]->makemodel;
206 is scalar @mm, 1, 'multiple one-to-many dispatch';
207 is $csv->get_objects->[0]->makemodels->[0]->model, 'Chair 0815', '...check 1';
208 is $csv->get_objects->[0]->makemodels->[0]->make, '213', '...check 2';
209 is $csv->get_objects->[0]->makemodels->[1]->model, 'Table 15', '...check 3';
210 is $csv->get_objects->[0]->makemodels->[1]->make, '523', '...check 4';
214 $csv = SL::Helper::Csv->new(
216 description;partnumber;sellprice;lastcost_as_number;buchungsgruppe;
218 numberformat => '1,000.00',
219 class => 'SL::DB::Part',
221 buchungsgruppe => "buchungsgruppen.1.description",
224 is $csv->parse, undef, 'wrong profile gets rejected';
225 is_deeply $csv->errors, [ 'buchungsgruppen.1.description', undef, "Profile path error. Indexed relationship is not OneToMany around here: 'buchungsgruppen.1'", undef ,0 ], 'error indicates wrong header';
226 isa_ok( ($csv->errors)[0], 'SL::Helper::Csv::Error', 'Errors get objectified');
230 $csv = SL::Helper::Csv->new(
232 description;partnumber;sellprice;lastcost;wiener;
233 Kaffee;;0.12;1,221.52;ja wiener
234 Beer;1123245;0.12;1.5234;nein kein wieder
236 numberformat => '1,000.00',
237 ignore_unknown_columns => 1,
239 class => 'SL::DB::Part',
241 lastcost => 'lastcost_as_number',
245 is $csv->get_objects->[0]->lastcost, '1221.52', 'strict_profile with ignore';
246 is $csv->get_objects->[0]->sellprice, undef, 'strict profile with ignore 2';
250 $csv = SL::Helper::Csv->new(
252 description;partnumber;sellprice;lastcost;wiener;
253 Kaffee;;0.12;1,221.52;ja wiener
254 Beer;1123245;0.12;1.5234;nein kein wieder
256 numberformat => '1,000.00',
258 class => 'SL::DB::Part',
260 lastcost => 'lastcost_as_number',
265 is_deeply( ($csv->errors)[0], [ 'description', undef, 'header field \'description\' is not recognized', undef, 0 ], 'strict_profile without ignore_columns throws error');
269 $csv = SL::Helper::Csv->new(
271 header => [ 'description' ],
272 class => 'SL::DB::Part',
275 is_deeply $csv->get_data, [ { description => 'Kaffee' } ], 'eol bug at the end of files';
279 $csv = SL::Helper::Csv->new(
280 file => \"Description\nKaffee",
281 class => 'SL::DB::Part',
284 is_deeply $csv->get_data, [ { description => 'Kaffee' } ], 'case insensitive header from csv works';
288 $csv = SL::Helper::Csv->new(
290 header => [ 'Description' ],
291 class => 'SL::DB::Part',
294 is_deeply $csv->get_data, [ { description => 'Kaffee' } ], 'case insensitive header as param works';