Revision 811f204f
Von Bernd Bleßmann vor mehr als 3 Jahren hinzugefügt
t/controllers/csvimport/customervendor.t | ||
---|---|---|
1 |
use Test::More tests => 41; |
|
2 |
|
|
3 |
use strict; |
|
4 |
|
|
5 |
use lib 't'; |
|
6 |
|
|
7 |
use Support::TestSetup; |
|
8 |
use List::MoreUtils qw(none any); |
|
9 |
|
|
10 |
use SL::DB::Customer; |
|
11 |
use SL::DB::CustomVariableConfig; |
|
12 |
use SL::DB::Default; |
|
13 |
|
|
14 |
use SL::Controller::CsvImport; |
|
15 |
use_ok 'SL::Controller::CsvImport::CustomerVendor'; |
|
16 |
|
|
17 |
Support::TestSetup::login(); |
|
18 |
|
|
19 |
##### |
|
20 |
sub do_import { |
|
21 |
my ($file, $settings) = @_; |
|
22 |
|
|
23 |
my $controller = SL::Controller::CsvImport->new( |
|
24 |
type => 'customers_vendors', |
|
25 |
); |
|
26 |
$controller->load_default_profile; |
|
27 |
$controller->profile->set( |
|
28 |
charset => 'utf-8', |
|
29 |
sep_char => ';', |
|
30 |
%$settings |
|
31 |
); |
|
32 |
|
|
33 |
my $worker = SL::Controller::CsvImport::CustomerVendor->new( |
|
34 |
controller => $controller, |
|
35 |
file => $file, |
|
36 |
); |
|
37 |
$worker->run(test => 0); |
|
38 |
|
|
39 |
return if $worker->controller->errors; |
|
40 |
|
|
41 |
# don't try and save objects that have errors |
|
42 |
$worker->save_objects unless scalar @{$worker->controller->data->[0]->{errors}}; |
|
43 |
|
|
44 |
return $worker->controller->data; |
|
45 |
} |
|
46 |
|
|
47 |
sub _obj_of { |
|
48 |
return $_[0]->{object_to_save} || $_[0]->{object}; |
|
49 |
} |
|
50 |
|
|
51 |
sub clear_up { |
|
52 |
SL::DB::Manager::Customer->delete_all(all => 1); |
|
53 |
SL::DB::Manager::CustomVariableConfig->delete_all(all => 1); |
|
54 |
|
|
55 |
SL::DB::Default->get->update_attributes(customernumber => '10000'); |
|
56 |
|
|
57 |
# Reset request to clear caches. Here especially for cvar-configs. |
|
58 |
$::request = Support::TestSetup->create_new_request; |
|
59 |
} |
|
60 |
|
|
61 |
##### |
|
62 |
|
|
63 |
# set numberformat and locale (so we can match errors) |
|
64 |
my $old_numberformat = $::myconfig{numberformat}; |
|
65 |
$::myconfig{numberformat} = '1.000,00'; |
|
66 |
my $old_locale = $::locale; |
|
67 |
$::locale = Locale->new('en'); |
|
68 |
|
|
69 |
clear_up; |
|
70 |
|
|
71 |
##### |
|
72 |
# import and update entries |
|
73 |
|
|
74 |
my $file = \<<EOL; |
|
75 |
name;street; |
|
76 |
CustomerName;CustomerStreet |
|
77 |
EOL |
|
78 |
|
|
79 |
my $entries = do_import($file, {update_policy => 'update_existing'}); |
|
80 |
|
|
81 |
ok none {'Updating existing entry in database' eq $_} @{$entries->[0]->{information}}, 'import entry - information (customer)'; |
|
82 |
is _obj_of($entries->[0])->customernumber, '10001', 'import entry - number (customer)'; |
|
83 |
is _obj_of($entries->[0])->name, 'CustomerName', 'import entry - name (customer)'; |
|
84 |
is _obj_of($entries->[0])->street, 'CustomerStreet', 'import entry - street (customer)'; |
|
85 |
is _obj_of($entries->[0]), $entries->[0]->{object}, 'import entry - object not object_to_save (customer)'; |
|
86 |
|
|
87 |
my $default_customernumer = SL::DB::Default->get->load->customernumber; |
|
88 |
is $default_customernumer, '10001', 'import entry - defaults range of numbers (customer)'; |
|
89 |
|
|
90 |
my $customer_id = _obj_of($entries->[0])->id; |
|
91 |
|
|
92 |
$entries = undef; |
|
93 |
|
|
94 |
$file = \<<EOL; |
|
95 |
customernumber;name;street; |
|
96 |
10001;CustomerName;NewCustomerStreet |
|
97 |
EOL |
|
98 |
|
|
99 |
$entries = do_import($file, {update_policy => 'update_existing'}); |
|
100 |
|
|
101 |
ok any {'Updating existing entry in database' eq $_} @{ $entries->[0]->{information} }, 'update entry - information (customer)'; |
|
102 |
is _obj_of($entries->[0])->customernumber, '10001', 'update entry - number (customer)'; |
|
103 |
is _obj_of($entries->[0])->name, 'CustomerName', 'update entry - name (customer)'; |
|
104 |
is _obj_of($entries->[0])->street, 'NewCustomerStreet', 'update entry - street (customer)'; |
|
105 |
is _obj_of($entries->[0]), $entries->[0]->{object_to_save}, 'update entry - object is object_to_save (customer)'; |
|
106 |
is _obj_of($entries->[0])->id, $customer_id, 'update entry - same id (customer)'; |
|
107 |
$default_customernumer = SL::DB::Default->get->load->customernumber; |
|
108 |
is $default_customernumer, '10001', 'update entry - defaults range of numbers (customer)'; |
|
109 |
|
|
110 |
$entries = undef; |
|
111 |
|
|
112 |
$file = \<<EOL; |
|
113 |
customernumber;name;street; |
|
114 |
10001;WrongCustomerName;WrongCustomerStreet |
|
115 |
EOL |
|
116 |
|
|
117 |
$entries = do_import($file, {update_policy => 'skip'}); |
|
118 |
|
|
119 |
ok any {'Skipping due to existing entry in database' eq $_} @{ $entries->[0]->{errors} }, 'skip entry - error (customer)'; |
|
120 |
|
|
121 |
$default_customernumer = SL::DB::Default->get->load->customernumber; |
|
122 |
is $default_customernumer, '10001', 'skip entry - defaults range of numbers (customer)'; |
|
123 |
|
|
124 |
$entries = undef; |
|
125 |
|
|
126 |
clear_up; |
|
127 |
##### |
|
128 |
|
|
129 |
$file = \<<EOL; |
|
130 |
name |
|
131 |
CustomerName |
|
132 |
EOL |
|
133 |
|
|
134 |
$entries = do_import($file); |
|
135 |
|
|
136 |
is scalar @$entries, 1, 'one entry - nuber of entries (customer)'; |
|
137 |
is _obj_of($entries->[0])->name, 'CustomerName', 'simple file - name only (customer)'; |
|
138 |
|
|
139 |
$entries = undef; |
|
140 |
clear_up; |
|
141 |
|
|
142 |
##### |
|
143 |
$file = \<<EOL; |
|
144 |
customernumber;name |
|
145 |
1;CustomerName1 |
|
146 |
2;CustomerName2 |
|
147 |
EOL |
|
148 |
|
|
149 |
$entries = do_import($file); |
|
150 |
|
|
151 |
is scalar @$entries, 2, 'two entries - number of entries (customer)'; |
|
152 |
is _obj_of($entries->[0])->name, 'CustomerName1', 'two entries, number and name - name (customer)'; |
|
153 |
is _obj_of($entries->[0])->customernumber, '1', 'two entries, number and name - number (customer)'; |
|
154 |
is _obj_of($entries->[1])->name, 'CustomerName2', 'two entries, number and name - name (customer)'; |
|
155 |
is _obj_of($entries->[1])->customernumber, '2', 'two entries, number and name - number (customer)'; |
|
156 |
|
|
157 |
$entries = undef; |
|
158 |
clear_up; |
|
159 |
|
|
160 |
##### |
|
161 |
$file = \<<EOL; |
|
162 |
name;creditlimit;discount |
|
163 |
CustomerName1;1.280,50;0,035 |
|
164 |
EOL |
|
165 |
|
|
166 |
$entries = do_import($file); |
|
167 |
|
|
168 |
is scalar @$entries, 1, 'creditlimit/discount - number of entries (customer)'; |
|
169 |
is _obj_of($entries->[0])->name, 'CustomerName1', 'creditlimit/discount - name (customer)'; |
|
170 |
is _obj_of($entries->[0])->creditlimit, 1280.5, 'creditlimit/discount - creditlimit (customer))'; |
|
171 |
# Should discount be given in percent or in decimal? |
|
172 |
is _obj_of($entries->[0])->discount, 0.035, 'creditlimit/discount - discount (customer)'; |
|
173 |
|
|
174 |
$entries = undef; |
|
175 |
clear_up; |
|
176 |
|
|
177 |
##### |
|
178 |
# Test import with cvars. |
|
179 |
# Customer/vendor cvars can have a default value, so the following cases are to be |
|
180 |
# tested |
|
181 |
# - new customer in csv - no cvars given -> one should be unset, the other one |
|
182 |
# should have the default value |
|
183 |
# - new customer in csv - both cvars given -> cvars should have the given values |
|
184 |
# - update customer with no cvars in csv -> cvars should not change |
|
185 |
# - update customer with both cvars in csv -> cvars should have the given values |
|
186 |
# (not explicitly testet: does an empty cvar field means to unset the cvar or to |
|
187 |
# leave it untouched?) |
|
188 |
|
|
189 |
# create cvars |
|
190 |
SL::DB::CustomVariableConfig->new( |
|
191 |
module => 'CT', |
|
192 |
name => 'no_default', |
|
193 |
description => 'no default', |
|
194 |
type => 'text', |
|
195 |
searchable => 1, |
|
196 |
sortkey => 1, |
|
197 |
includeable => 0, |
|
198 |
included_by_default => 0, |
|
199 |
)->save; |
|
200 |
|
|
201 |
SL::DB::CustomVariableConfig->new( |
|
202 |
module => 'CT', |
|
203 |
name => 'with_default', |
|
204 |
description => 'with default', |
|
205 |
type => 'text', |
|
206 |
default_value => 'this is the default', |
|
207 |
searchable => 1, |
|
208 |
sortkey => 1, |
|
209 |
includeable => 0, |
|
210 |
included_by_default => 0, |
|
211 |
)->save; |
|
212 |
|
|
213 |
# - new customer in csv - no cvars given -> one should be unset, the other one |
|
214 |
# should have the default value |
|
215 |
$file = \<<EOL; |
|
216 |
customernumber;name; |
|
217 |
1;CustomerName1 |
|
218 |
EOL |
|
219 |
|
|
220 |
$entries = do_import($file); |
|
221 |
|
|
222 |
is _obj_of($entries->[0])->customernumber, '1', 'cvar test - import customer 1 with no cvars - number (customer)'; |
|
223 |
is _obj_of($entries->[0])->cvar_by_name('no_default')->value, undef, 'cvar test - import customer 1 - do not set ungiven cvar which has no default'; |
|
224 |
is _obj_of($entries->[0])->cvar_by_name('with_default')->value, 'this is the default', 'cvar test - import customer 1 - do set ungiven cvar which has default'; |
|
225 |
|
|
226 |
$entries = undef; |
|
227 |
|
|
228 |
# - new customer in csv - both cvars given -> cvars should have the given values |
|
229 |
$file = \<<EOL; |
|
230 |
customernumber;name;cvar_no_default;cvar_with_default |
|
231 |
2;CustomerName2;"new cvar value abc";"new cvar value xyz" |
|
232 |
EOL |
|
233 |
|
|
234 |
$entries = do_import($file); |
|
235 |
|
|
236 |
is _obj_of($entries->[0])->customernumber, '2', 'cvar test - import customer 2 with cvars - number (customer)'; |
|
237 |
is _obj_of($entries->[0])->cvar_by_name('no_default')->value, 'new cvar value abc', 'cvar test - import customer 2 - do set given cvar which has default'; |
|
238 |
is _obj_of($entries->[0])->cvar_by_name('with_default')->value, 'new cvar value xyz', 'cvar test - import customer 2 - do set given cvar which has default'; |
|
239 |
|
|
240 |
$entries = undef; |
|
241 |
|
|
242 |
# - update customer with no cvars in csv -> cvars should not change |
|
243 |
$file = \<<EOL; |
|
244 |
customernumber;name;street |
|
245 |
1;CustomerName1;"street cs1" |
|
246 |
EOL |
|
247 |
|
|
248 |
$entries = do_import($file, {update_policy => 'update_existing'}); |
|
249 |
is _obj_of($entries->[0])->customernumber, '1', 'cvar test - update customer 1 - number (customer)'; |
|
250 |
is _obj_of($entries->[0])->street, 'street cs1', 'cvar test - update customer 1 - set new street (customer)'; |
|
251 |
is _obj_of($entries->[0])->cvar_by_name('no_default')->value, undef, 'cvar test - update customer 1 - do not set ungiven cvar which has no default'; |
|
252 |
is _obj_of($entries->[0])->cvar_by_name('with_default')->value, 'this is the default', 'cvar test - update customer 1 - do set ungiven cvar which has default'; |
|
253 |
|
|
254 |
$entries = undef; |
|
255 |
|
|
256 |
# - update customer with both cvars in csv -> cvars should have the given values |
|
257 |
$file = \<<EOL; |
|
258 |
customernumber;name;street;cvar_no_default;cvar_with_default |
|
259 |
1;CustomerName1;"new street cs1";totaly new cvar 123;totaly new cvar abc |
|
260 |
EOL |
|
261 |
|
|
262 |
$entries = do_import($file, {update_policy => 'update_existing'}); |
|
263 |
is _obj_of($entries->[0])->customernumber, '1', 'cvar test - update customer 1 - number (customer)'; |
|
264 |
is _obj_of($entries->[0])->street, 'new street cs1', 'cvar test - update customer 1 - set new street (customer)'; |
|
265 |
is _obj_of($entries->[0])->cvar_by_name('no_default')->value, 'totaly new cvar 123', 'cvar test - update customer 1 - do set given cvar which has no default (customer)'; |
|
266 |
is _obj_of($entries->[0])->cvar_by_name('with_default')->value, 'totaly new cvar abc', 'cvar test - update customer 1 - do set given cvar which has default (customer)'; |
|
267 |
|
|
268 |
$entries = undef; |
|
269 |
|
|
270 |
|
|
271 |
clear_up; |
|
272 |
|
|
273 |
$::myconfig{numberformat} = $old_numberformat; |
|
274 |
$::locale = $old_locale; |
|
275 |
|
|
276 |
1; |
|
277 |
|
|
278 |
##### |
|
279 |
# vim: ft=perl |
|
280 |
# set emacs to perl mode |
|
281 |
# Local Variables: |
|
282 |
# mode: perl |
|
283 |
# End: |
Auch abrufbar als: Unified diff
CsvImport - Tests für Kundenimport
Im Moment werden alle Tests nur für Kunden, nicht für Lieferanten durchgeführt.