]> git.imager.perl.org - bse.git/blame - t/130-importer/030-product.t
use the supplied name of the image file for working out the stored name
[bse.git] / t / 130-importer / 030-product.t
CommitLineData
57e4a9c7
TC
1#!perl -w
2use strict;
3use BSE::Test qw(base_url);
4use File::Spec;
5use File::Temp;
8921ff01 6use Test::More;
57e4a9c7 7
8921ff01
TC
8BEGIN {
9 eval "require Text::CSV;"
10 or plan skip_all => "Text::CSV not available";
11}
12
a7c8c69d 13plan tests => 11;
57e4a9c7
TC
14
15BEGIN {
16 unshift @INC, File::Spec->catdir(BSE::Test::base_dir(), "cgi-bin", "modules");
17}
18
19use BSE::Importer;
20use BSE::API qw(bse_init bse_make_product);
21
22my $base_cgi = File::Spec->catdir(BSE::Test::base_dir(), "cgi-bin");
23ok(bse_init($base_cgi), "initialize api")
24 or print "# failed to bse_init in $base_cgi\n";
25
26my $when = time;
27
28my $cfg = BSE::Cfg->new(path => $base_cgi, extra_text => <<CFG);
29[import profile simple$when]
30map_title=1
31map_retailPrice=2
32source=CSV
33price_dollar=1
34
35[import profile simpleupdate$when]
36map_linkAlias=1
37map_body=2
38source=CSV
39target=Product
40update_only=1
41sep_char=\\t
42code_field=linkAlias
1455f602
TC
43
44[import profile newdup$when]
45map_product_code=1
46map_title=2
47map_retailPrice=3
48skiplines=0
49source=CSV
50target=Product
51
57e4a9c7
TC
52CFG
53
54{
55 my @added;
56
57 my $imp = BSE::Importer->new(cfg => $cfg, profile => "simple$when",
58 callback => sub { note @_ });
59 $imp->process("t/data/importer/product-simple.csv");
60 @added = sort { $a->title cmp $b->title } $imp->leaves;
61
62 is(@added, 2, "imported two products");
63 is($added[0]->title, "test1", "check title of first import");
64 is($added[0]->retailPrice, 1000, "check price of first import");
65 is($added[1]->title, "test2", "check title of second import");
66 is($added[1]->retailPrice, 800, "check price of second import");
67
68 END {
69 $_->remove($cfg) for @added;
70 }
71}
72
73{
1455f602
TC
74 my $testa = bse_make_product
75 (
76 cfg => $cfg,
77 title => "test updates",
78 linkAlias => "P$when",
79 retailPrice => 500,
80 product_code => "C$when",
81 );
57e4a9c7 82
a7c8c69d
TC
83 $testa->set_prices({ 1 => 400 });
84
1455f602
TC
85 {
86 my $fh = File::Temp->new;
87 my $filename = $fh->filename;
88 print $fh <<EOS;
57e4a9c7
TC
89linkAlias\tbody
90"P$when"\t"This is the body text with multiple lines
91
92Yes, multiple lines with CSV!"
93EOS
1455f602
TC
94 close $fh;
95 my $imp = BSE::Importer->new(cfg => $cfg, profile => "simpleupdate$when",
96 callback => sub { note @_ });
97 $imp->process($filename);
ab004bbb 98 my $testb = BSE::TB::Articles->getByPkey($testa->id);
1455f602
TC
99 like($testb->body, qr/This is the body/, "check the body is updated");
100 }
101
102 { # fail to duplicate a product code
103 my $fh = File::Temp->new;
104 my $filename = $fh->filename;
105 my $id = $testa->id;
106 print $fh <<EOS;
107"C$when",A new title,100
108EOS
109 close $fh;
110 my $imp = BSE::Importer->new(cfg => $cfg, profile => "newdup$when", callback => sub { note @_ });
111 $imp->process($filename);
112 is_deeply([ $imp->leaves ], [], "should be no updated articles");
113 }
57e4a9c7 114
a7c8c69d
TC
115 SKIP:
116 {
117 my @prices = $testa->prices;
118 is(@prices, 1, "should still be a tier price")
119 or skip "No prices found", 2;
120 is($prices[0]->tier_id, 1, "check tier id");
121 is($prices[0]->retailPrice, 400, "check tier price");
122 }
123
57e4a9c7
TC
124 END {
125 $testa->remove($cfg) if $testa;
126 }
127}