Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move replay model ranking tests to their own class. #514

Merged
merged 2 commits into from
Dec 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
282 changes: 148 additions & 134 deletions project/thscoreboard/replays/test_models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import datetime
import logging
from unittest.mock import patch
from unittest import mock

from django.db import utils

Expand All @@ -17,6 +17,153 @@
REPLAY_2 = test_replays.GetRaw("th10_normal")


class ReplayRankTest(test_case.ReplayTestCase):
def setUp(self):
super().setUp()
self.author = self.createUser("author")
self.viewer = self.createUser("viewer")

# Ease testing by artificially giving every replay a different unique hash,
# so we can reuse the same filename in tests.
self._next_id = 0
self._mock_calculate_replay_hash = self.enterContext(
mock.patch.object(constant_helpers, "CalculateReplayFileHash")
)

def _ReturnIncrementing(replay_file_contents):
del replay_file_contents
val = self._next_id
self._next_id += 1
return val.to_bytes(length=4)

self._mock_calculate_replay_hash.side_effect = _ReturnIncrementing

def testRanks(self):
test_replays.CreateAsPublishedReplay(
filename="th6_extra",
user=self.author,
score=1_000_000_000,
)
test_replays.CreateAsPublishedReplay(
filename="th6_extra",
user=self.author,
score=900_000_000,
)
test_replays.CreateAsPublishedReplay(
filename="th7_extra",
user=self.author,
score=800_000_000,
)

replays = (
models.Replay.objects.order_by("-score").select_related("rank_view").all()
)

self.assertEqual(replays[0].GetRank(), 1)
self.assertEqual(replays[1].GetRank(), 2)
self.assertEqual(replays[2].GetRank(), 1)

def testRanksTasReplay(self):
test_replays.CreateAsPublishedReplay(
filename="th6_extra",
user=self.author,
score=1_000_000_000,
category=models.Category.TAS,
)

replay = models.Replay.objects.order_by("-score").first()

self.assertIsNone(replay.GetRank())

def testRanksBreakTiesUsingUploadDate(self):
test_replays.CreateAsPublishedReplay(
filename="th17_lunatic",
user=self.author,
score=9_999_999_990,
created_timestamp=datetime.datetime(
2001, 1, 1, tzinfo=datetime.timezone.utc
),
)
test_replays.CreateAsPublishedReplay(
filename="th17_lunatic",
user=self.author,
score=9_999_999_990,
created_timestamp=datetime.datetime(
2003, 3, 3, tzinfo=datetime.timezone.utc
),
)
test_replays.CreateAsPublishedReplay(
filename="th17_lunatic",
user=self.author,
score=9_999_999_990,
created_timestamp=datetime.datetime(
2002, 2, 2, tzinfo=datetime.timezone.utc
),
)

replays = (
models.Replay.objects.select_related("rank_view").order_by("created").all()
)

self.assertEqual(replays[0].GetRank(), 1)
self.assertEqual(replays[1].GetRank(), 2)
self.assertEqual(replays[2].GetRank(), 3)

def testStagePracticeReplaysAreUnranked(self) -> None:
test_replays.CreateAsPublishedReplay(
filename="th6_extra",
user=self.author,
replay_type=models.ReplayType.STAGE_PRACTICE,
)

replay = models.Replay.objects.select_related("rank_view").first()
self.assertIsNone(replay.GetRank())

def testRankCountsTasSeparately(self):
th05_mima = models.Shot.objects.get(
game_id=game_ids.GameIDs.TH05, shot_id="Mima"
)

test_replays.CreateReplayWithoutFile(
user=self.author,
difficulty=1,
shot=th05_mima,
score=10000,
category=models.Category.STANDARD,
)
test_replays.CreateReplayWithoutFile(
user=self.author,
difficulty=1,
shot=th05_mima,
score=7500,
category=models.Category.STANDARD,
)
test_replays.CreateReplayWithoutFile(
user=self.author,
difficulty=1,
shot=th05_mima,
score=20000,
category=models.Category.TAS,
)
replays = (
models.Replay.objects.filter(
category__in=[models.Category.STANDARD, models.Category.TAS]
)
.select_related("rank_view")
.order_by("created")
.all()
)

self.assertEqual(len(replays), 3)
(returned_standard_1, returned_standard_2, returned_tas) = replays
self.assertEqual(returned_standard_1.category, models.Category.STANDARD)
self.assertEqual(returned_standard_1.GetRank(), 1)
self.assertEqual(returned_standard_2.category, models.Category.STANDARD)
self.assertEqual(returned_standard_2.GetRank(), 2)
self.assertEqual(returned_tas.category, models.Category.TAS)
self.assertIsNone(returned_tas.GetRank())


