Revision f74b0dac
Von Sven Schöling vor mehr als 8 Jahren hinzugefügt
t/helper/csv.t | ||
---|---|---|
1 |
use Test::More tests => 75;
|
|
1 |
use Test::More tests => 84;
|
|
2 | 2 |
|
3 | 3 |
use lib 't'; |
4 | 4 |
use utf8; |
... | ... | |
726 | 726 |
|
727 | 727 |
##### |
728 | 728 |
|
729 |
# Mappings |
|
730 |
# simple case |
|
731 |
$csv = SL::Helper::Csv->new( |
|
732 |
file => \<<EOL, |
|
733 |
description,sellprice,lastcost_as_number,purchaseprice, |
|
734 |
Kaffee,0.12,'12,2','1,5234' |
|
735 |
EOL |
|
736 |
sep_char => ',', |
|
737 |
quote_char => "'", |
|
738 |
profile => [ |
|
739 |
{ |
|
740 |
profile => { listprice => 'listprice_as_number' }, |
|
741 |
mapping => { purchaseprice => 'listprice' }, |
|
742 |
class => 'SL::DB::Part', |
|
743 |
} |
|
744 |
], |
|
745 |
); |
|
746 |
ok $csv->parse, 'simple mapping parses'; |
|
747 |
is $csv->get_objects->[0]->listprice, 1.5234, 'simple mapping works'; |
|
748 |
|
|
749 |
$csv = SL::Helper::Csv->new( |
|
750 |
file => \<<EOL, |
|
751 |
description;partnumber;sellprice;purchaseprice;wiener; |
|
752 |
Kaffee;;0.12;1,221.52;ja wiener |
|
753 |
Beer;1123245;0.12;1.5234;nein kein wieder |
|
754 |
EOL |
|
755 |
numberformat => '1,000.00', |
|
756 |
ignore_unknown_columns => 1, |
|
757 |
strict_profile => 1, |
|
758 |
profile => [{ |
|
759 |
profile => { lastcost => 'lastcost_as_number' }, |
|
760 |
mapping => { purchaseprice => 'lastcost' }, |
|
761 |
class => 'SL::DB::Part', |
|
762 |
}] |
|
763 |
); |
|
764 |
ok $csv->parse, 'strict mapping parses'; |
|
765 |
is $csv->get_objects->[0]->lastcost, 1221.52, 'strict mapping works'; |
|
766 |
|
|
767 |
# swapping |
|
768 |
$csv = SL::Helper::Csv->new( |
|
769 |
file => \<<EOL, |
|
770 |
description;partnumber;sellprice;lastcost;wiener; |
|
771 |
Kaffee;1;0.12;1,221.52;ja wiener |
|
772 |
Beer;1123245;0.12;1.5234;nein kein wieder |
|
773 |
EOL |
|
774 |
numberformat => '1,000.00', |
|
775 |
ignore_unknown_columns => 1, |
|
776 |
strict_profile => 1, |
|
777 |
profile => [{ |
|
778 |
mapping => { partnumber => 'description', description => 'partnumber' }, |
|
779 |
class => 'SL::DB::Part', |
|
780 |
}] |
|
781 |
); |
|
782 |
ok $csv->parse, 'swapping parses'; |
|
783 |
is $csv->get_objects->[0]->partnumber, 'Kaffee', 'strict mapping works 1'; |
|
784 |
is $csv->get_objects->[0]->description, '1', 'strict mapping works 2'; |
|
785 |
|
|
786 |
# case insensitive shit |
|
787 |
$csv = SL::Helper::Csv->new( |
|
788 |
file => \"Description\nKaffee", # " # make emacs happy |
|
789 |
case_insensitive_header => 1, |
|
790 |
profile => [{ |
|
791 |
mapping => { description => 'description' }, |
|
792 |
class => 'SL::DB::Part' |
|
793 |
}], |
|
794 |
); |
|
795 |
$csv->parse; |
|
796 |
is $csv->get_objects->[0]->description, 'Kaffee', 'case insensitive mapping without profile works'; |
|
797 |
|
|
798 |
# case insensitive shit |
|
799 |
$csv = SL::Helper::Csv->new( |
|
800 |
file => \"Price\n4,99", # " # make emacs happy |
|
801 |
case_insensitive_header => 1, |
|
802 |
profile => [{ |
|
803 |
profile => { sellprice => 'sellprice_as_number' }, |
|
804 |
mapping => { price => 'sellprice' }, |
|
805 |
class => 'SL::DB::Part', |
|
806 |
}], |
|
807 |
); |
|
808 |
$csv->parse; |
|
809 |
is $csv->get_objects->[0]->sellprice, 4.99, 'case insensitive mapping with profile works'; |
|
810 |
|
|
729 | 811 |
|
730 | 812 |
# vim: ft=perl |
731 | 813 |
# set emacs to perl mode |
Auch abrufbar als: Unified diff
Csv: mapping support