1 use Test::More tests => 47;
 
   7 use Support::TestSetup;
 
   9 use_ok 'SL::Helper::Csv';
 
  11 Support::TestSetup::login();
 
  13 my $csv = SL::Helper::Csv->new(
 
  15   header => [ 'description' ],
 
  16   class  => 'SL::DB::Part',
 
  19 isa_ok $csv->_csv, 'Text::CSV_XS';
 
  20 isa_ok $csv->_io, 'IO::File';
 
  21 isa_ok $csv->parse, 'SL::Helper::Csv', 'parsing returns self';
 
  22 is_deeply $csv->get_data, [ { description => 'Kaffee' } ], 'simple case works';
 
  24 is $csv->get_objects->[0]->description, 'Kaffee', 'get_object works';
 
  27 $::myconfig{numberformat} = '1.000,00';
 
  28 $::myconfig{dateformat} = 'dd.mm.yyyy';
 
  30 $csv = SL::Helper::Csv->new(
 
  31   file   => \"Kaffee;0.12;12,2;1,5234\n",
 
  32   header => [ 'description', 'sellprice', 'lastcost_as_number', 'listprice' ],
 
  33   profile => { listprice => 'listprice_as_number' },
 
  34   class  => 'SL::DB::Part',
 
  38 is $csv->get_objects->[0]->sellprice, 0.12, 'numeric attr works';
 
  39 is $csv->get_objects->[0]->lastcost, 12.2, 'attr helper works';
 
  40 is $csv->get_objects->[0]->listprice, 1.5234, 'dispatch works';
 
  45 $csv = SL::Helper::Csv->new(
 
  47 description,sellprice,lastcost_as_number,listprice,
 
  48 Kaffee,0.12,'12,2','1,5234'
 
  52   profile => { listprice => 'listprice_as_number' },
 
  53   class  => 'SL::DB::Part',
 
  56 is scalar @{ $csv->get_objects }, 1, 'auto header works';
 
  57 is $csv->get_objects->[0]->description, 'Kaffee', 'get_object works on auto header';
 
  62 $csv = SL::Helper::Csv->new(
 
  64 ;;description;sellprice;lastcost_as_number;
 
  65 #####;Puppy;Kaffee;0.12;12,2;1,5234
 
  67   class  => 'SL::DB::Part',
 
  70 is scalar @{ $csv->get_objects }, 1, 'bozo header doesn\'t blow things up';
 
  74 $csv = SL::Helper::Csv->new(
 
  76 description;partnumber;sellprice;lastcost_as_number;
 
  77 Kaffee;;0.12;12,2;1,5234
 
  78 Beer;1123245;0.12;12,2;1,5234
 
  80   class  => 'SL::DB::Part',
 
  83 is scalar @{ $csv->get_objects }, 2, 'multiple objects work';
 
  84 is $csv->get_objects->[0]->description, 'Kaffee', 'first object';
 
  85 is $csv->get_objects->[1]->partnumber, '1123245', 'second object';
 
  89 $csv = SL::Helper::Csv->new(
 
  91 description;partnumber;sellprice;lastcost_as_number;
 
  93 Beer;1123245;0.12;1.5234
 
  95   numberformat => '1,000.00',
 
  96   class  => 'SL::DB::Part',
 
  99 is $csv->get_objects->[0]->lastcost, '1221.52', 'formatnumber';
 
 103 $csv = SL::Helper::Csv->new(
 
 105 "description;partnumber;sellprice;lastcost_as_number;
 
 106 Kaffee;;0.12;1,221.52
 
 107 Beer;1123245;0.12;1.5234
 
 109   numberformat => '1,000.00',
 
 110   class  => 'SL::DB::Part',
 
 112 is $csv->parse, undef, 'broken csv header won\'t get parsed';
 
 116 $csv = SL::Helper::Csv->new(
 
 118 description;partnumber;sellprice;lastcost_as_number;
 
 119 "Kaf"fee";;0.12;1,221.52
 
 120 Beer;1123245;0.12;1.5234
 
 122   numberformat => '1,000.00',
 
 123   class  => 'SL::DB::Part',
 
 125 is $csv->parse, undef, 'broken csv content won\'t get parsed';
 
 126 is_deeply $csv->errors, [ '"Kaf"fee";;0.12;1,221.52'."\n", 2023, 'EIQ - QUO character not allowed', 5, 2 ], 'error';
 
 127 isa_ok( ($csv->errors)[0], 'SL::Helper::Csv::Error', 'Errors get objectified');
 
 131 $csv = SL::Helper::Csv->new(
 
 133 description;partnumber;sellprice;lastcost_as_number;wiener;
 
 134 Kaffee;;0.12;1,221.52;ja wiener
 
 135 Beer;1123245;0.12;1.5234;nein kein wieder
 
 137   numberformat => '1,000.00',
 
 138   ignore_unknown_columns => 1,
 
 139   class  => 'SL::DB::Part',
 
 142 is $csv->get_objects->[0]->lastcost, '1221.52', 'ignore_unkown_columns works';
 
 146 $csv = SL::Helper::Csv->new(
 
 148 description;partnumber;sellprice;lastcost_as_number;buchungsgruppe;
 
 149 Kaffee;;0.12;1,221.52;Standard 7%
 
 150 Beer;1123245;0.12;1.5234;16 %
 
 152   numberformat => '1,000.00',
 
 153   class  => 'SL::DB::Part',
 
 155     buchungsgruppe => "buchungsgruppen.description",
 
 159 isa_ok $csv->get_objects->[0]->buchungsgruppe, 'SL::DB::Buchungsgruppe', 'deep dispatch auto vivify works';
 
 160 is $csv->get_objects->[0]->buchungsgruppe->description, 'Standard 7%', '...and gets set correctly';
 
 165 $csv = SL::Helper::Csv->new(
 
 167 description;partnumber;sellprice;lastcost_as_number;make_1;model_1;
 
 168   Kaffee;;0.12;1,221.52;213;Chair 0815
 
 169 Beer;1123245;0.12;1.5234;
 
 171   numberformat => '1,000.00',
 
 172   class  => 'SL::DB::Part',
 
 174     make_1 => "makemodels.0.make",
 
 175     model_1 => "makemodels.0.model",
 
 179 my @mm = $csv->get_objects->[0]->makemodel;
 
 180 is scalar @mm,  1, 'one-to-many dispatch';
 
 181 is $csv->get_objects->[0]->makemodels->[0]->model, 'Chair 0815', '... and works';
 
 186 $csv = SL::Helper::Csv->new(
 
 188 description;partnumber;sellprice;lastcost_as_number;make_1;model_1;make_2;model_2;
 
 189  Kaffee;;0.12;1,221.52;213;Chair 0815;523;Table 15
 
 191   numberformat => '1,000.00',
 
 192   class  => 'SL::DB::Part',
 
 194     make_1 => "makemodels.0.make",
 
 195     model_1 => "makemodels.0.model",
 
 196     make_2 => "makemodels.1.make",
 
 197     model_2 => "makemodels.1.model",
 
 202 print Dumper($csv->errors);
 
 204 @mm = $csv->get_objects->[0]->makemodel;
 
 205 is scalar @mm,  1, 'multiple one-to-many dispatch';
 
 206 is $csv->get_objects->[0]->makemodels->[0]->model, 'Chair 0815', '...check 1';
 
 207 is $csv->get_objects->[0]->makemodels->[0]->make, '213', '...check 2';
 
 208 is $csv->get_objects->[0]->makemodels->[1]->model, 'Table 15', '...check 3';
 
 209 is $csv->get_objects->[0]->makemodels->[1]->make, '523', '...check 4';
 
 213 $csv = SL::Helper::Csv->new(
 
 215 description;partnumber;sellprice;lastcost_as_number;buchungsgruppe;
 
 217   numberformat => '1,000.00',
 
 218   class  => 'SL::DB::Part',
 
 220     buchungsgruppe => "buchungsgruppen.1.description",
 
 223 is $csv->parse, undef, 'wrong profile gets rejected';
 
 224 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';
 
 225 isa_ok( ($csv->errors)[0], 'SL::Helper::Csv::Error', 'Errors get objectified');
 
 229 $csv = SL::Helper::Csv->new(
 
 231 description;partnumber;sellprice;lastcost;wiener;
 
 232 Kaffee;;0.12;1,221.52;ja wiener
 
 233 Beer;1123245;0.12;1.5234;nein kein wieder
 
 235   numberformat => '1,000.00',
 
 236   ignore_unknown_columns => 1,
 
 238   class  => 'SL::DB::Part',
 
 240     lastcost => 'lastcost_as_number',
 
 244 is $csv->get_objects->[0]->lastcost, '1221.52', 'strict_profile with ignore';
 
 245 is $csv->get_objects->[0]->sellprice, undef,  'strict profile with ignore 2';
 
 249 $csv = SL::Helper::Csv->new(
 
 251 description;partnumber;sellprice;lastcost;wiener;
 
 252 Kaffee;;0.12;1,221.52;ja wiener
 
 253 Beer;1123245;0.12;1.5234;nein kein wieder
 
 255   numberformat => '1,000.00',
 
 257   class  => 'SL::DB::Part',
 
 259     lastcost => 'lastcost_as_number',
 
 264 is_deeply( ($csv->errors)[0], [ 'description', undef, 'header field \'description\' is not recognized', undef, 0 ], 'strict_profile without ignore_columns throws error');
 
 268 $csv = SL::Helper::Csv->new(
 
 270   header => [ 'description' ],
 
 271   class  => 'SL::DB::Part',
 
 274 is_deeply $csv->get_data, [ { description => 'Kaffee' } ], 'eol bug at the end of files';
 
 278 $csv = SL::Helper::Csv->new(
 
 279   file   => \"Description\nKaffee",
 
 280   class  => 'SL::DB::Part',
 
 281   case_insensitive_header => 1,
 
 282   profile => { description => 'description' },
 
 285 is_deeply $csv->get_data, [ { description => 'Kaffee' } ], 'case insensitive header from csv works';
 
 289 $csv = SL::Helper::Csv->new(
 
 291   header => [ 'Description' ],
 
 292   class  => 'SL::DB::Part',
 
 293   case_insensitive_header => 1,
 
 294   profile => { description => 'description' },
 
 297 is_deeply $csv->get_data, [ { description => 'Kaffee' } ], 'case insensitive header as param works';
 
 301 $csv = SL::Helper::Csv->new(
 
 302   file   => \"\x{EF}\x{BB}\x{BF}description\nKaffee",
 
 303   class  => 'SL::DB::Part',
 
 307 is_deeply $csv->get_data, [ { description => 'Kaffee' } ], 'utf8 BOM works (bug 1872)';
 
 311 $csv = SL::Helper::Csv->new(
 
 313   header => [ 'Description' ],
 
 314   class  => 'SL::DB::Part',
 
 317 is_deeply $csv->get_data, undef, 'case insensitive header without flag ignores';
 
 321 $csv = SL::Helper::Csv->new(
 
 324   class  => 'SL::DB::Part',
 
 325   profile => { foo => '' },
 
 329 is_deeply $csv->get_data, [ { foo => 'Kaffee' } ], 'empty path still gets parsed into data';
 
 330 ok $csv->get_objects->[0], 'empty path gets ignored in object creation';
 
 334 $csv = SL::Helper::Csv->new(
 
 337   class  => 'SL::DB::Part',
 
 339   profile => { foo => '' },
 
 343 is_deeply $csv->get_data, [ { foo => 'Kaffee' } ], 'empty path still gets parsed into data (strict profile)';
 
 344 ok $csv->get_objects->[0], 'empty path gets ignored in object creation (strict profile)';
 
 346 $csv = SL::Helper::Csv->new(
 
 348   header => [ 'CVAR_grOUnDHog' ],
 
 349   class  => 'SL::DB::Part',
 
 351   case_insensitive_header => 1,
 
 352   profile => { cvar_Groundhog => '' },
 
 356 is_deeply $csv->get_data, [ { cvar_Groundhog => 'Phil' } ], 'using empty path to get cvars working';
 
 357 ok $csv->get_objects->[0], '...and not destorying the objects';