def test_dynamic_serial(self): class DynamicModel(properties.HasProperties): a = properties.Integer('') b = properties.Renamed('a') @properties.Integer('') def c(self): return self.a dm1 = DynamicModel() dm1.a = 5 assert dm1.b == 5 assert dm1.c == 5 dm_save_dynamic = dm1.serialize(save_dynamic=True) dm_skip_dynamic = dm1.serialize() assert dm_skip_dynamic == {'__class__': 'DynamicModel', 'a': 5} assert dm_save_dynamic == { '__class__': 'DynamicModel', 'a': 5, 'b': 5, 'c': 5 } dm2 = DynamicModel.deserialize(dm_skip_dynamic) assert properties.equal(dm1, dm2) dm3 = DynamicModel.deserialize(dm_save_dynamic) assert properties.equal(dm1, dm3)
def test_dynamic_serial(self): class DynamicModel(properties.HasProperties): a = properties.Integer('') b = properties.Renamed('a') @properties.Integer('') def c(self): return self.a dm1 = DynamicModel() dm1.a = 5 assert dm1.b == 5 assert dm1.c == 5 dm_save_dynamic = dm1.serialize(save_dynamic=True) dm_skip_dynamic = dm1.serialize() assert dm_skip_dynamic == {'__class__': 'DynamicModel', 'a': 5} assert dm_save_dynamic == {'__class__': 'DynamicModel', 'a': 5, 'b': 5, 'c': 5} dm2 = DynamicModel.deserialize(dm_skip_dynamic) assert properties.equal(dm1, dm2) dm3 = DynamicModel.deserialize(dm_save_dynamic) assert properties.equal(dm1, dm3)
def test_copy(self): class HasProps2(properties.HasProperties): my_list = properties.List('my list', properties.Bool('')) five = properties.GettableProperty('five', default=5) my_array = properties.Vector3Array('my array') class HasProps1(properties.HasProperties): my_hp2 = properties.Instance('my HasProps2', HasProps2) my_union = properties.Union( 'string or int', (properties.String(''), properties.Integer('')) ) hp1 = HasProps1( my_hp2=HasProps2( my_list=[True, True, False], my_array=[[1., 2., 3.], [4., 5., 6.]], ), my_union=10, ) hp1_copy = properties.copy(hp1) assert properties.equal(hp1, hp1_copy) assert hp1 is not hp1_copy assert hp1.my_hp2 is not hp1_copy.my_hp2 assert hp1.my_hp2.my_list is not hp1_copy.my_hp2.my_list assert hp1.my_hp2.my_array is not hp1_copy.my_hp2.my_array
def test_immutable_serial(self): class UidModel(properties.HasProperties): uid = properties.Uuid('unique id') um1 = UidModel() um2 = UidModel.deserialize(um1.serialize()) assert properties.equal(um1, um2)
def test_snapshot(): class HasSnapshot(properties.HasProperties): snapshot = spatial.base.InstanceSnapshot( 'Instance represented by JSON string', Something, ) input_dict = {'my_string': 'hi', 'my_int': 5} hs = HasSnapshot() hs.snapshot = input_dict s = hs.serialize(snapshot=True) assert isinstance(s['snapshot'], string_types) assert '"__class__": "Something"' in s['snapshot'] assert '"my_string": "hi"' in s['snapshot'] assert '"my_int": 5' in s['snapshot'] assert properties.equal(hs, HasSnapshot.deserialize(s)) ns = hs.serialize(include_class=False) assert ns['snapshot'] == input_dict assert properties.equal(hs, HasSnapshot.deserialize(ns))
def test_renamed(self): with self.assertRaises(TypeError): class BadRenamed(properties.HasProperties): new_prop = properties.Renamed('no_good') class MyHasProps(properties.HasProperties): my_int = properties.Integer('My integer') not_my_int = properties.Renamed('my_int') myp = MyHasProps() with warnings.catch_warnings(record=True) as w: myp.not_my_int = 5 assert len(w) == 1 assert issubclass(w[0].category, FutureWarning) assert myp.my_int == 5 with warnings.catch_warnings(record=True) as w: assert myp.not_my_int == 5 assert len(w) == 1 assert issubclass(w[0].category, FutureWarning) with warnings.catch_warnings(record=True) as w: del myp.not_my_int assert len(w) == 1 assert issubclass(w[0].category, FutureWarning) assert myp.my_int is None assert MyHasProps._props['not_my_int'].doc == ( "This property has been renamed 'my_int' and may be removed " "in the future." ) with self.assertRaises(TypeError): class MyHasProps(properties.HasProperties): my_int = properties.Integer('My integer') not_my_int = properties.Renamed('my_int', warn='no') class MyHasProps(properties.HasProperties): my_int = properties.Integer('My integer') not_my_int = properties.Renamed('my_int', warn=False, doc='') myp = MyHasProps() with warnings.catch_warnings(record=True) as w: myp.not_my_int = 5 assert len(w) == 0 assert myp.my_int == 5 assert MyHasProps._props['not_my_int'].doc == '' assert properties.equal( MyHasProps.deserialize({'my_int': 5}), MyHasProps.deserialize({'not_my_int': 5}) )
def test_base_functionality(self): with self.assertRaises(AttributeError): properties.GettableProperty('bad kwarg', _default=5) with self.assertRaises(AttributeError): properties.GettableProperty('bad kwarg', defualt=5) with self.assertRaises(TypeError): properties.Property('bad kwarg', required=5) class GettablePropOpt(properties.HasProperties): mygp = properties.GettableProperty('gettable prop') with self.assertRaises(TypeError): GettablePropOpt._props['mygp'].name = 5 with self.assertRaises(TypeError): GettablePropOpt._props['mygp'].doc = 5 with self.assertRaises(TypeError): GettablePropOpt._props['mygp'].terms = 5 with self.assertRaises(TypeError): GettablePropOpt._props['mygp'].terms = {'one': 1, 'two': 2} with self.assertRaises(TypeError): GettablePropOpt._props['mygp'].doc = {'args': (1,), 'otherargs': 5} gpo = GettablePropOpt() with self.assertRaises(AttributeError): setattr(gpo, 'mygp', 5) with self.assertRaises(AttributeError): GettablePropOpt(not_mygp=0) with self.assertRaises(AttributeError): GettablePropOpt(help='help') assert gpo.validate() assert gpo._props['mygp'].terms.name == 'mygp' assert gpo._props['mygp'].terms.cls is properties.GettableProperty assert gpo._props['mygp'].terms.args == ('gettable prop',) assert gpo._props['mygp'].terms.kwargs == {} assert gpo._props['mygp'].terms.meta == {} def twelve(): return 12 class GettablePropOpt(properties.HasProperties): mygp = properties.GettableProperty('gettable prop', default=twelve) assert GettablePropOpt().validate() assert GettablePropOpt().mygp == 12 class PropOpts(properties.HasProperties): myprop = properties.Property('empty property') maybe_none = properties.Property('maybe None', required=False) with self.assertRaises(ValueError): PropOpts().validate() assert PropOpts(myprop=5).validate() with warnings.catch_warnings(record=True) as w: assert PropOpts().equal(PropOpts()) assert len(w) == 1 assert issubclass(w[0].category, FutureWarning) assert properties.equal(PropOpts(), PropOpts()) assert properties.equal(PropOpts(myprop=5), PropOpts(myprop=5)) assert not properties.equal(PropOpts(myprop=5, maybe_none=3), PropOpts(myprop=5)) assert properties.equal(PropOpts(myprop=5, maybe_none=3), PropOpts(myprop=5, maybe_none=3)) assert not properties.equal(PropOpts(myprop=5), PropOpts()) assert not properties.equal(PropOpts(myprop=5), PropOpts(myprop=6)) assert not properties.equal(None, PropOpts(myprop=6)) assert not properties.equal(PropOpts(myprop=5), None) assert properties.equal(None, None) assert properties.Property('').equal(5, 5) assert not properties.Property('').equal(5, 'hi') assert properties.Property('').equal(np.array([1., 2.]), np.array([1., 2.])) assert not properties.Property('').equal(np.array([1., 2.]), np.array([3., 4.])) class NoAttributes(properties.HasProperties): a = properties.Integer('a') def __setattr__(self, attr, value): if attr[0] != '_' and value is not properties.undefined: raise AttributeError() return super(NoAttributes, self).__setattr__(attr, value) na = NoAttributes() with self.assertRaises(AttributeError): na.a = 5
def equal(self, other): """Compare this Grid to another Grid""" return properties.equal(self, other)
def test_base_functionality(self): with self.assertRaises(AttributeError): properties.GettableProperty('bad kwarg', _default=5) with self.assertRaises(AttributeError): properties.GettableProperty('bad kwarg', defualt=5) with self.assertRaises(TypeError): properties.Property('bad kwarg', required=5) with self.assertRaises(AttributeError): class PrivateProperty(properties.HasProperties): _secret = properties.GettableProperty('secret prop') class GettablePropOpt(properties.HasProperties): mygp = properties.GettableProperty('gettable prop') with self.assertRaises(TypeError): GettablePropOpt._props['mygp'].name = 5 with self.assertRaises(TypeError): GettablePropOpt._props['mygp'].doc = 5 with self.assertRaises(TypeError): GettablePropOpt._props['mygp'].terms = 5 with self.assertRaises(TypeError): GettablePropOpt._props['mygp'].terms = {'one': 1, 'two': 2} with self.assertRaises(TypeError): GettablePropOpt._props['mygp'].doc = { 'args': (1, ), 'otherargs': 5 } gpo = GettablePropOpt() with self.assertRaises(AttributeError): setattr(gpo, 'mygp', 5) with self.assertRaises(AttributeError): GettablePropOpt(not_mygp=0) with self.assertRaises(AttributeError): GettablePropOpt(help='help') assert gpo.validate() assert gpo._props['mygp'].terms.name == 'mygp' assert gpo._props['mygp'].terms.cls is properties.GettableProperty assert gpo._props['mygp'].terms.args == ('gettable prop', ) assert gpo._props['mygp'].terms.kwargs == {} assert gpo._props['mygp'].terms.meta == {} def twelve(): return 12 class GettablePropOpt(properties.HasProperties): mygp = properties.GettableProperty('gettable prop', default=twelve) assert GettablePropOpt().validate() assert GettablePropOpt().mygp == 12 class PropOpts(properties.HasProperties): myprop = properties.Property('empty property') with self.assertRaises(ValueError): PropOpts().validate() assert PropOpts(myprop=5).validate() with warnings.catch_warnings(record=True) as w: assert PropOpts().equal(PropOpts()) assert len(w) == 1 assert issubclass(w[0].category, FutureWarning) assert properties.equal(PropOpts(), PropOpts()) assert properties.equal(PropOpts(myprop=5), PropOpts(myprop=5)) assert not properties.equal(PropOpts(myprop=5), PropOpts()) assert not properties.equal(PropOpts(myprop=5), PropOpts(myprop=6)) with self.assertRaises(AttributeError): class BadDocOrder(properties.HasProperties): _doc_order = 5 with self.assertRaises(AttributeError): class BadDocOrder(properties.HasProperties): _doc_order = ['myprop', 'another_prop'] myprop = properties.Property('empty property') class WithDocOrder(properties.HasProperties): _doc_order = ['myprop1', 'myprop3', 'myprop2'] myprop1 = properties.Property('empty property') myprop2 = properties.Property('empty property') myprop3 = properties.Property('empty property') assert WithDocOrder().__doc__ == ( '\n\n**Required Properties:**\n\n' '* **myprop1** (:class:`Property <properties.Property>`): ' 'empty property\n' '* **myprop3** (:class:`Property <properties.Property>`): ' 'empty property\n' '* **myprop2** (:class:`Property <properties.Property>`): ' 'empty property') class NoMoreDocOrder(WithDocOrder): _doc_order = None assert properties.Property('').equal(5, 5) assert not properties.Property('').equal(5, 'hi') assert properties.Property('').equal(np.array([1., 2.]), np.array([1., 2.])) assert not properties.Property('').equal(np.array([1., 2.]), np.array([3., 4.]))