Class: ODDB::TestPatinfoInvoicer

Inherits:
Test::Unit::TestCase
  • Object
show all
Defined in:
test/test_plugin/patinfo_invoicer.rb

Defined Under Namespace

Classes: FlexMock

Instance Method Summary (collapse)

Instance Method Details

- (Object) setup



18
19
20
21
# File 'test/test_plugin/patinfo_invoicer.rb', line 18

def setup
  @app = FlexMock.new
  @plugin = PatinfoInvoicer.new(@app)
end

- (Object) test_adjust_annual_fee



22
23
24
25
26
27
28
29
30
31
32
# File 'test/test_plugin/patinfo_invoicer.rb', line 22

def test_adjust_annual_fee
  item1 = AbstractInvoiceItem.new
  item1.type = :annual_fee
  item1.time = Time.local(2005, 12, 1, 18, 26, 42)
  company = FlexMock.new 'company'
  company.should_receive(:invoice_date).with(:patinfo).and_return { Date.new(2006) }
  company.should_receive(:limit_invoice_duration).and_return false
  company.should_receive(:patinfo_price).and_return { 100 }
  @plugin.adjust_annual_fee(company, [item1])
  assert_equal(31.0/365.0, item1.quantity)
end

- (Object) test_adjust_annual_fee__2



33
34
35
36
37
38
39
40
41
42
43
# File 'test/test_plugin/patinfo_invoicer.rb', line 33

def test_adjust_annual_fee__2
  item1 = AbstractInvoiceItem.new
  item1.type = :annual_fee
  item1.time = Time.local(2005, 12, 2, 18, 26, 42)
  company = FlexMock.new 'company'
  company.should_receive(:invoice_date).with(:patinfo).and_return { Date.new(2006) }
  company.should_receive(:limit_invoice_duration).and_return false
  company.should_receive(:patinfo_price).and_return { 100 }
  @plugin.adjust_annual_fee(company, [item1])
  assert_equal(395.0/365.0, item1.quantity)
end

- (Object) test_create_invoice



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'test/test_plugin/patinfo_invoicer.rb', line 244

def test_create_invoice
  ptr1 = FlexMock.new
  ptr2 = FlexMock.new
  ptr3 = FlexMock.new
  ptr1.should_receive(:resolve).and_return { } # disables the neighborhood_names check
  ptr2.should_receive(:resolve).and_return { }
  ptr3.should_receive(:resolve).and_return { }
  item1 = AbstractInvoiceItem.new
  item1.yus_name = 'user1'
  item1.item_pointer = ptr1
  item2 = AbstractInvoiceItem.new
  item2.yus_name = 'user1'
  item2.item_pointer = ptr2
  item3 = AbstractInvoiceItem.new
  item3.yus_name = 'user1'
  item3.item_pointer = ptr3
  items = [item1, item2, item3]
  pointer = Persistence::Pointer.new(:invoice)
  invoice = FlexMock.new
  stored_ptr = pointer.dup
  stored_ptr.append(1)
  invoice.should_receive(:pointer).and_return { stored_ptr }
  item_ptr = Persistence::Pointer.new([:invoice, 1], [:item])
  item_vals1 = {
    :data         =>  {},
    :duration     =>  1,
    :expiry_time  => nil,
    :item_pointer =>  ptr1,
    :price        =>  nil,
    :quantity     =>  1,
    :text         =>  nil,
    :time         =>  nil,
    :type         =>  nil,
    :unit         =>  nil,
    :yus_name =>  'user1',
    :vat_rate     =>  nil,
  }
  item_vals2 = item_vals1.dup
  item_vals2.store(:item_pointer, ptr2)
  item_vals3 = item_vals1.dup
  item_vals3.store(:item_pointer, ptr3)
  expected = [
    [pointer.creator, {:yus_name => 'user1', 
      :keep_if_unpaid => true, :ydim_id => 2134}, invoice],
    [item_ptr.dup.creator, item_vals1, nil],
    [item_ptr.dup.creator, item_vals2, nil],
    [item_ptr.dup.creator, item_vals3, nil],
  ]
  @app.should_receive(:update, 4).and_return { |uptr, values| 
    exp_ptr, exp_vals, res = expected.shift
    assert_equal(exp_ptr, uptr)
    assert_equal(exp_vals, values)
    ## flag the pointer as used (because Item.init appends 
    ## an odba_id) the pointer must not be reused
    uptr.instance_variable_get('@directions').at(0).at(1).append('used')
    res
  }
  @plugin.create_invoice('user1', items, 2134)
  @app.flexmock_verify
