Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement download #1

Merged
merged 5 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
__pycache__
*.egg-info
logs/
Translator-Tests/
58 changes: 54 additions & 4 deletions test_harness/download.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,61 @@
"""Download tests."""
import glob
Copy link
Member

@sierra-moxon sierra-moxon Oct 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been using a file storage package called pystow - it handles compression/uncompression, storing everything in a consistent folder location that is programmatically retrievable, retrieving from URLs/FTP, refreshing the file when it changes - or not - etc. e.g. https://github.com/geneontology/gopreprocess/blob/d258d475dfe65886d39cc795e1fae78c2bab8d36/src/utils/download.py#L44

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looked interesting, so I just tried it out. It looks like it uses urllib underneath and on a Mac, I get an SSL certificate verification failed error. And then I was looking at their documentation and wasn't seeing an option for ensure_zip, without knowing the specific files we want to download (ensure_open_zip opens a specific file in the zip). We want to download the whole thing so we can parse out the tests we want to run. Do you know how we might use pystow for our specific use-case?

Copy link
Member

@sierra-moxon sierra-moxon Oct 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah no worries, its awesome that you tried it out! :)
https://stackoverflow.com/questions/52805115/certificate-verify-failed-unable-to-get-local-issuer-certificate -- that's the Mac fix SO that has worked for me.

Definitely would still need some software to iterate through the downloaded files (or even to iterate through all files in a source URL - e.g. a folder on an FTP site), but instead of keeping track of the path amongst methods, you can always retrieve the path that pystow stores the files by querying its metadata (and then managing the download URLs/etc in a conf file).

pystow does manage zip/unzip and refresh nicely. here's a example of the file structure it creates for me:

SMoxon@SMoxon-M82 .data % ls
ALLIANCE		MGI_GPI			SEMMEDDB		monarch
GAF_OUTPUT		MOUSE			SEMMEDDB_DOMAIN		oaklib
GO			ORTHO			SEMMEDDB_RANGE		pub
HUMAN			README.md		bioregistry		pykeen
HUMAN_ISO		README_testing.md	gilda
MGI			RGD			goa_files
SMoxon@SMoxon-M82 .data % cd RGD
SMoxon@SMoxon-M82 RGD % ls
rgd-src.gaf	rgd-src.gaf.gz	rgd.gaf
SMoxon@SMoxon-M82 RGD % 

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did a little more playing around with this. I got the SSL certificate error fixed from that SO above, but there doesn't seem to be a method in pystow to download and unzip an entire zip file. The only options are to download the zip but leave it unzipped, or download it and unzip but only save a specific file inside, which doesn't quite meet our needs.

import httpx
import io
import json
import logging
from pathlib import Path
from typing import List
import tempfile
from typing import List, Union
import zipfile

from .models import TestCase
from .models import TestCase, TestSuite


def download_tests(url: Path) -> List[TestCase]:
def download_tests(
suite: Union[str, List[str]], url: Path, logger: logging.Logger
) -> List[TestCase]:
"""Download tests from specified location."""
raise NotImplementedError()
assert Path(url).suffix == ".zip"
logger.info(f"Downloading tests from {url}...")
# download file from internet
with httpx.Client(follow_redirects=True) as client:
tests_zip = client.get(url)
tests_zip.raise_for_status()
# we already checked if zip before download, so now unzip
with tempfile.TemporaryDirectory() as tmpdir:
with zipfile.ZipFile(io.BytesIO(tests_zip.read())) as zip_ref:
zip_ref.extractall(tmpdir)

# Find all json files in the downloaded zip
tests_paths = glob.glob(f"{tmpdir}/**/*.json", recursive=True)

all_tests = []
suites = suite if type(suite) == list else [suite]
test_case_ids = []

logger.info(f"Reading in {len(tests_paths)} tests...")

