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 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 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_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 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 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_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 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 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_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 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_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 create(self, *records: Record, **kwargs): """ Creates new records in the Custodian. :param records: """ requests_data = self._get_request_data(*records) 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=requests_data, params=kwargs ) records = [] if ok: if isinstance(data, dict): return Record(obj, **data) elif isinstance(data, list): return [Record(obj, **d) for d in data] elif data.get('Msg', '').find('duplicate') != -1: raise RecordAlreadyExistsException else: raise CommandExecutionFailureException(data.get('Msg'))
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 factory(cls, obj, **raw_data): from trood.api.custodian.records.model import Record values = {} # Use meta fields only if object is evaluated if obj._evaluated: for field_name in obj.fields.keys(): if field_name in raw_data: field = obj.fields[field_name] if field_name in obj.fields: values[field_name] = cls.factory_field_value(field, raw_data[field_name]) else: values = raw_data record = Record(obj, _factory_mode=True) for key, value in values.items(): setattr(record, key, value) return record
def _query(self, obj_name: str, query_string: str, **kwargs): """ Performs an Custodian API call and returns a list of records :param obj_name: :param query_string: """ if kwargs.get("omit_outers", None) is False: del kwargs['omit_outers'] data, _ = self.client.execute( command=Command(name=self._get_record_command_name(obj_name), method=COMMAND_METHOD.GET), params={'q': query_string, **kwargs} ) records = [] for record_data in data: records.append(Record(obj_name, **record_data)) return records
def partial_update(self, obj: Object, pk, values, **kwargs): """ Performs partial update of existing record """ self._validation_class.validate_partial(obj, values) data, ok = self.client.execute( command=Command(name=self._get_record_command_name(obj, pk), method=COMMAND_METHOD.PATCH), data=self._serialization_class.serialize(obj, values), params=kwargs) if ok: return Record(obj=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 partial_update(self, obj_name: str, pk, values, **kwargs): """ Performs partial update of existing record :param obj_name: """ data, ok = self.client.execute( command=Command(name=self._get_record_command_name(obj_name, pk), method=COMMAND_METHOD.PATCH), data=values, params=kwargs ) if ok: return Record(obj_name, **data) else: if data.get('code') == 'cas_failed': raise CasFailureException(data.get('Msg', '')) else: raise RecordUpdateException(data.get('Msg', ''))
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 test_datetime_field_handled_right(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", "created_at": datetime.datetime.now() + datetime.timedelta(days=4) }) first_record = client.records.create(first_record) assert_that(first_record.created_at, instance_of(datetime.datetime))
def person_record(person_object): return Record(obj=person_object, id=23, age=20, name='Ivan', is_active=True, street="Street")
def person_record(): return Record(obj='person', age=20, name='Ivan', is_active=True, street="Street")