prevent the importer overwriting primary key fields
[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
57e4a9c7
TC
13BEGIN {
14 unshift @INC, File::Spec->catdir(BSE::Test::base_dir(), "cgi-bin", "modules");
15}
16
17use BSE::Importer;
18use BSE::API qw(bse_init bse_make_product);
19
20my $base_cgi = File::Spec->catdir(BSE::Test::base_dir(), "cgi-bin");
21ok(bse_init($base_cgi), "initialize api")
22 or print "# failed to bse_init in $base_cgi\n";
23
24my $when = time;
25
26my $cfg = BSE::Cfg->new(path => $base_cgi, extra_text => <<CFG);
27[import profile simple$when]
28map_title=1
29map_retailPrice=2
30source=CSV
31price_dollar=1
32
33[import profile simpleupdate$when]
34map_linkAlias=1
35map_body=2
36source=CSV
37target=Product
38update_only=1
39sep_char=\\t
40code_field=linkAlias
1455f602
TC
41
42[import profile newdup$when]
43map_product_code=1
44map_title=2
45map_retailPrice=3
46skiplines=0
47source=CSV
48target=Product
49
f5203422
TC
50[import profile avoidcorrupt$when]
51map_id=1
52map_articleId=2
53map_title=3
54map_product_code=4
55source=CSV
56target=Product
57sep_char=\\t
58codes=1
59code_field=product_code
60update_only=1
57e4a9c7
TC
61CFG
62
63{
64 my @added;
65
66 my $imp = BSE::Importer->new(cfg => $cfg, profile => "simple$when",
67 callback => sub { note @_ });
68 $imp->process("t/data/importer/product-simple.csv");
69 @added = sort { $a->title cmp $b->title } $imp->leaves;
70
71 is(@added, 2, "imported two products");
72 is($added[0]->title, "test1", "check title of first import");
73 is($added[0]->retailPrice, 1000, "check price of first import");
74 is($added[1]->title, "test2", "check title of second import");
75 is($added[1]->retailPrice, 800, "check price of second import");
76
77 END {
78 $_->remove($cfg) for @added;
79 }
80}
81
82{
1455f602
TC
83 my $testa = bse_make_product
84 (
85 cfg => $cfg,
86 title => "test updates",
87 linkAlias => "P$when",
88 retailPrice => 500,
89 product_code => "C$when",
90 );
57e4a9c7 91
a7c8c69d
TC
92 $testa->set_prices({ 1 => 400 });
93
1455f602
TC
94 {
95 my $fh = File::Temp->new;
96 my $filename = $fh->filename;
97 print $fh <<EOS;
57e4a9c7
TC
98linkAlias\tbody
99"P$when"\t"This is the body text with multiple lines
100
101Yes, multiple lines with CSV!"
102EOS
1455f602
TC
103 close $fh;
104 my $imp = BSE::Importer->new(cfg => $cfg, profile => "simpleupdate$when",
105 callback => sub { note @_ });
106 $imp->process($filename);
ab004bbb 107 my $testb = BSE::TB::Articles->getByPkey($testa->id);
1455f602
TC
108 like($testb->body, qr/This is the body/, "check the body is updated");
109 }
110
111 { # fail to duplicate a product code
112 my $fh = File::Temp->new;
113 my $filename = $fh->filename;
114 my $id = $testa->id;
115 print $fh <<EOS;
116"C$when",A new title,100
117EOS
118 close $fh;
119 my $imp = BSE::Importer->new(cfg => $cfg, profile => "newdup$when", callback => sub { note @_ });
120 $imp->process($filename);
121 is_deeply([ $imp->leaves ], [], "should be no updated articles");
122 }
57e4a9c7 123
a7c8c69d
TC
124 SKIP:
125 {
126 my @prices = $testa->prices;
127 is(@prices, 1, "should still be a tier price")
128 or skip "No prices found", 2;
129 is($prices[0]->tier_id, 1, "check tier id");
130 is($prices[0]->retailPrice, 400, "check tier price");
131 }
132
57e4a9c7
TC
133 END {
134 $testa->remove($cfg) if $testa;
135 }
136}
f5203422
TC
137
138{
139 my $code = "D$when";
140 my $testa = bse_make_product
141 (
142 cfg => $cfg,
143 title => "test updates",
144 linkAlias => "P$when",
145 retailPrice => 500,
146 product_code => $code,
147 );
148 diag "Product id ".$testa->id;
149 {
150 my $fh = File::Temp->new;
151 my $filename = $fh->filename;
152
153 print $fh <<EOS;
154id\tarticleId\ttitle\tproduct_code
1551\t2\tTesting\t$code
156EOS
157 close $fh;
158 my $imp = BSE::Importer->new(cfg => $cfg, profile => "avoidcorrupt$when",
159 callback => sub { note @_ });
160 $imp->process($filename);
161 my $testb = BSE::TB::Products->getByPkey($testa->id);
162 ok($testb, "managed to fully load the product");
163 is($testb->title, "Testing", "check the title is updated");
164 my $top = BSE::TB::Articles->getByPkey(1);
165 isnt($top->generator, "BSE::Generate::Product",
166 "make sure we didn't scribble over article 1");
167 }
168
169 END {
170 $testa->remove($cfg) if $testa;
171 }
172}
173
174done_testing();