# do the reading of the tests and make a tests list
for test_path in tests_paths:
with open(test_path, "r") as f:
test_json = json.load(f)
try:
test_suite = TestSuite.parse_obj(test_json)
if test_suite.id in suites:
# if suite is selected, grab all its test cases
test_case_ids.extend(test_suite.case_ids)
except Exception as e:
# not a Test Suite
pass
try:
test_case = TestCase.parse_obj(test_json)
all_tests.append(test_case)
except Exception as e:
# not a Test Case
pass

# only return the tests from the specified suites
tests = list(filter(lambda x: x in test_case_ids, all_tests))
logger.info(f"Passing along {len(tests)} tests")
return tests
26 changes: 20 additions & 6 deletions test_harness/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Translator SRI Automated Test Harness."""
from argparse import ArgumentParser
import json
from pathlib import Path
from urllib.parse import urlparse
from uuid import uuid4

from .run import run_tests
Expand All @@ -11,23 +11,28 @@
setup_logger()


def url_type(arg):
url = urlparse(arg)
if all((url.scheme, url.netloc)):
return arg
raise TypeError("Invalid URL")


def main(args):
"""Main Test Harness entrypoint."""
qid = str(uuid4())[:8]
logger = get_logger(qid, args["log_level"])
tests = []
if "tests_url" in args:
logger.info("Downloading tests...")
tests = download_tests(args["tests_url"])
tests = download_tests(args["suite"], args["tests_url"], logger)
elif "tests" in args:
tests = args["tests"]
else:
return logger.error(
"Please run this command with `-h` to see the available options."
)

logger.info("Running tests...")
report = run_tests(tests)
report = run_tests(tests, logger)

if args["save_to_dashboard"]:
logger.info("Saving to Testing Dashboard...")
Expand All @@ -53,7 +58,16 @@ def cli():
)

download_parser.add_argument(
"tests_url", type=Path, help="URL to download in order to find the test files"
"suite",
type=str,
help="The name/id of the suite(s) to run. Once tests have been downloaded, the test cases in this suite(s) will be run.",
)

download_parser.add_argument(
"--tests_url",
type=url_type,
default="https://github.com/NCATSTranslator/Tests/archive/refs/heads/main.zip",
help="URL to download in order to find the test files",
)

run_parser = subparsers.add_parser("run", help="Run a given set of tests")
Expand Down
32 changes: 32 additions & 0 deletions test_harness/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from enum import Enum
from pydantic import BaseModel
from typing import List, Optional


class Type(str, Enum):
Expand Down Expand Up @@ -37,6 +38,7 @@ class TestCase(BaseModel):
output_curie: str
"""

id: Optional[int]
type: Type
env: Env
query_type: QueryType
Expand All @@ -45,6 +47,36 @@ class TestCase(BaseModel):
output_curie: str


class Tests(BaseModel):
"""List of Test Cases."""

__root__: List[TestCase]

def __len__(self):
return len(self.__root__)

def __iter__(self):
return self.__root__.__iter__()

def __contains__(self, v):
return self.__root__.__contains__(v)

def __getitem__(self, i):
return self.__root__.__getitem__(i)


class TestSuite(BaseModel):
"""
Test Suite containing the ids of Test Cases.

id: int
case_ids: List[int]
"""

id: int
case_ids: List[int]


class TestResult(BaseModel):
"""Output of a Test."""

Expand Down
15 changes: 7 additions & 8 deletions test_harness/run.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
"""Run tests through the Test Runners."""
import logging
from pydantic import validate_arguments
from typing import List, Dict
from tqdm import tqdm
from typing import Dict

from ui_test_runner import run_ui_test
from ARS_Test_Runner.semantic_test import run_semantic_test as run_ars_test

from .models import TestCase
from .models import Tests

logger = logging.getLogger(__name__)


@validate_arguments
def run_tests(tests: List[TestCase]) -> Dict:
def run_tests(tests: Tests, logger: logging.Logger) -> Dict:
"""Send tests through the Test Runners."""
tests = Tests.parse_obj(tests)
logger.info(f"Running {len(tests)} tests...")
full_report = {}
# loop over all tests
for test in tests:
for test in tqdm(tests):
# check if acceptance test
if test.type == "acceptance":
full_report[test.input_curie] = {}
Expand Down
Loading