1010from __future__ import absolute_import
1111
1212from unittest import TestCase
13+ from mock import Mock
1314
15+ from Products .ZenModel .Device import Device
1416from Products .ZenModel .Hardware import Hardware
1517from Products .Zing .fact import device_info_fact , MetadataKeys
1618
17- class DummyDevice (object ):
18- id = "dummy_device"
19- title = "Dummy Device"
20-
21- def _propertyMap (self ): return []
22- def device (self ): return None
23- def getProductionState (self ): return 1000
24- def getPriorityConversions (self ): return {}
25- def getProdStateConversions (self ): return {}
26- def convertProdState (self , state ): return "Production"
27-
28- def getOSProductKey (self ): return ("os_model" , "os_manufacturer" )
29- def getHWProductKey (self ): return ("device_hw_model" , "device_hw_manufacturer" )
30- def getHWTag (self ): return "device_hw_tag"
31- def getHWSerialNumber (self ): return "device_hw_sn"
32-
33-
34- class DummyHardwareComponent (Hardware ):
35- id = "dummy_hw_comp"
36- title = "Dummy HW Component"
37-
38- tag = "comp_hw_tag"
39- serialNumber = "comp_hw_sn"
40-
41- def __init__ (self ):
42- # Prevent calling MEProduct.__init__ which relies on ZODB infrastructure
43- pass
44-
45- def _propertyMap (self ): return []
46- def device (self ): return None
47- def getProductionState (self ): return 1000
48- def getPriorityConversions (self ): return {}
49- def getProdStateConversions (self ): return {}
50- def convertProdState (self , state ): return "Production"
51-
52- def getOSProductKey (self ): return ("os_model" , "os_manufacturer" )
53-
54- # Its own hardware properties
55- def getProductKey (self ): return ("comp_hw_model" , "comp_hw_manufacturer" )
56-
57- # Inherited device properties (fallback)
58- def getHWProductKey (self ): return ("device_hw_model" , "device_hw_manufacturer" )
59- def getHWTag (self ): return "device_hw_tag"
60- def getHWSerialNumber (self ): return "device_hw_sn"
61-
6219
6320class TestFactGeneration (TestCase ):
6421
@@ -67,7 +24,24 @@ def test_device_hardware_extraction(self):
6724 Verify that device_info_fact correctly extracts hardware properties
6825 from a standard Device object using the fallback methods.
6926 """
70- device = DummyDevice ()
27+ # Create a mock Device. Using spec=Device ensures isinstance(device, Device) works.
28+ # It automatically provides dummy callable methods for titleOrId, getProductionState, etc.
29+ device = Mock (spec = Device )
30+ device .id = "test_device"
31+ device .title = "Test Device"
32+ device .meta_type = "Device"
33+
34+ # Stub the specific methods called during fact generation
35+ device .titleOrId .return_value = "Test Device"
36+ device ._propertyMap .return_value = []
37+ device .device .return_value = None
38+ device .getOSProductKey .return_value = ("os_model" , "os_manufacturer" )
39+
40+ # Mock the hardware fallback methods for a non-Hardware object
41+ device .getHWProductKey .return_value = ("device_hw_model" , "device_hw_manufacturer" )
42+ device .getHWTag .return_value = "device_hw_tag"
43+ device .getHWSerialNumber .return_value = "device_hw_sn"
44+
7145 fact = device_info_fact (device )
7246
7347 self .assertEqual (fact .data .get (MetadataKeys .HW_MODEL ), "device_hw_model" )
@@ -81,7 +55,23 @@ def test_hardware_component_extraction(self):
8155 a component that inherits from Hardware, rather than falling back to the
8256 parent device methods.
8357 """
84- hw_comp = DummyHardwareComponent ()
58+ # Create a mock Hardware component. spec=Hardware makes isinstance(device, Hardware) True
59+ hw_comp = Mock (spec = Hardware )
60+ hw_comp .id = "test_hw_comp"
61+ hw_comp .title = "Test HW Comp"
62+ hw_comp .meta_type = "HWComponent"
63+
64+ # Stub the specific methods called during fact generation
65+ hw_comp .titleOrId .return_value = "Test HW Comp"
66+ hw_comp ._propertyMap .return_value = []
67+ hw_comp .device .return_value = None
68+ hw_comp .getOSProductKey .return_value = ("os_model" , "os_manufacturer" )
69+
70+ # Its own hardware properties (which should be used because it's a Hardware instance)
71+ hw_comp .getProductKey .return_value = ("comp_hw_model" , "comp_hw_manufacturer" )
72+ hw_comp .tag = "comp_hw_tag"
73+ hw_comp .serialNumber = "comp_hw_sn"
74+
8575 fact = device_info_fact (hw_comp )
8676
8777 self .assertEqual (fact .data .get (MetadataKeys .HW_MODEL ), "comp_hw_model" )
0 commit comments