From eaec9cbf22b9ba6e6e73e078d1dfd3663c11dfc9 Mon Sep 17 00:00:00 2001 From: Skia Date: Thu, 20 Apr 2023 21:58:57 +0200 Subject: [PATCH] galaxy: tests: split Model and View for more efficient data usage --- galaxy/tests.py | 45 +++++++++++++++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/galaxy/tests.py b/galaxy/tests.py index 400dee63..70314574 100644 --- a/galaxy/tests.py +++ b/galaxy/tests.py @@ -34,7 +34,7 @@ from core.models import User from galaxy.models import Galaxy -class GalaxyTest(TestCase): +class GalaxyTestModel(TestCase): def setUp(self): self.root = User.objects.get(username="root") self.skia = User.objects.get(username="skia") @@ -137,18 +137,36 @@ class GalaxyTest(TestCase): self.maxDiff = None # Yes, we want to see the diff if any self.assertDictEqual(expected_scores, computed_scores) + def test_rule(self): + """ + Test on the default dataset generated by the `populate` command + that the number of queries to rule the galaxy is stable. + """ + galaxy = Galaxy.objects.create() + with self.assertNumQueries(58): + galaxy.rule(0) # We want everybody here + + +class GalaxyTestView(TestCase): + @classmethod + def setUpTestData(cls): + """ + Generate a plausible Galaxy once for every test + """ + call_command("generate_galaxy_test_data", "-v", "0") + galaxy = Galaxy.objects.create() + galaxy.rule(26) # We want a fast test + def test_page_is_citizen(self): """ Test that users can access the galaxy page of users who are citizens """ - with self.assertNumQueries(59): - galaxy = Galaxy.objects.create() - galaxy.rule(0) # We want all users here self.client.login(username="root", password="plop") - response = self.client.get(reverse("galaxy:user", args=[1])) + user = User.objects.get(last_name="n°500") + response = self.client.get(reverse("galaxy:user", args=[user.id])) self.assertContains( response, - 'Locate', + f'Reset on {user}', status_code=200, ) @@ -157,21 +175,20 @@ class GalaxyTest(TestCase): Test that trying to access the galaxy page of a user who is not citizens return a 404 """ - galaxy = Galaxy.objects.create() - galaxy.rule(0) # We want all users here self.client.login(username="root", password="plop") - response = self.client.get(reverse("galaxy:user", args=[2])) + user = User.objects.get(last_name="n°1") + response = self.client.get(reverse("galaxy:user", args=[user.id])) self.assertEquals(response.status_code, 404) def test_full_galaxy_state(self): """ Test on the more complex dataset generated by the `generate_galaxy_test_data` - command that the relation scores are correct + command that the relation scores are correct, and that the view exposes the + right data. """ - call_command("generate_galaxy_test_data", "-v", "0") - galaxy = Galaxy.objects.create() - galaxy.rule(26) # We want a fast test - state = Galaxy.objects.last().state + self.client.login(username="root", password="plop") + response = self.client.get(reverse("galaxy:data")) + state = response.json() galaxy_dir = Path(__file__).parent