@@ -53,52 +53,6 @@ def _last_request():
5353 return httpretty .last_request ()
5454
5555
56- class TestNormalisers :
57- @pytest .mark .parametrize (
58- "value, expected" ,
59- [
60- (None , None ),
61- (3 , 3 ),
62- ("3" , 3 ),
63- ("customer" , 3 ),
64- ("CUSTOMER" , 3 ),
65- ("Third-Party" , 4 ),
66- ("third_party" , 4 ),
67- ],
68- )
69- def test_source_kind (self , value , expected ):
70- assert metadata .normalise_source_kind (value ) == expected
71-
72- @pytest .mark .parametrize (
73- "value, expected" ,
74- [
75- (None , None ),
76- (6 , 6 ),
77- ("generic" , 6 ),
78- ("GENERIC" , 6 ),
79- ("PROVENANCE" , 4 ),
80- ],
81- )
82- def test_classification (self , value , expected ):
83- assert metadata .normalise_classification (value ) == expected
84-
85- def test_invalid_source_kind_name (self ):
86- with pytest .raises (ValueError , match = "Invalid source_kind" ):
87- metadata .normalise_source_kind ("not-a-kind" )
88-
89- def test_invalid_classification_name (self ):
90- with pytest .raises (ValueError , match = "Invalid classification" ):
91- metadata .normalise_classification ("nope" )
92-
93- def test_invalid_type (self ):
94- with pytest .raises (ValueError ):
95- metadata .normalise_source_kind (3.14 )
96-
97- def test_bool_rejected (self ):
98- with pytest .raises (ValueError ):
99- metadata .normalise_source_kind (True )
100-
101-
10256class TestListMetadata :
10357 @httpretty .activate (allow_net_connect = False )
10458 def test_success_returns_results_and_page_info (self ):
@@ -128,7 +82,7 @@ def test_success_returns_results_and_page_info(self):
12882 assert sent .headers .get ("Accept" ) == "application/json"
12983
13084 @httpretty .activate (allow_net_connect = False )
131- def test_filters_normalised_to_integers (self ):
85+ def test_filters_sent_as_lowercased_names (self ):
13286 httpretty .register_uri (
13387 httpretty .GET ,
13488 LIST_URL ,
@@ -138,12 +92,12 @@ def test_filters_normalised_to_integers(self):
13892 )
13993
14094 metadata .list_metadata (
141- PKG , source_kind = "customer " , classification = "GENERIC" , page = 2 , page_size = 50
95+ PKG , source_kind = "custom " , classification = "GENERIC" , page = 2 , page_size = 50
14296 )
14397
14498 qs = _last_request ().querystring # pylint: disable=no-member
145- assert qs ["source_kind" ] == ["3 " ]
146- assert qs ["classification" ] == ["6 " ]
99+ assert qs ["source_kind" ] == ["custom " ]
100+ assert qs ["classification" ] == ["generic " ]
147101 assert qs ["page" ] == ["2" ]
148102 assert qs ["page_size" ] == ["50" ]
149103
@@ -163,6 +117,24 @@ def test_non_positive_page_options_omitted(self):
163117 assert "page" not in qs
164118 assert "page_size" not in qs
165119
120+ @httpretty .activate (allow_net_connect = False )
121+ def test_blank_filters_omitted (self ):
122+ # A whitespace-only filter normalises to an empty string, which must be
123+ # dropped rather than sent as an empty query param (the backend 4xxs it).
124+ httpretty .register_uri (
125+ httpretty .GET ,
126+ LIST_URL ,
127+ body = json .dumps ({"results" : []}),
128+ status = 200 ,
129+ content_type = "application/json" ,
130+ )
131+
132+ metadata .list_metadata (PKG , source_kind = " " , classification = "" )
133+
134+ qs = _last_request ().querystring # pylint: disable=no-member
135+ assert "source_kind" not in qs
136+ assert "classification" not in qs
137+
166138 @httpretty .activate (allow_net_connect = False )
167139 def test_404_raises_api_exception (self ):
168140 httpretty .register_uri (
@@ -180,10 +152,17 @@ def test_404_raises_api_exception(self):
180152 assert exc_info .value .detail == "Not found."
181153
182154 @httpretty .activate (allow_net_connect = False )
183- def test_422_raises_with_fields (self ):
155+ def test_invalid_filter_name_surfaces_backend_error (self ):
156+ # The API client lowercases/strips filter values but does not validate
157+ # them; an unknown name is forwarded and the backend rejects it via
158+ # EnumFieldV2.
159+ message = (
160+ "bogus is not valid for source_kind - must be one of "
161+ "['UNKNOWN', 'SYSTEM', 'UPSTREAM', 'CUSTOM', 'THIRD_PARTY']"
162+ )
184163 body = {
185164 "detail" : "Invalid query parameters." ,
186- "fields" : {"source_kind" : ["Not a valid choice." ]},
165+ "fields" : {"source_kind" : [message ]},
187166 }
188167 httpretty .register_uri (
189168 httpretty .GET ,
@@ -194,10 +173,12 @@ def test_422_raises_with_fields(self):
194173 )
195174
196175 with pytest .raises (ApiException ) as exc_info :
197- metadata .list_metadata (PKG , source_kind = 3 )
176+ metadata .list_metadata (PKG , source_kind = "bogus" )
198177
178+ qs = _last_request ().querystring # pylint: disable=no-member
179+ assert qs ["source_kind" ] == ["bogus" ]
199180 assert exc_info .value .status == 422
200- assert exc_info .value .fields == {"source_kind" : ["Not a valid choice." ]}
181+ assert exc_info .value .fields == {"source_kind" : [message ]}
201182
202183
203184class TestGetMetadata :
0 commit comments