Skip to content

Commit

Permalink
Merge branch 'main' into CONCD-746-rsar-quick-tips
Browse files Browse the repository at this point in the history
  • Loading branch information
rasarkar committed Mar 25, 2024
2 parents b68d0ae + 467ba23 commit 695d967
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
35 changes: 18 additions & 17 deletions concordia/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,17 +491,13 @@ def _asset_reservation_test_payload(self, user_id, anonymous=False):
# Acquire the reservation: 1 acquire
# + 1 reservation check
# + 1 session if not anonymous and using a database:
expected_update_queries = 2
if not anonymous and settings.SESSION_ENGINE.endswith("db"):
expected_acquire_queries = 3
else:
expected_acquire_queries = 2

# Release the reservation:
# 1 release + 1 session if not anonymous and using a database:
if not anonymous and settings.SESSION_ENGINE.endswith("db"):
expected_release_queries = 2
else:
expected_release_queries = 1
expected_update_queries += 1
expected_acquire_queries = expected_update_queries
if not anonymous:
# + 1 get user ID from request
expected_acquire_queries += 1

with self.assertNumQueries(expected_acquire_queries):
resp = self.client.post(reverse("reserve-asset", args=(asset.pk,)))
Expand All @@ -513,7 +509,7 @@ def _asset_reservation_test_payload(self, user_id, anonymous=False):

# Confirm that an update did not change the pk when it updated the timestamp:

with self.assertNumQueries(expected_acquire_queries):
with self.assertNumQueries(expected_update_queries):
resp = self.client.post(reverse("reserve-asset", args=(asset.pk,)))
data = self.assertValidJSON(resp, expected_status=200)
self.assertEqual(1, AssetTranscriptionReservation.objects.count())
Expand All @@ -526,6 +522,11 @@ def _asset_reservation_test_payload(self, user_id, anonymous=False):
self.assertLess(reservation.created_on, updated_reservation.updated_on)

# Release the reservation now that we're done:
# 1 release + 1 session if not anonymous and using a database:
if not anonymous and settings.SESSION_ENGINE.endswith("db"):
expected_release_queries = 2
else:
expected_release_queries = 1

with self.assertNumQueries(expected_release_queries):
resp = self.client.post(
Expand Down Expand Up @@ -559,8 +560,8 @@ def test_asset_reservation_competition(self):
self.client.logout()
self.login_user()

# 1 session check + 1 acquire
with self.assertNumQueries(2 if settings.SESSION_ENGINE.endswith("db") else 1):
# 1 session check + 1 acquire + get user ID from request
with self.assertNumQueries(3 if settings.SESSION_ENGINE.endswith("db") else 2):
resp = self.client.post(reverse("reserve-asset", args=(asset.pk,)))
self.assertEqual(409, resp.status_code)
self.assertEqual(1, AssetTranscriptionReservation.objects.count())
Expand All @@ -586,11 +587,11 @@ def test_asset_reservation_expiration(self):

self.login_user()

# 1 session check + 1 reservation check + 1 acquire
# 1 reservation check + 1 acquire + 1 get user ID from request
expected_queries = 3
if settings.SESSION_ENGINE.endswith("db"):
expected_queries = 3
else:
expected_queries = 2
# 1 session check
expected_queries += 1

with self.assertNumQueries(expected_queries):
resp = self.client.post(reverse("reserve-asset", args=(asset.pk,)))
Expand Down
7 changes: 5 additions & 2 deletions concordia/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@ def request_accepts_json(request):
def get_or_create_reservation_token(request):
if "reservation_token" not in request.session:
request.session["reservation_token"] = token_hex(22)
user = request.user
user = getattr(request, "user", None)
if user is not None:
uid = user.id
if uid is None:
uid = get_anonymous_user().id
# We should probably call get_anonymous_user,
# but I'm going to avoid doing so since it would
# incur yet *another* database query
uid = 8
request.session["reservation_token"] += str(uid).zfill(6)
return request.session["reservation_token"]

Expand Down

0 comments on commit 695d967

Please sign in to comment.