end

- (Object) test_filter_paid



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'test/test_plugin/patinfo_invoicer.rb', line 44

def test_filter_paid
  ptr1 = FlexMock.new
  ptr2 = FlexMock.new
  ptr3 = FlexMock.new
  ptr4 = FlexMock.new
  ptr1.should_receive(:resolve).and_return { } # disables the neighborhood_names check
  ptr2.should_receive(:resolve).and_return { }
  ptr3.should_receive(:resolve).and_return { }
  ptr4.should_receive(:resolve).and_return { }
  day = Date.today - 1
  ## Item that has been paid more than a year ago
  ## (inv1) - should be in new invoice
  item1 = AbstractInvoiceItem.new
  item1.item_pointer = ptr1
  item1.text = '11111 11'
  item1.type = :annual_fee
  item1.time = Time.local(day.year, day.month, 
    day.day, 10, 32)
  ## Item that has never before been uploaded
  ## should be in new invoice
  item2 = AbstractInvoiceItem.new
  item2.item_pointer = ptr2
  item2.text = '22222 22'
  item2.type = :annual_fee
  item2.time = Time.local(day.year, day.month, 
    day.day, 10, 32)
  ## Item that is already once in new invoice
  ## (item1) - should not be in new invoice
  item3 = AbstractInvoiceItem.new
  item3.item_pointer = ptr3
  item3.text = '11111 11'
  item3.type = :annual_fee
  item3.time = Time.local(day.year, day.month, 
    day.day, 16, 23)
  ## Item that has been paid less than a year ago
  ## (inv2) - should not be in new invoice
  item4 = AbstractInvoiceItem.new
  item4.item_pointer = ptr4
  item4.text = '33333 33'
  item4.type = :annual_fee
  item4.time = Time.local(day.year, day.month, 
    day.day, 16, 23)
  items = [item1, item2, item3, item4]

  inv1 = FlexMock.new
  inv1.should_receive(:items).and_return {
    itm1 = FlexMock.new
    itm1.should_receive(:expired?).and_return { true }
    itm1.should_receive(:type).and_return { :annual_fee }
    itm1.should_receive(:item_pointer).and_return { 'model 1' }
    itm1.should_receive(:text).and_return { '11111 11' }
    itm2 = FlexMock.new
    itm2.should_receive(:expired?).and_return { false }
    itm2.should_receive(:type).and_return { :processing }
    itm2.should_receive(:item_pointer).and_return { 'model 1' }
    itm2.should_receive(:text).and_return { '11111 11' }
    {1 => itm1, 2 => itm2}
  }
  inv2 = FlexMock.new
  inv2.should_receive(:items).and_return {
    itm1 = FlexMock.new
    itm1.should_receive(:expired?).and_return { false }
    itm1.should_receive(:type).and_return { :annual_fee }
    itm1.should_receive(:item_pointer).and_return { 'model 3' }
    itm1.should_receive(:text).and_return { '33333 33' }
    {1 => itm1}
  }
  inv3 = FlexMock.new
  inv3.should_receive(:items).and_return {
    itm1 = FlexMock.new
    itm1.should_receive(:expired?).and_return { false }
    itm1.should_receive(:type).and_return { :annual_fee }
    itm1.should_receive(:item_pointer).and_return { 'model 4' }
    itm1.should_receive(:text).and_return { '44444 44' }
    {1 => itm1}
  }
  @app.should_receive(:invoices).and_return { 
    {
      1 => inv1, 
      2 => inv2,
      3 => inv3,
    }
  }
  expected = [item1, item2]
  assert_equal(expected, @plugin.filter_paid(items))
end

- (Object) test_group_by_company



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'test/test_plugin/patinfo_invoicer.rb', line 184

