Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: auto sort by to fix duplicate result bug #100

Merged
merged 1 commit into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pysnc/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ def generate_query(self, encoded_query=None, order_by=None) -> str:
query = '^'.join(filter(None, (query, encoded_query)))
if order_by:
query = '^'.join((query, order_by))
# dont start with ^
if query.startswith('^'):
query = query[1:]
return query


Expand Down
6 changes: 3 additions & 3 deletions pysnc/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ def __init__(self, client: 'ServiceNowClient', table: str, batch_size=500):
self.__total: Optional[int] = None
self.__limit: Optional[int] = None
self.__page: int = -1
self.__order: Optional[str] = None
self.__order: str = "ORDERBYsys_id" # we *need* a default order in the event we page, see issue#96
self.__is_new_record: bool = False
self.__display_value: Union[bool, str] = 'all'

Expand Down Expand Up @@ -427,7 +427,7 @@ def order_by(self, column: str):
if column:
self.__order = "ORDERBY%s" % column
else:
self.__order = None
self.__order = "ORDERBYsys_id"

def order_by_desc(self, column: str):
"""
Expand All @@ -438,7 +438,7 @@ def order_by_desc(self, column: str):
if column:
self.__order = "ORDERBYDESC%s" % column
else:
self.__order = None
self.__order = 'ORDERBYDESCsys_id'

def pop_record(self) -> 'GlideRecord':
"""
Expand Down
2 changes: 1 addition & 1 deletion test/test_snc_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def test_link_list(self):
gr.add_query("name","CONTAINS","a")
link = gr.get_link_list()
print(link)
self.assertTrue(link.endswith('sys_user_list.do?sysparm_query=active%3Dtrue%5EnameCONTAINSa'))
self.assertTrue(link.endswith('sys_user_list.do?sysparm_query=active%3Dtrue%5EnameCONTAINSa%5EORDERBYsys_id'))
client.session.close()


Expand Down
14 changes: 7 additions & 7 deletions test/test_snc_api_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ def test_get_query(self):
o = gr.add_query('name', 'alm_asset')
o.add_or_condition('name', 'bsm_chart')
enc_query = gr.get_encoded_query()
self.assertEqual(enc_query, 'name=alm_asset^ORname=bsm_chart')
self.assertEqual(enc_query, 'name=alm_asset^ORname=bsm_chart^ORDERBYsys_id')
client.session.close()

def test_get_query_two(self):
client = ServiceNowClient(self.c.server, self.c.credentials)
gr = client.GlideRecord('sys_user')
gr.get('6816f79cc0a8016401c5a33be04be441')
enc_query = gr.get_encoded_query()
self.assertEqual(enc_query, '')
self.assertEqual(enc_query, 'ORDERBYsys_id') # always have default orderby
client.session.close()

def test_null_query(self):
Expand Down Expand Up @@ -102,14 +102,14 @@ def test_double_query(self):
gr.add_encoded_query('test=what')

query = gr.get_encoded_query()
self.assertEqual(query, "active=true^test=what")
self.assertEqual(query, "active=true^test=what^ORDERBYsys_id")

gr = client.GlideRecord('sys_user')
gr.add_encoded_query('test=what')
gr.add_query('active','true')

query = gr.get_encoded_query()
self.assertEqual(query, "active=true^test=what")
self.assertEqual(query, "active=true^test=what^ORDERBYsys_id")
client.session.close()

def test_get_true(self):
Expand Down Expand Up @@ -176,12 +176,12 @@ def test_code_query_one(self):
q.add_query('sys_id', '6816f79cc0a8016401c5a33be04be441')
q.add_query('second', 'asdf')
self.assertEqual(q.generate_query(), 'sys_id=6816f79cc0a8016401c5a33be04be441^second=asdf')
self.assertEqual(gr.get_encoded_query(), '')
self.assertEqual(gr.get_encoded_query(), 'ORDERBYsys_id')
gr.query(q)
self.assertEqual(len(gr), 1)
self.assertEqual(gr.get_encoded_query(), '')
self.assertEqual(gr.get_encoded_query(), 'ORDERBYsys_id')
gr.add_encoded_query(q.generate_query())
self.assertEqual(gr.get_encoded_query(), 'sys_id=6816f79cc0a8016401c5a33be04be441^second=asdf')
self.assertEqual(gr.get_encoded_query(), 'sys_id=6816f79cc0a8016401c5a33be04be441^second=asdf^ORDERBYsys_id')
gr.query()
self.assertEqual(len(gr), 1)

Expand Down
10 changes: 10 additions & 0 deletions test/test_snc_batching.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,14 @@ def test_default_limit(self):
self.assertEqual(params['sysparm_limit'], 100, "batch size still 100 if we have a limit over batch size")
client.session.close()

def test_default_order(self):
client = ServiceNowClient(self.c.server, self.c.credentials)
gr = client.GlideRecord('problem')

self.assertEqual(gr._parameters()['sysparm_query'], 'ORDERBYsys_id')
gr.order_by('number')
self.assertEqual(gr._parameters()['sysparm_query'], 'ORDERBYnumber')

gr.order_by(None)
self.assertEqual(gr._parameters()['sysparm_query'], 'ORDERBYsys_id')
client.session.close()
Loading