Skip to content

Commit 2ec538e

Browse files
author
Adrian Chang
committed
More defensive programming on input
1 parent e3a7787 commit 2ec538e

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

libs/labelbox/src/labelbox/schema/user_group.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,10 @@ def get(self) -> "UserGroup":
110110
111111
Raises:
112112
ResourceNotFoundError: If the query fails to fetch the group information.
113+
ValueError: If the group ID is not provided.
113114
"""
115+
if not self.id:
116+
raise ValueError("Group id is required")
114117
query = """
115118
query GetUserGroupPyApi($id: ID!) {
116119
userGroup(where: {id: $id}) {
@@ -156,7 +159,12 @@ def update(self) -> "UserGroup":
156159
Raises:
157160
ResourceNotFoundError: If the update fails due to unknown user group
158161
UnprocessableEntityError: If the update fails due to a malformed input
162+
ValueError: If the group id or name is not provided
159163
"""
164+
if not self.id:
165+
raise ValueError("Group id is required")
166+
if not self.name:
167+
raise ValueError("Group name is required")
160168
query = """
161169
mutation UpdateUserGroupPyApi($id: ID!, $name: String!, $color: String!, $projectIds: [String!]!, $userIds: [String!]!) {
162170
updateUserGroup(

libs/labelbox/tests/unit/schema/test_user_group.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ def test_constructor(self):
6565
assert len(group.projects) == 0
6666
assert len(group.users) == 0
6767

68+
def test_update_with_exception_name(self):
69+
group = self.group
70+
group.id = ""
71+
72+
with pytest.raises(ValueError):
73+
group.get()
74+
6875
def test_get(self):
6976
projects = [
7077
{
@@ -170,6 +177,20 @@ def test_update_resource_error_unknown_id(self):
170177
with pytest.raises(ResourceNotFoundError) as e:
171178
group.update()
172179

180+
def test_update_with_exception_name(self):
181+
group = self.group
182+
group.name = ""
183+
184+
with pytest.raises(ValueError):
185+
group.update()
186+
187+
def test_update_with_exception_name(self):
188+
group = self.group
189+
group.id = ""
190+
191+
with pytest.raises(ValueError):
192+
group.update()
193+
173194
def test_create_with_exception_id(self):
174195
group = self.group
175196
group.id = "group_id"

0 commit comments

Comments
 (0)