Skip to content

Commit 3276182

Browse files
author
Matt Sokoloff
committed
fix cursor pagination bug
1 parent e6e4dd6 commit 3276182

File tree

3 files changed

+21
-30
lines changed

3 files changed

+21
-30
lines changed

labelbox/pagination.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def increment_page(self, results: Dict[str, Any]):
115115
self.next_cursor = results
116116

117117
def fetched_all(self) -> bool:
118-
return bool(self.next_cursor)
118+
return not self.next_cursor
119119

120120
def fetch_results(self) -> Dict[str, Any]:
121121
self.params.update({'from': self.next_cursor, 'first': _PAGE_SIZE})
@@ -141,9 +141,7 @@ def increment_page(self):
141141
self._fetched_pages += 1
142142

143143
def fetched_all(self, n_items: int) -> bool:
144-
if n_items < _PAGE_SIZE:
145-
return True
146-
return False
144+
return n_items < _PAGE_SIZE
147145

148146
def fetch_results(self) -> Dict[str, Any]:
149147
query = self.query % (self._fetched_pages * _PAGE_SIZE, _PAGE_SIZE)

tests/integration/conftest.py

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from collections import namedtuple
22
from enum import Enum
33
from datetime import datetime
4+
from labelbox.orm.db_object import experimental
45
from random import randint
56
from string import ascii_letters
67
from types import SimpleNamespace
@@ -50,29 +51,15 @@ def testing_api_key(environ: str) -> str:
5051
return os.environ["LABELBOX_TEST_API_KEY_STAGING"]
5152

5253

53-
def experimental_endpoint(fn):
54-
55-
def experimental(client, *args, **kwargs):
56-
try:
57-
client.endpoint = client.endpoint.replace("/graphql", "/_gql")
58-
return fn(client, *args, **kwargs)
59-
finally:
60-
client.endpoint = client.endpoint.replace("/_gql", "/graphql")
61-
62-
return experimental
63-
64-
65-
@experimental_endpoint
6654
def cancel_invite(client, invite_id):
6755
"""
6856
Do not use. Only for testing.
6957
"""
7058
query_str = """mutation CancelInvitePyApi($where: WhereUniqueIdInput!) {
7159
cancelInvite(where: $where) {id}}"""
72-
client.execute(query_str, {'where': {'id': invite_id}})
60+
client.execute(query_str, {'where': {'id': invite_id}}, experimental=True)
7361

7462

75-
@experimental_endpoint
7663
def get_project_invites(client, project_id):
7764
"""
7865
Do not use. Only for testing.
@@ -83,15 +70,14 @@ def get_project_invites(client, project_id):
8370
invites(from: $from, first: $first) { nodes { %s
8471
projectInvites { projectId projectRoleName } } nextCursor}}}
8572
""" % (id_param, id_param, results_query_part(Invite))
86-
return list(
87-
PaginatedCollection(client,
88-
query_str, {id_param: project_id},
89-
['project', 'invites', 'nodes'],
90-
Invite,
91-
cursor_path=['project', 'invites', 'nextCursor']))
73+
return PaginatedCollection(client,
74+
query_str, {id_param: project_id},
75+
['project', 'invites', 'nodes'],
76+
Invite,
77+
cursor_path=['project', 'invites', 'nextCursor'],
78+
experimental=True)
9279

9380

94-
@experimental_endpoint
9581
def get_invites(client):
9682
"""
9783
Do not use. Only for testing.
@@ -103,9 +89,9 @@ def get_invites(client):
10389
client,
10490
query_str, {}, ['organization', 'invites', 'nodes'],
10591
Invite,
106-
cursor_path=['organization', 'invites', 'nextCursor'])
107-
return list(
108-
invites) # list() so that it makes the request to the right endpoint.
92+
cursor_path=['organization', 'invites', 'nextCursor'],
93+
experimental=True)
94+
return invites
10995

11096

11197
@pytest.fixture
@@ -199,6 +185,10 @@ def sample_video() -> str:
199185
def organization(client):
200186
# Must have at least one seat open in your org to run these tests
201187
org = client.get_organization()
188+
# Clean up before and after incase this wasn't run for some reason.
189+
for invite in get_invites(client):
190+
if "@labelbox.com" in invite.email:
191+
cancel_invite(client, invite.uid)
202192
yield org
203193
for invite in get_invites(client):
204194
if "@labelbox.com" in invite.email:

tests/integration/test_user_management.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,19 @@ def test_org_invite(client, organization, environ, queries):
1212
raise ValueError(
1313
f"Expected tests to run against either prod or staging. Found {environ}"
1414
)
15+
1516
invite = organization.invite_user(dummy_email, role)
1617

1718
if environ.value == "prod":
19+
1820
invite_limit_after = organization.invite_limit()
1921
# One user added
2022
assert invite_limit.remaining - invite_limit_after.remaining == 1
2123
# An invite shouldn't effect the user count until after it is accepted
2224

2325
outstanding_invites = queries.get_invites(client)
2426
in_list = False
27+
2528
for invite in outstanding_invites:
2629
if invite.uid == invite.uid:
2730
in_list = True
@@ -44,7 +47,7 @@ def test_project_invite(client, organization, project_pack, queries):
4447
roles['NONE'],
4548
project_roles=[project_role_1, project_role_2])
4649

47-
project_invite = queries.get_project_invites(client, project_1.uid)[0]
50+
project_invite = next(queries.get_project_invites(client, project_1.uid))
4851

4952
assert set([(proj_invite.project.uid, proj_invite.role.uid)
5053
for proj_invite in project_invite.project_roles

0 commit comments

Comments
 (0)