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

1from django.test import TransactionTestCase as TestCase 

2 

3from .models import Model 

4from . import app_label 

5 

6from sm.utils import random_string 

7 

8from django_countries import countries 

9 

10import os 

11import django 

12 

13os.environ["DJANGO_SETTINGS_MODULE"] = "sm.settings" 

14django.setup() 

15 

16 

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 

23 

24 def setUp(self): 

25 self.testitem, created = self.get_or_create_testitem() 

26 

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) 

30 

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!?") 

38 

39 def test_name(self): 

40 self.assertEqual(self.testitem.name, self.teststring, "name not correct") 

41 

42 def test_name___str__(self): 

43 self.assertEqual("%s" % self.testitem, self.teststring, "name not correct") 

44 

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 ) 

51 

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) 

57 

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") 

61 

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 ) 

69 

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 ) 

77 

78 def test_nonexisting_country_flag(self): 

79 self.testitem.country = self.teststring 

80 self.assertEqual(self.testitem.country.flag_url, None) 

81 

82 def test___str__wo_country(self): 

83 self.testitem.country = None 

84 self.assertEqual(self.testitem.__str__(), self.teststring)