galaxy: tests: split Model and View for more efficient data usage

This commit is contained in:
Skia
2023-04-20 21:58:57 +02:00
parent 1f2bbff2d5
commit eaec9cbf22

View File

@ -34,7 +34,7 @@ from core.models import User
from galaxy.models import Galaxy from galaxy.models import Galaxy
class GalaxyTest(TestCase): class GalaxyTestModel(TestCase):
def setUp(self): def setUp(self):
self.root = User.objects.get(username="root") self.root = User.objects.get(username="root")
self.skia = User.objects.get(username="skia") 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.maxDiff = None # Yes, we want to see the diff if any
self.assertDictEqual(expected_scores, computed_scores) 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): def test_page_is_citizen(self):
""" """
Test that users can access the galaxy page of users who are citizens 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") 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( self.assertContains(
response, response,
'<a onclick="focus_node(get_node_from_id(8))">Locate</a>', f'<a onclick="focus_node(get_node_from_id({user.id}))">Reset on {user}</a>',
status_code=200, status_code=200,
) )
@ -157,21 +175,20 @@ class GalaxyTest(TestCase):
Test that trying to access the galaxy page of a user who is not Test that trying to access the galaxy page of a user who is not
citizens return a 404 citizens return a 404
""" """
galaxy = Galaxy.objects.create()
galaxy.rule(0) # We want all users here
self.client.login(username="root", password="plop") 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) self.assertEquals(response.status_code, 404)
def test_full_galaxy_state(self): def test_full_galaxy_state(self):
""" """
Test on the more complex dataset generated by the `generate_galaxy_test_data` 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") self.client.login(username="root", password="plop")
galaxy = Galaxy.objects.create() response = self.client.get(reverse("galaxy:data"))
galaxy.rule(26) # We want a fast test state = response.json()
state = Galaxy.objects.last().state
galaxy_dir = Path(__file__).parent galaxy_dir = Path(__file__).parent