def test_group_by_company
  old_invoice = FlexMock.new
  company1 = FlexMock.new
  company1.should_receive(:invoice_date).with(:patinfo).and_return { nil }
  company1.should_receive(:patinfo_price).and_return { nil }
  company2 = FlexMock.new
  company2.should_receive(:invoice_date).with(:patinfo).and_return { nil }
  company2.should_receive(:patinfo_price).and_return { nil }
  @app.should_receive(:invoices).and_return { { 1 => old_invoice } }
  old_item = FlexMock.new
  old_invoice.should_receive(:items).and_return { { 1 => old_item } }
  old_item.should_receive(:type).and_return { :annual_fee }
  item_pointer = FlexMock.new
  old_item.should_receive(:item_pointer).and_return { item_pointer }
  sequence = FlexMock.new
  sequence.should_receive(:is_a?).with(Sequence).and_return true
  item_pointer.should_receive(:resolve).and_return { sequence }
  sequence.should_receive(:company).and_return { company2 }
  pointer = FlexMock.new
  time = Time.now
  item1 = AbstractInvoiceItem.new
  item1.yus_name = 'user1'
  item1.item_pointer = pointer
  item1.text = '11111 11'
  item1.time = time
  item2 = AbstractInvoiceItem.new
  item2.yus_name = 'user2'
  item2.item_pointer = pointer
  item2.text = '22222 22'
  item1.time = time
  item2.time = time
  item3 = AbstractInvoiceItem.new
  item3.yus_name = 'user1'
  item3.item_pointer = pointer
  item3.text = '33333 33'
  item3.time = time
  item4 = AbstractInvoiceItem.new
  item4.yus_name = 'user2'
  item4.item_pointer = pointer
  item4.text = '44444 44'
  item4.time = time
  company_donor1 = FlexMock.new
  company_donor2 = FlexMock.new
  company_donor1.should_receive(:company).and_return { company1 }
  company_donor2.should_receive(:company).and_return { company2 }
  companies = [company_donor1, company_donor2] * 2
  pointer.should_receive(:resolve).and_return { companies.shift }

  items = [item1, item2, item3, item4]
  comps = @plugin.group_by_company(items)
  comp1 = comps[company1]
  comp2 = comps[company2]
  assert_equal(3, comp1.size)
  item = comp1.shift
  assert_equal([item1, item3], comp1)
  assert_instance_of(AbstractInvoiceItem, item)
  assert_equal(1000, item.price)
  assert_equal(:activation, item.type)
  assert_equal([item2, item4], comp2)
end

- (Object) test_recent_items



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'test/test_plugin/patinfo_invoicer.rb', line 130

def test_recent_items
  ptr = FlexMock.new
  ptr.should_receive(:resolve).and_return { true }
  today = Date.today
  item1 = AbstractInvoiceItem.new
  item1.item_pointer = ptr
  item1.time = Time.local(today.year, today.month,
    today.day) - (24*60*60)
  item1.text = '12345 01'
  item2 = AbstractInvoiceItem.new
  item2.item_pointer = ptr
  item2.time = Time.local(today.year, today.month,
    today.day, 23, 59, 59) - (2*24*60*60)
  item2.text = '12345 02'
  item3 = AbstractInvoiceItem.new
  item3.item_pointer = ptr
  item3.time = Time.local(today.year, today.month,
    today.day, 23, 59, 59) - (24*60*60)
  item3.text = '12345 03'
  item4 = AbstractInvoiceItem.new
  item4.item_pointer = ptr
  item4.time = Time.local(today.year, today.month,
    today.day)
  item4.text = '12345 04'
  item5 = AbstractInvoiceItem.new
  item5.item_pointer = ptr
  item5.time = Time.local(today.year, today.month,
    today.day, 23, 59, 58) - (24*60*60)
  item5.text = '12345 03'
  items = {
    1 =>  item1,
    2 =>  item2,
    3 =>  item3,
    4 =>  item4,
    5 =>  item5,
  }
  slate = FlexMock.new
  slate.should_receive(:items).and_return {
    items
  }
  @app.should_receive(:slate).and_return { |name| 
    assert_equal(:patinfo, name)
    slate
  }
  @app.should_receive(:active_pdf_patinfos).and_return {
    {
      '12345_01.pdf' => 1, '12345_02.12347435.pdf' => 1,
      '12345_03.pdf' => 1, '12345_04.e1718.pdf' => 1, 
                           '12345_03.12345654.pdf' => 1,
    } 
  }
  assert_equal(2, @plugin.recent_items(today - 1).size)
  assert_equal([item3, item1], @plugin.recent_items(today - 1))
end