-
Notifications
You must be signed in to change notification settings - Fork 178
Expand file tree
/
Copy pathtest_slug.py
More file actions
140 lines (128 loc) · 3.1 KB
/
test_slug.py
File metadata and controls
140 lines (128 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
"""Test Slug."""
# external
from typing import Any
import pytest
# local
from validators import ValidationError, slug
@pytest.mark.parametrize(
"value",
[
# with numbers
"hello123",
"123hello",
"123",
# with hyphens
"hello-world",
"hello-world-test",
"test-123-abc",
# with mixed alphanumeric
"hello123-world456",
"123-456-789",
"a1-b2-c3",
# with single character
"a",
"1",
"z",
# with complex slug
"test-123-abc-456-def",
"this-is-a-very-long-slug-with-many-words",
# with multiple hyphens
"a-b-c",
"1-2-3",
# very long
"a" * 100,
# very long with hyphens
"a-" * 50 + "end",
# very long with hyphens and different alphabets|letters
"a1-b2-c3-d4-e5-f6-g7-h8-i9-j0",
# old
"123-asd-7sda",
"123-k-123",
"dac-12sa-459",
"dac-12sa7-ad31as",
],
)
def test_returns_true_on_valid_slug(value: str):
"""Test returns true on valid slug."""
assert slug(value)
@pytest.mark.parametrize(
"value",
[
# with empty_string
"",
# with upper case
"Hello",
"HELLO",
"hello-World",
"Test-Slug",
# with special characters
"hello world",
"hello_world",
"hello.world",
"hello@world",
"hello#world",
"hello$world",
# starting with hyphen
"-hello",
"-123",
"-hello-world",
# with consecutive hyphens
"hello--world",
"test---slug",
"a--b",
# with unicode characters
"café",
"привет",
"東京",
"hello-мир",
# with spaces
"hello world",
" hello ",
"hello world",
# with only hyphens
"-",
"--",
"---",
# with mixed case
"HelloWorld",
"helloWorld",
"HELLOworld",
# old
"some.slug&",
"1231321%",
" 21312",
"-47q-p--123",
],
)
def test_returns_failed_validation_on_invalid_slug(value: str):
"""Test returns failed validation on invalid slug."""
assert isinstance(slug(value), ValidationError)
@pytest.mark.parametrize(
("val", "input_type"),
[
(1, "int"),
(1.0, "float"),
(None, "NoneType"),
([], "list"),
((), "tuple"),
({}, "dict"),
],
)
def test_non_string_input(val: Any, input_type: Any):
"""Test with non string input."""
# given
message = f"expected string or bytes-like object, got '{input_type}'"
# when
result = slug(val)
# then
assert isinstance(result, ValidationError)
assert result.reason == message
def test_slug_function_signature():
"""Test with keyword argument."""
# given
message = "slug() got some positional-only arguments passed as keyword arguments: 'value'"
# when
result = slug(value="test-slug")
# then
assert isinstance(result, ValidationError)
assert result.reason == message