Skip to content

Commit

Permalink
Add exported install function and test
Browse files Browse the repository at this point in the history
  • Loading branch information
H0R5E committed Jan 16, 2025
1 parent b83e48c commit c8f5b9a
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 11 deletions.
34 changes: 24 additions & 10 deletions packages/dtocean-data/src/dtocean_data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@
from importlib.metadata import version
from pathlib import Path
from platform import system
from typing import Literal, TypeAlias, get_args
from typing import Sequence
from urllib.request import urlopen
from zipfile import ZipFile

from polite_config.paths import UserDataPath

ValidArch: TypeAlias = Literal["windows", "linux"]

_ARCH = system().lower() if system().lower() != "windows" else "win64"
_ARCH_VALID: list[ValidArch] = list(get_args(ValidArch))
_VERSION = version("dtocean-data")
_DATA_URL_BASE = (
"https://github.com/DTOcean/dtocean-data-next/releases/"
"download/v2025.01.0/dtocean-data-v2025.01.0"
Expand All @@ -22,14 +18,32 @@
"https://github.com/DTOcean/NEMOH/releases/download/v2024.12.2/"
"nemoh-{}-v2024.12.2".format(_ARCH)
)
_VERSION = version("dtocean-data")
_USER_DIR = UserDataPath("dtocean_data", "DTOcean", _VERSION)


def _check_arch(arch: ValidArch):
if arch not in _ARCH_VALID:
raise NotImplementedError(
"The {} platform is not supported".format(arch)
)
def install(options: Sequence[str]):
def get_option(option: str):
match option:
case "data":
return _DATA_URL_BASE
case "nemoh":
return _NEMOH_URL_BASE
case _:
raise ValueError("Unrecognised option")

arch = system().lower()

for option in options:
url_base = get_option(option)

match arch:
case "linux":
_extract_linux(url_base)
case "windows":
_extract_windows(url_base)
case _:
raise NotImplementedError("Unsupported architecture")


def _extract_linux(url_base: str):
Expand Down
47 changes: 47 additions & 0 deletions packages/dtocean-data/tests/test_offline.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from platform import system

import pytest

from dtocean_data import _DATA_URL_BASE, _NEMOH_URL_BASE, install


@pytest.mark.parametrize(
"options",
[
("data",),
("nemoh",),
(
"data",
"nemoh",
),
],
)
def test_install(mocker, options):
arch = system().lower()

match arch:
case "linux":
extract = mocker.patch("dtocean_data._extract_linux")
case "windows":
extract = mocker.patch("dtocean_data._extract_windows")
case _:
pytest.skip("{} system not supported".format(arch))

install(options)

expected = set([_DATA_URL_BASE, _NEMOH_URL_BASE])
test = set([call.args[0] for call in extract.call_args_list])
assert test <= expected


def test_install_bad_option():
with pytest.raises(ValueError) as excinfo:
install(["bad"])
assert "Unrecognised option" in str(excinfo.value)


def test_install_bad_arch(mocker):
mocker.patch("dtocean_data.system", return_value="bad")
with pytest.raises(NotImplementedError) as excinfo:
install(["data"])
assert "Unsupported architecture" in str(excinfo.value)
7 changes: 6 additions & 1 deletion packages/dtocean-data/tests/test_online.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
from platform import system
from urllib.error import URLError
from urllib.request import urlopen

Expand All @@ -23,7 +24,11 @@ def is_online():
return False


pytestmark = pytest.mark.skipif(not is_online(), reason="running offline")
pytest.mark.skipif(not is_online(), reason="running offline")
pytest.mark.skipif(
system().lower() not in ["linux", "windows"],
reason="system not supported",
)


def test_download_archive(tmp_path):
Expand Down

0 comments on commit c8f5b9a

Please sign in to comment.