Skip to content

Commit

Permalink
Use mock VERSION_PINS env variable for tests. Encasuplate version_pin…
Browse files Browse the repository at this point in the history
… logic in function
  • Loading branch information
nanglo123 committed Sep 27, 2024
1 parent 7871311 commit 2af67dd
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 34 deletions.
70 changes: 40 additions & 30 deletions src/pyobo/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
"DATABASE_DIRECTORY",
"SPECIES_REMAPPING",
"VERSION_PINS",
"get_version_pins",
]


logger = logging.getLogger(__name__)

PYOBO_MODULE = pystow.module("pyobo")
Expand Down Expand Up @@ -103,32 +103,42 @@
"issn",
}

# Load version pin dictionary from the environmental variable VERSION_PINS
try:
VERSION_PINS_STR = os.getenv("VERSION_PINS")
if not VERSION_PINS_STR:
VERSION_PINS = {}
else:
VERSION_PINS = json.loads(VERSION_PINS_STR)
invalid_prefixes = []
for prefix, version in VERSION_PINS.items():
if not isinstance(prefix, str) or not isinstance(version, str):
logger.error(f"The prefix:{prefix} and version:{version} name must both be strings")
invalid_prefixes.append(prefix)
for prefix in invalid_prefixes:
VERSION_PINS.pop(prefix)
except ValueError as e:
logger.error(
"The value for the environment variable VERSION_PINS must be a valid JSON string: %s" % e
)
VERSION_PINS = {}

if VERSION_PINS:
logger.debug(
f"These are the resource versions that are pinned.\n{VERSION_PINS}. "
f"\nPyobo will download the latest version of a resource if it's "
f"not pinned.\nIf you want to use a specific version of a "
f"resource, edit your VERSION_PINS environmental "
f"variable which is a JSON string to include a prefix and version "
f"name."
)

def get_version_pins():
"""Retrieve the resource version pins."""
try:
version_pins_str = os.getenv("VERSION_PINS")
if not version_pins_str:
version_pins = {}
else:
version_pins = json.loads(version_pins_str)
invalid_prefixes = []
for prefix, version in version_pins.items():
if not isinstance(prefix, str) or not isinstance(version, str):
logger.error(
f"The prefix:{prefix} and version:{version} name must both be strings"
)
invalid_prefixes.append(prefix)
for prefix in invalid_prefixes:
version_pins.pop(prefix)
except ValueError as e:
logger.error(
"The value for the environment variable VERSION_PINS must be a valid JSON string: %s"
% e
)
version_pins = {}

if version_pins:
logger.debug(
f"These are the resource versions that are pinned.\n"
f"{version_pins}. "
f"\nPyobo will download the latest version of a resource if it's "
f"not pinned.\nIf you want to use a specific version of a "
f"resource, edit your VERSION_PINS environmental "
f"variable which is a JSON string to include a prefix and version "
f"name."
)
return version_pins


VERSION_PINS = get_version_pins()
14 changes: 10 additions & 4 deletions tests/test_version_pins.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
# -*- coding: utf-8 -*-

"""Tests for PyOBO VERSION_PINS."""

import os
import unittest
from unittest import mock

from pyobo.api.utils import get_version
from pyobo.constants import VERSION_PINS
from pyobo.constants import get_version_pins

MOCK_VERSION_PINS = '{"ncbitaxon": "2024-05-08", "vo":"2024-04-09", ' '"chebi":"235", "bfo":5}'


@mock.patch.dict(os.environ, {"VERSION_PINS": MOCK_VERSION_PINS})
class TestVersionPins(unittest.TestCase):
"""Test using VERSION_PINS."""

def test_correct_version_pin_types(self):
"""Test resource and version type."""
for resource_prefix, version in VERSION_PINS.items():
version_pins = get_version_pins()
for resource_prefix, version in version_pins.items():
self.assertIsInstance(resource_prefix, str)
self.assertIsInstance(version, str)

def test_use_correct_version_pin(self):
"""Tests correct resource version is used."""
for resource_prefix, version in VERSION_PINS.items():
version_pins = get_version_pins()
for resource_prefix, version in version_pins.items():
self.assertEqual(get_version(resource_prefix), version)

0 comments on commit 2af67dd

Please sign in to comment.