feat(models): add property parent_object to models that have a parent

!22
This commit is contained in:
2024-06-10 00:49:19 +09:30
parent 11ec62feb6
commit fe0696fee6
12 changed files with 417 additions and 38 deletions

View File

@ -212,6 +212,13 @@ class DeviceSoftware(DeviceCommonFields, SaveHistory):
)
@property
def parent_object(self):
""" Fetch the parent object """
return self.device
class DeviceOperatingSystem(DeviceCommonFields, SaveHistory):
@ -247,3 +254,10 @@ class DeviceOperatingSystem(DeviceCommonFields, SaveHistory):
blank = True,
default = None,
)
@property
def parent_object(self):
""" Fetch the parent object """
return self.device

View File

@ -69,6 +69,14 @@ class OperatingSystemVersion(OperatingSystemCommonFields, SaveHistory):
unique = False,
)
@property
def parent_object(self):
""" Fetch the parent object """
return self.operating_system
def __str__(self):
return self.operating_system.name + ' ' + self.name

View File

@ -102,6 +102,14 @@ class SoftwareVersion(SoftwareCommonFields, SaveHistory):
unique = False,
)
@property
def parent_object(self):
""" Fetch the parent object """
return self.software
def __str__(self):
return self.name

View File

@ -0,0 +1,70 @@
import pytest
import unittest
from django.test import TestCase
from access.models import Organization
from config_management.models.groups import ConfigGroups, ConfigGroupSoftware
from itam.models.device import Device, DeviceOperatingSystem
from itam.models.operating_system import OperatingSystem, OperatingSystemVersion
class DeviceOperatingSystemModel(TestCase):
model = DeviceOperatingSystem
@classmethod
def setUpTestData(self):
""" Setup Test
"""
organization = Organization.objects.create(name='test_org')
self.parent_item = Device.objects.create(
organization=organization,
name = 'device_name'
)
os = OperatingSystem.objects.create(
organization=organization,
name = 'os_name'
)
os_version = OperatingSystemVersion.objects.create(
name = "12",
operating_system = os,
)
self.item = self.model.objects.create(
organization = organization,
operating_system_version = os_version,
device = self.parent_item
)
def test_model_has_property_parent_object(self):
""" Check if model contains 'parent_object'
This is a required property for all models that have a parent
"""
assert hasattr(self.model, 'parent_object')
def test_model_property_parent_object_returns_object(self):
""" Check if model contains 'parent_object'
This is a required property for all models that have a parent
"""
assert self.item.parent_object == self.parent_item

View File

@ -0,0 +1,62 @@
import pytest
import unittest
from django.test import TestCase
from access.models import Organization
from config_management.models.groups import ConfigGroups, ConfigGroupSoftware
from itam.models.device import Device, DeviceSoftware
from itam.models.software import Software
class DeviceSoftwareModel(TestCase):
model = DeviceSoftware
@classmethod
def setUpTestData(self):
""" Setup Test
"""
organization = Organization.objects.create(name='test_org')
self.parent_item = Device.objects.create(
organization=organization,
name = 'device_name'
)
self.software_item = Software.objects.create(
organization=organization,
name = 'software_name',
)
self.item = self.model.objects.create(
software = self.software_item,
device = self.parent_item
)
def test_model_has_property_parent_object(self):
""" Check if model contains 'parent_object'
This is a required property for all models that have a parent
"""
assert hasattr(self.model, 'parent_object')
def test_model_property_parent_object_returns_object(self):
""" Check if model contains 'parent_object'
This is a required property for all models that have a parent
"""
assert self.item.parent_object == self.parent_item

View File

@ -0,0 +1,57 @@
import pytest
import unittest
from django.test import TestCase
from access.models import Organization
from config_management.models.groups import ConfigGroups, ConfigGroupSoftware
from itam.models.operating_system import OperatingSystem, OperatingSystemVersion
class OperatingSystemVersionModel(TestCase):
model = OperatingSystemVersion
@classmethod
def setUpTestData(self):
""" Setup Test
"""
organization = Organization.objects.create(name='test_org')
self.parent_item = OperatingSystem.objects.create(
organization=organization,
name = 'os_name'
)
self.item = self.model.objects.create(
name = "12",
operating_system = self.parent_item,
)
def test_model_has_property_parent_object(self):
""" Check if model contains 'parent_object'
This is a required property for all models that have a parent
"""
assert hasattr(self.model, 'parent_object')
def test_model_property_parent_object_returns_object(self):
""" Check if model contains 'parent_object'
This is a required property for all models that have a parent
"""
assert self.item.parent_object == self.parent_item