africompta 1.9.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1001 @@
1
+ require 'test/unit'
2
+
3
+ TESTBIG=false
4
+
5
+ class TC_AfriCompta < Test::Unit::TestCase
6
+ def setup
7
+ dputs(1) { 'Setting up - deleting and reloading everything' }
8
+ Entities.delete_all_data
9
+
10
+ dputs(2) { 'Resetting SQLite' }
11
+ SQLite.dbs_close_all
12
+ dputs(2) { 'Putting testGestion' }
13
+ FileUtils.cp('db.testGestion', 'data/compta.db')
14
+ SQLite.dbs_open_load_migrate
15
+
16
+ dputs(3) { 'And searching for some accounts' }
17
+ @root = Accounts.match_by_name('Root')
18
+ @cash = Accounts.match_by_name('Cash')
19
+ @income = Accounts.match_by_name('Income')
20
+ @outcome = Accounts.match_by_name('Outcome')
21
+ @local = Users.match_by_name('local')
22
+ end
23
+
24
+ def teardown
25
+ Entities.save_all
26
+ end
27
+
28
+ # This should work - but for some reason the indexes get mixed up
29
+ # and end up being strings instead of integers....
30
+ def tes_load_db
31
+ setup_clean_accounts
32
+ Movements.create('any', '2013-01-01', 10, @cash, @income)
33
+
34
+ SQLite.dbs_close_all
35
+ Accounts.delete_all(true)
36
+ SQLite.dbs_open_load
37
+
38
+ Accounts.dump true
39
+ end
40
+
41
+ def test_db
42
+ movs = Movements.search_all
43
+ assert_equal 4, movs.length
44
+ accs = Accounts.search_all
45
+ assert_equal 5, accs.length
46
+ users = Users.search_all
47
+ assert_equal 2, users.length
48
+
49
+ assert_equal [{:id => 1,
50
+ :value => 1000.0,
51
+ :desc => 'Salary',
52
+ :revision => nil,
53
+ :account_src_id => [2],
54
+ :global_id => '5544436cf81115c6faf577a7e2307e92-1',
55
+ :rev_index => 1,
56
+ :index => 1,
57
+ :account_dst_id => [3]},
58
+ {:id => 2,
59
+ :value => 100.0,
60
+ :desc => 'Gift',
61
+ :revision => nil,
62
+ :account_src_id => [2],
63
+ :global_id => '5544436cf81115c6faf577a7e2307e92-2',
64
+ :rev_index => 2,
65
+ :index => 2,
66
+ :account_dst_id => [3]},
67
+ {:id => 3,
68
+ :value => 40.0,
69
+ :desc => 'Train',
70
+ :revision => nil,
71
+ :account_src_id => [4],
72
+ :global_id => '5544436cf81115c6faf577a7e2307e92-3',
73
+ :rev_index => 3,
74
+ :index => 3,
75
+ :account_dst_id => [2]},
76
+ {:id => 4,
77
+ :value => 20.0,
78
+ :desc => 'Restaurant',
79
+ :revision => nil,
80
+ :account_src_id => [4],
81
+ :global_id => '5544436cf81115c6faf577a7e2307e92-4',
82
+ :rev_index => 4,
83
+ :index => 4,
84
+ :account_dst_id => [2]}],
85
+ movs.collect { |m|
86
+ m.to_hash.delete_if { |k, v| k == :date
87
+ } }.sort { |a, b| a[:id] <=> b[:id] }
88
+
89
+ assert_equal [
90
+ {:id => 1,
91
+ :multiplier => 1.0,
92
+ :total => '0',
93
+ :desc => 'Full description',
94
+ :account_id => 0,
95
+ :name => 'Root',
96
+ :global_id => '5544436cf81115c6faf577a7e2307e92-1',
97
+ :deleted => false,
98
+ :keep_total => false,
99
+ :index => 1,
100
+ :rev_index => 1},
101
+ {:id => 2,
102
+ :multiplier => -1.0,
103
+ :total => '1040.0',
104
+ :desc => 'Full description',
105
+ :account_id => 1,
106
+ :name => 'Cash',
107
+ :global_id => '5544436cf81115c6faf577a7e2307e92-2',
108
+ :deleted => false,
109
+ :keep_total => true,
110
+ :index => 5,
111
+ :rev_index => 2},
112
+ {:id => 3,
113
+ :multiplier => 1.0,
114
+ :total => '1100.0',
115
+ :desc => 'Full description',
116
+ :account_id => 1,
117
+ :name => 'Income',
118
+ :global_id => '5544436cf81115c6faf577a7e2307e92-3',
119
+ :deleted => false,
120
+ :keep_total => false,
121
+ :index => 8,
122
+ :rev_index => 3},
123
+ {:id => 4,
124
+ :multiplier => 1.0,
125
+ :total => '-60.0',
126
+ :desc => 'Full description',
127
+ :account_id => 1,
128
+ :name => 'Outcome',
129
+ :global_id => '5544436cf81115c6faf577a7e2307e92-4',
130
+ :deleted => false,
131
+ :keep_total => false,
132
+ :index => 10,
133
+ :rev_index => 4},
134
+ {:global_id => '5544436cf81115c6faf577a7e2307e92-5',
135
+ :multiplier => -1.0,
136
+ :total => '0',
137
+ :desc => 'Full description',
138
+ :account_id => 1,
139
+ :name => 'Lending',
140
+ :index => 9,
141
+ :rev_index => 5,
142
+ :deleted => false,
143
+ :keep_total => true,
144
+ :id => 5}],
145
+ accs.collect { |a| a.to_hash }.sort { |a, b| a[:id] <=> b[:id] }
146
+
147
+ assert_equal [{:full => '5544436cf81115c6faf577a7e2307e92',
148
+ :pass => '152020265102732202950475079275867584513',
149
+ :account_index => 11,
150
+ :movement_index => 5,
151
+ :name => 'local',
152
+ :id => 1},
153
+ {:pass => 'bar',
154
+ :full => 'foo bar',
155
+ :account_index => 0,
156
+ :movement_index => 0,
157
+ :name => 'foo',
158
+ :id => 2}],
159
+ users.collect { |u| u.to_hash }
160
+ end
161
+
162
+ def test_mov
163
+ # Test all methods copied into Movements
164
+ mov = Movements.match_by_desc('Train')
165
+
166
+ assert_equal 40, mov.value
167
+ assert_equal "Train\r5544436cf81115c6faf577a7e2307e92-3\t40.000\t2012-07-02\t" +
168
+ "5544436cf81115c6faf577a7e2307e92-4\t5544436cf81115c6faf577a7e2307e92-2",
169
+ mov.to_s
170
+ assert_equal "{\"str\":\"Train\\r5544436cf81115c6faf577a7e2307e92-3\\t40.000\\t" +
171
+ "2012-07-02\\t5544436cf81115c6faf577a7e2307e92-4\\t" +
172
+ "5544436cf81115c6faf577a7e2307e92-2\"}",
173
+ mov.to_json
174
+ assert_equal({:global_id => '5544436cf81115c6faf577a7e2307e92-2',
175
+ :desc => 'Full description',
176
+ :total => '1040.0',
177
+ :multiplier => -1.0,
178
+ :account_id => 1,
179
+ :name => 'Cash',
180
+ :id => 2,
181
+ :deleted => false,
182
+ :keep_total => true,
183
+ :rev_index => 2,
184
+ :index => 5
185
+ },
186
+ mov.get_other_account(mov.account_src).to_hash)
187
+
188
+ # This is 11th of July 2012
189
+ assert_equal '1040.0', @cash.total
190
+
191
+ mov.set('new', '11/7/12', 120, @cash, @income)
192
+ assert_equal "new\r5544436cf81115c6faf577a7e2307e92-3\t120.000\t2012-07-11\t" +
193
+ "5544436cf81115c6faf577a7e2307e92-2\t5544436cf81115c6faf577a7e2307e92-3",
194
+ mov.to_s
195
+ assert_equal 1200, @cash.total
196
+
197
+ assert_equal 120.0, mov.get_value(@cash)
198
+
199
+ assert_equal true, mov.is_in_account(@cash)
200
+ assert_equal false, mov.is_in_account(@outcome)
201
+
202
+ assert_equal 11, mov.get_index
203
+ end
204
+
205
+ def test_movs
206
+ assert_equal '1040.0', @cash.total
207
+ assert_equal '-60.0', @outcome.total
208
+
209
+ # Overwriting movement with id 4: Restaurant with value = 20.0
210
+ mov = Movements.from_s "Restaurant\r5544436cf81115c6faf577a7e2307e92-4\t10" +
211
+ "\t2012-07-12\t" +
212
+ "5544436cf81115c6faf577a7e2307e92-4\t5544436cf81115c6faf577a7e2307e92-2"
213
+ assert_equal(1050.0, @cash.total)
214
+ assert_equal(-50.0, @outcome.total)
215
+ assert_equal(10.0, mov.value)
216
+
217
+ # Creating new movement
218
+ newmov = Movements.from_s "Car\r5544436cf81115c6faf577a7e2307e92-5\t100" +
219
+ "\t2012-07-12\t" +
220
+ "5544436cf81115c6faf577a7e2307e92-4\t5544436cf81115c6faf577a7e2307e92-2"
221
+ assert_equal 950.0, @cash.total
222
+ assert_equal(-150.0, @outcome.total)
223
+ assert_equal(100.0, newmov.value)
224
+
225
+ # Testing JSON
226
+ mov_json = newmov.to_json
227
+ assert_equal "{\"str\":\"Car\\r5544436cf81115c6faf577a7e2307e92-5\\t100.000\\t" +
228
+ "2012-07-12\\t5544436cf81115c6faf577a7e2307e92-4\\t" +
229
+ "5544436cf81115c6faf577a7e2307e92-2\"}",
230
+ mov_json
231
+ assert_equal(950.0, @cash.total)
232
+ newmov.value = 50
233
+ assert_equal(1000.0, @cash.total)
234
+ newmov = Movements.from_json mov_json
235
+ assert_equal(950.0, @cash.total)
236
+ assert_equal(100.0, newmov.value)
237
+
238
+ assert_equal 23, @local.movement_index
239
+
240
+ Movements.create('test_mov', '2012-02-29', 100.0, @cash, @outcome)
241
+ assert_equal 25, @local.movement_index
242
+ end
243
+
244
+ def test_account
245
+ tree = []
246
+ @root.get_tree { |a| tree.push a.name }
247
+ assert_equal %w( Root Lending Cash Income Outcome ).sort, tree.sort
248
+
249
+ assert_equal 'Root', @root.path
250
+ assert_equal 'Root::Outcome', @outcome.path
251
+
252
+ assert_equal 4, @outcome.rev_index
253
+ @outcome.new_index
254
+ assert_equal 11, @outcome.rev_index
255
+ @outcome.new_index
256
+ assert_equal 12, @outcome.rev_index
257
+ @outcome.name = 'Outcome2'
258
+ assert_equal 13, @outcome.rev_index
259
+ @outcome.name = 'Outcome'
260
+ assert_equal 14, @outcome.rev_index
261
+
262
+ foo = Users.create('foo', 'foo bar', 'foobar')
263
+ box = Accounts.create('Cashbox', 'Running cash', @cash,
264
+ '5544436cf81115c6faf577a7e2307e92-7')
265
+
266
+ assert_equal '1040.0', @cash.total
267
+ @cash.set_nochildmult('Cash_2', 'All money', @root, 1, [foo.name])
268
+ assert_equal 'Cash_2', @cash.name
269
+ assert_equal 'All money', @cash.desc
270
+ assert_equal 1, @cash.multiplier
271
+ assert_equal(-1, box.multiplier)
272
+ assert_equal(-1040.0, @cash.total)
273
+
274
+ assert_equal([{:value => 20.0,
275
+ :desc => 'Restaurant',
276
+ :account_src_id => [4],
277
+ :revision => nil,
278
+ :account_dst_id => [2],
279
+ :global_id => '5544436cf81115c6faf577a7e2307e92-4',
280
+ :index => 4,
281
+ :rev_index => 4,
282
+ :id => 4},
283
+ {:value => 100.0,
284
+ :desc => 'Gift',
285
+ :account_src_id => [2],
286
+ :revision => nil,
287
+ :account_dst_id => [3],
288
+ :global_id => '5544436cf81115c6faf577a7e2307e92-2',
289
+ :index => 2,
290
+ :rev_index => 2,
291
+ :id => 2},
292
+ {:value => 40.0,
293
+ :desc => 'Train',
294
+ :account_src_id => [4],
295
+ :revision => nil,
296
+ :account_dst_id => [2],
297
+ :global_id => '5544436cf81115c6faf577a7e2307e92-3',
298
+ :index => 3,
299
+ :rev_index => 3,
300
+ :id => 3},
301
+ {:value => 1000.0,
302
+ :desc => 'Salary',
303
+ :account_src_id => [2],
304
+ :revision => nil,
305
+ :account_dst_id => [3],
306
+ :global_id => '5544436cf81115c6faf577a7e2307e92-1',
307
+ :index => 1,
308
+ :rev_index => 1,
309
+ :id => 1}],
310
+ @cash.movements.collect { |m| m.to_hash.delete_if { |k, v| k == :date
311
+ } })
312
+
313
+ assert_equal "All money\r5544436cf81115c6faf577a7e2307e92-2\t-1040.000\t" +
314
+ "Cash_2\t1\t5544436cf81115c6faf577a7e2307e92-1\tfalse\tfalse",
315
+ @cash.to_s
316
+
317
+ assert_equal false, @cash.is_empty
318
+ box = Accounts.create('Cashbox', 'Running cash', @cash,
319
+ '5544436cf81115c6faf577a7e2307e92-8')
320
+ assert_equal true, box.is_empty
321
+
322
+ assert_equal(1, @cash.multiplier)
323
+ assert_equal(1, box.multiplier)
324
+ @cash.set_child_multiplier_total(-1, true)
325
+ assert_equal(-1, @cash.multiplier)
326
+ assert_equal(-1, box.multiplier)
327
+
328
+ assert_equal 7, Accounts.search_all.length
329
+ box.delete
330
+ assert_equal 7, Accounts.search_all.length
331
+ assert_equal true, box.deleted
332
+ end
333
+
334
+ def test_accounts
335
+ assert_equal 2, @cash.id
336
+
337
+ box = Accounts.create('Cashbox', 'Running cash', @cash,
338
+ '5544436cf81115c6faf577a7e2307e92-8')
339
+ assert_equal({:multiplier => -1,
340
+ :desc => 'Running cash',
341
+ :total => 0,
342
+ :account_id => 2,
343
+ :global_id => '5544436cf81115c6faf577a7e2307e92-8',
344
+ :name => 'Cashbox',
345
+ :id => 6,
346
+ :deleted => false,
347
+ :keep_total => true,
348
+ :rev_index => 11}, box.to_hash)
349
+ assert_equal 'Root::Cash::Cashbox', box.path
350
+ assert_equal(-1, @cash.multiplier)
351
+ assert_equal(-1, box.multiplier)
352
+
353
+ box_s = box.to_s
354
+ box.delete
355
+ dputs(1) { "box_s is #{box_s.inspect}" }
356
+ box = Accounts.from_s(box_s)
357
+ assert_equal({:multiplier => -1.0,
358
+ :desc => 'Running cash',
359
+ :total => 0.0,
360
+ :account_id => 2,
361
+ :global_id => '5544436cf81115c6faf577a7e2307e92-8',
362
+ :name => 'Cashbox',
363
+ :id => 6,
364
+ :deleted => false,
365
+ :keep_total => true,
366
+ :rev_index => 21}, box.to_hash)
367
+
368
+ course = Accounts.create_path('Root::Income::Course', 'course')
369
+ assert_equal 'Root::Income::Course', course.get_path
370
+ assert_equal '5544436cf81115c6faf577a7e2307e92-7', course.global_id
371
+
372
+ ccard = Accounts.create_path('Credit::Card', 'credit-card')
373
+ assert_equal 'Credit::Card', ccard.get_path
374
+ assert_equal '5544436cf81115c6faf577a7e2307e92-9', ccard.global_id
375
+ end
376
+
377
+ def test_users
378
+ Users.create('foo2', 'foo foo', 'foo2bar')
379
+ foo = Users.match_by_name('foo2')
380
+ assert_equal 'foo foo', foo.full
381
+ assert_equal 'foo2bar', foo.pass
382
+ assert_equal 0, foo.movement_index
383
+ assert_equal 0, foo.account_index
384
+
385
+ foo.update_movement_index
386
+ assert_equal 4, foo.movement_index
387
+ foo.update_account_index
388
+ assert_equal 10, foo.account_index
389
+ end
390
+
391
+ def test_remote
392
+ rem = Remotes.create(:url => 'http://localhost:3302/acaccount',
393
+ :name => 'foo', :pass => 'bar')
394
+ assert_equal 0, rem.account_index
395
+ assert_equal 0, rem.movement_index
396
+
397
+ rem.update_movement_index
398
+ assert_equal 4, rem.movement_index
399
+ rem.update_account_index
400
+ assert_equal 10, rem.account_index
401
+
402
+ rem2 = Remotes.create(:url => 'http://localhost:3302/acaccount',
403
+ :name => 'foo', :pass => 'bar', :account_index => 10,
404
+ :movement_index => 20)
405
+ assert_equal 10, rem2.account_index
406
+ assert_equal 20, rem2.movement_index
407
+ end
408
+
409
+ def test_merge_get
410
+ rep = ACaccess.get('version/foo,ba')
411
+ assert_equal 'User foo not known with pass ba', rep
412
+
413
+ rep = ACaccess.get('version/foo,bar')
414
+ assert_equal $VERSION.to_s, rep
415
+
416
+ rep = ACaccess.get('index/foo,bar')
417
+ assert_equal '11,5', rep
418
+
419
+ rep = ACaccess.get('accounts_get_one/5544436cf81115c6faf577a7e2307e92-2' +
420
+ '/foo,bar')
421
+ assert_equal "Full description\r5544436cf81115c6faf577a7e2307e92-2\t" +
422
+ "1040.000\tCash\t-1\t5544436cf81115c6faf577a7e2307e92-1\tfalse\ttrue", rep
423
+
424
+ rep = ACaccess.get('accounts_get/foo,bar')
425
+ assert_equal "Full description\r5544436cf81115c6faf577a7e2307e92-1\t0.000\t" +
426
+ "Root\t1\t\tfalse\tfalse\n" +
427
+ "Full description\r5544436cf81115c6faf577a7e2307e92-2\t1040.000\t" +
428
+ "Cash\t-1\t5544436cf81115c6faf577a7e2307e92-1\tfalse\ttrue\n" +
429
+ "Full description\r5544436cf81115c6faf577a7e2307e92-3\t1100.000\t" +
430
+ "Income\t1\t5544436cf81115c6faf577a7e2307e92-1\tfalse\tfalse\n" +
431
+ "Full description\r5544436cf81115c6faf577a7e2307e92-5\t0.000\t" +
432
+ "Lending\t-1\t5544436cf81115c6faf577a7e2307e92-1\tfalse\ttrue\n" +
433
+ "Full description\r5544436cf81115c6faf577a7e2307e92-4\t-60.000\t" +
434
+ "Outcome\t1\t5544436cf81115c6faf577a7e2307e92-1\tfalse\tfalse", rep
435
+
436
+ ACaccess.get('reset_user_account_indexes/foo,bar')
437
+ rep = ACaccess.get('accounts_get/foo,bar')
438
+ assert_equal '', rep
439
+
440
+ rep = ACaccess.get('accounts_get_all/foo,bar')
441
+ assert_equal "Full description\r5544436cf81115c6faf577a7e2307e92-1\t0.000\t" +
442
+ "Root\t1\t\tfalse\tfalse\tRoot\n" +
443
+ "Full description\r5544436cf81115c6faf577a7e2307e92-2\t1040.000\t" +
444
+ "Cash\t-1\t5544436cf81115c6faf577a7e2307e92-1\tfalse\ttrue\tRoot::Cash\n" +
445
+ "Full description\r5544436cf81115c6faf577a7e2307e92-3\t1100.000\t" +
446
+ "Income\t1\t5544436cf81115c6faf577a7e2307e92-1\tfalse\tfalse\tRoot::Income\n" +
447
+ "Full description\r5544436cf81115c6faf577a7e2307e92-4\t-60.000\t" +
448
+ "Outcome\t1\t5544436cf81115c6faf577a7e2307e92-1\tfalse\tfalse\tRoot::Outcome\n" +
449
+ "Full description\r5544436cf81115c6faf577a7e2307e92-5\t0.000\t" +
450
+ "Lending\t-1\t5544436cf81115c6faf577a7e2307e92-1\tfalse\ttrue\tRoot::Lending", rep
451
+
452
+ rep = ACaccess.get('movements_get_one/5544436cf81115c6faf577a7e2307e92-4/foo,bar')
453
+ assert_equal "Restaurant\r5544436cf81115c6faf577a7e2307e92-4\t20.000\t" +
454
+ "2012-07-11\t5544436cf81115c6faf577a7e2307e92-4\t" +
455
+ '5544436cf81115c6faf577a7e2307e92-2', rep
456
+
457
+ rep = ACaccess.get('movements_get_all/0,100/foo,bar')
458
+ assert_equal "Salary\r5544436cf81115c6faf577a7e2307e92-1\t1000.000\t" +
459
+ "2012-07-01\t5544436cf81115c6faf577a7e2307e92-2\t" +
460
+ "5544436cf81115c6faf577a7e2307e92-3\nGift\r" +
461
+ "5544436cf81115c6faf577a7e2307e92-2\t100.000\t2012-07-10\t" +
462
+ "5544436cf81115c6faf577a7e2307e92-2\t5544436cf81115c6faf577a7e2307e92-3\n" +
463
+ "Train\r5544436cf81115c6faf577a7e2307e92-3\t40.000\t2012-07-02\t" +
464
+ "5544436cf81115c6faf577a7e2307e92-4\t5544436cf81115c6faf577a7e2307e92-2\n" +
465
+ "Restaurant\r5544436cf81115c6faf577a7e2307e92-4\t20.000\t2012-07-11\t" +
466
+ "5544436cf81115c6faf577a7e2307e92-4\t5544436cf81115c6faf577a7e2307e92-2\n",
467
+ rep
468
+
469
+ rep = ACaccess.get('movements_get/foo,bar')
470
+ assert_equal "Salary\r5544436cf81115c6faf577a7e2307e92-1\t1000.000\t" +
471
+ "2012-07-01\t5544436cf81115c6faf577a7e2307e92-2\t" +
472
+ "5544436cf81115c6faf577a7e2307e92-3\nGift\r" +
473
+ "5544436cf81115c6faf577a7e2307e92-2\t100.000\t2012-07-10\t" +
474
+ "5544436cf81115c6faf577a7e2307e92-2\t5544436cf81115c6faf577a7e2307e92-3\n" +
475
+ "Train\r5544436cf81115c6faf577a7e2307e92-3\t40.000\t2012-07-02\t" +
476
+ "5544436cf81115c6faf577a7e2307e92-4\t5544436cf81115c6faf577a7e2307e92-2\n" +
477
+ "Restaurant\r5544436cf81115c6faf577a7e2307e92-4\t20.000\t2012-07-11\t" +
478
+ "5544436cf81115c6faf577a7e2307e92-4\t5544436cf81115c6faf577a7e2307e92-2\n",
479
+ rep
480
+
481
+ ACaccess.get('reset_user_movement_indexes/foo,bar')
482
+ rep = ACaccess.get('movements_get/foo,bar')
483
+ assert_equal '', rep
484
+ end
485
+
486
+ def test_merge_post
487
+ input = {'user' => 'foo', 'pass' => 'bar'}
488
+
489
+ rep = ACaccess.post('account_get_id', input.merge('account' => 'Root'))
490
+ assert_equal '1', rep
491
+ rep = ACaccess.post('account_get_id', input.merge('account' => 'Root::Cash'))
492
+ assert_equal '2', rep
493
+
494
+ mov_pizza = "Pizza\r5544436cf81115c6faf577a7e2307e92-5\t12.0\t" +
495
+ "2012-07-20\t5544436cf81115c6faf577a7e2307e92-4\t" +
496
+ '5544436cf81115c6faf577a7e2307e92-2'
497
+ mov_panini = "Panini\r5544436cf81115c6faf577a7e2307e92-6\t14.0\t" +
498
+ "2012-07-21\t5544436cf81115c6faf577a7e2307e92-4\t" +
499
+ '5544436cf81115c6faf577a7e2307e92-2'
500
+ assert_equal '-60.0', @outcome.total
501
+ assert_equal '1040.0', @cash.total
502
+ rep = ACaccess.post('movements_put', input.merge('movements' =>
503
+ [{:str => mov_pizza}.to_json, {:str => mov_panini}.to_json].to_json))
504
+ assert_equal 'ok', rep
505
+
506
+ mov_pizza_merged = Movements.match_by_desc('Pizza')
507
+ assert_equal 12.0, mov_pizza_merged.value
508
+ mov_panini_merged = Movements.match_by_desc('Panini')
509
+ assert_equal 14.0, mov_panini_merged.value
510
+ assert_equal(-86.0, @outcome.total)
511
+ assert_equal 1014.0, @cash.total
512
+ Entities.Movements.save
513
+
514
+ Entities.Movements.load
515
+ mov_pizza_merged = Movements.match_by_desc('Pizza')
516
+ assert_equal '2012-07-20', mov_pizza_merged.date.to_s
517
+ mov_panini_merged = Movements.match_by_desc('Panini')
518
+ assert_equal '2012-07-21', mov_panini_merged.date.to_s
519
+
520
+ dputs(1) { 'Going to overwrite pizza and panini' }
521
+ dputs(2) { "Movements are #{Movements.search_all.inspect}" }
522
+ rep = ACaccess.post('movements_put', input.merge('movements' =>
523
+ [{:str => mov_pizza.sub('12.0', '22.0')}.to_json,
524
+ {:str => mov_panini.sub('14.0', '24.0')}.to_json].to_json))
525
+ dputs(2) { "Movements are #{Movements.search_all.inspect}" }
526
+ assert_equal 'ok', rep
527
+ mov_pizza_merged = Movements.match_by_desc('Pizza')
528
+ assert_equal 22.0, mov_pizza_merged.value, mov_pizza_merged.inspect
529
+ mov_panini_merged = Movements.match_by_desc('Panini')
530
+ assert_equal 24.0, mov_panini_merged.value, mov_panini_merged.inspect
531
+
532
+ end
533
+
534
+ # This is only relevant when testing with big data from solar
535
+ def tes_gettree
536
+ tree = []
537
+ Accounts.matches_by_account_id(0).to_a.each { |a|
538
+ a.get_tree { |b| tree.push b.rev_index }
539
+ }
540
+ tree_d = []
541
+ Accounts.matches_by_account_id(0).to_a.each { |a|
542
+ a.get_tree_debug { |b| tree_d.push b.rev_index }
543
+ }
544
+
545
+ assert_equal 389, tree.count
546
+ assert_equal 389, tree_d.count
547
+ end
548
+
549
+ def setup_clean_accounts(loans = false)
550
+ Entities.delete_all_data()
551
+ Users.create('local', '123456789', 'bar')
552
+ @root = Accounts.create('Root')
553
+ @income = Accounts.create('Income', '', @root)
554
+ @spending = Accounts.create('Spending', '', @root)
555
+ @cash = Accounts.create('Cash', '', @root)
556
+ @cash.multiplier = -1
557
+ @cash.keep_total = true
558
+ if loans
559
+ @loan = Accounts.create('Loan', '', @cash)
560
+ @loan.multiplier = 1
561
+ @loan.keep_total = true
562
+ @zcash = Accounts.create('ZCash', '', @cash)
563
+ @zcash.multiplier = -1
564
+ @zcash.keep_total = true
565
+ end
566
+ end
567
+
568
+ def get_sorted_accounts(name)
569
+ Accounts.search_by_name(name).sort {
570
+ |a, b| a.path <=> b.path
571
+ }
572
+ end
573
+
574
+ def test_archive
575
+ setup_clean_accounts
576
+ @base = []
577
+ [1001, 1009, 1012, 1106, 1112, 1201].each { |b|
578
+ #[ 1001, 1009 ].each{|b|
579
+ @base[b] = Accounts.create "Base_#{b}", '', @income
580
+ }
581
+
582
+ def testmov(base, cash, years)
583
+ d = 1
584
+ years.each { |y|
585
+ Movements.create("inscr #{base.desc}", "#{y}-01-2#{d}",
586
+ y.to_i + d, base, cash)
587
+ d += 1
588
+ }
589
+ end
590
+
591
+ # This should never happen, but still it's possible...
592
+ Movements.create 'buggy', '2011-01-01', 100, @income, @cash
593
+
594
+ # Create different test cases in different accounts
595
+ # This has most == last == 2010
596
+ testmov(@base[1001], @cash, %w( 2010 2010 ))
597
+ # This has most == 2010, last == 2011
598
+ testmov(@base[1009], @cash, %w( 2010 2010 2011 ))
599
+ if true
600
+ # This has most == last == 2011
601
+ testmov(@base[1012], @cash, %w( 2010 2011 2011 ))
602
+ # This has most == 2011, last == 2012
603
+ testmov(@base[1106], @cash, %w( 2011 2011 2012 ))
604
+ # This has most == last == 2012 and movements in 2011
605
+ testmov(@base[1112], @cash, %w( 2011 2012 2012 ))
606
+ # This has most == last == 2012 and no movements in 2011
607
+ testmov(@base[1201], @cash, %w( 2012 2012 ))
608
+ end
609
+
610
+ Accounts.archive(1, 2012)
611
+
612
+ # Name, account-count, movs-count, path of first occurence
613
+ [[1001, 1, 2, 'Archive::2010::Income::Base_1001'],
614
+ [1009, 3, 2, 'Archive::2010::Income::Base_1009'],
615
+ [1012, 3, 1, 'Archive::2010::Income::Base_1012'],
616
+ [1106, 2, 2, 'Archive::2011::Income::Base_1106'],
617
+ [1112, 2, 1, 'Archive::2011::Income::Base_1112'],
618
+ [1201, 1, 2, 'Root::Income::Base_1201']
619
+ ].each { |b|
620
+ name, count, movs, path = b
621
+ accs = get_sorted_accounts("Base_#{name}")
622
+ dputs(3) { "Accounts are #{accs.collect { |a| a.path }.join('-') }" }
623
+ assert_equal count, accs.count, "Count for #{name}"
624
+ assert_equal movs, accs.first.movements.count, "movs for #{name}"
625
+ assert_equal path, accs.first.path, "path for #{name}"
626
+ }
627
+ end
628
+
629
+ def add_movs
630
+ Movements.create('Year 2010', '2011-01-01', 10, @cash, @income)
631
+ Movements.create('Year 2011', '2012-05-01', 20, @cash, @income)
632
+ Movements.create('Year 2012', '2012-06-01', 30, @cash, @income)
633
+ end
634
+
635
+ def test_archive_start_june
636
+ setup_clean_accounts
637
+ add_movs
638
+ Accounts.archive(6, 2012)
639
+ incomes = get_sorted_accounts('Income')
640
+ cash = get_sorted_accounts('cash')
641
+
642
+ assert_equal 3, incomes.length
643
+ assert_equal 1, incomes[0].movements.length
644
+ assert_equal 1, incomes[1].movements.length
645
+ assert_equal 1, incomes[2].movements.length
646
+
647
+ assert_equal 3, cash.length
648
+ assert_equal 1, cash[0].movements.length
649
+ assert_equal 2, cash[1].movements.length
650
+ assert_equal 2, cash[2].movements.length
651
+ end
652
+
653
+ # Testing keep_total true and false
654
+ def test_archive_multiple_invocations
655
+ setup_clean_accounts
656
+ add_movs
657
+ Accounts.archive(6, 2012)
658
+ incomes = get_sorted_accounts('Income')
659
+ cash = get_sorted_accounts('cash')
660
+
661
+ assert_equal 3, incomes.length
662
+ assert_equal 1, incomes[0].movements.length
663
+ assert_equal 1, incomes[1].movements.length
664
+ assert_equal 1, incomes[2].movements.length
665
+ cash.each { |a|
666
+ dputs(1) { "#{a.path} - #{a.movements.length}" }
667
+ }
668
+ assert_equal 3, cash.length
669
+ assert_equal 1, cash[0].movements.length
670
+ assert_equal 2, cash[1].movements.length
671
+ assert_equal 2, cash[2].movements.length
672
+
673
+ Accounts.archive(6, 2012)
674
+ incomes = get_sorted_accounts('Income')
675
+ cash = get_sorted_accounts('cash')
676
+
677
+ assert_equal 3, incomes.length
678
+ assert_equal 1, incomes[0].movements.length
679
+ assert_equal 1, incomes[1].movements.length
680
+ assert_equal 1, incomes[2].movements.length
681
+ cash.each { |a|
682
+ dputs(1) { "#{a.path} - #{a.movements.length}" }
683
+ }
684
+ assert_equal 3, cash.length
685
+ assert_equal 1, cash[0].movements.length
686
+ assert_equal 2, cash[1].movements.length
687
+ assert_equal 2, cash[2].movements.length
688
+ end
689
+
690
+ def test_archive_sum_up
691
+ # Make sure that everything still sums up
692
+ setup_clean_accounts
693
+ assert_equal(0, Accounts.match_by_name('Spending').total)
694
+ add_movs
695
+ assert_equal(0, Accounts.match_by_name('Spending').total)
696
+ Movements.create('Year 2012 - 1', '2012-06-01', 20, @spending, @cash)
697
+
698
+ assert_equal(-20, Accounts.match_by_name('Spending').total)
699
+ assert_equal(40, Accounts.match_by_name('Cash').total)
700
+ @cash.update_total
701
+ assert_equal(40, Accounts.match_by_name('Cash').total)
702
+
703
+ dputs(2) { '**** - Archiving for 2012 - *****' }
704
+ Accounts.archive(6, 2012)
705
+ cashs = get_sorted_accounts('Cash')
706
+ (0..2).each { |i|
707
+ dputs(3) { "Path for #{i} is #{cashs[i].get_path}" }
708
+ }
709
+ assert_equal 10, cashs[0].total
710
+ assert_equal 30, cashs[1].total
711
+ assert_equal 40, cashs[2].total
712
+ end
713
+
714
+ def test_archive_sum_up_consecutive
715
+ # Test two consecutive runs on the archive like 2013, then 2014, and
716
+ # make sure that the accounts that hold only a "final"-movement get
717
+ # deleted
718
+ setup_clean_accounts
719
+ add_movs
720
+ Movements.create('Year 2012 - 1', '2012-06-01', -30, @cash, @spending)
721
+ Accounts.dump(true)
722
+
723
+ dputs(1) { '**** - Archiving 1st for 2013 - *****' }
724
+ Accounts.archive(6, 2013)
725
+ Accounts.dump(true)
726
+ incomes = get_sorted_accounts('Income')
727
+ cash = get_sorted_accounts('Cash')
728
+ assert_equal 4, incomes.count
729
+ assert_equal 0, incomes[3].total
730
+ assert_equal 30, incomes[2].total, incomes[2].inspect
731
+ assert_equal 4, cash.count
732
+ assert_equal 30, cash[3].total
733
+ # check also after re-calculating of the totals!
734
+ incomes[3].update_total
735
+ assert_equal 0, incomes[3].total
736
+ cash[3].update_total
737
+ assert_equal 30, cash[3].total
738
+
739
+ dputs(1) { '**** - Archiving 2nd for 2014 - *****' }
740
+ # @cash and @spending are now pointing to the archived ones...
741
+ cash = Accounts.get_by_path('Root::Cash')
742
+ spending = Accounts.get_by_path('Root::Spending')
743
+ Movements.create('Year 2013 - 2', '2013-06-01', -30, cash, spending)
744
+ Accounts.archive(6, 2014)
745
+ Accounts.dump(true)
746
+
747
+ incomes = get_sorted_accounts('Income')
748
+ dputs(3) { incomes.inspect }
749
+ cash = get_sorted_accounts('Income')
750
+ dputs(3) { cash.inspect }
751
+ # We lost the actual account, as it should be empty
752
+ assert_equal 4, incomes.count
753
+ assert_equal true, incomes[3].deleted
754
+ assert_equal 30, incomes[2].total, incomes[2].inspect
755
+ assert_equal 4, cash.count
756
+ assert_equal 30, cash[2].total, cash[2].inspect
757
+
758
+ spending = get_sorted_accounts('Spending')
759
+ assert_equal 3, spending.count
760
+ assert_equal(0, spending[2].total)
761
+ end
762
+
763
+ def test_creation
764
+ Entities.delete_all_data
765
+ end
766
+
767
+ def load_big_data
768
+ dputs(1) { 'Setting up big data' }
769
+ Entities.delete_all_data
770
+
771
+ dputs(2) { 'Resetting SQLite' }
772
+ SQLite.dbs_close_all
773
+ dputs(2) { 'Loading big data' }
774
+ FileUtils.cp('db.solar', 'data/compta.db')
775
+ SQLite.dbs_open_load_migrate
776
+ end
777
+
778
+ def test_big_data_archive
779
+ TESTBIG or return
780
+
781
+ require 'benchmark'
782
+ require 'perftools'
783
+ load_big_data
784
+ Accounts.archive(1, 2012)
785
+ end
786
+
787
+ def test_big_data_merge
788
+ TESTBIG or return
789
+
790
+ require 'benchmark'
791
+ require 'perftools'
792
+ load_big_data
793
+
794
+ ACaccess.get('reset_user_indexes/ineiti,lasj')
795
+ dputs(1) { Benchmark.measure {
796
+ PerfTools::CpuProfiler.start('perfcheck_merge_1st') do
797
+ ACaccess.get('accounts_get/ineiti,lasj')
798
+ end
799
+ }.to_s
800
+ }
801
+ dputs(1) { Benchmark.measure {
802
+ PerfTools::CpuProfiler.start('perfcheck_merge_2nd') do
803
+ ACaccess.get('accounts_get/ineiti,lasj')
804
+ end
805
+ }.to_s
806
+ }
807
+ puts 'Now run
808
+ pprof.rb --pdf perfcheck_merge_1st > perfcheck_merge_1st.pdf
809
+ pprof.rb --pdf perfcheck_merge_2nd > perfcheck_merge_2nd.pdf
810
+ open perfcheck_merge_1st.pdf perfcheck_merge_2nd.pdf
811
+ '
812
+ end
813
+
814
+ def test_big_data_check
815
+ TESTBIG or return
816
+
817
+ load_big_data
818
+
819
+ ACaccess.get('reset_user_indexes/ineiti,lasj')
820
+ dputs(1) { Benchmark.measure {
821
+ ACaccess.get('movements_get_all/1,10000/ineiti,lasj')
822
+ }.to_s
823
+ }
824
+ dputs(1) { Benchmark.measure {
825
+ ACaccess.get('movements_get_all/1,10000/ineiti,lasj')
826
+ }.to_s
827
+ }
828
+ end
829
+
830
+ def test_archive_2
831
+ TESTBIG or return
832
+
833
+ load_big_data
834
+ Accounts.archive(1, 2012,
835
+ Accounts.get_by_path('Root::Caisses::Centre::Josué'))
836
+ end
837
+
838
+ def test_archive_3
839
+ TESTBIG or return
840
+
841
+ load_big_data
842
+ Accounts.archive(1, 2012,
843
+ Accounts.get_by_path('Root::Caisses::Centre::Rubia Centre'))
844
+ end
845
+
846
+ def test_archive_negative
847
+ setup_clean_accounts true
848
+
849
+ mov = Movements.create 'lending', '2013-01-01', 100, @loan, @income
850
+ Movements.create 'lending 2', '2013-01-01', 100, @zcash, @income
851
+
852
+ assert_equal -100, @loan.total
853
+ assert_equal @loan, mov.account_src
854
+ assert_equal 1, @loan.multiplier
855
+ Accounts.archive
856
+
857
+ @loan_2013 = Accounts.get_by_path('Archive::2013::Cash::Loan')
858
+ assert @loan_2013
859
+ assert_equal -100, @loan_2013.total
860
+ assert_equal 1, @loan_2013.multiplier
861
+ mov = Movements.find_by_desc 'lending'
862
+ assert_equal @loan_2013, mov.account_src
863
+ end
864
+
865
+ def test_archive_negative_2
866
+ setup_clean_accounts true
867
+
868
+ mov = Movements.create 'lending', '2013-01-01', 100, @loan, @zcash
869
+
870
+ assert_equal -100, @loan.total
871
+ assert_equal 1, @loan.multiplier
872
+ assert_equal @loan, mov.account_src
873
+ Accounts.archive
874
+
875
+ @loan_2013 = Accounts.get_by_path('Archive::2013::Cash::Loan')
876
+ assert @loan_2013
877
+ assert_equal -100, @loan_2013.total
878
+ assert_equal 1, @loan_2013.multiplier
879
+ mov = Movements.find_by_desc 'lending'
880
+ assert_equal @loan_2013, mov.account_src
881
+ end
882
+
883
+ def test_archive_negative_3
884
+ setup_clean_accounts true
885
+
886
+ mov = Movements.create 'lending', '2013-01-01', 100, @loan, @zcash
887
+
888
+ assert_equal -100, @loan.total
889
+ assert_equal 1, @loan.multiplier
890
+ assert_equal @loan, mov.account_src
891
+ Accounts.archive
892
+
893
+ @loan_2013 = Accounts.get_by_path('Archive::2013::Cash::Loan')
894
+ assert @loan_2013
895
+ assert_equal -100, @loan_2013.total
896
+ assert_equal 1, @loan_2013.multiplier
897
+ mov = Movements.find_by_desc 'lending'
898
+ assert_equal @loan_2013, mov.account_src
899
+ end
900
+
901
+ def test_archive_keep_root
902
+ setup_clean_accounts
903
+ Accounts.archive
904
+ Accounts.dump_raw
905
+
906
+ assert root = Accounts.get_by_path('Root')
907
+ assert_not_equal true, root.deleted
908
+ end
909
+
910
+ def test_archive_negative_4
911
+ setup_clean_accounts true
912
+ Movements.create 'lending 2012-1', '2012-02-02', 100, @loan, @income
913
+ Movements.create 'lending 2012-2', '2012-02-02', 100, @loan, @outcome
914
+
915
+ assert_equal -200, @loan.total
916
+ Accounts.archive(1, 2013)
917
+
918
+ assert income = Accounts.get_by_path('Root::Income')
919
+ assert loan = Accounts.get_by_path('Root::Cash::Loan')
920
+
921
+ mov = Movements.create 'lending 2013-1', '2013-02-02', 100, loan, income
922
+ mov = Movements.create 'lending 2013-2', '2013-02-02', 100, income, loan
923
+ Accounts.archive(1, 2014)
924
+
925
+ archive_2012 = Accounts.get_by_path('Archive::2012')
926
+ assert_equal 2, archive_2012.movements.length
927
+ end
928
+
929
+ def tes_archive_profeda
930
+ Entities.delete_all_data()
931
+ SQLite.dbs_close_all
932
+ FileUtils.cp('db.profeda', 'data/compta.db')
933
+ MigrationVersions.create(:class_name => 'Account', :version => 1)
934
+ SQLite.dbs_open_load_migrate
935
+
936
+ assert avance = Accounts.find_by_name('Avance maintenance')
937
+ #avance.dump true
938
+ dputs(1) { 'Before archiving: ' }
939
+ #Accounts.get_by_path( "Archive::2012" ).dump true
940
+ #Accounts.dump
941
+ assert_equal 1, avance.multiplier
942
+
943
+ Accounts.archive
944
+ dputs(1) { 'After archiving: ' }
945
+ assert archive_2012 = Accounts.get_by_path('Archive::2012')
946
+ archive_2012.dump true
947
+ #Accounts.dump
948
+ end
949
+
950
+ def test_get_by_path
951
+ cash = Accounts.get_by_path('Root::Cash')
952
+
953
+ assert_equal 'Cash', cash.name
954
+ end
955
+
956
+ dputs(0) { 'Disabled test_speed' }
957
+
958
+ def tes_speed
959
+ require 'rubygems'
960
+ require 'perftools'
961
+ PerfTools::CpuProfiler.start('/tmp/profile') do
962
+ (2010..2012).each { |year|
963
+ dputs(1) { "Doing year #{year}" }
964
+ (1..12).each { |month|
965
+ (1..10).each { |day|
966
+ (1..1).each { |t|
967
+ date = "#{year}-#{month}-#{day}"
968
+ Movements.create("Test #{date}", date, t, @cash, @income)
969
+ }
970
+ }
971
+ }
972
+ }
973
+ end
974
+ end
975
+
976
+ def test_round
977
+ a = 2.563
978
+ assert_equal Float, a.class
979
+ assert_equal 3, a.round
980
+ assert_equal 2.6, a.round(1)
981
+ assert_equal 2.56, a.round(2)
982
+ end
983
+
984
+ def test_create_path
985
+ accounts = Accounts.search_all.size
986
+
987
+ base1 = Accounts.create_path('Root::Income::Base', 'base')
988
+ assert_equal accounts + 1, Accounts.search_all.size
989
+
990
+ base2 = Accounts.create_path('Root::Income::Base', 'base')
991
+ assert_equal accounts + 1, Accounts.search_all.size
992
+ assert_equal base1, base2
993
+
994
+ base3 = Accounts.create_path('Root::Income::Base', 'base', true)
995
+ assert_equal accounts + 2, Accounts.search_all.size
996
+ assert_not_equal base1, base3
997
+
998
+ credit = Accounts.create_path('Credit::Card', 'mastercard')
999
+ assert_equal 'Credit::Card', credit.path
1000
+ end
1001
+ end