def test_client_returns_list_of_records_on_bulk_create(person_object: Object, client: Client): records_to_create = [ Record(obj=person_object, name='Ivan Petrov', is_active=False, street="street", age=42), Record(obj=person_object, name='Nikolay Kozlov', is_active=True, street="street", age=36) ] with requests_mock.Mocker() as mocker: mocker.put('/'.join( [client.server_url, 'data/bulk/{}'.format(person_object.name)]), json={ 'status': 'OK', 'data': [{ **x.serialize(), **{ 'id': records_to_create.index(x) + 1 } } for x in records_to_create] }) created_records = client.records.bulk_create(*records_to_create) for created_record in created_records: assert_that(created_record, instance_of(Record)) assert_that(created_record.name)
def test_client_returns_list_of_records_on_bulk_update(person_object: Object, client: Client): records_to_update = [ Record(obj=person_object, name='Ivan Petrov', is_active=False, age=15, id=23, street=""), Record(obj=person_object, name='Nikolay Kozlov', is_active=True, age=44, id=34, street="") ] with requests_mock.Mocker() as mocker: mocker.post('/'.join( [client.server_url, 'data/bulk/{}'.format(person_object.name)]), json={ 'status': 'OK', 'data': [x.serialize() for x in records_to_update] }) updated_records = client.records.bulk_update(*records_to_update) for updated_record in updated_records: assert_that(updated_record, instance_of(Record)) assert_that(updated_record.name) assert_that(updated_records, equal_to(records_to_update))
def setup_objects(self, client): if not client.objects.get('a'): a_object = Object(name='a', fields=[ NumberField(name='id', optional=True, default={'func': 'nextval'}), StringField(name='name') ], key='id', cas=False, objects_manager=client.objects) b_object = Object(name='b', key='id', cas=False, fields=[ NumberField(name='id', optional=True, default={'func': 'nextval'}), ObjectsField(name='a_records', link_type=LINK_TYPES.INNER, obj=a_object) ], objects_manager=client.objects) self.a_object = client.objects.create(a_object) self.b_object = client.objects.create(b_object) self.a_record = client.records.create( Record(self.a_object, name="A record")) else: self.a_object = client.objects.get('a') self.b_object = client.objects.get('b') self.a_record = client.records.create( Record(self.a_object, name="A record"))
def test_slice(self, client: Client): client.records.delete(*[x for x in client.records.query('person')]) assert_that(client.records.query('person'), has_length(0)) client.records.create( Record(obj='person', **{ 'name': 'Feodor', 'is_active': True, 'age': 23, 'street': 'street' }), Record(obj='person', **{ 'name': 'Victor', 'is_active': False, 'age': 22, 'street': 'street' }), Record(obj='person', **{ 'name': 'Artem', 'is_active': True, 'age': 35, 'street': 'street' }), Record(obj='person', **{ 'name': 'Anton', 'is_active': False, 'age': 55, 'street': 'street' })) assert_that(client.records.query('person'), has_length(4)) two_first_records = client.records.query('person')[:2] assert_that(two_first_records, has_length(2))
def setup_objects(self, client): if not client.objects.get('a'): object_a = Object(name='a', fields=[ NumberField(name='id', optional=True, default={'func': 'nextval'}) ], key='id', cas=False, objects_manager=client.objects) object_b = Object(name='b', key='id', cas=False, fields=[ NumberField(name='id', optional=True, default={'func': 'nextval'}), GenericField(name='target_object', link_type=LINK_TYPES.INNER, objs=[object_a]), StringField(name='name'), ], objects_manager=client.objects) object_a_with_manually_set_b_set = Object( name='a', fields=[ NumberField(name='id', optional=True, default={'func': 'nextval'}), GenericField(name='b_set', link_type=LINK_TYPES.OUTER, obj=object_b, outer_link_field='target_object', optional=True) ], key='id', cas=False, objects_manager=client.objects) client.objects.create(object_a) self.object_b = client.objects.create(object_b) self.object_a = client.objects.update( object_a_with_manually_set_b_set) a_record = Record(self.object_a) self.a_record = client.records.create(a_record) b_record = Record(self.object_b, target_object={ "_object": "a", "id": self.a_record.id }, name="B record") self.b_record = client.records.create(b_record) else: self.object_b = client.objects.get('b') self.object_a = client.objects.get('a') self.a_record = client.records.query(self.object_a)[0] self.b_record = client.records.query(self.object_b)[0]
def two_records(client, existing_person_object): client.records.bulk_delete(*[x for x in client.records.query(existing_person_object)]) first_record = Record(obj=existing_person_object, **{'name': 'Feodor', 'is_active': True, 'age': 20, 'street': 'street'}) second_record = Record(obj=existing_person_object, **{'name': 'Victor', 'is_active': False, 'age': 40, 'street': 'street'}) first_record, second_record = client.records.bulk_create(first_record, second_record) return first_record, second_record
def test_client_updates_record_with_partial_data(existing_person_object: Object, client: Client): record = Record(obj=existing_person_object, id=23, age=20, name='Ivan', is_active=True, street="Street") record = client.records.create(record=record) assert_that(record, instance_of(Record)) # perform partial update record = client.records.partial_update(existing_person_object, record.get_pk(), {'name': 'Petr'}) assert_that(record.name, equal_to('Petr')) # check that new data got stored in Custodian record = client.records.get(existing_person_object, record.get_pk()) assert_that(record.name, equal_to('Petr'))
def test_new_record_can_be_retrieved_by_pk(self, person_record: Record, client: Client): """ Create a new record and check it has PK field assigned :param person_record: :param client: """ person_record.id = None person_record = client.records.create(person_record) assert_that( client.records.get(person_record.obj, person_record.get_pk()), instance_of(Record))
def test_record_is_deleted(self, person_record: Record, client: Client): """ Delete the record and verify it does not exist in the database :param person_record: :param client: """ person_record = client.records.create(person_record) pk = person_record.get_pk() deleted_record = client.records.delete(person_record) assert_that(person_record.get_pk(), is_(None)) assert_that(client.records.get(person_record.obj, pk), is_(None)) assert_that(deleted_record, instance_of(Record))
def test_client_returns_list_of_records_without_pk_value_on_bulk_delete( client: Client): records_to_delete = [ Record(obj='person', name='Ivan Petrov', is_active=False, id=23), Record(obj='person', name='Nikolay Kozlov', is_active=True, id=34) ] with requests_mock.Mocker() as mocker: mocker.delete('/'.join([client.server_url, 'data/person']), json={'status': 'OK'}) client.records.delete(*records_to_delete) for record in records_to_delete: assert_that(not record.exists())
def test_client_deletes(person_record: Record, client: Client): record_data = person_record.data record_data['id'] = 99 assert_that(person_record.exists()) record_data = person_record.data with requests_mock.Mocker() as mocker: mocker.delete('/'.join( [client.server_url, 'data/{}'.format(person_record.obj)]), json={ 'status': 'OK', 'data': record_data }) client.records.delete(person_record) assert_that(person_record.exists(), is_not(True))
def test_record_is_updated(self, person_record: Record, client: Client): """ Change the record`s field value and store this change in the database :param person_record: :param client: """ person_record.id = None assert_that(person_record.id, is_(None)) person_record = client.records.create(person_record) new_name = 'Feodor' assert_that(new_name, not_(equal_to(person_record.name))) person_record.name = new_name person_record = client.records.update(person_record) assert_that(person_record.name, equal_to(new_name))
def test_client_returns_none_for_nonexistent_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': 'FAIL', 'data': {} }) record = client.records.get(person_record.obj, person_record.get_pk()) assert_that(record, is_(None))
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_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 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 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_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.data record_data['id'] = 45 with requests_mock.Mocker() as mocker: mocker.post('/'.join( [client.server_url, 'data/{}'.format(person_record.obj)]), 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_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 delete(self, record: Record): """ Deletes the record from the Custodian :param record: :return: """ self.client.execute(command=Command(name=self._get_record_command_name( record.obj, record.get_pk()), method=COMMAND_METHOD.DELETE)) setattr(record, record.obj.key, None)
def bulk_create(self, *records: Record): """ Creates new records in the Custodian :param records: :return: """ self._check_records_have_same_object(*records) obj = records[0].obj data, ok = self.client.execute( command=Command(name=self._get_record_command_name(obj), method=COMMAND_METHOD.POST), data=[record.serialize() for record in records]) records = [] if ok: for i in range(0, len(data)): records.append(Record(obj, **data[i])) return list(records) else: raise CommandExecutionFailureException(data.get('Msg'))
def two_records(client): client.records.delete(*[x for x in client.records.query('person')]) first_record = Record(obj='person', **{ 'name': 'Feodor', 'is_active': True, 'age': 20, 'street': 'street' }) second_record = Record(obj='person', **{ 'name': 'Victor', 'is_active': False, 'age': 40, 'street': 'street' }) first_record, second_record = client.records.create( first_record, second_record) return first_record, second_record
def test_field_value_serializing_with_nested_record_as_array_of_ids( self, client: Client): self.setup_objects(client) b_record = Record(self.b_object, a_records=[self.a_record]) # create and check value with depth set to 1 b_record_with_depth_set_to_1 = client.records.create(b_record) serialized_data = b_record_with_depth_set_to_1.serialize() assert_that(serialized_data['a_records'], has_length(1)) assert_that(serialized_data['a_records'][0], instance_of(float))
def test_field_value_creation_and_retrieving_with_nested_record_as_object_and_depth_set_to_2( self, client: Client): self.setup_objects(client) b_record = Record(self.b_object, a_records=[self.a_record]) # create and check value with depth set to 2 b_record_with_depth_set_to_2 = client.records.create(b_record, depth=2) assert_that(b_record_with_depth_set_to_2.a_records, instance_of(list)) assert_that(b_record_with_depth_set_to_2.a_records, has_length(1)) assert_that(b_record_with_depth_set_to_2.a_records[0], instance_of(Record))
def get(self, obj_name: str, record_id: str, **kwargs): """ Retrieves an existing record from Custodian :param obj_name: :param record_id: """ data, ok = self.client.execute( command=Command(name=self._get_record_command_name(obj_name, record_id), method=COMMAND_METHOD.GET), params=kwargs ) return Record(obj_name, **data) if ok else None
def test_record_cration_with_given_id(self, person_record: Record, client: Client): """ Create a new record with given id :param person_record: :param client: """ person_record.id = 199 person_record = client.records.create(person_record) assert_that(person_record, instance_of(Record)) assert_that(person_record.id, equal_to(199))
def test_new_record_is_created(self, person_record: Record, client: Client): """ Create a new record and check it has PK field assigned :param person_record: :param client: """ person_record.id = None assert_that(person_record.id, is_(None)) # create new record person_record = client.records.create(person_record) assert_that(person_record.id, instance_of(int))
def test_records_are_created(self, person_object: Object, client: Client): """ Create two records and verify pk values are assigned :param person_object: :param client: """ first_record = Record(obj=person_object, **{ 'name': 'Feodor', 'is_active': True, 'age': 23, "street": "St" }) second_record = Record(obj=person_object, **{ 'name': 'Victor', 'is_active': False, 'age': 22, "street": "St" }) first_record, second_record = client.records.bulk_create( first_record, second_record) assert_that(first_record.get_pk(), instance_of(int)) assert_that(second_record.get_pk(), instance_of(int)) self._created_records = (first_record, second_record)
def test_new_record_is_not_created_if_pk_is_duplicated( self, person_record: Record, client: Client): """ Try to create a new record with duplicated PK. Client should raise exception :param person_record: :param client: """ person_record.id = None assert_that(person_record.id, is_(None)) # create new record person_record = client.records.create(person_record) with pytest.raises(RecordAlreadyExistsException): client.records.create(person_record)