diff --git a/argopy/tests/helpers/mocked_http.py b/argopy/tests/helpers/mocked_http.py index 01dbde35..d753fbf3 100644 --- a/argopy/tests/helpers/mocked_http.py +++ b/argopy/tests/helpers/mocked_http.py @@ -25,9 +25,7 @@ """ import contextlib -import json -import os -import sys +from pathlib import Path import threading from collections import ChainMap from http.server import BaseHTTPRequestHandler, HTTPServer @@ -36,60 +34,86 @@ from urllib.parse import unquote import socket import json +import importlib log = logging.getLogger("argopy.tests.mocked_http") -LOG_SERVER_CONTENT = False # Also log the list of files/uris available from the mocked server +LOG_SERVER_CONTENT = ( + False # Should we list files/uris available from the mocked server in the log ? +) requests = pytest.importorskip("requests") port = 9898 # Select the port to run the local server on mocked_server_address = "http://127.0.0.1:%i" % port -start_with = lambda f, x: f[0:len(x)] == x if len(x) <= len(f) else False # noqa: E731 +start_with = ( + lambda f, x: f[0 : len(x)] == x if len(x) <= len(f) else False +) # noqa: E731 -sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', '..')) -from argopy.data_fetchers.erddap_data import api_server as erddap_api_server +""" +Load test data and create a dictionary mapping of URL requests as keys, and expected responses as values -# Dictionary mapping of URL requests as keys, and expected responses as values: -# (this will be filling the mocked http server content) -# The real address must be made relative +This will be filling the mocked http server content. +The uri requested will be made relative, because the server name will be replaced by the mocked server address. +""" MOCKED_REQUESTS = {} -DATA_FOLDER = os.path.dirname(os.path.realpath(__file__)).replace("helpers", "test_data") -DB_FILE = os.path.join(DATA_FOLDER, "httpmocked_uri_index.json") -if os.path.exists(DB_FILE): +TESTDATA_FOLDER = ( + Path(importlib.util.find_spec("argopy.tests").submodule_search_locations[0]) + .resolve() + .joinpath("test_data") +) +if not TESTDATA_FOLDER.exists(): + raise RuntimeError( + "Can't find tests data folder at: %s\n Note that test data are not included in the pypi distribution. You should fork the repo to get test data." + % TESTDATA_FOLDER + ) + +DB_FILE = TESTDATA_FOLDER.joinpath("httpmocked_uri_index.json") +URI = [] + +if DB_FILE.exists(): with open(DB_FILE, "r") as f: URI = json.load(f) for resource in URI: - test_data_file = os.path.join(DATA_FOLDER, "%s.%s" % (resource['sha'], resource['ext'])) - with open(test_data_file, mode='rb') as file: + test_data_file = TESTDATA_FOLDER.joinpath( + "%s.%s" % (resource["sha"], resource["ext"]) + ) + with open(test_data_file, mode="rb") as file: data = file.read() - # Remove all specific api/server, that will be served by the mocked http server: + # Remove all specific api/server names from absolute URIs + # Because these are arguments passed to methods that will use mocked_server_address instead + # (See for instance the argument 'server' in `argopy.data_fetchers.erddap_data.ErddapArgoDataFetcher`) patterns = [ - "https://github.com/euroargodev/argopy-data/raw/master", - "https://erddap.ifremer.fr/erddap", - "https://data-argo.ifremer.fr", - "https://api.ifremer.fr", - "https://coastwatch.pfeg.noaa.gov/erddap", - "https://www.ocean-ops.org/api/1", - "https://dataselection.euro-argo.eu/api", - "https://vocab.nerc.ac.uk/collection", - "https://argovisbeta02.colorado.edu", - "https://dx.doi.org", - "https://archimer.ifremer.fr", + "https://github.com/euroargodev/argopy-data/raw/master", + "https://erddap.ifremer.fr/erddap", + "https://data-argo.ifremer.fr", + "https://api.ifremer.fr", + "https://coastwatch.pfeg.noaa.gov/erddap", + "https://www.ocean-ops.org/api/1", + "https://dataselection.euro-argo.eu/api", + "https://vocab.nerc.ac.uk/collection", + "https://argovisbeta02.colorado.edu", + "https://dx.doi.org", + "https://archimer.ifremer.fr", ] for pattern in patterns: - if start_with(resource['uri'], pattern): - MOCKED_REQUESTS[resource['uri'].replace(pattern, "")] = data + if start_with(resource["uri"], pattern): + MOCKED_REQUESTS[resource["uri"].replace(pattern, "")] = data else: - log.debug("Loading this sub-module without DB_FILE ! %s" % DB_FILE) + raise RuntimeError( + "Can't find test data index file at: %s.\n Note that test data are not included in the pypi distribution. You should fork the repo to get test data." + % DB_FILE + ) def get_html_landing_page(): """Return a webpage with a listing of all available files with a href links""" html = ["\n"] - html.append("

Mocked HTTP server is up and running, serving %i files

" % len(URI)) + html.append( + "

Mocked HTTP server is up and running, serving %i files

" % len(URI) + ) html.append("