-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from maykinmedia/feature-validate-slug-fields
Add slug field validation
- Loading branch information
Showing
3 changed files
with
60 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
from typing import Literal | ||
|
||
from django.core.exceptions import ValidationError as DjangoValidationError | ||
from django.core.validators import validate_slug | ||
|
||
import pytest | ||
from pydantic import ValidationError | ||
from pydantic.fields import PydanticUndefined | ||
|
||
from django_setup_configuration.fields import DjangoModelRef | ||
|
@@ -51,6 +55,56 @@ class Config(ConfigurationModel): | |
assert field.is_required() is True | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"invalid_values", | ||
( | ||
"", | ||
"hello world", | ||
"[email protected]", | ||
"$price", | ||
"my.variable", | ||
"résumé", | ||
"hello!", | ||
"!", | ||
"#", | ||
"+", | ||
), | ||
) | ||
def test_slug_validation_fails_on_both_pydantic_and_django(invalid_values): | ||
|
||
class Config(ConfigurationModel): | ||
slug = DjangoModelRef(TestModel, "slug") | ||
|
||
with pytest.raises(ValidationError): | ||
Config(slug=invalid_values) | ||
|
||
with pytest.raises(DjangoValidationError): | ||
validate_slug(invalid_values) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"valid_values", | ||
( | ||
"a", | ||
"foo-bar", | ||
"foo_bar", | ||
"foo_bar_baz", | ||
"foo-bar-baz", | ||
"fO0-B4r-Baz", | ||
"foo-bar-baz", | ||
"foobarbaz", | ||
"FooBarBaz", | ||
), | ||
) | ||
def test_slug_validation_succeeds_on_both_pydantic_and_django(valid_values): | ||
|
||
class Config(ConfigurationModel): | ||
slug = DjangoModelRef(TestModel, "slug") | ||
|
||
Config.model_validate(dict(slug=valid_values)) | ||
validate_slug(valid_values) # does not raise | ||
|
||
|
||
def test_no_default_makes_field_required(): | ||
|
||
class Config(ConfigurationModel): | ||
|