Coverage for clusterpackage / test_models.py: 100%

44 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 status.models import Model as StatusModel 

6from cluster.models import Model as ClusterModel 

7from clusterpackagetype.models import Model as ClusterpackagetypeModel 

8 

9from . import app_label 

10 

11from sm.utils import random_string 

12 

13import os 

14import django 

15 

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

17django.setup() 

18 

19 

20class Tester(TestCase): 

21 model = Model 

22 testdescription = "%s" % random_string() 

23 testhost = "%s" % random_string() 

24 teststring = random_string() 

25 fixtures = [ 

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

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

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

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

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

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

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

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

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

35 ] 

36 testitem = None 

37 

38 def setUp(self): 

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

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

41 self.package_type = ClusterpackagetypeModel.objects.all().first() 

42 self.description = self.testdescription 

43 self.host = self.testhost 

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

45 

46 def get_or_create_testitem(self): 

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

48 name=self.teststring, 

49 cluster=self.cluster, 

50 status=self.status, 

51 package_type=self.package_type, 

52 host=self.testhost, 

53 description=self.testdescription, 

54 ) 

55 return (self.testitem, created) 

56 

57 def test_create(self): 

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

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

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

61 obj, created = self.get_or_create_testitem() 

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

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

64 

65 def test_description(self): 

66 self.assertEqual( 

67 self.testitem.description, self.testdescription, "description not correct" 

68 ) 

69 

70 def test_name(self): 

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

72 

73 def test_natural_key(self): 

74 self.assertEqual( 

75 (self.cluster.name, self.teststring), 

76 self.testitem.natural_key(), 

77 "natural key not correct", 

78 ) 

79 

80 def test___str__(self): 

81 self.assertEqual( 

82 "%s" % self.testitem.name, 

83 "%s" % self.teststring, 

84 "string representation not correct", 

85 ) 

86 

87 def test_absolute_url(self): 

88 self.assertEqual( 

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

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

91 "absolute url not built correctly", 

92 )