Skip to content

Commit

Permalink
Add test for invalid version string
Browse files Browse the repository at this point in the history
  • Loading branch information
gregorybchris committed Dec 30, 2024
1 parent 92d1c69 commit b98e63f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/myxa/models.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import logging
import re
from enum import StrEnum
from typing import Literal, Optional, Self, Union

from pydantic import BaseModel, Field

from myxa.errors import UserError

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -58,6 +61,9 @@ def to_str(self) -> str:

@classmethod
def from_str(cls, s: str) -> Self:
if not re.match(r"\d+\.\d+", s):
msg = f"Invalid version string: {s}"
raise UserError(msg)
parts = s.split(".")
major = int(parts[0])
minor = int(parts[1])
Expand Down
10 changes: 10 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import pytest

from myxa.errors import UserError
from myxa.models import Version


class TestModels:
def test_version_invalid_from_str_raises_user_error(self) -> None:
with pytest.raises(UserError, match="Invalid version string: 100"):
Version.from_str("100")

0 comments on commit b98e63f

Please sign in to comment.