def update(id_: str, phone_id: str, entity: dict, dao: GenericSQLDAO = None): entity_to_update = dao.get(phone_id) if entity_to_update.contact.id_ != id_: entity_to_update = None if not entity_to_update: return error_response(status_code=404, message="Phone not found", data={"id_": id_}) entity_fields = dao.fields.keys() for key, value in entity.items(): if key not in entity_fields: raise KeyError("{key} not in {entity}" .format(key=key, entity=dao.return_class)) entity_to_update.__dict__[key] = value dao.update(entity_to_update) return success_response(message="Phone updated", data={"Phone": dict(entity_to_update)})
def read_one(id_: str, dao: GenericSQLDAO = None): result = dao.get(id_=id_) if not result: return success_response(status_code=404, message="EntityForTest not found in database", data={"id_": id_}) return success_response(message="EntityForTest retrieved", data={"EntityForTest": dict(result)})
def delete(id_: str, dao: GenericSQLDAO): entity = dao.get(id_=id_) if not entity: return error_response(status_code=404, message="EntityForTest not found", data={"id_": id_}) dao.remove(entity) return success_response(message="EntityForTest deleted", data={"EntityForTest": dict(entity)})
def read_one(id_: str, phone_id: str, dao: GenericSQLDAO = None): result = dao.get(id_=phone_id) if result.contact.id_ != id_: result = None if not result: return success_response(status_code=404, message="Phone not found in database", data={"id_": id_}) return success_response(message="Phone retrieved", data={"Phone": dict(result)})
def delete(id_: str, phone_id: str, dao: GenericSQLDAO): entity = dao.get(id_=phone_id) if entity.contact.id_ != id_: entity = None if not entity: return error_response(status_code=404, message="Phone not found", data={"id_": id_}) dao.remove(entity) return success_response(message="Phone deleted", data={"Phone": dict(entity)})
def test_get_child(self, mysql_mock): generic_dao = GenericSQLDAO(return_class=TestEntityWithChild, prefix='') id_ = "12345678901234567890123456789012" db = mysql_mock.return_value db.get_results.return_value = [[ id_, datetime(2020, 7, 26, 12, 00, 00), datetime(2020, 7, 26, 12, 00, 00), "Anom", None, id_ ]] entity = generic_dao.get(id_=id_) assert mysql_mock.mock_calls[1] == call().query( "SELECT id_, creation_datetime, last_modified_datetime," " name, birthday, child_id_ " "FROM test_entity_with_childs WHERE id_ = %s " "LIMIT %s OFFSET %s;", ['12345678901234567890123456789012', 1, 0]) assert entity == TestEntityWithChild(id_, datetime(2020, 7, 26, 12, 00, 00), datetime(2020, 7, 26, 12, 00, 00), child=TestEntity(id_))