Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 34f94d7

Browse files
committed
Added support for order_by kwarg on .filter() and .where()
1 parent b254493 commit 34f94d7

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

quickbooks/mixins.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,16 +163,18 @@ def all(cls, start_position="", max_results=100, qb=None):
163163
return cls.where("", start_position=start_position, max_results=max_results, qb=qb)
164164

165165
@classmethod
166-
def filter(cls, start_position="", max_results="", qb=None, **kwargs):
166+
def filter(cls, order_by="", start_position="", max_results="", qb=None, **kwargs):
167167
"""
168+
:param order_by:
168169
:param start_position:
169170
:param max_results:
170171
:param qb:
171172
:param kwargs: field names and values to filter the query
172173
:return: Filtered list
173174
"""
174175
return cls.where(build_where_clause(**kwargs),
175-
start_position=start_position, max_results=max_results, qb=qb)
176+
start_position=start_position, max_results=max_results, order_by=order_by,
177+
qb=qb)
176178

177179
@classmethod
178180
def choose(cls, choices, field="Id", qb=None):
@@ -185,9 +187,10 @@ def choose(cls, choices, field="Id", qb=None):
185187
return cls.where(build_choose_clause(choices, field), qb=qb)
186188

187189
@classmethod
188-
def where(cls, where_clause="", start_position="", max_results="", qb=None):
190+
def where(cls, where_clause="", order_by="", start_position="", max_results="", qb=None):
189191
"""
190192
:param where_clause: QBO SQL where clause (DO NOT include 'WHERE')
193+
:param order_by:
191194
:param start_position:
192195
:param max_results:
193196
:param qb:
@@ -196,14 +199,17 @@ def where(cls, where_clause="", start_position="", max_results="", qb=None):
196199
if where_clause:
197200
where_clause = "WHERE " + where_clause
198201

202+
if order_by:
203+
order_by = " ORDERBY " + order_by
204+
199205
if start_position:
200206
start_position = " STARTPOSITION " + str(start_position)
201207

202208
if max_results:
203209
max_results = " MAXRESULTS " + str(max_results)
204210

205-
select = "SELECT * FROM {0} {1}{2}{3}".format(
206-
cls.qbo_object_name, where_clause, start_position, max_results)
211+
select = "SELECT * FROM {0} {1}{2}{3}{4}".format(
212+
cls.qbo_object_name, where_clause, order_by, start_position, max_results)
207213

208214
return cls.query(select, qb=qb)
209215

tests/unit/test_mixins.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
from quickbooks.objects.base import PhoneNumber, QuickbooksBaseObject
1515
from quickbooks.objects.department import Department
16+
from quickbooks.objects.customer import Customer
1617
from quickbooks.objects.journalentry import JournalEntry, JournalEntryLine
1718
from quickbooks.objects.salesreceipt import SalesReceipt
1819
from quickbooks.mixins import ObjectListMixin
@@ -157,7 +158,8 @@ def test_all_with_qb(self):
157158
@patch('quickbooks.mixins.ListMixin.where')
158159
def test_filter(self, where):
159160
Department.filter(max_results=25, start_position='1', Active=True)
160-
where.assert_called_once_with("Active = True", max_results=25, start_position='1', qb=None)
161+
where.assert_called_once_with("Active = True", max_results=25, start_position='1',
162+
order_by='', qb=None)
161163

162164
def test_filter_with_qb(self):
163165
with patch.object(self.qb_client, 'query') as query:
@@ -166,13 +168,13 @@ def test_filter_with_qb(self):
166168

167169
@patch('quickbooks.mixins.ListMixin.query')
168170
def test_where(self, query):
169-
Department.where("Active=True", 1, 10)
171+
Department.where("Active=True", start_position=1, max_results=10)
170172
query.assert_called_once_with("SELECT * FROM Department WHERE Active=True STARTPOSITION 1 MAXRESULTS 10",
171173
qb=None)
172174

173175
def test_where_with_qb(self):
174176
with patch.object(self.qb_client, 'query') as query:
175-
Department.where("Active=True", 1, 10, qb=self.qb_client)
177+
Department.where("Active=True", start_position=1, max_results=10, qb=self.qb_client)
176178
self.assertTrue(query.called)
177179

178180
@patch('quickbooks.mixins.QuickBooks.query')
@@ -202,6 +204,16 @@ def test_count(self, query):
202204
count = Department.count(where_clause="Active=True", qb=self.qb_client)
203205
query.assert_called_once_with("SELECT COUNT(*) FROM Department WHERE Active=True")
204206

207+
@patch('quickbooks.mixins.ListMixin.query')
208+
def test_order_by(self, query):
209+
Customer.filter(Active=True, order_by='DisplayName')
210+
query.assert_called_once_with("SELECT * FROM Customer WHERE Active = True ORDERBY DisplayName", qb=None)
211+
212+
def test_order_by_with_qb(self):
213+
with patch.object(self.qb_client, 'query') as query:
214+
Customer.filter(Active=True, order_by='DisplayName', qb=self.qb_client)
215+
self.assertTrue(query.called)
216+
205217

206218
class ReadMixinTest(unittest.TestCase):
207219
def setUp(self):

0 commit comments

Comments
 (0)