Skip to content

Commit 55b04f2

Browse files
sleepinggenius2jeremystretch
authored andcommitted
Adds tests for replication and adoption for module import
1 parent 40de5dd commit 55b04f2

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

netbox/dcim/tests/test_views.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1848,6 +1848,53 @@ def test_module_component_replication(self):
18481848
self.assertHttpStatus(self.client.post(**request), 302)
18491849
self.assertEqual(Interface.objects.filter(device=device).count(), 5)
18501850

1851+
@override_settings(EXEMPT_VIEW_PERMISSIONS=['*'])
1852+
def test_module_bulk_replication(self):
1853+
self.add_permissions('dcim.add_module')
1854+
1855+
# Add 5 InterfaceTemplates to a ModuleType
1856+
module_type = ModuleType.objects.first()
1857+
interface_templates = [
1858+
InterfaceTemplate(module_type=module_type, name=f'Interface {i}') for i in range(1, 6)
1859+
]
1860+
InterfaceTemplate.objects.bulk_create(interface_templates)
1861+
1862+
form_data = self.form_data.copy()
1863+
device = Device.objects.get(pk=form_data['device'])
1864+
1865+
# Create a module *without* replicating components
1866+
module_bay = ModuleBay.objects.get(pk=form_data['module_bay'])
1867+
csv_data = [
1868+
"device,module_bay,module_type,replicate_components",
1869+
f"{device.name},{module_bay.name},{module_type.model},false"
1870+
]
1871+
request = {
1872+
'path': self._get_url('import'),
1873+
'data': {
1874+
'csv': '\n'.join(csv_data),
1875+
}
1876+
}
1877+
1878+
initial_count = self._get_queryset().count()
1879+
self.assertHttpStatus(self.client.post(**request), 200)
1880+
self.assertEqual(self._get_queryset().count(), initial_count + len(csv_data) - 1)
1881+
self.assertEqual(Interface.objects.filter(device=device).count(), 0)
1882+
1883+
# Create a second module (in the next bay) with replicated components
1884+
module_bay = ModuleBay.objects.get(pk=(form_data['module_bay'] + 1))
1885+
csv_data[1] = f"{device.name},{module_bay.name},{module_type.model},true"
1886+
request = {
1887+
'path': self._get_url('import'),
1888+
'data': {
1889+
'csv': '\n'.join(csv_data),
1890+
}
1891+
}
1892+
1893+
initial_count = self._get_queryset().count()
1894+
self.assertHttpStatus(self.client.post(**request), 200)
1895+
self.assertEqual(self._get_queryset().count(), initial_count + len(csv_data) - 1)
1896+
self.assertEqual(Interface.objects.filter(device=device).count(), 5)
1897+
18511898
@override_settings(EXEMPT_VIEW_PERMISSIONS=['*'])
18521899
def test_module_component_adoption(self):
18531900
self.add_permissions('dcim.add_module')
@@ -1885,6 +1932,49 @@ def test_module_component_adoption(self):
18851932
# Check that the Interface now has a module
18861933
self.assertIsNotNone(interface.module)
18871934

1935+
@override_settings(EXEMPT_VIEW_PERMISSIONS=['*'])
1936+
def test_module_bulk_adoption(self):
1937+
self.add_permissions('dcim.add_module')
1938+
1939+
interface_name = "Interface-1"
1940+
1941+
# Add an interface to the ModuleType
1942+
module_type = ModuleType.objects.first()
1943+
InterfaceTemplate(module_type=module_type, name=interface_name).save()
1944+
1945+
form_data = self.form_data.copy()
1946+
device = Device.objects.get(pk=form_data['device'])
1947+
1948+
# Create an interface to be adopted
1949+
interface = Interface(device=device, name=interface_name, type=InterfaceTypeChoices.TYPE_10GE_FIXED)
1950+
interface.save()
1951+
1952+
# Ensure that interface is created with no module
1953+
self.assertIsNone(interface.module)
1954+
1955+
# Create a module with adopted components
1956+
module_bay = ModuleBay.objects.filter(device=device).first()
1957+
csv_data = [
1958+
"device,module_bay,module_type,replicate_components,adopt_components",
1959+
f"{device.name},{module_bay.name},{module_type.model},false,true"
1960+
]
1961+
request = {
1962+
'path': self._get_url('import'),
1963+
'data': {
1964+
'csv': '\n'.join(csv_data),
1965+
}
1966+
}
1967+
1968+
initial_count = self._get_queryset().count()
1969+
self.assertHttpStatus(self.client.post(**request), 200)
1970+
self.assertEqual(self._get_queryset().count(), initial_count + len(csv_data) - 1)
1971+
1972+
# Re-retrieve interface to get new module id
1973+
interface.refresh_from_db()
1974+
1975+
# Check that the Interface now has a module
1976+
self.assertIsNotNone(interface.module)
1977+
18881978

18891979
class ConsolePortTestCase(ViewTestCases.DeviceComponentViewTestCase):
18901980
model = ConsolePort

0 commit comments

Comments
 (0)