@@ -851,7 +851,53 @@ def test_delimited_list_default_delimiter(web_request, parser):
851851 assert data ["ids" ] == "1,2,3"
852852
853853
854- def test_delimited_list_as_string_v2 (web_request , parser ):
854+ @pytest .mark .skipif (
855+ MARSHMALLOW_VERSION_INFO [0 ] < 3 , reason = "fields.Tuple added in marshmallow3"
856+ )
857+ def test_delimited_tuple_default_delimiter (web_request , parser ):
858+ """
859+ Test load and dump from DelimitedTuple, including the use of a datetime
860+ type (similar to a DelimitedList test below) which confirms that we aren't
861+ relying on __str__, but are properly de/serializing the included fields
862+ """
863+ web_request .json = {"ids" : "1,2,2020-05-04" }
864+ schema_cls = dict2schema (
865+ {
866+ "ids" : fields .DelimitedTuple (
867+ (fields .Int , fields .Int , fields .DateTime (format = "%Y-%m-%d" ))
868+ )
869+ }
870+ )
871+ schema = schema_cls ()
872+
873+ parsed = parser .parse (schema , web_request )
874+ assert parsed ["ids" ] == (1 , 2 , datetime .datetime (2020 , 5 , 4 ))
875+
876+ data = schema .dump (parsed )
877+ assert data ["ids" ] == "1,2,2020-05-04"
878+
879+
880+ @pytest .mark .skipif (
881+ MARSHMALLOW_VERSION_INFO [0 ] < 3 , reason = "fields.Tuple added in marshmallow3"
882+ )
883+ def test_delimited_tuple_incorrect_arity (web_request , parser ):
884+ web_request .json = {"ids" : "1,2" }
885+ schema_cls = dict2schema (
886+ {"ids" : fields .DelimitedTuple ((fields .Int , fields .Int , fields .Int ))}
887+ )
888+ schema = schema_cls ()
889+
890+ with pytest .raises (ValidationError ):
891+ parser .parse (schema , web_request )
892+
893+
894+ def test_delimited_list_with_datetime (web_request , parser ):
895+ """
896+ Test that DelimitedList(DateTime(format=...)) correctly parses and dumps
897+ dates to and from strings -- indicates that we're doing proper
898+ serialization of values in dump() and not just relying on __str__ producing
899+ correct results
900+ """
855901 web_request .json = {"dates" : "2018-11-01,2018-11-02" }
856902 schema_cls = dict2schema (
857903 {"dates" : fields .DelimitedList (fields .DateTime (format = "%Y-%m-%d" ))}
@@ -877,6 +923,27 @@ def test_delimited_list_custom_delimiter(web_request, parser):
877923 parsed = parser .parse (schema , web_request )
878924 assert parsed ["ids" ] == [1 , 2 , 3 ]
879925
926+ dumped = schema .dump (parsed )
927+ data = dumped .data if MARSHMALLOW_VERSION_INFO [0 ] < 3 else dumped
928+ assert data ["ids" ] == "1|2|3"
929+
930+
931+ @pytest .mark .skipif (
932+ MARSHMALLOW_VERSION_INFO [0 ] < 3 , reason = "fields.Tuple added in marshmallow3"
933+ )
934+ def test_delimited_tuple_custom_delimiter (web_request , parser ):
935+ web_request .json = {"ids" : "1|2" }
936+ schema_cls = dict2schema (
937+ {"ids" : fields .DelimitedTuple ((fields .Int , fields .Int ), delimiter = "|" )}
938+ )
939+ schema = schema_cls ()
940+
941+ parsed = parser .parse (schema , web_request )
942+ assert parsed ["ids" ] == (1 , 2 )
943+
944+ data = schema .dump (parsed )
945+ assert data ["ids" ] == "1|2"
946+
880947
881948def test_delimited_list_load_list_errors (web_request , parser ):
882949 web_request .json = {"ids" : [1 , 2 , 3 ]}
@@ -891,6 +958,22 @@ def test_delimited_list_load_list_errors(web_request, parser):
891958 assert errors ["ids" ] == ["Not a valid delimited list." ]
892959
893960
961+ @pytest .mark .skipif (
962+ MARSHMALLOW_VERSION_INFO [0 ] < 3 , reason = "fields.Tuple added in marshmallow3"
963+ )
964+ def test_delimited_tuple_load_list_errors (web_request , parser ):
965+ web_request .json = {"ids" : [1 , 2 ]}
966+ schema_cls = dict2schema ({"ids" : fields .DelimitedTuple ((fields .Int , fields .Int ))})
967+ schema = schema_cls ()
968+
969+ with pytest .raises (ValidationError ) as excinfo :
970+ parser .parse (schema , web_request )
971+ exc = excinfo .value
972+ assert isinstance (exc , ValidationError )
973+ errors = exc .args [0 ]
974+ assert errors ["ids" ] == ["Not a valid delimited tuple." ]
975+
976+
894977# Regresion test for https://github.com/marshmallow-code/webargs/issues/149
895978def test_delimited_list_passed_invalid_type (web_request , parser ):
896979 web_request .json = {"ids" : 1 }
@@ -902,6 +985,19 @@ def test_delimited_list_passed_invalid_type(web_request, parser):
902985 assert excinfo .value .messages == {"json" : {"ids" : ["Not a valid delimited list." ]}}
903986
904987
988+ @pytest .mark .skipif (
989+ MARSHMALLOW_VERSION_INFO [0 ] < 3 , reason = "fields.Tuple added in marshmallow3"
990+ )
991+ def test_delimited_tuple_passed_invalid_type (web_request , parser ):
992+ web_request .json = {"ids" : 1 }
993+ schema_cls = dict2schema ({"ids" : fields .DelimitedTuple ((fields .Int ,))})
994+ schema = schema_cls ()
995+
996+ with pytest .raises (ValidationError ) as excinfo :
997+ parser .parse (schema , web_request )
998+ assert excinfo .value .messages == {"json" : {"ids" : ["Not a valid delimited tuple." ]}}
999+
1000+
9051001def test_missing_list_argument_not_in_parsed_result (web_request , parser ):
9061002 # arg missing in request
9071003 web_request .json = {}
0 commit comments