Skip to content

Commit

Permalink
Fix crash when getting all members
Browse files Browse the repository at this point in the history
Hadn't filtered by just profiles so was crashing trying to coerce submissions into members.
  • Loading branch information
wjdp committed Jul 30, 2023
1 parent 99bf810 commit 0b96d5b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
11 changes: 8 additions & 3 deletions src/lumina/database/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,17 @@ class ResultNotFound(DbError):
pass


def scan_members() -> list[MemberModel]:
response = get_member_table().scan()
def get_members() -> list[MemberModel]:
options = {
"IndexName": GSI_SK,
"KeyConditionExpression": Key(MEMBER_SORT_KEY).eq(SK_PROFILE),
}
response = get_member_table().query(**options) # type: ignore
data = response["Items"]
while response.get("LastEvaluatedKey"):
response = get_member_table().scan(
ExclusiveStartKey=response["LastEvaluatedKey"]
ExclusiveStartKey=response["LastEvaluatedKey"],
**options, # type: ignore
)
data.extend(response["Items"])
return [MemberModel(**item) for item in data]
Expand Down
2 changes: 1 addition & 1 deletion src/lumina/endpoints/member.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def list_members(
"""
Read all members details. Restricted to Alumni Network managers.
"""
all_members = lumina.database.operations.scan_members()
all_members = lumina.database.operations.get_members()
return [MemberPrivateResponse.from_model(m) for m in all_members]


Expand Down
10 changes: 6 additions & 4 deletions tests/unit/lumina/database/test_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@ def test_create_member(fred_bloggs):
assert fred_bloggs.phone is None


def test_scan_members_empty():
assert operations.scan_members() == []
def test_get_members_empty():
assert operations.get_members() == []


def test_scan_members(fred_bloggs):
assert operations.scan_members() == [fred_bloggs]
def test_get_members_one(fred_bloggs):
# Put a submission to ensure we don't get it back
operations.put_submission(_make_submission(101))
assert operations.get_members() == [fred_bloggs]


def test_get_member_exists(fred_bloggs):
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/lumina/endpoints/test_member.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ def test_forbidden(self, auth_fred_bloggs):

def test_success(self, auth_admin_bloggs, snapshot):
with mock.patch(
"lumina.database.operations.scan_members",
"lumina.database.operations.get_members",
return_value=[FRED_BLOGGS, ALICE_BLOGGS],
) as mock_scan_members:
) as mock_get_members:
response = client.get("/member")
assert response.status_code == HTTPStatus.OK
assert response.json() == snapshot
assert mock_scan_members.called
assert mock_get_members.called


class TestReadMember:
Expand Down

0 comments on commit 0b96d5b

Please sign in to comment.