Skip to content

Commit 5a5c1af

Browse files
committed
Add TestEmptyStringIsNoneMixin
1 parent 7725919 commit 5a5c1af

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

tests/test_forms_v2.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from werkzeug.datastructures import MultiDict
66

77
wtforms = pytest.importorskip("wtforms")
8+
from flask_mongoengine.wtf import fields # noqa
89

910

1011
@pytest.fixture()
@@ -136,3 +137,37 @@ class AllFieldsModel(db.Document):
136137
"map_field": {},
137138
"sequence_field": 1,
138139
}
140+
141+
142+
class TestEmptyStringIsNoneMixin:
143+
"""Special mixin to ignore empty strings **before** parent class processing."""
144+
145+
class ParentClass:
146+
def __init__(self):
147+
self.data = True
148+
149+
def process_formdata(self, valuelist):
150+
self.data = False
151+
152+
class FakeClass(fields.EmptyStringIsNoneMixin, ParentClass):
153+
"""Just MRO setter."""
154+
155+
pass
156+
157+
@pytest.mark.parametrize("value", ["", None, [], {}, (), (""), [""]])
158+
def test__process_formdata__does_not_call_parent_method_if_value_is_empty(
159+
self, value
160+
):
161+
obj = self.FakeClass()
162+
assert obj.data is True
163+
obj.process_formdata(value)
164+
assert obj.data is None
165+
166+
@pytest.mark.parametrize("value", [[None], [1], (1,), (" ",), [" "]])
167+
def test__process_formdata__does_call_parent_method_if_value_is_not_empty(
168+
self, value
169+
):
170+
obj = self.FakeClass()
171+
assert obj.data is True
172+
obj.process_formdata(value)
173+
assert obj.data is False

0 commit comments

Comments
 (0)