class ReplayTest(test_case.ReplayTestCase):
def setUp(self):
super().setUp()
Expand Down Expand Up @@ -111,139 +258,6 @@ def testGetShortenedComment_LongComment(self) -> None:
expected_shortened_comment = "a" * limits.MAX_SHORTENED_COMMENT_LENGTH + "..."
self.assertEqual(replay.GetShortenedComment(), expected_shortened_comment)

def testRanks(self):
with patch("replays.constant_helpers.CalculateReplayFileHash") as mocked_hash:
mocked_hash.return_value = bytes(0)
test_replays.CreateAsPublishedReplay(
filename="th6_extra",
user=self.author,
score=1_000_000_000,
)
mocked_hash.return_value = bytes(1)
test_replays.CreateAsPublishedReplay(
filename="th6_extra",
user=self.author,
score=900_000_000,
)
mocked_hash.return_value = bytes(2)
test_replays.CreateAsPublishedReplay(
filename="th7_extra",
user=self.author,
score=800_000_000,
)

replays = (
models.Replay.objects.order_by("-score").select_related("rank_view").all()
)

self.assertEquals(replays[0].GetRank(), 1)
self.assertEquals(replays[1].GetRank(), 2)
self.assertEquals(replays[2].GetRank(), 1)

def testRanksTasReplay(self):
test_replays.CreateAsPublishedReplay(
filename="th6_extra",
user=self.author,
score=1_000_000_000,
category=models.Category.TAS,
)

replay = models.Replay.objects.order_by("-score").first()

self.assertIsNone(replay.GetRank())

def testRanksBreakTiesUsingUploadDate(self):
with patch("replays.constant_helpers.CalculateReplayFileHash") as mocked_hash:
mocked_hash.return_value = bytes(0)
test_replays.CreateAsPublishedReplay(
filename="th17_lunatic",
user=self.author,
score=9_999_999_990,
created_timestamp=datetime.datetime(
2001, 1, 1, tzinfo=datetime.timezone.utc
),
)
mocked_hash.return_value = bytes(1)
test_replays.CreateAsPublishedReplay(
filename="th17_lunatic",
user=self.author,
score=9_999_999_990,
created_timestamp=datetime.datetime(
2003, 3, 3, tzinfo=datetime.timezone.utc
),
)
mocked_hash.return_value = bytes(2)
test_replays.CreateAsPublishedReplay(
filename="th17_lunatic",
user=self.author,
score=9_999_999_990,
created_timestamp=datetime.datetime(
2002, 2, 2, tzinfo=datetime.timezone.utc
),
)

replays = (
models.Replay.objects.select_related("rank_view").order_by("created").all()
)

self.assertEquals(replays[0].GetRank(), 1)
self.assertEquals(replays[1].GetRank(), 2)
self.assertEquals(replays[2].GetRank(), 3)

def testStagePracticeReplaysAreUnranked(self) -> None:
test_replays.CreateAsPublishedReplay(
filename="th6_extra",
user=self.author,
replay_type=models.ReplayType.STAGE_PRACTICE,
)

replay = models.Replay.objects.select_related("rank_view").first()
self.assertIsNone(replay.GetRank())

def testRankCountsTasSeparately(self):
th05_mima = models.Shot.objects.get(
game_id=game_ids.GameIDs.TH05, shot_id="Mima"
)

test_replays.CreateReplayWithoutFile(
user=self.author,
difficulty=1,
shot=th05_mima,
score=10000,
category=models.Category.STANDARD,
)
test_replays.CreateReplayWithoutFile(
user=self.author,
difficulty=1,
shot=th05_mima,
score=7500,
category=models.Category.STANDARD,
)
test_replays.CreateReplayWithoutFile(
user=self.author,
difficulty=1,
shot=th05_mima,
score=20000,
category=models.Category.TAS,
)
replays = (
models.Replay.objects.filter(
category__in=[models.Category.STANDARD, models.Category.TAS]
)
.select_related("rank_view")
.order_by("created")
.all()
)

self.assertEqual(len(replays), 3)
(returned_standard_1, returned_standard_2, returned_tas) = replays
self.assertEqual(returned_standard_1.category, models.Category.STANDARD)
self.assertEqual(returned_standard_1.GetRank(), 1)
self.assertEqual(returned_standard_2.category, models.Category.STANDARD)
self.assertEqual(returned_standard_2.GetRank(), 2)
self.assertEqual(returned_tas.category, models.Category.TAS)
self.assertIsNone(returned_tas.GetRank())

def testReplayQueryForGhosts(self):
inactive_user = self.createUser("inactive")

Expand Down
Loading