]>
Commit | Line | Data |
---|---|---|
57e4a9c7 TC |
1 | #!perl -w |
2 | use strict; | |
3 | use BSE::Test qw(base_url); | |
4 | use File::Spec; | |
5 | use File::Temp; | |
8921ff01 | 6 | use Test::More; |
57e4a9c7 | 7 | |
8921ff01 TC |
8 | BEGIN { |
9 | eval "require Text::CSV;" | |
10 | or plan skip_all => "Text::CSV not available"; | |
11 | } | |
12 | ||
a7c8c69d | 13 | plan tests => 11; |
57e4a9c7 TC |
14 | |
15 | BEGIN { | |
16 | unshift @INC, File::Spec->catdir(BSE::Test::base_dir(), "cgi-bin", "modules"); | |
17 | } | |
18 | ||
19 | use BSE::Importer; | |
20 | use BSE::API qw(bse_init bse_make_product); | |
21 | ||
22 | my $base_cgi = File::Spec->catdir(BSE::Test::base_dir(), "cgi-bin"); | |
23 | ok(bse_init($base_cgi), "initialize api") | |
24 | or print "# failed to bse_init in $base_cgi\n"; | |
25 | ||
26 | my $when = time; | |
27 | ||
28 | my $cfg = BSE::Cfg->new(path => $base_cgi, extra_text => <<CFG); | |
29 | [import profile simple$when] | |
30 | map_title=1 | |
31 | map_retailPrice=2 | |
32 | source=CSV | |
33 | price_dollar=1 | |
34 | ||
35 | [import profile simpleupdate$when] | |
36 | map_linkAlias=1 | |
37 | map_body=2 | |
38 | source=CSV | |
39 | target=Product | |
40 | update_only=1 | |
41 | sep_char=\\t | |
42 | code_field=linkAlias | |
1455f602 TC |
43 | |
44 | [import profile newdup$when] | |
45 | map_product_code=1 | |
46 | map_title=2 | |
47 | map_retailPrice=3 | |
48 | skiplines=0 | |
49 | source=CSV | |
50 | target=Product | |
51 | ||
57e4a9c7 TC |
52 | CFG |
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 |
89 | linkAlias\tbody |
90 | "P$when"\t"This is the body text with multiple lines | |
91 | ||
92 | Yes, multiple lines with CSV!" | |
93 | EOS | |
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 | |
108 | EOS | |
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 | } |