def test_connect_method_delete_qt_rest_api_err(self, session): response = Mock() response.status_code = 401 session.return_value.delete.return_value = response with self.assertRaises(QtRestApiError): connect(method='delete', uri='some_uri', headers='some_headers')
def test_connect_method_get_data_type_none_qt_rest_api_err(self, session): response = Mock() response.status_code = 401 session.return_value.get.return_value = response with self.assertRaises(QtRestApiError): connect(method='get', uri='some_uri', headers='some_headers')
def test_connect_method_delete_conn_err(self, session): mock = Mock() mock.delete.side_effect = requests.exceptions.ConnectionError( "Connection error") session.return_value = mock with self.assertRaises(QtConnectionError): connect(method='delete', uri='some_uri', headers='some_headers')
def test_connect_method_post_data_type_data_conn_err(self, session): mock = Mock() mock.post.side_effect = requests.exceptions.ConnectionError( "Connection error") session.return_value = mock with self.assertRaises(QtConnectionError): connect(method='post', uri='some_uri', headers='some_headers', data_type="data")
def read(self) -> List: """ Fetch model where model_id is existing ID""" self.headers["Content-Type"] = "application/json" res = connect("get", f"{self.url}users/profile", self.headers) re_dict = res.json() del self.headers['Content-Type'] if "settings" in re_dict: model_list = [] for i in re_dict["settings"]: model = Model() if "files" in i: document_list = [] for f in i["files"]: document = Document() document.set_id(f) document_list.append(document) model.with_documents(document_list) model.set_id(i["id"]) model.set_description(i["title"]) model.set_workers(i["numWorkers"]) if "searchDictionaries" in i: for ex in i["searchDictionaries"]: model.get_extractor(ex) model_list.append(model) else: model_list = [] return model_list
def test_connect_equal_get(self, session): response = Mock() response.status_code = 200 session.return_value.get.return_value = response result = connect(method='get', uri='some_uri', headers='some_headers') self.assertEqual(response, result)
def delete(self, model_id: str) -> bool: """Delete data container""" if not isinstance(model_id, str): raise QtArgumentError("Argument type error: String is expected as model_id") res = connect("delete", f"{self.url}search/{model_id}", self.headers) return res.ok
def test_connect_method_delete(self, session): response = Mock() response.status_code = 200 session.return_value.delete.return_value = response result = connect(method='delete', uri='some_uri', headers='some_headers') self.assertEqual(response, result)
def fetch(self, job_id: str) -> Dict: """ Fetch job where job_id is existing ID""" self.headers["Content-Type"] = "application/json" if not isinstance(job_id, str): raise QtArgumentError("Argument type error: String is expected as job_id") res = connect("get", f"{self.url}search/config/{job_id}", self.headers) self.id = res.json()['id'] del self.headers['Content-Type'] return res.json()
def test_connect_method_put_data_type_data(self, session): response = Mock() response.status_code = 200 session.return_value.put.return_value = response result = connect(method='put', uri='some_uri', headers='some_headers', data_type="data") self.assertEqual(response, result)
def progress(self, job_id: str = None) -> Dict: """Show progress for submitted for job""" url_path = "progress" if job_id is not None: if isinstance(job_id, str): url_path = f"{url_path}/{job_id}" else: raise QtArgumentError("Expected string") res = connect("get", f"{self.url}search/{url_path}", self.headers) return res.json()
def create(self, file: str) -> str: """Upload files for data mining""" if not isinstance(file, str): raise QtArgumentError( "Argument type error: String is expected as file path") files = {'file': open(file, 'rb')} res = connect("post", f"{self.url}search/file", self.headers, "files", files) return res.json()["uuid"]
def create(self) -> Dict: """Creating a new Job""" self.headers["Content-Type"] = "application/json" if description not in self.temp_dict: raise QtJobError("Job error: Description is mandatory. Please add description with set_description method") if files not in self.temp_dict: raise QtJobError("Job error: Please add list of documents with with_documents method ") res = connect("post", f"{self.url}search/new/{self.model_id}", self.headers, "data", json.dumps(self.temp_dict)) self.id = res.json()['id'] del self.headers['Content-Type'] return res.json()
def fetch(self, model_id: str) -> Model: """ Fetch model where model_id is existing ID""" self.headers["Content-Type"] = "application/json" if not isinstance(model_id, str): raise QtArgumentError("Argument type error: String is expected as model_id") res = connect("get", f"{self.url}search/config/{model_id}", self.headers) result = res.json() del self.headers['Content-Type'] self.id = model_id self.set_returned_data(result) return self
def read(self) -> List: """Convert to easy readable list""" result_list = [] res = connect("get", f"{self.url}reports/{self.id}/json", self.headers) for item in res.json(): if page_number in item and item[page_number]: page_num = item[page_number] if "values" in item and item["values"]: for i in item["values"]: field = Field() field.set_source(item["source"]) position = Position() position.set_page_num(page_num) if start in i: position.set_start(i[start]) if end in i: position.set_end(i[end]) if line in i: position.set_line(i[line]) field.set_position(position) if str_field in i: field.set_str(i[str_field]) if type_field in i: field.set_type(i[type_field]) if category in i: field.set_category(i[category]) if vocab_name in i: field.set_vocab_name(i[vocab_name]) if vocab_id in i: field.set_vocab_id(i[vocab_id]) list_field_ext = [] if "extIntervalSimples" in i and i["extIntervalSimples"]: for ext_int_item in i["extIntervalSimples"]: field_value = FieldValue() position_value = Position() position_value.set_page_num(page_num) if start in ext_int_item: position_value.set_start(ext_int_item[start]) if end in ext_int_item: position_value.set_end(ext_int_item[end]) if line in ext_int_item: position_value.set_line(ext_int_item[line]) field_value.set_position(position_value) if str_field in ext_int_item: field_value.set_str(ext_int_item[str_field]) list_field_ext.append(field_value) field.set_values(list_field_ext) result_list.append(field) return result_list
def clone(self, model_id: str) -> Model: """ Update model where model_id is existing ID""" self.headers["Content-Type"] = "application/json" if not isinstance(model_id, str): raise QtArgumentError("Argument type error: String is expected as model_id") if tag_files not in self.temp_dictionary: raise QtModelError("Model: You have to add files using with_document method and Document class") data = {tag_files: self.temp_dictionary[tag_files]} res = connect("post", f"{self.url}search/new/{model_id}", self.headers, "data", json.dumps(data)) result = res.json() self.id = model_id self.set_returned_data(result) del self.headers['Content-Type'] return self
def update(self, model_id: str, update_files: List) -> Model: """ Update model where model_id is existing ID""" self.headers["Content-Type"] = "application/json" if not isinstance(model_id, str): raise QtArgumentError("Argument type error: String is expected as model_id") if not isinstance(update_files, List): raise QtArgumentError("Argument type error: List is expected as update file") else: self.clear() data = {tag_files: update_files} res = connect("post", f"{self.url}search/update/{model_id}", self.headers, "data", json.dumps(data)) result = res.json() del self.headers['Content-Type'] self.id = model_id self.set_returned_data(result) return self
def raw_exporter(self, path: str) -> None: """Exporting in JSON format""" if not isinstance(path, str): raise QtArgumentError( "Argument type error: String is expected as path") directory = os.path.dirname(path) if len(directory) == 0: directory = "." if not (os.access(directory, os.W_OK) and os.access(directory, os.X_OK)): raise QtArgumentError("Argument error: No write permission") extension = os.path.splitext(path)[1].lower() if extension != ".json": raise QtFileTypeError( "File type error: Please use json extension saving file") res = connect("get", f"{self.url}reports/{self.id}/json", self.headers) with open(path, "w") as json_file: json.dump(res.json(), json_file)
def result_xlsx_exporter(self, path: str) -> None: """Exporting in Excel format""" if not isinstance(path, str): raise QtArgumentError( "Argument type error: String is expected as path") directory = os.path.dirname(path) if len(directory) == 0: directory = "." if not (os.access(directory, os.W_OK) and os.access(directory, os.X_OK)): raise QtArgumentError("Argument error: No write permission") extension = os.path.splitext(path)[1].lower() if extension != ".xlsx": raise QtFileTypeError( "File type error: Please use xlsx extension saving file") res = connect("get", f"{self.url}reports/{self.id}/xlsx", self.headers) with open(path, 'wb') as excel_file: excel_file.write(res.content)
def create(self) -> Model: """Creating a new model""" self.headers["Content-Type"] = "application/json" data = {} if not self.temp_dictionary[search_vocabulary]: raise QtModelError("Model error: Please add parameters using add_extractor function") if tag_files in self.temp_dictionary and search_vocabulary in self.temp_dictionary: data[tag_files] = self.temp_dictionary[tag_files] data[exclude_utt] = self.temp_dictionary[exclude_utt] data[num_workers] = self.temp_dictionary[num_workers] data[chunk] = self.temp_dictionary[chunk] if search_vocabulary in self.temp_dictionary: data[search_vocabulary] = self.temp_dictionary[search_vocabulary] if description in self.temp_dictionary: data[description] = self.temp_dictionary[description] res = connect("post", f"{self.url}search/new", self.headers, "data", json.dumps(data)) self.id = res.json()['id'] del self.headers['Content-Type'] return self
def search(self, model_id: str, param_from: int = 0, size: int = None, f1: int = None, f2: int = None) -> Dict: """Search full-text and faceted search""" if not isinstance(model_id, str): raise QtArgumentError("Argument type error: String is expected as model_id") if isinstance(param_from, int): parameters = [('from', param_from)] else: raise QtArgumentError("Argument type error: Integer is expected as parameter") if size is not None: if isinstance(size, int) and 0 < size <= 200: parameters.append(('size', size)) else: raise QtArgumentError("Argument type error: Integer between 0 and 200 is expected as parameter") if f1 is not None and f2 is not None: parameters.append(('f', f1)) parameters.append(('f', f2)) elif f1 is None and f2 is None: pass else: raise QtArgumentError("Argument error: Query filters must be used in pairs") res = connect("get", f"{self.url}search/{model_id}", self.headers, "params", parameters) return res.json()
def test_connect_method_put_arg_err(self): with self.assertRaises(QtArgumentError): connect(method='put', uri='some_uri', headers='some_headers', data_type="params")
def test_connect_data_type_arg_err(self): with self.assertRaises(QtArgumentError): connect(method='some_method', uri='some_uri', headers='some_headers', data_type='123')