Coverage for operatingsystem / test_models.py: 100%

69 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 vendor.models import Model as VendorModel 

5 

6from . import app_label 

7 

8from sm.utils import random_string 

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 fixtures = [ 

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

22 "%s/fixtures/01_initial.yaml" % app_label, 

23 ] 

24 testitem = None 

25 

26 def setUp(self): 

27 self.vendor = VendorModel.objects.all().first() 

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

29 

30 def get_or_create_testitem(self): 

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

32 version=self.teststring, 

33 vendor=self.vendor, 

34 ) 

35 return (self.testitem, created) 

36 

37 def test_create(self): 

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

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

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

41 obj, created = self.get_or_create_testitem() 

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

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

44 

45 def test_version(self): 

46 self.assertEqual(self.testitem.version, self.teststring, "version not correct") 

47 

48 def test___str__(self): 

49 self.assertEqual( 

50 "{} {}".format(self.vendor.name, self.teststring), 

51 "{} {}".format(self.testitem.vendor.name, self.testitem.version), 

52 "string representation not correct", 

53 ) 

54 

55 # Class specific tests 

56 def test_natural_key__(self): 

57 self.assertEqual( 

58 self.testitem.natural_key(), (self.vendor.name, self.teststring) 

59 ) 

60 

61 def test_nothing_exception(self): 

62 from django.core.exceptions import ObjectDoesNotExist 

63 

64 with self.assertRaises(ObjectDoesNotExist) as context: 

65 self.model.objects.get_by_natural_key() 

66 self.assertTrue("Nothing to query" in str(context.exception)) 

67 

68 def test_get_rhel_7_by_nat_key(self): 

69 item = self.model.objects.get_by_natural_key("RHEL 7") 

70 self.assertIsInstance(item, self.model, "object not correct model!?") 

71 

72 def test_get_rhel_7_by_nat_key2(self): 

73 item = self.model.objects.get_by_natural_key("Red Hat 7.0") 

74 self.assertIsInstance(item, self.model, "object not correct model!?") 

75 

76 def test_get_rhel_7_by_nat_key3(self): 

77 item = self.model.objects.get_by_natural_key("Red Hat7") 

78 self.assertIsInstance(item, self.model, "object not correct model!?") 

79 

80 def test_get_sles10_nat_key(self): 

81 vendor = VendorModel.objects.get_by_natural_key("Novell") 

82 item, created = self.model.objects.get_or_create(vendor=vendor, version="10.0") 

83 self.assertIsInstance(item, self.model, "object not a Operatingsystem model!?") 

84 item = self.model.objects.get_by_natural_key("SLES 10.0") 

85 self.assertIsInstance(item, self.model, "object not a Operatingsystem model!?") 

86 

87 def test_nat_key_funny_doesnt_exist(self): 

88 from django.core.exceptions import ObjectDoesNotExist 

89 

90 with self.assertRaises(ObjectDoesNotExist) as context: 

91 self.model.objects.get_by_natural_key("Hugo Boss 7.0") 

92 self.assertTrue("matching query does not exist" in str(context.exception)) 

93 

94 def test_nat_key_vendor_version_exists(self): 

95 item = self.model.objects.get_by_natural_key( 

96 vendor=self.vendor.name, version=self.teststring 

97 ) 

98 self.assertIsInstance(item, self.model, "object not correct model!?") 

99 

100 def test_nat_key_get_with_tuple(self): 

101 item = self.model.objects.get_by_natural_key( 

102 ("%s" % self.vendor.name, self.teststring) 

103 ) 

104 self.assertIsInstance(item, self.model, "object not correct model!?") 

105 

106 def test_query_with_dict_exception(self): 

107 with self.assertRaises(Exception) as context: 

108 self.model.objects.get_by_natural_key({}) 

109 self.assertTrue("No idea how to handle query with " in str(context.exception)) 

110 self.assertTrue("'dict'>" in str(context.exception)) 

111 

112 def test_get_absolute_url(self): 

113 self.assertEqual( 

114 "/%s/detail/%i/" % (app_label, self.testitem.id), 

115 self.testitem.get_absolute_url(), 

116 "reverse url not correct", 

117 )