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
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,
'<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,
)
@ -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