Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

Commit

Permalink
fix: one class can have multiple games
Browse files Browse the repository at this point in the history
  • Loading branch information
dionizh committed Jan 18, 2022
1 parent 4d4e1f8 commit 610601c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
19 changes: 19 additions & 0 deletions aimmo/migrations/0028_manygames.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 2.2.26 on 2022-01-18 10:56

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('aimmo', '0027_add_game_data'),
]

operations = [
migrations.AlterField(
model_name='game',
name='game_class',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='common.Class', verbose_name='Class'),
),
]
2 changes: 1 addition & 1 deletion aimmo/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Game(models.Model):
related_name="owned_games",
on_delete=models.SET_NULL,
)
game_class = models.OneToOneField(
game_class = models.ForeignKey(
Class,
verbose_name="Class",
on_delete=models.CASCADE,
Expand Down
28 changes: 25 additions & 3 deletions aimmo/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,17 +351,39 @@ def test_delete_game(self):
Check for 204 when deleting a game
"""
client = self.login()
game2 = models.Game(id=2, name="test", worksheet_id=2)
game2.save()

data = {"game_ids": [self.game.id]}
klass, _, _ = create_class_directly("test@example.com", "my class")

form = AddGameForm(
Class.objects.all(),
data={"game_class": klass.id},
)

game2 = form.save()
assert game2.game_class == klass

data = {"game_ids": [game2.id]}
response = client.post(reverse("game-delete-games"), data)

assert response.status_code == 204
assert models.Game.objects.all().count() == 2
assert models.Game.objects.filter(is_archived=True).count() == 1
assert models.Game.objects.filter(is_archived=False).count() == 1

# then test adding game again for the same class
form = AddGameForm(
Class.objects.all(),
data={"game_class": klass.id},
)

game3 = form.save()
assert game3.game_class == klass

# test only one active game at a time
assert (
models.Game.objects.filter(game_class=klass, is_archived=False).count() == 1
)

def test_delete_non_existent_game(self):
c = self.login()
response = c.delete(reverse("game-detail", kwargs={"pk": 2}))
Expand Down

0 comments on commit 610601c

Please sign in to comment.