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

Fix unit test errors due to new version of pydantic #8

Merged
merged 1 commit into from
Jul 20, 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
20 changes: 9 additions & 11 deletions src/bugjira/config.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,36 @@
import json

from pydantic import BaseModel, validator, constr
import pydantic
from pydantic import BaseModel, field_validator, constr


class BugzillaConfig(BaseModel):
model_config = pydantic.ConfigDict(extra='forbid')

URL: constr(strip_whitespace=True, min_length=1)
api_key: constr(strip_whitespace=True, min_length=1)

class Config:
extra = 'forbid'


class JiraConfig(BaseModel):
model_config = pydantic.ConfigDict(extra='forbid')

URL: constr(strip_whitespace=True, min_length=1)
token_auth: constr(strip_whitespace=True, min_length=1)

class Config:
extra = 'forbid'


class ConfigDict(BaseModel):
model_config = pydantic.ConfigDict(extra='forbid')

bugzilla: BugzillaConfig
jira: JiraConfig

class Config:
extra = 'forbid'


class Config(BaseModel):
"""A BaseModel to validate config dicts"""

config_dict: dict

@validator("config_dict")
@field_validator("config_dict")
def validate_minimum_config(cls, v):
ConfigDict(**v)
return v
Expand Down
10 changes: 5 additions & 5 deletions src/bugjira/issue.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Any

from pydantic import BaseModel, validator
from pydantic import BaseModel, field_validator

from bugjira.util import is_bugzilla_key, is_jira_key

Expand All @@ -12,12 +12,12 @@ class Issue(BaseModel):
"""

key: str
bugzilla: Any
jira_issue: Any
bugzilla: Any = None
jira_issue: Any = None


class BugzillaIssue(Issue):
@validator("key")
@field_validator("key")
def validate_key(cls, key):
if not is_bugzilla_key(key):
raise ValueError(f"{key} is not a \
Expand All @@ -26,7 +26,7 @@ def validate_key(cls, key):


class JiraIssue(Issue):
@validator("key")
@field_validator("key")
def validate_key(cls, key):
if not is_jira_key(key):
raise ValueError(f"{key} is not a \
Expand Down
22 changes: 11 additions & 11 deletions tests/unit/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ def test_config_extra_top_level_value(good_config_dict):
assert len(excinfo.value.errors()) == 1
error = excinfo.value.errors()[0]
assert error.get("loc") == ("config_dict", "foo")
assert error.get("msg") == "extra fields not permitted"
assert error.get("type") == "value_error.extra"
assert error.get("msg") == "Extra inputs are not permitted"
assert error.get("type") == "extra_forbidden"


def test_config_extra_bugzilla_value(good_config_dict):
Expand All @@ -62,8 +62,8 @@ def test_config_extra_bugzilla_value(good_config_dict):
assert len(excinfo.value.errors()) == 1
error = excinfo.value.errors()[0]
assert error.get("loc") == ("config_dict", "bugzilla", "foo")
assert error.get("msg") == "extra fields not permitted"
assert error.get("type") == "value_error.extra"
assert error.get("msg") == "Extra inputs are not permitted"
assert error.get("type") == "extra_forbidden"


def test_config_extra_jira_value(good_config_dict):
Expand All @@ -81,8 +81,8 @@ def test_config_extra_jira_value(good_config_dict):
assert len(excinfo.value.errors()) == 1
error = excinfo.value.errors()[0]
assert error.get("loc") == ("config_dict", "jira", "foo")
assert error.get("msg") == "extra fields not permitted"
assert error.get("type") == "value_error.extra"
assert error.get("msg") == "Extra inputs are not permitted"
assert error.get("type") == "extra_forbidden"


def test_config_missing_value(good_config_dict):
Expand All @@ -99,8 +99,8 @@ def test_config_missing_value(good_config_dict):
assert len(excinfo.value.errors()) == 1
error = excinfo.value.errors()[0]
assert error.get("loc") == ("config_dict", "jira", "token_auth")
assert error.get("msg") == "field required"
assert error.get("type") == "value_error.missing"
assert error.get("msg") == "Field required"
assert error.get("type") == "missing"


def test_config_empty_value(good_config_dict):
Expand All @@ -116,7 +116,7 @@ def test_config_empty_value(good_config_dict):

assert len(excinfo.value.errors()) == 1
error = excinfo.value.errors()[0]
assert error.get("ctx").get("limit_value") == 1
assert error.get("ctx").get("min_length") == 1
assert error.get("loc") == ("config_dict", "jira", "token_auth")


Expand All @@ -132,8 +132,8 @@ def test_config_empty_values(config_defaults):
config_path=(config_defaults + "/data/config/missing_config.json")
)
for error in excinfo.value.errors():
assert error.get("type") == "value_error.any_str.min_length"
assert error.get("ctx").get("limit_value") == 1
assert error.get("type") == "string_too_short"
assert error.get("ctx").get("min_length") == 1


def test_config_no_file(config_defaults):
Expand Down