Skip to content

Commit

Permalink
fix: updating pytests for subtype addition and making subtype optional
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas authored and Thomas65535 committed Sep 5, 2024
1 parent fa5b855 commit d14a679
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 32 deletions.
24 changes: 13 additions & 11 deletions src/hrflow_connectors/core/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
BaseModel,
Field,
ValidationError,
constr,
create_model,
root_validator,
validator,
Expand Down Expand Up @@ -765,20 +764,23 @@ class ConnectorModel(BaseModel):
description: str
url: str
type: ConnectorType
subtype: constr(regex=CONNECTOR_SUBTYPE_FORMAT_REGEX) = Field(
..., description="Lowercased string with no spaces"
subtype: t.Optional[str] = Field(
regex=CONNECTOR_SUBTYPE_FORMAT_REGEX,
description="Lowercased string with no spaces",
)
actions: t.List[ConnectorAction]

@validator("subtype", pre=True, always=True)
def check_subtype(cls, value: str) -> str:
cleaned_value = value.lower()
if cleaned_value != value:
raise ValueError(f"ConnectorModel's `subtype` {value} must be lowercase.")
if " " in cleaned_value:
raise ValueError(
f"ConnectorModel's `subtype` {value} must not contain any spaces."
)
def check_subtype(cls, value: t.Optional[str], values: dict) -> str:
if value is None:
cleaned_value = values.get("name", "").lower().replace(" ", "")
else:
cleaned_value = value.lower().replace(" ", "")
if cleaned_value != value:
raise ValueError(
"ConnectorModel's `subtype`={} must be lowercase without any"
" spaces.".format(value)
)
return cleaned_value

def logo(self, connectors_directory: Path) -> str:
Expand Down
35 changes: 14 additions & 21 deletions tests/core/test_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -1690,28 +1690,19 @@ def new_format(*args, **kwargs):
).format(parameters_action_name.name)


def test_connector_lowercase_subtype_constraint():
subtype = "SmartLeads"
with pytest.raises(ValidationError) as excinfo:
Connector(
name="SmartLeads",
type=ConnectorType.Other,
subtype=subtype,
description=DESCRIPTION,
url="https://www.smartleads.test/",
actions=[],
)
expected_error = (
"1 validation error for ConnectorModel"
"\nsubtype"
"\n ConnectorModel's `subtype` {} must be lowercase. (type=value_error)"
.format(subtype)
def test_connector_model_subtype_missing():
model = Connector(
name="SmartLeads",
type=ConnectorType.Other,
description=DESCRIPTION,
url="https://www.smartleads.test/",
actions=[],
)
assert str(excinfo.value) == expected_error
assert model.model.subtype == "smartleads"


def test_connector_space_subtype_constraint():
subtype = "smart leads"
def test_connector_subtype_constraint():
subtype = "Smart Leads"
with pytest.raises(ValidationError) as excinfo:
Connector(
name="SmartLeads",
Expand All @@ -1722,7 +1713,9 @@ def test_connector_space_subtype_constraint():
actions=[],
)
expected_error = (
"1 validation error for ConnectorModel\nsubtype\n ConnectorModel's `subtype`"
" {} must not contain any spaces. (type=value_error)".format(subtype)
"1 validation error for ConnectorModel\nsubtype\n ConnectorModel's"
" `subtype`={} must be lowercase without any spaces. (type=value_error)".format(
subtype
)
)
assert str(excinfo.value) == expected_error

0 comments on commit d14a679

Please sign in to comment.