33
44import pytest
55from bson .json_util import RELAXED_JSON_OPTIONS
6+ from markupsafe import Markup
67from werkzeug .datastructures import MultiDict
78
89wtforms = pytest .importorskip ("wtforms" )
@@ -212,6 +213,105 @@ def test__process_formdata__does_call_parent_method_if_value_is_not_empty(
212213 assert obj .data is False
213214
214215
216+ class TestMongoDictField :
217+ def test_mongo_dict_field_mro_not_changed (self ):
218+ field_mro = list (mongo_fields .MongoDictField .__mro__ [:5 ])
219+ assert field_mro == [
220+ mongo_fields .MongoDictField ,
221+ mongo_fields .MongoTextAreaField ,
222+ mongo_fields .EmptyStringIsNoneMixin ,
223+ wtf_fields .TextAreaField ,
224+ wtf_fields .StringField ,
225+ ]
226+
227+ def test_mongo_dict_field_default_dict_in_form_object (self , db ):
228+ class DefaultDictModel (db .Document ):
229+ """Should populate form with {}."""
230+
231+ dict_field = db .DictField ()
232+
233+ DefaultDictForm = DefaultDictModel .to_wtf_form ()
234+
235+ form = DefaultDictForm ()
236+
237+ assert str (form .dict_field ) == Markup (
238+ '<textarea id="dict_field" name="dict_field">\r \n {}</textarea>'
239+ )
240+ assert form .dict_field .data == {} # This is mongoengine default
241+ assert form .dict_field .null is False
242+
243+ assert form .validate ()
244+ form .save ()
245+
246+ obj = DefaultDictModel .objects .get (id = form .instance .pk )
247+ object_dict = json .loads (obj .to_json (json_options = RELAXED_JSON_OPTIONS ))
248+ object_dict .pop ("_id" )
249+
250+ assert object_dict == {"dict_field" : {}}
251+
252+ @pytest .mark .parametrize (
253+ ["null" , "expected_obj" ],
254+ [
255+ (True , {"placeholder_string" : "1" , "null_dict_field" : None }),
256+ (False , {"placeholder_string" : "1" }),
257+ ],
258+ )
259+ def test_mongo_dict_field_default_null_dict_in_form_object (
260+ self , db , null , expected_obj
261+ ):
262+ class DefaultDictModel (db .Document ):
263+ """Should populate form with empty form."""
264+
265+ placeholder_string = db .StringField (default = "1" )
266+ null_dict_field = db .DictField (default = None , null = null )
267+
268+ DefaultDictForm = DefaultDictModel .to_wtf_form ()
269+
270+ form = DefaultDictForm ()
271+
272+ assert str (form .null_dict_field ) == Markup (
273+ '<textarea id="null_dict_field" name="null_dict_field">\r \n </textarea>'
274+ )
275+ assert form .null_dict_field .data is None
276+
277+ assert form .validate ()
278+ form .save ()
279+
280+ obj = DefaultDictModel .objects .get (id = form .instance .pk )
281+ object_dict = json .loads (obj .to_json (json_options = RELAXED_JSON_OPTIONS ))
282+ object_dict .pop ("_id" )
283+
284+ assert object_dict == expected_obj
285+
286+ def test__parse_json_data__raise_error_when_input_is_incorrect_json (self , db ):
287+ class DictModel (db .Document ):
288+ """Should populate form with empty form."""
289+
290+ dict_field = db .DictField ()
291+
292+ FormClass = DictModel .to_wtf_form ()
293+ form = FormClass (MultiDict ({"dict_field" : "foobar" }))
294+ assert not form .validate ()
295+ assert form .errors == {
296+ "dict_field" : [
297+ "Cannot load data: Expecting value: line 1 column 1 (char 0)"
298+ ]
299+ }
300+
301+ def test__ensure_data_is_dict__raise_error_when_input_is_a_list (self , db ):
302+ class DictModel (db .Document ):
303+ """Should populate form with empty form."""
304+
305+ dict_field = db .DictField ()
306+
307+ FormClass = DictModel .to_wtf_form ()
308+ form = FormClass (MultiDict ({"dict_field" : "[]" }))
309+ assert not form .validate ()
310+ assert form .errors == {
311+ "dict_field" : ["Not a valid dictionary (list input detected)." ]
312+ }
313+
314+
215315class TestMongoEmailField :
216316 def test_email_field_mro_not_changed (self ):
217317 field_mro = list (mongo_fields .MongoEmailField .__mro__ [:4 ])
@@ -223,6 +323,12 @@ def test_email_field_mro_not_changed(self):
223323 ]
224324
225325
326+ class TestMongoFloatField :
327+ def test_ensure_widget_not_accidentally_replaced (self ):
328+ field = mongo_fields .MongoFloatField
329+ assert isinstance (field .widget , wtf_widgets .NumberInput )
330+
331+
226332class TestMongoHiddenField :
227333 def test_hidden_field_mro_not_changed (self ):
228334 field_mro = list (mongo_fields .MongoHiddenField .__mro__ [:4 ])
@@ -317,9 +423,3 @@ def test_url_field_mro_not_changed(self):
317423 wtf_fields .URLField ,
318424 wtf_fields .StringField ,
319425 ]
320-
321-
322- class TestMongoFloatField :
323- def test_ensure_widget_not_accidentally_replaced (self ):
324- field = mongo_fields .MongoFloatField
325- assert isinstance (field .widget , wtf_widgets .NumberInput )
0 commit comments