forked from routablehq/python-quickbooks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_mixins.py
More file actions
383 lines (294 loc) · 13.6 KB
/
test_mixins.py
File metadata and controls
383 lines (294 loc) · 13.6 KB
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
import os
import unittest
from future.moves.urllib.parse import quote
from quickbooks.objects import Bill, Invoice
from tests.integration.test_base import QuickbooksUnitTestCase
try:
from mock import patch
except ImportError:
from unittest.mock import patch
from quickbooks import client
from quickbooks.objects.base import PhoneNumber, QuickbooksBaseObject
from quickbooks.objects.department import Department
from quickbooks.objects.customer import Customer
from quickbooks.objects.journalentry import JournalEntry, JournalEntryLine
from quickbooks.objects.salesreceipt import SalesReceipt
from quickbooks.mixins import ObjectListMixin
class ToJsonMixinTest(unittest.TestCase):
def test_to_json(self):
phone = PhoneNumber()
phone.FreeFormNumber = "555-555-5555"
json = phone.to_json()
self.assertEquals(json, '{\n "FreeFormNumber": "555-555-5555"\n}')
class FromJsonMixinTest(unittest.TestCase):
def setUp(self):
self.json_data = {
'DocNumber': '123',
'TotalAmt': 100,
'Line': [
{
"Id": "0",
"Description": "Test",
"Amount": 25.54,
"DetailType": "JournalEntryLineDetail",
"JournalEntryLineDetail": {
"PostingType": "Debit",
}
},
],
}
def test_from_json(self):
entry = JournalEntry()
new_obj = entry.from_json(self.json_data)
self.assertEquals(type(new_obj), JournalEntry)
self.assertEquals(new_obj.DocNumber, "123")
self.assertEquals(new_obj.TotalAmt, 100)
line = new_obj.Line[0]
self.assertEquals(type(line), JournalEntryLine)
self.assertEquals(line.Description, "Test")
self.assertEquals(line.Amount, 25.54)
self.assertEquals(line.DetailType, "JournalEntryLineDetail")
self.assertEquals(line.JournalEntryLineDetail.PostingType, "Debit")
def test_from_json_missing_detail_object(self):
test_obj = QuickbooksBaseObject()
new_obj = test_obj.from_json(self.json_data)
self.assertEquals(type(new_obj), QuickbooksBaseObject)
self.assertEquals(new_obj.DocNumber, "123")
self.assertEquals(new_obj.TotalAmt, 100)
class ToDictMixinTest(unittest.TestCase):
def test_to_dict(self):
json_data = {
'DocNumber': '123',
'TotalAmt': 100,
'Line': [
{
"Id": "0",
"Description": "Test",
"Amount": 25.54,
"DetailType": "JournalEntryLineDetail",
"JournalEntryLineDetail": {
"PostingType": "Debit",
}
},
],
}
entry = JournalEntry.from_json(json_data)
expected = {
'DocNumber': '123',
'SyncToken': 0,
'domain': 'QBO',
'TxnDate': '',
'TotalAmt': 100,
'ExchangeRate': 1,
'CurrencyRef': None,
'PrivateNote': '',
'sparse': False,
'Line': [{
'LinkedTxn': [],
'Description': 'Test',
'JournalEntryLineDetail': {
'TaxAmount': 0,
'Entity': None,
'DepartmentRef': None,
'TaxCodeRef': None,
'BillableStatus': None,
'TaxApplicableOn': 'Sales',
'PostingType': 'Debit',
'AccountRef': None,
'ClassRef': None,
},
'DetailType': 'JournalEntryLineDetail',
'LineNum': 0,
'Amount': 25.54,
'CustomField': [],
'Id': '0',
}],
'Adjustment': False,
'Id': None,
'TxnTaxDetail': None,
}
self.assertEquals(expected, entry.to_dict())
class ListMixinTest(QuickbooksUnitTestCase):
@patch('quickbooks.mixins.ListMixin.where')
def test_all(self, where):
Department.all()
where.assert_called_once_with('', order_by='', max_results=100, start_position='', qb=None)
def test_all_with_qb(self):
with patch.object(self.qb_client, 'query') as query:
Department.all(qb=self.qb_client)
self.assertTrue(query.called)
@patch('quickbooks.mixins.ListMixin.where')
def test_filter(self, where):
Department.filter(max_results=25, start_position='1', Active=True)
where.assert_called_once_with("Active = True", max_results=25, start_position='1',
order_by='', qb=None)
def test_filter_with_qb(self):
with patch.object(self.qb_client, 'query') as query:
Department.filter(Active=True, qb=self.qb_client)
self.assertTrue(query.called)
@patch('quickbooks.mixins.ListMixin.query')
def test_where(self, query):
Department.where("Active=True", start_position=1, max_results=10)
query.assert_called_once_with("SELECT * FROM Department WHERE Active=True STARTPOSITION 1 MAXRESULTS 10",
qb=None)
@patch('quickbooks.mixins.ListMixin.query')
def test_where_start_position_0(self, query):
Department.where("Active=True", start_position=0, max_results=10)
query.assert_called_once_with("SELECT * FROM Department WHERE Active=True STARTPOSITION 0 MAXRESULTS 10",
qb=None)
def test_where_with_qb(self):
with patch.object(self.qb_client, 'query') as query:
Department.where("Active=True", start_position=1, max_results=10, qb=self.qb_client)
self.assertTrue(query.called)
@patch('quickbooks.mixins.QuickBooks.query')
def test_query(self, query):
select = "SELECT * FROM Department WHERE Active=True"
Department.query(select)
query.assert_called_once_with(select)
def test_query_with_qb(self):
with patch.object(self.qb_client, 'query') as query:
select = "SELECT * FROM Department WHERE Active=True"
Department.query(select, qb=self.qb_client)
self.assertTrue(query.called)
@patch('quickbooks.mixins.ListMixin.where')
def test_choose(self, where):
Department.choose(['name1', 'name2'], field="Name")
where.assert_called_once_with("Name in ('name1', 'name2')", qb=None)
def test_choose_with_qb(self):
with patch.object(self.qb_client, 'query') as query:
Department.choose(['name1', 'name2'], field="Name", qb=self.qb_client)
self.assertTrue(query.called)
@patch('quickbooks.mixins.QuickBooks.query')
def test_count(self, query):
count = Department.count(where_clause="Active=True", qb=self.qb_client)
query.assert_called_once_with("SELECT COUNT(*) FROM Department WHERE Active=True")
@patch('quickbooks.mixins.ListMixin.query')
def test_order_by(self, query):
Customer.filter(Active=True, order_by='DisplayName')
query.assert_called_once_with("SELECT * FROM Customer WHERE Active = True ORDERBY DisplayName", qb=None)
def test_order_by_with_qb(self):
with patch.object(self.qb_client, 'query') as query:
Customer.filter(Active=True, order_by='DisplayName', qb=self.qb_client)
self.assertTrue(query.called)
class ReadMixinTest(QuickbooksUnitTestCase):
@patch('quickbooks.mixins.QuickBooks.get_single_object')
def test_get(self, get_single_object):
Department.get(1)
get_single_object.assert_called_once_with("Department", pk=1)
def test_get_with_qb(self):
with patch.object(self.qb_client, 'get_single_object') as get_single_object:
Department.get(1, qb=self.qb_client)
self.assertTrue(get_single_object.called)
class UpdateMixinTest(QuickbooksUnitTestCase):
@patch('quickbooks.mixins.QuickBooks.create_object')
def test_save_create(self, create_object):
department = Department()
department.save(qb=self.qb_client)
create_object.assert_called_once_with("Department", department.to_json())
def test_save_create_with_qb(self):
with patch.object(self.qb_client, 'create_object') as create_object:
department = Department()
department.save(qb=self.qb_client)
self.assertTrue(create_object.called)
@patch('quickbooks.mixins.QuickBooks.update_object')
def test_save_update(self, update_object):
department = Department()
department.Id = 1
json = department.to_json()
department.save(qb=self.qb_client)
update_object.assert_called_once_with("Department", json)
def test_save_update_with_qb(self):
with patch.object(self.qb_client, 'update_object') as update_object:
department = Department()
department.Id = 1
json = department.to_json()
department.save(qb=self.qb_client)
self.assertTrue(update_object.called)
class DownloadPdfTest(QuickbooksUnitTestCase):
@patch('quickbooks.client.QuickBooks.download_pdf')
def test_download_invoice(self, download_pdf):
receipt = SalesReceipt()
receipt.Id = "1"
receipt.download_pdf(self.qb_client)
download_pdf.assert_called_once_with('SalesReceipt', "1")
def test_download_missing_id(self):
from quickbooks.exceptions import QuickbooksException
receipt = SalesReceipt()
self.assertRaises(QuickbooksException, receipt.download_pdf)
class ObjectListTest(unittest.TestCase):
def setUp(self):
class TestSubclass(ObjectListMixin):
def __init__(self, obj_list):
super(TestSubclass, self).__init__()
self._object_list = obj_list
self.TestSubclass = TestSubclass
def test_object_list_mixin_with_primitives(self):
test_primitive_list = [1, 2, 3]
test_subclass_primitive_obj = self.TestSubclass(test_primitive_list)
self.assertEquals(test_primitive_list, test_subclass_primitive_obj[:])
for index in range(0, len(test_subclass_primitive_obj)):
self.assertEquals(test_primitive_list[index], test_subclass_primitive_obj[index])
for prim in test_subclass_primitive_obj:
self.assertEquals(True, prim in test_subclass_primitive_obj)
self.assertEquals(3, test_subclass_primitive_obj.pop())
test_subclass_primitive_obj.append(4)
self.assertEquals([1, 2, 4], test_subclass_primitive_obj[:])
test_subclass_primitive_obj[0] = 5
self.assertEquals([5, 2, 4], test_subclass_primitive_obj[:])
del test_subclass_primitive_obj[0]
self.assertEquals([2, 4], test_subclass_primitive_obj[:])
self.assertEquals([4, 2], list(reversed(test_subclass_primitive_obj)))
def test_object_list_mixin_with_qb_objects(self):
pn1, pn2, pn3, pn4, pn5 = PhoneNumber(), PhoneNumber(), PhoneNumber(), PhoneNumber(), PhoneNumber()
test_object_list = [pn1, pn2, pn3]
test_subclass_object_obj = self.TestSubclass(test_object_list)
self.assertEquals(test_object_list, test_subclass_object_obj[:])
for index in range (0, len(test_subclass_object_obj)):
self.assertEquals(test_object_list[index], test_subclass_object_obj[index])
for obj in test_subclass_object_obj:
self.assertEquals(True, obj in test_subclass_object_obj)
self.assertEquals(pn3, test_subclass_object_obj.pop())
test_subclass_object_obj.append(pn4)
self.assertEquals([pn1, pn2, pn4], test_subclass_object_obj[:])
test_subclass_object_obj[0] = pn5
self.assertEquals([pn5, pn2, pn4], test_subclass_object_obj[:])
del test_subclass_object_obj[0]
self.assertEquals([pn2, pn4], test_subclass_object_obj[:])
self.assertEquals([pn4, pn2], list(reversed(test_subclass_object_obj)))
class DeleteMixinTest(QuickbooksUnitTestCase):
def test_delete_unsaved_exception(self):
from quickbooks.exceptions import QuickbooksException
bill = Bill()
self.assertRaises(QuickbooksException, bill.delete, qb=self.qb_client)
@patch('quickbooks.mixins.QuickBooks.delete_object')
def test_delete(self, delete_object):
bill = Bill()
bill.Id = 1
bill.delete(qb=self.qb_client)
self.assertTrue(delete_object.called)
class SendMixinTest(QuickbooksUnitTestCase):
@patch('quickbooks.mixins.QuickBooks.misc_operation')
def test_send(self, mock_misc_op):
invoice = Invoice()
invoice.Id = 2
invoice.send(qb=self.qb_client)
mock_misc_op.assert_called_with("invoice/2/send", None, 'application/octet-stream')
@patch('quickbooks.mixins.QuickBooks.misc_operation')
def test_send_with_send_to_email(self, mock_misc_op):
invoice = Invoice()
invoice.Id = 2
email = "test@email.com"
send_to_email = quote(email, safe='')
invoice.send(qb=self.qb_client, send_to=email)
mock_misc_op.assert_called_with("invoice/2/send?sendTo={}".format(send_to_email), None, 'application/octet-stream')
class VoidMixinTest(QuickbooksUnitTestCase):
@patch('quickbooks.mixins.QuickBooks.post')
def test_void(self, post):
invoice = Invoice()
invoice.Id = 2
invoice.void(qb=self.qb_client)
self.assertTrue(post.called)
def test_delete_unsaved_exception(self):
from quickbooks.exceptions import QuickbooksException
invoice = Invoice()
self.assertRaises(QuickbooksException, invoice.void, qb=self.qb_client)