Coverage for location / test_models.py: 100%
50 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-03-24 12:43 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-03-24 12:43 +0000
1from django.test import TransactionTestCase as TestCase
3from .models import Model
4from . import app_label
6from sm.utils import random_string
8from django_countries import countries
10import os
11import django
13os.environ["DJANGO_SETTINGS_MODULE"] = "sm.settings"
14django.setup()
17class Tester(TestCase):
18 model = Model
19 teststring = random_string()
20 country = dict(countries)["MW"]
21 fixtures = ["%s/fixtures/01_initial.yaml" % app_label]
22 testitem = None
24 def setUp(self):
25 self.testitem, created = self.get_or_create_testitem()
27 def get_or_create_testitem(self):
28 self.testitem, created = self.model.objects.get_or_create(name=self.teststring)
29 return (self.testitem, created)
31 def test_create(self):
32 # Since we want to test if creation works, we
33 # need to manually prune the DB and create a testitem
34 self.model.objects.all().delete()
35 obj, created = self.get_or_create_testitem()
36 self.assertEqual(created, True, "the object was already there?")
37 self.assertIsInstance(obj, self.model, "object not correct model!?")
39 def test_name(self):
40 self.assertEqual(self.testitem.name, self.teststring, "name not correct")
42 def test_name___str__(self):
43 self.assertEqual("%s" % self.testitem, self.teststring, "name not correct")
45 def test_get_absolute_url(self):
46 self.assertEqual(
47 "/%s/detail/%i/" % (app_label, self.testitem.id),
48 self.testitem.get_absolute_url(),
49 "reverse url not correct",
50 )
52 def test_delete(self):
53 res = self.testitem.delete()
54 self.assertEqual(res[0], 1)
55 self.assertTrue("%s.Model" % app_label in res[1])
56 self.assertEqual(res[1]["%s.Model" % app_label], 1)
58 # Additional test, specific to this class
59 def test_name___str___wo_country(self):
60 self.assertEqual("%s" % self.testitem.name, self.teststring, "name not correct")
62 def test___str___w_country(self):
63 self.testitem.country = self.country
64 self.assertEqual(
65 "%s" % self.testitem,
66 "{} / {}".format(self.teststring, self.country),
67 "name not correct",
68 )
70 def test___str___w_nonexistant_country(self):
71 self.testitem.country = self.teststring
72 self.assertEqual(
73 "%s" % self.testitem,
74 "{} / {}".format(self.teststring, self.teststring),
75 "name not correct",
76 )
78 def test_nonexisting_country_flag(self):
79 self.testitem.country = self.teststring
80 self.assertEqual(self.testitem.country.flag_url, None)
82 def test___str__wo_country(self):
83 self.testitem.country = None
84 self.assertEqual(self.testitem.__str__(), self.teststring)