def media(numeros: CalcularMediaPost) -> (CalcularMediaResponse200, Error): if numeros.numeros: numbers = np.array(numeros.numeros) if numbers.dtype == np.float64 or numbers.dtype == np.int32: redondear = numeros.redondear if redondear: media_numero = np.trunc(np.mean(numbers)) else: media_numero = np.mean(numbers) return CalcularMediaResponse200(media_numero) else: return Error("El array de números contiene tipos no válidos"), 400 else: return Error("El array está vacío"), 400
def test_error_in_url(self): self.url = "https://raw.githubusercontent.com/RafaelBarreroBosonit/prueba_swagger/master/csvs/numero_error.csv" c = media_csv_url(CalcularMediaCsvUrl(self.url)) expected_error = Error('Archivo no encontrado') expected_code = 404 self.assertEqual(c[0], expected_error) self.assertEqual(c[1], expected_code)
def cuadrados_numeros(numeros: list) -> (CalcularCuadradoResponse, Error): list_array = [] if numeros: numbers = np.array(numeros) for number in numbers: try: cuadrado = int(np.square(int(number))) except ValueError: if number.isnumeric(): cuadrado = float(np.square(float(number))) else: return Error("No se admiten letras"), 400 except TypeError: return Error("Los datos introducidos no son válidos"), 400 list_array.append(CalcularCuadradoArray(cuadrado, number)) return CalcularCuadradoResponse(list_array, len(list_array)) else: return Error("No se ha introducido números"), 400
def media_csv_url(url: CalcularMediaCsvUrl) -> (CalcularMediaResponse200, Error): base_path = Path(__file__).resolve().parent folder_csv = "input" absolute_path = Path(base_path).joinpath(folder_csv) # Absolute path of input folder try: os.mkdir(absolute_path) except FileExistsError: pass try: r = requests.get(url.url) except requests.exceptions.MissingSchema: return Error("La URL no es válida"), 404 except requests.exceptions.InvalidURL: return Error("La URL no es válida"), 404 if r.status_code == 200: file_name = url.url.split("/")[-1] absolute_path_file = Path(absolute_path).joinpath(file_name) # Absolute path of file with open(absolute_path_file, 'wb+') as file: file.write(r.content) with open(absolute_path_file, 'r') as file: my_reader = csv.reader(file, delimiter=',') next(my_reader) for row in my_reader: n = row[0] break n_lower = n.lower() if not n_lower.islower(): numbers = np.fromstring(n, sep=',') redondear = url.redondear if redondear: media_numero = np.trunc(np.mean(numbers)) else: media_numero = np.mean(numbers) os.remove(absolute_path_file) return CalcularMediaResponse200(media_numero) else: os.remove(absolute_path_file) return Error("El archivo no es válido, contiene letras"), 400 else: return Error("Archivo no encontrado"), 404
def samples_id_nearest_neighbours_get(id): # noqa: E501 """samples_id_nearest_neighbours_get Return the list of nearest neighbours of a sample based on a sample ID. # noqa: E501 :param id: :type id: str :rtype: List[Neighbour] """ sample_graph = get_db() try: node = SampleNode.get(id, sample_graph) except NotFound: return Error(404, 'Not found'), 404 else: model = node.to_model() if not model.nearest_neighbours: return Error(404, 'Not found'), 404 else: return node.to_model().nearest_neighbours, 200
def samples_id_nearest_leaf_node_get(id): # noqa: E501 """samples_id_nearest_leaf_node_get Return the nearest leaf node of a sample based on a sample ID. # noqa: E501 :param id: :type id: str :rtype: NearestLeaf """ sample_graph = get_db() try: node = SampleNode.get(id, sample_graph) except NotFound: return Error(404, 'Not found'), 404 else: model = node.to_model() if not model.nearest_leaf_node: return Error(404, 'Not found'), 404 else: return node.to_model().nearest_leaf_node, 200
def samples_id_delete(id): # noqa: E501 """samples_id_delete Delete a sample based on a sample ID. # noqa: E501 :param id: :type id: str :rtype: None """ sample_graph = get_db() try: SampleNode.delete(id, sample_graph) except NotFound: return Error(404, 'Not found'), 404 else: return '', 204
def samples_id_nearest_leaf_node_put(id, nearest_leaf=None): # noqa: E501 """samples_id_nearest_leaf_node_put Replace the nearest leaf node of a sample based on a sample ID. # noqa: E501 :param id: :type id: str :param nearest_leaf: New nearest leaf node to replace old one. :type nearest_leaf: dict | bytes :rtype: NearestLeaf """ if connexion.request.is_json: nearest_leaf = NearestLeaf.from_dict( connexion.request.get_json()) # noqa: E501 sample_graph = get_db() try: updated = SampleNode.update(id, sample_graph, leaf=nearest_leaf) except NotFound: return Error(404, 'Not found'), 404 else: return updated.to_model().nearest_leaf_node, 200
def test_list_none(self): c = cuadrados_numeros([None]) expected_error = Error('Los datos introducidos no son válidos') expected_code = 400 self.assertEqual(c[0], expected_error) self.assertEqual(c[1], expected_code)
def test_list_string_none(self): c = media(CalcularMediaPost([1, "asdasd", None, 5])) expected_error = Error('El array de números contiene tipos no válidos') expected_code = 400 self.assertEqual(c[0], expected_error) self.assertEqual(c[1], expected_code)
def test_none(self): c = media(CalcularMediaPost(None)) expected_error = Error('El array está vacío') expected_code = 400 self.assertEqual(c[0], expected_error) self.assertEqual(c[1], expected_code)
def test_list_as_string(self): c = media_csv_url(CalcularMediaCsvUrl('[1, "asdasd", "2", 5]')) expected_error = Error('La URL no es válida') expected_code = 404 self.assertEqual(c[0], expected_error) self.assertEqual(c[1], expected_code)
def test_empty(self): c = media_csv_url(CalcularMediaCsvUrl()) expected_error = Error("La URL no es válida") expected_code = 404 self.assertEqual(c[0], expected_error) self.assertEqual(c[1], expected_code)
def test_string_none(self): c = media_csv_url(CalcularMediaCsvUrl("None")) expected_error = Error('La URL no es válida') expected_code = 404 self.assertEqual(c[0], expected_error) self.assertEqual(c[1], expected_code)
def test_list_empty(self): c = cuadrados_numeros([]) expected_error = Error('No se ha introducido números') expected_code = 400 self.assertEqual(c[0], expected_error) self.assertEqual(c[1], expected_code)
def test_list_string(self): c = cuadrados_numeros([1, "asdasd", "2", 5]) expected_error = Error('No se admiten letras') expected_code = 400 self.assertEqual(c[0], expected_error) self.assertEqual(c[1], expected_code)
def test_list_empty(self): c = media(CalcularMediaPost([])) expected_error = Error("El array está vacío") expected_code = 400 self.assertEqual(c[0], expected_error) self.assertEqual(c[1], expected_code)