def test_patch_entity_new_list_with_entities(self, mock_patch): self.customers[0].base.contacts = Properties(email='email') self.customers[0].base.contacts.otherContacts = [ Properties(email1=Properties(a=Properties(b=1))) ] self.customers[0].patch() body = { 'base': { 'contacts': { 'email': 'email', 'fax': None, 'mobilePhone': None, 'phone': None, 'mobileDevices': [], 'otherContacts': [{ 'email1': { 'a': { 'b': 1 } } }] } } } mock_patch.assert_called_with(self.base_url_customer + '/' + self.customers[0].id, headers=self.headers_expected, json=body)
def test_patch_entity_with_rename_dict(self, mock_patch): self.customers[0].extended = Properties(a=1, prova=Properties(b=1)) self.customers[0].base.contacts = Properties(email1=Properties(a=1)) self.customers[0].patch() body = { 'extended': { 'prova': { 'b': 1, 'oggetto': None, 'list': [] }, 'a': 1 }, 'base': { 'contacts': { 'email': None, 'email1': { 'a': 1 }, 'fax': None, 'mobilePhone': None, 'phone': None, 'otherContacts': [], 'mobileDevices': [] } } } mock_patch.assert_called_with(self.base_url_customer + '/' + self.customers[0].id, headers=self.headers_expected, json=body)
def test_patch_entity_list(self, mock_patch): self.customers[0].extended = Properties(a=1, prova=Properties(b=1)) self.customers[0].base.contacts.otherContacts = [ Properties(email1=Properties(a=1)) ] self.customers[0].patch() body = { 'extended': { 'prova': { 'b': 1, 'oggetto': None, 'list': [] }, 'a': 1 }, 'base': { 'contacts': { 'otherContacts': [{ 'email1': { 'a': 1 } }] } } } mock_patch.assert_called_with(self.base_url_customer + '/' + self.customers[0].id, headers=self.headers_expected, json=body)
def test_post_event(self, mock_post): self.node.add_event(a=[Properties(a='b')], b='c', d=Properties(f='g', h=Properties(i='j')), k=dict(l=Properties(m='n', o='p'))) mock_post.assert_called_with(self.base_events_url, headers=self.headers_expected, json={ 'a': [{ 'a': 'b' }], 'b': 'c', 'd': { 'f': 'g', 'h': { 'i': 'j' } }, 'k': { 'l': { 'm': 'n', 'o': 'p' } } })
def test_post_customer_creation_first_method(self, mock_post): expected_body = { 'base': { 'contacts': { 'email': '*****@*****.**' } }, 'extra': 'extra', 'extended': { 'prova': 'prova' }, 'tags': { 'auto': ['auto'], 'manual': ['manual'] } } mock_post.return_value = json.loads( FakeHTTPResponse(resp_path='tests/util/fake_post_response').text) c = Customer( node=self.node, base=Properties(contacts=Properties(email='*****@*****.**'))) c.extra = 'extra' c.extended.prova = 'prova' c.tags.auto = ['auto'] c.tags.manual = ['manual'] c.post() mock_post.assert_called_with(body=expected_body, force_update=False)
def test_add_customer_tags(self, mock_get): c = Customer(node=self.node, base=Properties(contacts=Properties(email='email'))) c.extended.prova = 'prova' c.tags.auto = ['auto'] c.tags.manual = ['manual'] self.node.add_customer(**c.to_dict()) body = { 'nodeId': self.node.node_id, 'base': { 'contacts': { 'email': 'email' } }, 'extended': { 'prova': 'prova' }, 'tags': { 'auto': ['auto'], 'manual': ['manual'] } } mock_get.assert_called_with(self.base_url, headers=self.headers_expected, json=body)
def test_update_customer_not_full(self, mock_patch): c = Customer(node=self.node, id='01', base=Properties(contacts=Properties(email='email'))) c.extra = 'extra' self.node.update_customer(c.id, **c.get_mutation_tracker()) body = {'extra': 'extra'} mock_patch.assert_called_with(self.base_url + '/01', headers=self.headers_expected, json=body)
def test_customer_patch_new_prop(self): c = Customer(node=self.node, default_attributes={ 'prop': { 'prop1': 'value1' }, 'prop2': 'value2' }, prop3=Properties(prop4='value4')) c.prop5 = Properties(prop6='value5') assert c.mute == {'prop5': {'prop6': 'value5'}}, c.mute
def test_patch_all_base(self, mock_patch): self.customers[0].base = Properties() self.customers[0].patch() body = { 'base': { 'pictureUrl': None, 'title': None, 'prefix': None, 'firstName': None, 'lastName': None, 'middleName': None, 'gender': None, 'dob': None, 'locale': None, 'timezone': None, 'contacts': None, 'address': None, 'credential': None, 'educations': [], 'likes': [], 'socialProfile': None, 'jobs': [], 'subscriptions': [] } } mock_patch.assert_called_with(self.base_url_customer + '/' + self.customers[0].id, headers=self.headers_expected, json=body)
def test_patch_all_extended(self, mock_patch): self.customers[0].extended = Properties() self.customers[0].patch() body = {'extended': {'prova': None}} mock_patch.assert_called_with(self.base_url_customer + '/' + self.customers[0].id, headers=self.headers_expected, json=body)
def test_patch_entity(self, mock_patch): self.customers[0].extended = Properties(a=1, prova=Properties(b=1)) self.customers[0].patch() body = { 'extended': { 'prova': { 'b': 1, 'oggetto': None, 'list': [] }, 'a': 1 } } mock_patch.assert_called_with(self.base_url_customer + '/' + self.customers[0].id, headers=self.headers_expected, json=body)
def test_patch_extended_entity_and_base_entity(self, mock_patch): self.customers[0].extended = Properties(a=1, prova=Properties(b=1)) self.customers[0].base = Properties(contacts=Properties(email='email')) self.customers[0].patch() body = { 'extended': { 'prova': { 'b': 1, 'oggetto': None, 'list': [] }, 'a': 1 }, 'base': { 'pictureUrl': None, 'title': None, 'prefix': None, 'firstName': None, 'lastName': None, 'middleName': None, 'gender': None, 'dob': None, 'locale': None, 'timezone': None, 'contacts': { 'email': 'email', 'fax': None, 'mobilePhone': None, 'phone': None, 'otherContacts': [], 'mobileDevices': [] }, 'address': None, 'credential': None, 'educations': [], 'likes': [], 'socialProfile': None, 'jobs': [], 'subscriptions': [] } } mock_patch.assert_called_with(self.base_url_customer + '/' + self.customers[0].id, headers=self.headers_expected, json=body)
def test_update_customer_full(self, mock_get): c = Customer( node=self.node, id='01', base=Properties(contacts=Properties(email='email', fax='fax'))) c.base.contacts.email = 'email1234' self.node.update_customer(full_update=True, **c.to_dict()) body = { 'id': '01', 'base': { 'contacts': { 'email': 'email1234', 'fax': 'fax' } }, 'extended': {}, 'tags': { 'auto': [], 'manual': [] } } mock_get.assert_called_with(self.base_url + '/01', headers=self.headers_expected, json=body)
def __getattr__(self, item): """ Check if a key is in the dictionary and return it if it's a simple attribute. Otherwise, if the element contains an object or list, redirect this element at the corresponding class. :param item: the key of the base properties dict :return: the item in the attributes dictionary if it's present, raise AttributeError otherwise. """ try: if isinstance(self.attributes[item], dict): return Properties.from_dict(parent_attr=item, parent=self, attributes=self.attributes[item]) else: return self.attributes[item] except KeyError as e: raise AttributeError("%s object has no attribute %s" % (type(self).__name__, e))
def test_create_customer_with_default_schema(self): c = Customer(node=self.node, default_attributes={ 'prop': { 'prop1': 'value1' }, 'prop2': 'value2' }, prop3=Properties(prop4='value4')) internal = { 'prop': { 'prop1': 'value1' }, 'prop2': 'value2', 'prop3': { 'prop4': 'value4' } } assert c.attributes == internal, c.attributes
def test_customer_properties_unexistent_attr(self): with self.assertRaises(AttributeError) as context: p = Properties({'attributo': 1}) attr = p.attr self.assertTrue('attr' in str(context.exception))