-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
483 lines (457 loc) · 17.9 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
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
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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
# coding: utf-8
"""
тест
"""
import PocketClass
import PocketDB
# import os
import sqlite3
# import base64
# try:
# # For Python 3.0 and later
# import urllib as urllib2
# except ImportError:
# Fall back to Python 2's urllib2
# import urllib2 as urllib2
# from urllib2 import quote
# from httplib2 import Http
# import httplib
# import requests
# from requests.auth import HTTPBasicAuth
import unittest
class TestAll(unittest.TestCase):
# тестирование PocketDB
def test_db_init(self):
pdb = PocketDB.PocketsDB('test1_db')
self.assertFalse(pdb.check_first_start())
tbl_names = [u'Pockets',
u'Balances',
u'CreditBalances',
u'OutItems',
u'InItems',
u'Contacts',
u'Credits',
u'Currency',
u'Settings',
u'Actions',
u'InAction',
u'OutAction',
u'BetweenAction',
u'ExchangeAction',
u'Credit1InAction',
u'Credit1OutAction',
u'Credit2InAction',
u'Credit2OutAction',
u'KWArgs']
tbl_names.sort()
return_value = []
con = sqlite3.connect(pdb.db_name)
cur = con.cursor()
try:
cur.execute("""SELECT name FROM sqlite_master
WHERE type='table' AND name in (%s)
""" % (', '.join('?' for _ in tbl_names)), tbl_names
)
except sqlite3.OperationalError:
pass
for row in cur:
return_value.append(row[0])
con.close()
return_value.sort()
self.assertListEqual(tbl_names, return_value)
pdb._drops()
def test_all_actions(self):
pdb = PocketDB.PocketsDB('test2_db')
pdb.add_action(2, 3)
con = sqlite3.connect(pdb.db_name)
cur = con.cursor()
cur.execute("SELECT * FROM Actions")
for row in cur:
self.assertEquals(row[2], 2)
self.assertEquals(row[3], 3)
pocket1 = u'pocket1'
pocket2 = u'pocket2'
credit1 = u'for one day'
contact1 = u'my friend'
item1 = u'item1'
item2 = u'item2'
sum1 = 123.45
sum2 = 456.78
amount1 = 1.0
amount2 = 2.0
act_data = [[pocket1, item1, sum1, amount1, u'in'],
[pocket1, item1, sum1, amount1, u'out'],
[pocket1, pocket2, sum1, u'between'],
[pocket1, pocket2, sum1, sum2, u'exchange'],
[pocket1, credit1, sum1, sum2, u'get'],
[pocket1, credit1, sum1, sum2, amount2, u'give'],
[pocket1, credit1, sum1, sum2, amount1, u'give back'],
[pocket1, credit1, sum1, sum2, u'get back']
]
tbl_names = ['InAction',
'OutAction',
'BetweenAction',
'ExchangeAction',
'Credit1InAction',
'Credit2InAction',
'Credit1OutAction',
'Credit2OutAction']
pdb.action_in(*act_data[0])
pdb.action_out(*act_data[1])
pdb.action_between(*act_data[2])
pdb.action_exchange(*act_data[3])
pdb.action_credit1_in(*act_data[4])
pdb.action_credit2_in(*act_data[5])
pdb.action_credit1_out(*act_data[6])
pdb.action_credit2_out(*act_data[7])
# data = pdb.prepare_send_data()
# for t in data:
# for k in t:
# print k,
# print ' '
pdb._drops()
for i in range(8):
cur.execute("SELECT * FROM %s" % tbl_names[i])
for row in cur:
self.assertListEqual(act_data[i], [x for x in row[2:]])
con.close()
def test_upd_balances(self):
_name = 'Test'
_cur = u'руб'
_cont = 'My contact'
_bal = 123
_p_bal = 234.5
_c_bal = 345.6
pcs = PocketClass.Pockets('test3_db')
pdb = pcs.db
pcs.set_pocket(_name, _cur, _bal)
pcs.set_credit(_name, _cur, _cont, _bal)
pdb.recreate_refs(pcs)
pdb.upd_pocket_balance(_name, _p_bal)
pdb.upd_credit_balance(_name, _c_bal)
con = sqlite3.connect(pdb.db_name)
cur = con.cursor()
try:
cur.execute("SELECT Balance FROM Balances")
except sqlite3.OperationalError:
pass
for row in cur:
self.assertEqual(row[0], _p_bal)
try:
cur.execute("SELECT Balance FROM CreditBalances")
except sqlite3.OperationalError:
pass
for row in cur:
self.assertEqual(row[0], _c_bal)
con.close()
pdb._drops()
def test_get_data(self):
currency = [[u'руб', 11, '2'], [u'USD', 2, '3'], [u'EUR', 1, '1']]
in_items = [u'item1', u'item2', u'Item3']
out_items = [u'Item4', u'Item5', u'item6']
contacts = [u'contact 1', u'contact 2', u'contact 3']
pockets = {u'p1': [u'p1', u'руб', 123.4],
u'p2': [u'p2', u'руб', 234.5],
u'p3': [u'p3', u'USD', 345.6]}
credit_s = {u'c1': [u'c1', u'руб', u'contact 1', 456.7],
u'c2': [u'c2', u'руб', u'contact 2', 567.8],
u'c3': [u'c3', u'USD', u'contact 3', 678.9]}
pcs = PocketClass.Pockets('test4_db')
pdb = pcs.db
for i in currency:
pcs.set_cur(*i)
for i in in_items:
pcs.set_in_item(i)
for i in out_items:
pcs.set_out_item(i)
for c in contacts:
pcs.set_contact(c)
for p in pockets:
pcs.set_pocket(*pockets[p])
for c in credit_s:
pcs.set_credit(*credit_s[c])
pdb.recreate_refs(pcs)
return_val = pdb.get_currency()
return_val.sort()
currency.sort()
self.assertListEqual(return_val, currency)
return_val = pdb.get_items_in()
return_val.sort()
in_items.sort()
self.assertListEqual(return_val, in_items)
return_val = pdb.get_items_out()
return_val.sort()
out_items.sort()
self.assertListEqual(return_val, out_items)
return_val = pdb.get_contacts()
return_val.sort()
contacts.sort()
self.assertListEqual(return_val, contacts)
return_val = pdb.get_pockets()
if return_val == -1:
raise Exception('Pockets table broken.', return_val)
for p in return_val:
self.assertListEqual(p[:len(pockets[p[0]])], pockets[p[0]])
return_val = pdb.get_credits()
if return_val == -1:
raise Exception('Credits table broken.', return_val)
for c in return_val:
self.assertListEqual(c[:len(credit_s[c[0]])], credit_s[c[0]])
pdb._drops()
def test_type_convert(self):
acts = [['str', 'some text', 'some text'],
['str', False, 'False'],
['str', 123, '123'],
['int', '123', 123],
['int', '-123', -123],
['float', '-123', -123.0],
['float', '123.4', 123.4],
['long', '1234', 1234L],
['NoneType', '1234', None],
['NoneType', None, None],
['NoneType', 0, None],
['bool', 0, False],
['bool', 1, True],
['bool', '0', False],
['bool', '1', True]
]
for a in acts:
self.assertEqual(PocketDB.convert_to_type(a[1], a[0]), a[2])
def test_convert_to_str(self):
acts = [['some text', 'some text'],
[False, '0'],
[True, '1'],
[123, '123'],
[-123, '-123'],
[123.4, '123.4'],
[None, 'None'],
[0, '0'],
]
for a in acts:
self.assertEqual(PocketDB.convert_type_to_str(a[0]), a[1])
def test_dump_get_kwargs(self):
pdb = PocketDB.PocketsDB('test7_db')
kw1 = {u'Ref_Key': u"9747544e-11c8-11e4-589e-0018f3e1b84e",
u'IsFolder': False,
u'Description': u"Русские буквы",
u'Активность': True}
kw2 = {u'Ref_Key': u"44747adc-5dd5-11e3-95ac-005056c00008",
u'ВалютнаяСуммаBalance': 123.45,
u'ExtDimension1': u"Name",
u'Активность': True,
u'Пассивность': True}
kw3 = {u'Ref_Key': u"6b4785a2-5ee9-11e5-6c8d-0018f3e1b84e",
u'SomeValue': None,
u'Description': 345,
u'Активность': True}
pct1 = PocketClass.OnePocket(u'Test1', u'руб', 123, **kw1)
pct2 = PocketClass.OneCredit(u'Test2', u'руб', u'My contact', 23, **kw2)
# третий - для произвольного объекта, например статьи расхода
pdb.dump_kwargs(pct1)
pdb.dump_kwargs(pct2)
pdb.dump_kwargs('Item1', 'in_items', **kw3)
return_value = []
con = sqlite3.connect(pdb.db_name)
cur = con.cursor()
try:
cur.execute("SELECT * FROM KWArgs")
except sqlite3.OperationalError:
raise Exception('KWArgs table is wrong')
for row in cur:
return_value.append([row[1], row[2], row[3], row[4], row[5]])
con.close()
return_value.sort()
model = []
for k in pct1.kwargs:
model.append([pct1.name, pct1.__name__,
k,
PocketDB.get_type(pct1.kwargs[k]),
PocketDB.convert_type_to_str(pct1.kwargs[k])])
for k in pct2.kwargs:
model.append([pct2.name, pct2.__name__,
k,
PocketDB.get_type(pct2.kwargs[k]),
PocketDB.convert_type_to_str(pct2.kwargs[k])])
for k in kw3:
model.append([u'Item1', u'in_items',
k, PocketDB.get_type(kw3[k]),
PocketDB.convert_type_to_str(kw3[k])])
model.sort()
kw1_t = pdb.get_kwargs(pct1)
kw2_t = pdb.get_kwargs(pct2)
kw3_t = pdb.get_kwargs('Item1', 'in_items')
pdb._drops()
self.assertListEqual(model, return_value)
self.assertDictEqual(kw1, kw1_t)
self.assertDictEqual(kw2, kw2_t)
self.assertDictEqual(kw3, kw3_t)
# тестирование PocketClass
def test_one_pocket(self):
# print ('test init one pocket')
kw = {'Ref_Key': "9747544e-11c8-11e4-589e-0018f3e1b84e",
'IsFolder': False,
'Description': "Название",
'Активность': True,}
one_pcs = PocketClass.OnePocket('Test', 'руб', 123, **kw)
self.assertEqual(one_pcs.name, "Test")
self.assertEqual(one_pcs.kwargs['Description'], "Название")
self.assertEqual(one_pcs.kwargs['Ref_Key'],
"9747544e-11c8-11e4-589e-0018f3e1b84e")
self.assertEqual(one_pcs.balance, 123)
def test_one_credit(self):
# print ('test init one credit')
kw = {'Ref_Key': "9747544e-11c8-11e4-589e-0018f3e1b84e",
'IsFolder': False,
'Description': "Название",
'Активность': True,}
one_crd = PocketClass.OneCredit('Test', 'руб', 'My contact', 123, **kw)
self.assertEqual(one_crd.name, "Test")
self.assertEqual(one_crd.kwargs['Description'], "Название")
self.assertEqual(one_crd.kwargs['Ref_Key'],
"9747544e-11c8-11e4-589e-0018f3e1b84e")
self.assertEqual(one_crd.balance, 123)
self.assertEqual(one_crd.contact, 'My contact')
def test_pockets_init(self):
# print ('test init one pocket in pocketclass')
pcs = PocketClass.Pockets('test8_db')
kw = {'Ref_Key': "9747544e-11c8-11e4-589e-0018f3e1b84e",
'IsFolder': False,
'Description': "Название",
'Активность': True,}
args = ['Test', 'руб', 123]
pcs.set_pocket(*args, **kw)
self.assertEqual(pcs.pockets[0].name, "Test")
self.assertEqual(pcs.pockets[0].kwargs['Description'], "Название")
self.assertEqual(pcs.pockets[0].kwargs['Ref_Key'],
"9747544e-11c8-11e4-589e-0018f3e1b84e")
self.assertEqual(pcs.pockets[0].balance, 123)
pcs._drop_db()
def test_credits_init(self):
# print ('test init one credit in pocketclass')
pcs = PocketClass.Pockets('test9_db')
kw = {'Ref_Key': "9747544e-11c8-11e4-589e-0018f3e1b84e",
'IsFolder': False,
'Description': "Название",
'Активность': True,}
args = ['Test', 'руб', 'My contact', 123]
pcs.set_credit(*args, **kw)
self.assertEqual(pcs.credits[0].name, "Test")
self.assertEqual(pcs.credits[0].contact, "My contact")
self.assertEqual(pcs.credits[0].kwargs['Description'], "Название")
self.assertEqual(pcs.credits[0].kwargs['Ref_Key'],
"9747544e-11c8-11e4-589e-0018f3e1b84e")
self.assertEqual(pcs.credits[0].balance, 123)
pcs._drop_db()
def test_drop_pocket(self):
# print ('test drop pocket')
pcs = PocketClass.Pockets('test10_db')
args = ['Test', 'руб', 123]
pcs.set_pocket(*args)
pcs._drop_pocket('Test')
self.assertEqual(len(pcs.pockets), 0)
pcs._drop_db()
def test_drop_credit(self):
# print ('test drop credit')
pcs = PocketClass.Pockets('test11_db')
args = ['Test', 'руб', 'My contact', 123]
pcs.set_credit(*args)
pcs._drop_credit('Test')
self.assertEqual(len(pcs.credits), 0)
pcs._drop_db()
def test_get_one(self):
pcs = PocketClass.Pockets('test12_db')
args = ['Test', 'руб', 123]
pcs.set_pocket(*args)
args = ['Test', 'руб', 'My contact', 123]
pcs.set_credit(*args)
pcs._drop_db()
one = pcs.get_one('Test')
two = pcs.get_one('Test', 'OnePocket')
three = pcs.get_one('Test', 'OneCredit')
self.assertEqual(one, two)
self.assertEqual(one, pcs.pockets[0])
self.assertEqual(three, pcs.credits[0])
def test_create_fill_from_db(self):
in_items = [u'item1', u'item2', u'Item3']
out_items = [u'Item4', u'Item5', u'item6']
contacts = [u'contact 1', u'contact 2', u'contact 3']
pockets = {u'p1': [u'p1', u'руб', 123.4],
u'p2': [u'p2', u'руб', 234.5],
u'p3': [u'p3', u'USD', 345.6]}
credit_s = {u'c1': [u'c1', u'руб', u'contact 1', 456.7],
u'c2': [u'c2', u'руб', u'contact 2', 567.8],
u'c3': [u'c3', u'USD', u'contact 3', 678.9]}
kw1 = {u'Ref_Key': u"9747544e-11c8-11e4-589e-0018f3e1b84e",
u'IsFolder': False,
u'Description': u"Русские буквы",
u'Активность': True}
kw2 = {u'Ref_Key': u"44747adc-5dd5-11e3-95ac-005056c00008",
u'ВалютнаяСуммаBalance': 123.45,
u'ExtDimension1': u"Name",
u'Активность': True,
u'Пассивность': True}
settings = [u'http://google.com', u'12345678']
pcs = PocketClass.Pockets('test13_db')
for i in in_items:
pcs.set_in_item(i)
for i in out_items:
pcs.set_out_item(i)
for c in contacts:
pcs.set_contact(c)
for p in pockets:
pcs.set_pocket(*pockets[p], **kw1)
for c in credit_s:
pcs.set_credit(*credit_s[c], **kw2)
pcs.create_db()
pcs.db.reset_settings(*settings)
pcs2 = PocketClass.Pockets('test13_db')
pcs2.fill_from_db()
pcs2._drop_db()
pcs.contacts.sort()
pcs.in_items.sort()
pcs.out_items.sort()
pcs2.contacts.sort()
pcs2.in_items.sort()
pcs2.out_items.sort()
self.assertMultiLineEqual(pcs.get_all_info(), pcs2.get_all_info())
self.assertEqual(pcs.credits[0].kwargs[u'ВалютнаяСуммаBalance'],
pcs2.credits[0].kwargs[u'ВалютнаяСуммаBalance'])
self.assertEqual(type(pcs.credits[0].kwargs[u'ВалютнаяСуммаBalance']),
float)
# def test_get_o_data(self):
# pcs = PocketClass.Pockets('MyPythonMoney.db')
# pcs.get_settings()
# pcs.get_data()
#
# def test_post_o_data(self):
# pcs = PocketClass.Pockets('MyPythonMoney.db')
# pcs.get_settings()
# pcs.post_data()
# def test_odata(self):
# # print ('test odata')
# pcs = PocketClass.Pockets('MyPythonMoney.db')
# pcs.get_settings()
#
# ourl = '/odata/standard.odata'
# jformat = '/?$format=json'
# journ = urllib2.quote('ЖурналОпераций')
# reg_slice = '/AccountingRegister_%s/Balance' % journ
# # reg_slice = unicode(reg_slice, 'utf-8')
# URL = pcs.settings['URL']
# # furl = "money.kter.ru"
# # lurl = "/money/odata/standard.odata/"
# full_url = URL + ourl + reg_slice + jformat
# authorization = pcs.settings['Authorization']
#
# headers = {
# 'Authorization': authorization
# }
# req = urllib2.Request(full_url, None, headers)
# result = urllib2.urlopen(req)
#
# data = result.read()
#
# # print(data)
# self.assertEqual(1,1)
if __name__ == '__main__':
unittest.main()