def test_searching_over_collection_elements(drone_doc_parsed_classes, drone_doc, session): """Test searching over collection elements.""" expanded_base_url = DocUrl.doc_url for class_ in drone_doc_parsed_classes: target_property_1 = '' target_property_2 = '' for prop in drone_doc.parsed_classes[class_][ 'class'].supportedProperty: if isinstance(prop.prop, HydraLink): continue # Find nested object so we can test searching of elements by # properties of nested objects. if expanded_base_url in prop.prop: object_ = gen_dummy_object(class_, drone_doc) # Setting property of a nested object as target for property_ in object_[prop.title]: if property_ != '@type': object_[prop.title][property_] = 'target_1' target_property_1 = '{}[{}]'.format( prop.title, property_) break break elif target_property_1 is not '': for property_ in object_: if property_ != '@type': object_[property_] = 'target_2' target_property_2 = property_ break break if target_property_1 is not '' and target_property_2 is not '': # Set search parameters search_params = { target_property_1: 'target_1', target_property_2: 'target_2' } obj_id = str(uuid.uuid4()) response = crud.insert(object_=object_, id_=obj_id, session=session) search_result = crud.get_collection( API_NAME='api', type_=class_, session=session, paginate=True, page_size=5, search_params=search_params) assert len(search_result['members']) > 0 search_item_id = search_result['members'][0]['@id'].split( '/')[-1] assert search_item_id == obj_id break
def test_searching(self): """Test searching over collection elements.""" for class_ in self.doc_collection_classes: target_property_1 = "" target_property_2 = "" for prop in self.doc.parsed_classes[class_][ "class"].supportedProperty: if isinstance(prop.prop, HydraLink): continue # Find nested object so we can test searching of elements by # properties of nested objects. if "vocab:" in prop.prop: object_ = gen_dummy_object(class_, self.doc) # Setting property of a nested object as target for property_ in object_[prop.title]: if property_ != "@type": object_[prop.title][property_] = "target_1" target_property_1 = "{}[{}]".format( prop.title, property_) break break elif target_property_1 is not "": for property_ in object_: if property_ != "@type": object_[property_] = "target_2" target_property_2 = property_ break break if target_property_1 is not "" and target_property_2 is not "": # Set search parameters search_params = { target_property_1: "target_1", target_property_2: "target_2" } obj_id = str(uuid.uuid4()) response = crud.insert(object_=object_, id_=obj_id, session=self.session) search_result = crud.get_collection( API_NAME="api", type_=class_, session=self.session, paginate=True, page_size=5, search_params=search_params) assert len(search_result["members"]) > 0 search_item_id = search_result["members"][0]["@id"].split( '/')[-1] assert search_item_id == obj_id break
def get(self, type_: str) -> Response: """Retrieve a collection of items from the database.""" if get_authentication(): if request.authorization is None: return failed_authentication() else: try: auth = check_authorization(request, get_session()) if auth is False: return failed_authentication() except Exception as e: status_code, message = e.get_HTTP() # type: ignore return set_response_headers(jsonify(message), status_code=status_code) endpoint_ = checkEndpoint("GET", type_) if endpoint_['method']: # Collections if type_ in get_doc().collections: collection = get_doc().collections[type_]["collection"] try: response = crud.get_collection(get_api_name(), collection.class_.title, session=get_session()) return set_response_headers(jsonify(hydrafy(response))) except Exception as e: status_code, message = e.get_HTTP() # type: ignore return set_response_headers(jsonify(message), status_code=status_code) # Non Collection classes elif type_ in get_doc( ).parsed_classes and type_ + "Collection" not in get_doc( ).collections: try: response = crud.get_single(type_, api_name=get_api_name(), session=get_session()) return set_response_headers(jsonify(hydrafy(response))) except Exception as e: status_code, message = e.get_HTTP() # type: ignore return set_response_headers(jsonify(message), status_code=status_code) abort(endpoint_['status'])
def on_get(self, req, resp, type_): """Retrieve a collection of items from the database.""" if get_authentication(resp): if req.auth is None: return failed_authentication(resp) else: try: auth = check_authorization(req, get_session(resp)) if auth is False: return failed_authentication(resp) except Exception as e: status_code, message = e.get_HTTP() # type: ignore resp.media = message return set_response_headers(resp, status_code=status_code) if checkEndpoint(resp, "GET", type_): # Collections if type_ in get_doc(resp).collections: collection = get_doc(resp).collections[type_]["collection"] try: resp.media = crud.get_collection(get_api_name(resp), collection.class_.title, session=get_session(resp)) return set_response_headers(resp) except Exception as e: status_code, message = e.get_HTTP() resp.media = message return set_response_headers(resp, status_code=status_code) # Non Collection classes elif type_ in get_doc( resp ).parsed_classes and type_ + "Collection" not in get_doc( resp).collections: try: resp.media = hydrafy( resp, crud.get_single(type_, api_name=get_api_name(resp), session=get_session(resp))) return set_response_headers(resp) except Exception as e: status_code, message = e.get_HTTP() resp.media = message return set_response_headers(resp, status_code=status_code)
def get(self, path: str) -> Response: """ Retrieve a collection of items from the database. """ auth_response = check_authentication_response() if isinstance(auth_response, Response): return auth_response endpoint_ = checkEndpoint("GET", path) if endpoint_['method']: # If endpoint and GET method is supported in the API if path in get_doc().collections: # If collection name in document's collections collection = get_doc().collections[path]["collection"] try: # Get collection details from the database response = crud.get_collection(get_api_name(), collection.class_.title, session=get_session(), path=path) return set_response_headers( jsonify(hydrafy(response, path=path))) except ClassNotFound as e: status_code, message = e.get_HTTP() return set_response_headers(jsonify(message), status_code=status_code) # If class is supported elif path in get_doc( ).parsed_classes and path + "Collection" not in get_doc( ).collections: try: class_type = get_doc().parsed_classes[path]['class'].title response = crud.get_single(class_type, api_name=get_api_name(), session=get_session(), path=path) return set_response_headers( jsonify(hydrafy(response, path=path))) except (ClassNotFound, InstanceNotFound) as e: status_code, message = e.get_HTTP() return set_response_headers(jsonify(message), status_code=status_code) abort(endpoint_['status'])
def get(self, type_: str) -> Response: """ Retrieve a collection of items from the database. """ auth_response = check_authentication_response() if type(auth_response) == Response: return auth_response endpoint_ = checkEndpoint("GET", type_) if endpoint_['method']: # If endpoint and GET method is supported in the API if type_ in get_doc().collections: # If collection name in document's collections collection = get_doc().collections[type_]["collection"] try: # Get collection details from the database response = crud.get_collection(get_api_name(), collection.class_.title, session=get_session()) return set_response_headers(jsonify(hydrafy(response))) except Exception as e: status_code, message = e.get_HTTP() # type: ignore return set_response_headers(jsonify(message), status_code=status_code) # If class is supported elif type_ in get_doc( ).parsed_classes and type_ + "Collection" not in get_doc( ).collections: try: response = crud.get_single(type_, api_name=get_api_name(), session=get_session()) return set_response_headers(jsonify(hydrafy(response))) except Exception as e: status_code, message = e.get_HTTP() # type: ignore return set_response_headers(jsonify(message), status_code=status_code) abort(endpoint_['status'])