Skip to content

Commit 59de366

Browse files
committed
Add a util for extracting the chosen archives from the database
1 parent 47ae609 commit 59de366

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

code_submitter/utils.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from typing import Dict
2+
3+
import databases
4+
from sqlalchemy.sql import select
5+
6+
from .tables import Archive, ChoiceHistory
7+
8+
9+
async def get_chosen_submissions(database: databases.Database) -> Dict[str, bytes]:
10+
"""
11+
Return a mapping of teams to their the chosen archive.
12+
"""
13+
14+
# Note: Ideally we'd group by team in SQL, however that doesn't seem to work
15+
# properly -- we don't get the ordering applied before the grouping.
16+
17+
rows = await database.fetch_all(
18+
select([
19+
Archive.c.team,
20+
Archive.c.content,
21+
ChoiceHistory.c.created,
22+
]).select_from(
23+
Archive.join(ChoiceHistory),
24+
).order_by(
25+
Archive.c.team,
26+
ChoiceHistory.c.created.asc(),
27+
),
28+
)
29+
30+
# Rely on later keys replacing earlier occurrences of the same key.
31+
return {x['team']: x['content'] for x in rows}

tests/tests_utils.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import datetime
2+
3+
import test_utils
4+
from code_submitter import utils
5+
from code_submitter.tables import Archive, ChoiceHistory
6+
7+
8+
class UtilsTests(test_utils.InTransactionTestCase):
9+
def setUp(self) -> None:
10+
super().setUp()
11+
12+
self.await_(self.database.execute(
13+
Archive.insert().values(
14+
id=8888888888,
15+
content=b'8888888888',
16+
username='someone_else',
17+
team='ABC',
18+
created=datetime.datetime(2020, 8, 8, 12, 0),
19+
),
20+
))
21+
self.await_(self.database.execute(
22+
Archive.insert().values(
23+
id=2222222222,
24+
content=b'2222222222',
25+
username='a_colleague',
26+
team='SRZ2',
27+
created=datetime.datetime(2020, 2, 2, 12, 0),
28+
),
29+
))
30+
self.await_(self.database.execute(
31+
Archive.insert().values(
32+
id=1111111111,
33+
content=b'1111111111',
34+
username='test_user',
35+
team='SRZ2',
36+
created=datetime.datetime(2020, 1, 1, 12, 0),
37+
),
38+
))
39+
40+
def test_get_chosen_submissions_nothing_chosen(self) -> None:
41+
result = self.await_(utils.get_chosen_submissions(self.database))
42+
self.assertEqual({}, result)
43+
44+
def test_get_chosen_submissions_multiple_chosen(self) -> None:
45+
self.await_(self.database.execute(
46+
ChoiceHistory.insert().values(
47+
archive_id=8888888888,
48+
username='someone_else',
49+
created=datetime.datetime(2020, 8, 8, 12, 0),
50+
),
51+
))
52+
self.await_(self.database.execute(
53+
ChoiceHistory.insert().values(
54+
archive_id=1111111111,
55+
username='test_user',
56+
created=datetime.datetime(2020, 3, 3, 12, 0),
57+
),
58+
))
59+
self.await_(self.database.execute(
60+
ChoiceHistory.insert().values(
61+
archive_id=2222222222,
62+
username='test_user',
63+
created=datetime.datetime(2020, 2, 2, 12, 0),
64+
),
65+
))
66+
67+
result = self.await_(utils.get_chosen_submissions(self.database))
68+
self.assertEqual(
69+
{
70+
'SRZ2': b'1111111111',
71+
'ABC': b'8888888888',
72+
},
73+
result,
74+
)

0 commit comments

Comments
 (0)