Skip to content

Commit f88292f

Browse files
committed
[graphene] Fix default order_by handling in queries
In newer versions of Graphene, when an input is passed as null, it overwrites the argument’s default value as None. This commit updates the code to fix this issue and support a future change that will be made in a future version in which the value will be Undefined when the filter is not defined. Signed-off-by: Jose Javier Merchante <[email protected]>
1 parent c775f0c commit f88292f

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

sortinghat/core/schema.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1754,6 +1754,9 @@ def resolve_organizations(self, info, filters=None,
17541754
page_size=settings.SORTINGHAT_API_PAGE_SIZE,
17551755
order_by='name',
17561756
**kwargs):
1757+
if not order_by:
1758+
order_by = 'name'
1759+
17571760
query = Organization.objects.all_organizations().annotate(enrollments_count=Count('enrollments')
17581761
).order_by(to_snake_case(order_by))
17591762

@@ -1817,6 +1820,9 @@ def resolve_individuals(self, info, filters=None,
18171820
page_size=settings.SORTINGHAT_API_PAGE_SIZE,
18181821
order_by='mk',
18191822
**kwargs):
1823+
if not order_by:
1824+
order_by = 'mk'
1825+
18201826
query = Individual.objects.annotate(identities_count=Count('identities')).order_by(to_snake_case(order_by))
18211827

18221828
if filters and 'uuid' in filters:

tests/test_schema.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1924,6 +1924,38 @@ def test_order_by_enrollments(self):
19241924
org = orgs[2]
19251925
self.assertEqual(org['name'], org3.name)
19261926

1927+
def test_order_by_null(self):
1928+
"""Check whether it returns the organizations ordered by default (name)"""
1929+
1930+
indv1 = Individual.objects.create(mk='a9b403e150dd4af8953a52a4bb841051e4b705d9')
1931+
indv2 = Individual.objects.create(mk='0010a5211c03c46d340ada434b9f5b5072a8d491')
1932+
indv3 = Individual.objects.create(mk='86172d829d61adabde125d2442093213f745fbfd')
1933+
1934+
org1 = Organization.add_root(name='Example')
1935+
Enrollment.objects.create(individual=indv1, group=org1)
1936+
Enrollment.objects.create(individual=indv2, group=org1)
1937+
Enrollment.objects.create(individual=indv3, group=org1)
1938+
1939+
org2 = Organization.add_root(name='Bitergia')
1940+
Enrollment.objects.create(individual=indv1, group=org2)
1941+
Enrollment.objects.create(individual=indv2, group=org2)
1942+
1943+
org3 = Organization.add_root(name='LibreSoft')
1944+
1945+
# Test default order by null
1946+
client = graphene.test.Client(schema)
1947+
executed = client.execute("""{ organizations (orderBy: null){ entities { name } } }""",
1948+
context_value=self.context_value)
1949+
1950+
orgs = executed['data']['organizations']['entities']
1951+
1952+
org = orgs[0]
1953+
self.assertEqual(org['name'], org2.name)
1954+
org = orgs[1]
1955+
self.assertEqual(org['name'], org1.name)
1956+
org = orgs[2]
1957+
self.assertEqual(org['name'], org3.name)
1958+
19271959
def test_pagination(self):
19281960
"""Check whether it returns the organizations searched when using pagination"""
19291961

@@ -4295,6 +4327,29 @@ def test_order_by_identities_count(self):
42954327
indv = individuals[2]
42964328
self.assertEqual(indv['mk'], indv1.mk)
42974329

4330+
def test_order_by_null(self):
4331+
"""Check whether it returns the indifiduals sorted by default (mk) when ordering by null"""
4332+
4333+
indv1 = Individual.objects.create(mk='aaaa03e150dd4af8953a52a4bb841051e4b705d9')
4334+
indv2 = Individual.objects.create(mk='cccc4b709e5446d250b4fde0e34b78a2b4fde0e3')
4335+
indv3 = Individual.objects.create(mk='bbbbde0e34b78a185c4b709e5442d045451c')
4336+
Profile.objects.create(name='AA', individual=indv1)
4337+
Profile.objects.create(name='ZZ', individual=indv2)
4338+
Profile.objects.create(name='MM', individual=indv3)
4339+
4340+
client = graphene.test.Client(schema)
4341+
executed = client.execute("""{ individuals(orderBy: null) { entities { mk } } }""",
4342+
context_value=self.context_value)
4343+
4344+
individuals = executed['data']['individuals']['entities']
4345+
4346+
indv = individuals[0]
4347+
self.assertEqual(indv['mk'], indv1.mk)
4348+
indv = individuals[1]
4349+
self.assertEqual(indv['mk'], indv3.mk)
4350+
indv = individuals[2]
4351+
self.assertEqual(indv['mk'], indv2.mk)
4352+
42984353
def test_pagination(self):
42994354
"""Check whether it returns the individuals searched when using pagination"""
43004355

0 commit comments

Comments
 (0)