-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_form_manager.py
More file actions
160 lines (120 loc) · 5.04 KB
/
test_form_manager.py
File metadata and controls
160 lines (120 loc) · 5.04 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import pytest
from pydantic import BaseModel, EmailStr
from ellar.common import ModuleRouter
from ellar.testing import Test
from zform import ZForm, FormManager
from zform.fields import StringField, EmailField, FieldBase
class UserFormModel(BaseModel):
username: str
email: EmailStr
@pytest.fixture
def user_form_manager():
return FormManager.from_schema(UserFormModel)
def test_zform_manager_initialization(user_form_manager):
assert isinstance(user_form_manager, FormManager)
assert user_form_manager.model_field.type_ == UserFormModel
assert len(list(user_form_manager)) == 2
def test_zform_manager_fields(user_form_manager):
fields = list(user_form_manager)
assert len(fields) == 2
assert isinstance(fields[0], StringField)
assert isinstance(fields[1], EmailField)
@pytest.mark.asyncio
async def test_zform_manager_validate_valid_data(create_context):
ctx = create_context({"username": "testuser", "email": "test@example.com"})
user_form_manager = FormManager.from_schema(UserFormModel, ctx=ctx)
assert await user_form_manager.validate_async()
assert user_form_manager.errors == {}
assert user_form_manager.value == UserFormModel(
username="testuser", email="test@example.com"
)
@pytest.mark.asyncio
async def test_zform_manager_validate_invalid_data(create_context):
ctx = create_context({"username": "testuser", "email": "invalid_email"})
user_form_manager = FormManager.from_schema(
UserFormModel,
ctx=ctx,
)
is_valid = await user_form_manager.validate_async()
assert not is_valid
assert user_form_manager.errors["email"] == [
"value is not a valid email address: An email address must have an @-sign."
]
def test_zform_manager_populate_form(user_form_manager):
user_form_manager.populate_form(
data={"username": "populateduser", "email": "populated@example.com"}
)
assert user_form_manager.data == {
"username": "populateduser",
"email": "populated@example.com",
}
# Integration test with Ellar router
def test_zform_with_route_function_parameter():
router = ModuleRouter("/")
@router.http_route("/login", methods=["POST"])
def login(form: ZForm[UserFormModel]):
if form.validate():
return {"status": "success", "data": form.value.dict()}
return {"status": "error", "errors": form.errors}
client = Test.create_test_module(routers=[router]).get_test_client()
# Test with valid data
response = client.post(
"/login", data={"username": "testuser", "email": "test@example.com"}
)
assert response.status_code == 200
assert response.json() == {
"status": "success",
"data": {"username": "testuser", "email": "test@example.com"},
}
# Test with invalid data
response = client.post(
"/login", data={"username": "testuser", "email": "invalid_email"}
)
assert response.status_code == 200
assert response.json()["status"] == "error"
assert "errors" in response.json()
def test_zform_inline_instantiation():
router = ModuleRouter("/")
@router.http_route("/login", methods=["POST"])
def login():
form = FormManager.from_schema(UserFormModel)
if form.validate():
return {"status": "success", "data": form.value.dict()}
return {"status": "error", "errors": form.errors}
client = Test.create_test_module(routers=[router]).get_test_client()
# Test with valid data
response = client.post(
"/login", data={"username": "testuser", "email": "test@example.com"}
)
assert response.status_code == 200
assert response.json() == {
"status": "success",
"data": {"username": "testuser", "email": "test@example.com"},
}
# Test with invalid data
response = client.post(
"/login", data={"username": "testuser", "email": "invalid_email"}
)
assert response.status_code == 200
assert response.json()["status"] == "error"
assert "errors" in response.json()
def test_zform_from_fields():
fields = [StringField(name="username"), EmailField(name="email")]
form = FormManager.from_fields(fields)
assert isinstance(form, FormManager)
assert len(list(form)) == 2
assert all(isinstance(field, FieldBase) for field in list(form))
def test_zform_from_fields_with_populate_form():
fields = [StringField(name="username"), EmailField(name="email")]
form = FormManager.from_fields(fields)
form.populate_form(data={"username": "testuser", "email": "test@example.com"})
assert form.get_field("username").value == "testuser"
assert form.get_field("email").value == "test@example.com"
def test_zform_from_fields_with_validate(create_context):
fields = [StringField(name="username"), EmailField(name="email")]
ctx = create_context({"username": "testuser", "email": "test@example.com"})
form = FormManager.from_fields(fields, ctx=ctx)
is_valid = form.validate()
assert is_valid
assert form.errors == {}
assert form.value == {"username": "testuser", "email": "test@example.com"}