Coverage for server / test_models.py: 100%

42 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-03-24 12:43 +0000

1from django.test import TransactionTestCase as TestCase 

2from django.urls import reverse 

3 

4from .models import Model 

5from cluster.models import Model as ClusterModel 

6from patchtime.models import Model as PatchtimeModel 

7from location.models import Model as LocationModel 

8from servermodel.models import Model as ServermodelModel 

9from domain.models import Model as DomainModel 

10from status.models import Model as StatusModel 

11from . import app_label 

12 

13from sm.utils import random_string 

14 

15import os 

16import django 

17 

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

19django.setup() 

20 

21 

22class Tester(TestCase): 

23 model = Model 

24 teststring = random_string() 

25 fixtures = [ 

26 "%s/fixtures/01_initial.yaml" % "vendor", 

27 "%s/fixtures/01_initial.yaml" % "domain", 

28 "%s/fixtures/01_initial.yaml" % "location", 

29 "%s/fixtures/01_initial.yaml" % "status", 

30 "%s/fixtures/01_initial.yaml" % "operatingsystem", 

31 "%s/fixtures/01_initial.yaml" % "clustersoftware", 

32 "%s/fixtures/01_initial.yaml" % "patchtime", 

33 "%s/fixtures/01_initial.yaml" % "cluster", 

34 "%s/fixtures/01_initial.yaml" % "servermodel", 

35 ] 

36 testitem = None 

37 

38 def setUp(self): 

39 self.cluster = ClusterModel.objects.all().first() 

40 self.patchtime = PatchtimeModel.objects.all().first() 

41 self.location = LocationModel.objects.all().first() 

42 self.servermodel = ServermodelModel.objects.all().first() 

43 self.domain = DomainModel.objects.all().first() 

44 self.status = StatusModel.objects.all().first() 

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

46 

47 def get_or_create_testitem(self): 

48 self.testitem, created = self.model.objects.get_or_create( 

49 hostname=self.teststring, 

50 cluster=self.cluster, 

51 patchtime=self.patchtime, 

52 location=self.location, 

53 servermodel=self.servermodel, 

54 domain=self.domain, 

55 status=self.status, 

56 ) 

57 return (self.testitem, created) 

58 

59 def test_create(self): 

60 # Since we want to test if creation works, we 

61 # need to manually prune the DB and create a testitem 

62 self.model.objects.all().delete() 

63 obj, created = self.get_or_create_testitem() 

64 self.assertEqual(created, True, "the object was already there?") 

65 self.assertIsInstance(obj, self.model, "object not correct model!?") 

66 

67 def test_name(self): 

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

69 

70 def test__str__(self): 

71 self.assertEqual( 

72 "%s" % (self.teststring), 

73 "%s" % (self.testitem.hostname), 

74 "string representation not correct", 

75 ) 

76 

77 def test_absolute_url(self): 

78 self.assertEqual( 

79 "%s" % (self.testitem.get_absolute_url()), 

80 "%s" % (reverse("%s:detail" % app_label, kwargs={"pk": self.testitem.pk})), 

81 "aboslute url not built correctly", 

82 )