def test_client_retrieves_existing_record(person_record: Record, client: Client): with requests_mock.Mocker() as mocker: mocker.get('/'.join([ client.server_url, 'data/single/{}/{}'.format(person_record.obj.name, person_record.get_pk()) ]), json={ 'status': 'OK', 'data': person_record.serialize() }) record = client.records.get(person_record.obj, person_record.get_pk()) assert_that(record, is_(instance_of(Record)))
def test_client_deletes(person_record: Record, client: Client): assert_that(person_record.exists()) record_data = person_record.serialize() with requests_mock.Mocker() as mocker: mocker.delete('/'.join([ client.server_url, 'data/single/{}/{}'.format(person_record.obj.name, person_record.get_pk()) ]), json={ 'status': 'OK', 'data': record_data }) client.records.delete(person_record) assert_that(person_record.exists(), is_not(True))
def test_record_is_serialized_with_generic_field_value(self, task_object: Object, person_object: Object): """ Create two records and verify pk values are assigned :param person_object: :param client: """ task_record = Record( task_object, **{'name': 'Some task', 'owner': {"_object": person_object.name, person_object.key: 5}} ) data = task_record.serialize() assert_that(data, instance_of(dict)) assert_that(data, has_entry('name', 'Some task')) assert_that(data, has_entry('owner', {'_object': person_object.name, person_object.key: 5}))
def update(self, record: Record, **kwargs): """ Updates an existing record in the Custodian """ data, ok = self.client.execute(command=Command( name=self._get_record_command_name(record.obj, record.get_pk()), method=COMMAND_METHOD.PATCH), data=record.serialize(), params=kwargs) if ok: return Record(obj=record.obj, **data) else: if data.get('code') == 'cas_failed': raise CasFailureException(data.get('Msg', '')) else: raise RecordUpdateException(data.get('Msg', ''))
def create(self, record: Record, **kwargs) -> Record: """ Creates a new record in the Custodian :param record: :return: """ data, ok = self.client.execute(command=Command( name=self._get_record_command_name(record.obj), method=COMMAND_METHOD.POST), data=record.serialize(), params=kwargs) if ok: return Record(obj=record.obj, **data) elif data.get('Msg', '').find('duplicate') != -1: raise RecordAlreadyExistsException else: raise CommandExecutionFailureException(data.get('Msg'))
def test_client_returns_updated_record_on_record_update( person_record: Record, client: Client): record_data = person_record.serialize() record_data['is_active'] = False with requests_mock.Mocker() as mocker: mocker.post('/'.join([ client.server_url, 'data/single/{}/{}'.format(person_record.obj.name, person_record.get_pk()) ]), json={ 'status': 'OK', 'data': record_data }) record = client.records.update(person_record) assert_that(record, is_(instance_of(Record))) assert_that(record.exists()) assert_that(record.is_active, is_(False))
def test_client_returns_new_record_on_record_creation(person_record: Record, client: Client): person_record.id = None assert_that(person_record.exists(), is_not(True)) record_data = person_record.serialize() record_data['id'] = 45 with requests_mock.Mocker() as mocker: mocker.put('/'.join([ client.server_url, 'data/single/{}'.format(person_record.obj.name) ]), json={ 'status': 'OK', 'data': record_data }) record = client.records.create(person_record) assert_that(record, is_(instance_of(Record))) assert_that(record.exists()) assert_that(record.id, equal_to(45))
def test_client_returns_iterable_of_records_on_bulk_query( person_object: Object, person_record: Record, client: Client): query = client.records.query(person_object).filter(address__city_id__eq=45) with requests_mock.Mocker() as mocker: mocker.get( '/'.join( [client.server_url, 'data/bulk/{}'.format(person_object.name)]), json={ 'status': 'OK', 'data': [ # nothing terrible in is that records are duplicated person_record.serialize(), person_record.serialize() ] }) assert_that(len(query), equal_to(2)) for record in query: assert_that(record, instance_of(Record))