def test_client_retrieves_list_of_objects(client: Client, person_object: Object): with requests_mock.Mocker() as mocker: mocker.get('/'.join([client.server_url, 'meta/']), json={ 'status': 'OK', 'data': [person_object.serialize()] }) objs = client.objects.get_all() assert_that(objs, has_length(1)) assert_that(person_object.serialize(), equal_to(objs[0].serialize()))
def test_client_retrieves_existing_object(person_object: Object): client = Client(server_url='http://mocked/custodian') with requests_mock.Mocker() as mocker: mocker.get('http://mocked/custodian/meta/{}'.format( person_object.name), json={ 'status': 'OK', 'data': person_object.serialize() }) obj = client.objects.get(person_object.name) assert_that(obj, is_(instance_of(Object))) assert_that(person_object.serialize(), equal_to(obj.serialize()))
def test_client_makes_correct_request_on_object_creation( person_object: Object, client: Client): with requests_mock.Mocker() as mocker: mocker.put('/'.join([client.server_url, 'meta']), json={ 'status': 'OK', 'data': person_object.serialize() }) mocker.get('/'.join([client.server_url, 'meta', person_object.name]), json={ 'status': 'OK', 'data': person_object.serialize() }) client.objects.create(person_object) assert_that(person_object, is_(instance_of(Object)))
def test_object_with_related_outer_object_field_serializes_itself(client): address_object = Object(name='address', key='id', cas=True, fields=[ NumberField(name='id', optional=True), StringField(name='street') ], objects_manager=client.objects) person_obj = Object(name='person', key='id', cas=True, fields=[ NumberField(name='id', optional=True), StringField(name='street'), RelatedObjectField(name='addresses', obj=address_object, outer_link_field='address_id', link_type=LINK_TYPES.OUTER, many=True) ], objects_manager=client.objects) serialized_object = person_obj.serialize() assert_that(serialized_object, is_(instance_of(dict))) expected_serialized_field = { 'name': 'addresses', 'type': 'array', 'optional': False, 'linkMeta': 'address', 'linkType': 'outer', 'outerLinkField': 'address_id' } assert_that(serialized_object['fields'][2], equal_to(expected_serialized_field))
def test_field_serialization(self, existing_person_object, client): address_object = Object(name='address', key='id', cas=False, fields=[ NumberField(name='id', optional=True), StringField(name='street'), ObjectsField(name='neighbours', obj=existing_person_object, link_type=LINK_TYPES.INNER) ], objects_manager=client.objects) serialized_object = address_object.serialize() assert_that(serialized_object, is_(instance_of(dict))) expected_serialized_field = { 'name': 'neighbours', 'type': 'objects', 'optional': False, 'linkMeta': 'person', 'linkType': 'inner' } assert_that(serialized_object['fields'][2], equal_to(expected_serialized_field)) client.objects.create(address_object)
def test_client_makes_correct_request_on_object_update(person_object: Object, client: Client): with requests_mock.Mocker() as mocker: mocker.post('/'.join( [client.server_url, 'meta/{}'.format(person_object.name)]), json={ 'status': 'OK', 'data': person_object.serialize() }) # mock preprocess request mocker.get('/'.join( [client.server_url, 'meta/{}'.format(person_object.name)]), json={ 'status': 'OK', 'data': person_object.serialize() }) obj = client.objects.update(person_object) assert_that(obj, is_(instance_of(Object)))
def test_client_makes_correct_request_on_object_delete(person_object: Object): client = Client(server_url='http://mocked/custodian') with requests_mock.Mocker() as mocker: mocker.delete('/'.join( [client.server_url, 'meta/{}'.format(person_object.name)]), json={ 'status': 'OK', 'data': person_object.serialize() }) obj = client.objects.delete(person_object) assert_that(obj, is_(instance_of(Object)))
def test_object_serializes_itself(client): obj = Object(name='person', key='id', cas=True, fields=[ NumberField(name='id', optional=True), StringField(name='name'), BooleanField(name='is_active') ], objects_manager=client.objects) serialized_object = obj.serialize() assert_that(serialized_object, is_(instance_of(dict))) assert_that(serialized_object, has_entry('name', obj.name)) assert_that(serialized_object['fields'][0]['type'], equal_to('number'))
def update(self, obj: Object) -> Object: """ Updates existing object in Custodian :param obj: :return: """ self._pre_process_reverse_relations(obj) data, ok = self.client.execute(command=Command( name=self._get_object_command_name(obj.name), method=COMMAND_METHOD.POST), data=obj.serialize()) if ok: self._cache.flush() return obj else: raise ObjectUpdateException(data.get('Msg'))
def test_inner_generic_field_serialization(self, client: Client): object_a = Object(name='a', fields=[NumberField(name='id')], key='id', cas=False, objects_manager=client.objects) object_b = Object(name='b', key='id', cas=False, fields=[ NumberField(name='id', optional=True), GenericField(name='target_object', link_type=LINK_TYPES.INNER, objs=[object_a]) ], objects_manager=client.objects) object_b.fields["target_object"].objs = [object_a] object_a_with_manually_set_b_set = Object( name='a', fields=[ NumberField(name='id'), GenericField(name='b_set', link_type=LINK_TYPES.OUTER, obj=object_b, outer_link_field='target_object') ], key='id', cas=False, objects_manager=client.objects) serialized_object_b = object_b.serialize() assert_that(serialized_object_b["fields"][1], has_entry("linkMetaList", [object_a.name])) assert_that(serialized_object_b["fields"][1], has_entry("type", "generic")) assert_that(serialized_object_b["fields"][1], has_entry("name", "target_object")) client.objects.create(object_a) client.objects.create(object_b) client.objects.update(object_a_with_manually_set_b_set)
def test_actions_serialization(self, client: Client): object_a = Object(name='a', fields=[NumberField(name='id')], key='id', cas=False, objects_manager=client.objects, actions=[ Action(method=METHODS.CREATE, protocol='REST', args=["http://localhost/"], include_values={'a': 'b.c.name'}) ]) serialized_object = object_a.serialize() assert_that(serialized_object['actions'], has_length(1)) assert_that(serialized_object['actions'][0]['method'], equal_to(METHODS.CREATE)) assert_that(serialized_object['actions'][0]['protocol'], equal_to('REST')) assert_that(serialized_object['actions'][0]['args'], has_length(1)) assert_that(serialized_object['actions'][0]['includeValues'], has_length(1)) client.objects.create(object_a)
def test_object_with_related_inner_object_field_serializes_itself( person_object, client): obj = Object(name='address', key='id', cas=True, fields=[ NumberField(name='id', optional=True), StringField(name='street'), RelatedObjectField(name='person', obj=person_object, link_type=LINK_TYPES.INNER) ], objects_manager=client.objects) serialized_object = obj.serialize() assert_that(serialized_object, is_(instance_of(dict))) expected_serialized_field = { 'name': 'person', 'type': 'object', 'optional': False, 'linkMeta': 'person', 'linkType': 'inner' } assert_that(serialized_object['fields'][2], equal_to(expected_serialized_field))