Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision a5c1b85d

Von Bernd Bleßmann vor mehr als 3 Jahren hinzugefügt

  • ID a5c1b85dc4af790dbedea5cbb81d4409deb44164
  • Vorgänger 30683648
  • Nachfolger 98bdc3f1

CsvImport Lieferscheine: Unit-Tests

Unterschiede anzeigen:

t/controllers/csvimport/delivery_orders.t
1
use Test::More tests => 15;
2

  
3
use strict;
4

  
5
use lib 't';
6
use utf8;
7

  
8
use Support::TestSetup;
9

  
10
use List::MoreUtils qw(none any);
11

  
12
use SL::Controller::CsvImport;
13
use_ok 'SL::Controller::CsvImport::DeliveryOrder';
14

  
15
use SL::Dev::ALL qw(:ALL);
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 => 'delivery_orders',
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::DeliveryOrder->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 clear_up {
48
  foreach (qw(RecordLink Order DeliveryOrder Customer Part)) {
49
    "SL::DB::Manager::${_}"->delete_all(all => 1);
50
  }
51
  SL::DB::Manager::Employee->delete_all(where => [ '!login' => 'unittests' ]);
52
}
53

  
54
#####
55

  
56
# set numberformat and locale (so we can match errors)
57
my $old_numberformat      = $::myconfig{numberformat};
58
$::myconfig{numberformat} = '1.000,00';
59
my $old_locale            = $::locale;
60
$::locale                 = Locale->new('en');
61

  
62
clear_up;
63

  
64
#####
65
my @customers;
66
my @parts;
67
my @orders;
68
my $file;
69
my $entries;
70
my $entry;
71

  
72
# simple import
73
@customers = (new_customer(name => 'TestCustomer1', discount => 0)->save);
74
@parts = (
75
  new_part(description => 'TestPart1', ean => '')->save,
76
  new_part(description => 'TestPart2', ean => '')->save
77
);
78

  
79
$file = \<<EOL;
80
datatype;customer
81
datatype;description;qty
82
datatype
83
DeliveryOrder;TestCustomer1
84
OrderItem;TestPart1;5
85
OrderItem;TestPart2;10
86
EOL
87

  
88
$entries = do_import($file);
89

  
90
$entry = $entries->[0];
91
is $entry->{object}->customer_id, $customers[0]->id, 'simple import: customer_id';
92

  
93
$entry = $entries->[1];
94
is $entry->{object}->parts_id,    $parts[0]->id,     'simple import: part 1: parts_id';
95
is $entry->{object}->qty,         5,                 'simple import: part 1: qty';
96

  
97
$entry = $entries->[2];
98
is $entry->{object}->parts_id,    $parts[1]->id,     'simple import: part 2: parts_id';
99
is $entry->{object}->qty,         10,                'simple import: part 2: qty';
100

  
101

  
102
$entries = undef;
103
clear_up;
104

  
105
#####
106
# with source order
107
@customers = (new_customer(name => 'TestCustomer1', discount => 0)->save);
108
@parts = (
109
  new_part(description => 'TestPart1', ean => '')->save,
110
  new_part(description => 'TestPart2', ean => '')->save,
111
  new_part(description => 'TestPart3', ean => '')->save
112
);
113
@orders = (
114
  create_sales_order(
115
    save       => 1,
116
    customer   => $customers[0],
117
    ordnumber  => '1234',
118
    orderitems => [ create_order_item(part => $parts[0], qty =>  3, sellprice => 70),
119
                    create_order_item(part => $parts[1], qty => 10, sellprice => 50),
120
                    create_order_item(part => $parts[2], qty =>  8, sellprice => 80)
121
    ]
122
  )
123
);
124

  
125
$file = \<<EOL;
126
datatype;customer;ordnumber
127
datatype;description;qty
128
datatype
129
DeliveryOrder;TestCustomer1;1234
130
OrderItem;TestPart1;5
131
OrderItem;TestPart2;10
132
OrderItem;TestPart3;7
133
OrderItem;TestPart3;1
134
EOL
135

  
136
$entries = do_import($file);
137

  
138
$entry = $entries->[0];
139

  
140
is $entry->{object}->ordnumber, '1234', 'with source order: ordnumber';
141

  
142
my $linked = $orders[0]->linked_records(to => 'DeliveryOrder');
143
ok(scalar @$linked == 1, 'with source order: order linked to one delivery order');
144
ok($linked->[0]->id == $entry->{object}->id, 'with source order: order linked to imported delivery order');
145

  
146

  
147
$linked = $entry->{object}->linked_records(from => 'Order');
148
ok(scalar @$linked == 1, 'with source order: delivery order linked from one order');
149
ok($linked->[0]->id == $orders[0]->id, 'with source order: delivery order linked from source order');
150

  
151
$entry = $entries->[1];
152
$linked = $entry->{object}->linked_records(from => 'OrderItem');
153
ok(scalar @$linked == 0, 'with source order: delivered qty > ordered qty: delivery order item not linked');
154

  
155
$entry = $entries->[2];
156
$linked = $entry->{object}->linked_records(from => 'OrderItem');
157
ok(scalar @$linked == 1, 'with source order: same qtys: delivery order item linked');
158
ok($linked->[0]->id == $orders[0]->items_sorted->[1]->id, 'with source order: same qtys: delivery order item linked from source order item');
159

  
160
$entry = $entries->[3];
161
$linked = $entry->{object}->linked_records(from => 'OrderItem');
162
ok(scalar @$linked == 0, 'with source order: delivered qty < ordered qty: delivery order item not linked');
163

  
164
#####
165
clear_up;
166

  
167
$::myconfig{numberformat} = $old_numberformat;
168
$::locale                 = $old_locale;
169

  
170
1;
171

  
172
#####
173
# vim: ft=perl
174
# set emacs to perl mode
175
# Local Variables:
176
# mode: perl
177
# End:

Auch abrufbar als: Unified diff