class Entity(VersionedEntity): properties = { 'test_identity': VersionedProperty(is_identity=True), 'test_static_1': VersionedProperty(is_static=True), 'test_state_1': VersionedProperty(is_state=True), 'test_static_2': VersionedProperty(is_static=True), 'test_state_2': VersionedProperty(is_state=True) }
class TestEntity(VersionedEntity): """Simple entity for testing the model serializer.""" label = 'TestEntity' state_label = 'TestEntityState' properties = { 'identity': VersionedProperty(is_identity=True, type=str), 'static1': VersionedProperty(is_static=True, type=int), 'state1': VersionedProperty(is_state=True, type=float) } children = {'children': ('HAS_CHILD', TestChildEntity)}
class Entity(VersionedEntity): properties = { 'test_id': VersionedProperty(is_identity=True), 'test_static': VersionedProperty(is_static=True), 'test_state': VersionedProperty(is_state=True), 'test_concat': VersionedProperty( is_static=True, concat_properties=['test_state', 'test_static'] ) }
class Entity(VersionedEntity): properties = { 'test_static': VersionedProperty( is_static=True, is_state=True ) }
def test_string_to_types(self, m_registry): m_model = mock.Mock() m_model.properties = { 'test_int': VersionedProperty(is_static=True, type=int), 'test_float': VersionedProperty(is_static=True, type=float), 'test_str': VersionedProperty(is_static=True, type=str) } m_registry.models = mock.MagicMock() m_registry.models.get = mock.Mock(return_value=m_model) # Test valid int new_val = prep_val('m', 'test_int', '42') self.assertEqual(new_val, 42) self.assertTrue(isinstance(new_val, int)) # Test valid float new_val = prep_val('m', 'test_float', '0.42') self.assertEqual(new_val, 0.42) self.assertTrue(isinstance(new_val, float)) # Test valid string new_val = prep_val('m', 'test_str', 'somestr') self.assertEqual(new_val, 'somestr') self.assertTrue(isinstance(new_val, str))
def test_conversion_error(self, m_registry): """Test that PropertyNotFoundError is raised.""" m_model = mock.Mock() m_model.properties = { 'test_prop': VersionedProperty(is_static=True, type=int) } m_registry.models = mock.MagicMock() m_registry.models.get = mock.Mock(return_value=m_model) # Try converting incompatible string to int with raise_for_error with self.assertRaises(ConversionError): prep_val('m', 'test_prop', '42x', raise_for_error=True) # Try converting incompatible string to int without raise_for_error new_val = prep_val('m', 'test_prop', '42x', raise_for_error=False) self.assertEqual(new_val, '42x')
def test_invalid_property(self, m_registry): """Test that PropertyNotFoundError is raised.""" m_model = mock.Mock() m_model.properties = { 'test_prop': VersionedProperty(is_static=True, type=int) } m_registry.models = mock.MagicMock() m_registry.models.get = mock.Mock(return_value=m_model) # Test with raise for error with self.assertRaises(PropertyNotFoundError): prep_val('m', 'p', 42, raise_for_error=True) # Test without raise for error(should still raise for this error.) with self.assertRaises(PropertyNotFoundError): prep_val('m', 'p', 42, raise_for_error=False)
def test_string_to_bool(self, m_registry): m_model = mock.Mock() m_model.properties = { 'test_bool': VersionedProperty(is_static=True, type=bool) } m_registry.models = mock.MagicMock() m_registry.models.get = mock.Mock(return_value=m_model) # Test false self.assertFalse(prep_val('m', 'test_bool', 'false')) self.assertFalse(prep_val('m', 'test_bool', 'FALSE')) self.assertFalse(prep_val('m', 'test_bool', 'no')) self.assertFalse(prep_val('m', 'test_bool', 'NO')) # Test true self.assertTrue(prep_val('m', 'test_bool', 'true')) self.assertTrue(prep_val('m', 'test_bool', 'TRUE')) self.assertTrue(prep_val('m', 'test_bool', 'yes')) self.assertTrue(prep_val('m', 'test_bool', 'YES'))
class Entity(VersionedEntity): properties = { 'identity_1': VersionedProperty(is_identity=True), 'identity_2': VersionedProperty(is_identity=True) }
class TestChildEntity(VersionedEntity): """Simple child entity for testing the model serializer.""" label = "TestChildEntity" state_label = 'TestChildEntityState' properties = {'identity': VersionedProperty(is_identity=True, type=str)}