Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam-D-Lewis committed Dec 30, 2024
1 parent 0ff8463 commit f92bd8c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
13 changes: 6 additions & 7 deletions src/_nebari/config_set.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pathlib
from typing import Optional

from packaging.requirements import Requirement
from packaging.requirements import SpecifierSet
from pydantic import BaseModel, ConfigDict, field_validator

from _nebari._version import __version__
Expand All @@ -12,22 +12,21 @@ class ConfigSetMetadata(BaseModel):
model_config: ConfigDict = ConfigDict(extra="allow", arbitrary_types_allowed=True)
name: str # for use with guided init
description: Optional[str] = None
nebari_version: str | Requirement
nebari_version: str | SpecifierSet

@field_validator("nebari_version")
@classmethod
def validate_version_requirement(cls, version_req):
if isinstance(version_req, str):
version_req = Requirement(f"nebari{version_req}")
version_req.specifier.prereleases = True
version_req = SpecifierSet(version_req, prereleases=True)

return version_req

def check_version(self, version):
if version not in self.nebari_version.specifier:
if version not in self.nebari_version:
raise ValueError(
f"Current Nebari version {__version__} is not compatible with "
f'required version {self.nebari_version.specifier} for "{self.name}" config set.'
f'Nebari version "{version}" is not compatible with '
f'version requirement {self.nebari_version} for "{self.name}" config set.'
)


Expand Down
27 changes: 16 additions & 11 deletions tests/tests_unit/test_config_set.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,39 @@
from unittest.mock import patch

import pytest
from packaging.requirements import Requirement
from packaging.requirements import SpecifierSet

from _nebari.config_set import ConfigSetMetadata, read_config_set

test_version = "2024.12.2"


test_version = "2024.12.2"


@pytest.mark.parametrize(
"version_input,should_pass",
"version_input,test_version,should_pass",
[
(">=2024.12.0,<2025.0.0", True),
(Requirement("nebari>=2024.12.0,<2025.0.0"), True),
(">=2025.0.0", False),
(Requirement("nebari>=2025.0.0"), False),
# Standard version tests
(">=2024.12.0,<2025.0.0", "2024.12.2", True),
(SpecifierSet(">=2024.12.0,<2025.0.0", prereleases=True), "2024.12.2", True),
# Pre-release version requirement tests
(">=2024.12.0rc1,<2025.0.0", "2024.12.0rc1", True),
(SpecifierSet(">=2024.12.0rc1", prereleases=True), "2024.12.0rc2", True),
# Pre-release test version against standard requirement
(">=2024.12.0,<2025.0.0", "2024.12.1rc1", True),
(SpecifierSet(">=2024.12.0,<2025.0.0", prereleases=True), "2024.12.1rc1", True),
# Failing cases
(">=2025.0.0", "2024.12.2rc1", False),
(SpecifierSet(">=2025.0.0rc1", prereleases=True), "2024.12.2", False),
],
)
def test_version_requirement(version_input, should_pass):
def test_version_requirement(version_input, test_version, should_pass):
metadata = ConfigSetMetadata(name="test-config", nebari_version=version_input)

if should_pass:
metadata.check_version(test_version)
else:
with pytest.raises(ValueError) as exc_info:
metadata.check_version(test_version)
assert "Current Nebari version" in str(exc_info.value)
assert "Nebari version" in str(exc_info.value)


def test_valid_version_requirement_with_requirement_object():
Expand Down

0 comments on commit f92bd8c

Please sign in to comment.