Skip to content

Commit

Permalink
refactor: tidy up some view tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danjac committed Sep 12, 2024
1 parent 1b518cb commit 2590d6a
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions radiofeed/episodes/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,28 @@
_index_url = reverse_lazy("episodes:index")


@pytest.fixture
def player_episode(auth_user, client, episode):
AudioLogFactory(user=auth_user, episode=episode)
def assert_episode_in_player(client, episode_id):
assert (
client.session[PlayerDetails.session_key] == episode_id
), f"Episode {episode_id} not in sesssion"


def assert_episode_not_in_player(client, episode_id):
assert (
client.session.get(PlayerDetails.session_key) != episode_id
), f"Episode {episode_id} in sesssion"


def save_episode_to_player(client, episode_id):
session = client.session
session[PlayerDetails.session_key] = episode.pk
session[PlayerDetails.session_key] = episode_id
session.save()


@pytest.fixture
def player_episode(auth_user, client, episode):
AudioLogFactory(user=auth_user, episode=episode)
save_episode_to_player(client, episode.pk)
return episode


Expand Down Expand Up @@ -212,7 +226,7 @@ def test_play_from_start(self, client, auth_user, episode):
)

assert AudioLog.objects.filter(user=auth_user, episode=episode).exists()
assert client.session[PlayerDetails.session_key] == episode.pk
assert_episode_in_player(client, episode.pk)

@pytest.mark.django_db
def test_another_episode_in_player(self, client, auth_user, player_episode):
Expand All @@ -229,7 +243,7 @@ def test_another_episode_in_player(self, client, auth_user, player_episode):

assert AudioLog.objects.filter(user=auth_user, episode=episode).exists()

assert client.session[PlayerDetails.session_key] == episode.pk
assert_episode_in_player(client, episode.pk)

@pytest.mark.django_db
def test_resume(self, client, auth_user, player_episode):
Expand All @@ -243,7 +257,7 @@ def test_resume(self, client, auth_user, player_episode):
)
)

assert client.session[PlayerDetails.session_key] == player_episode.pk
assert_episode_in_player(client, player_episode.pk)


class TestClosePlayer:
Expand Down Expand Up @@ -279,7 +293,7 @@ def test_close(

assert not response.context["is_playing"]

assert player_episode.pk not in client.session
assert_episode_not_in_player(client, player_episode.pk)


class TestPlayerTimeUpdate:
Expand All @@ -300,10 +314,7 @@ def test_is_running(self, client, player_episode):

@pytest.mark.django_db
def test_player_log_missing(self, client, auth_user, episode):
session = client.session
session[PlayerDetails.session_key] = episode.pk
session.save()

save_episode_to_player(client, episode.pk)
assert_204(
client.post(
self.url,
Expand Down

0 comments on commit 2590d6a

Please sign in to comment.