@@ -138,6 +138,258 @@ def testGet(self):
138138 self .assertEqual (self .dummy_client .response_code , 200 )
139139 self .assertEqual (results ['data' ]['data' ], 'joe' )
140140
141+ def testOutputFormat (self ):
142+ """ test of @fields and @verbose implementation """
143+
144+ from roundup .exceptions import UsageError
145+
146+ self .maxDiff = 4000
147+ # create sample data
148+ try :
149+ self .db .status .create (name = 'open' )
150+ except ValueError :
151+ pass
152+ try :
153+ self .db .status .create (name = 'closed' )
154+ except ValueError :
155+ pass
156+ try :
157+ self .db .priority .create (name = 'normal' )
158+ except ValueError :
159+ pass
160+ try :
161+ self .db .priority .create (name = 'critical' )
162+ except ValueError :
163+ pass
164+ self .db .issue .create (
165+ title = 'foo1' ,
166+ status = self .db .status .lookup ('open' ),
167+ priority = self .db .priority .lookup ('normal' ),
168+ nosy = [ "1" , "2" ]
169+ )
170+ issue_open_norm = self .db .issue .create (
171+ title = 'foo2' ,
172+ status = self .db .status .lookup ('open' ),
173+ priority = self .db .priority .lookup ('normal' ),
174+ assignedto = "3"
175+ )
176+ issue_open_crit = self .db .issue .create (
177+ title = 'foo5' ,
178+ status = self .db .status .lookup ('open' ),
179+ priority = self .db .priority .lookup ('critical' )
180+ )
181+ base_path = self .db .config ['TRACKER_WEB' ] + 'rest/data/issue/'
182+
183+
184+ # Check formating for issues status=open; @fields and verbose tests
185+ form = cgi .FieldStorage ()
186+ form .list = [
187+ cgi .MiniFieldStorage ('status' , 'open' ),
188+ cgi .MiniFieldStorage ('@fields' , 'nosy,status' ),
189+ cgi .MiniFieldStorage ('@verbose' , '2' )
190+ ]
191+
192+ expected = {'data' :
193+ {'@total_size' : 3 ,
194+ 'collection' : [
195+ {'status' : {'id' : '9' ,
196+ 'name' : 'open' ,
197+ 'link' : 'http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/status/9' },
198+ 'id' : '1' ,
199+ 'nosy' : [
200+ {'username' : 'admin' ,
201+ 'id' : '1' ,
202+ 'link' : 'http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/user/1' },
203+ {'username' : 'anonymous' ,
204+ 'id' : '2' ,
205+ 'link' : 'http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/user/2' }
206+ ],
207+ 'link' : 'http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/issue/1' ,
208+ 'title' : 'foo1' },
209+ {'status' : {
210+ 'id' : '9' ,
211+ 'name' : 'open' ,
212+ 'link' : 'http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/status/9' },
213+ 'id' : '2' ,
214+ 'nosy' : [
215+ {'username' : 'joe' ,
216+ 'id' : '3' ,
217+ 'link' : 'http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/user/3' }
218+ ],
219+ 'link' : 'http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/issue/2' ,
220+ 'title' : 'foo2' },
221+ {'status' : {
222+ 'id' : '9' ,
223+ 'name' : 'open' ,
224+ 'link' : 'http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/status/9' },
225+ 'id' : '3' ,
226+ 'nosy' : [],
227+ 'link' : 'http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/issue/3' ,
228+ 'title' : 'foo5' }
229+ ]}}
230+
231+ results = self .server .get_collection ('issue' , form )
232+ self .assertDictEqual (expected , results )
233+
234+ # Check formating for issues status=open; @fields and verbose tests
235+ form = cgi .FieldStorage ()
236+ form .list = [
237+ cgi .MiniFieldStorage ('status' , 'open' )
238+ # default cgi.MiniFieldStorage('@verbose', '1')
239+ ]
240+
241+ expected = {'data' :
242+ {'@total_size' : 3 ,
243+ 'collection' : [
244+ {'id' : '1' ,
245+ 'link' : 'http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/issue/1' ,},
246+ { 'id' : '2' ,
247+
248+ 'link' : 'http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/issue/2' },
249+ {'id' : '3' ,
250+ 'link' : 'http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/issue/3' } ]}}
251+
252+
253+ results = self .server .get_collection ('issue' , form )
254+ self .assertDictEqual (expected , results )
255+
256+ # Generate failure case, unknown field.
257+ form = cgi .FieldStorage ()
258+ form .list = [
259+ cgi .MiniFieldStorage ('status' , 'open' ),
260+ cgi .MiniFieldStorage ('@fields' , 'title,foo' )
261+ ]
262+
263+ expected = {'error' : {
264+ 'msg' : UsageError ("Failed to find property 'foo' "
265+ "for class issue." ,),
266+ 'status' : 400 }}
267+
268+ results = self .server .get_collection ('issue' , form )
269+ # I tried assertDictEqual but seems it can't handle
270+ # the exception value of 'msg'. So I am using repr to check.
271+ self .assertEqual (repr (sorted (expected ['error' ])),
272+ repr (sorted (results ['error' ]))
273+ )
274+
275+ # Check formating for issues status=open; @fields and verbose tests
276+ form = cgi .FieldStorage ()
277+ form .list = [
278+ cgi .MiniFieldStorage ('status' , 'open' ),
279+ cgi .MiniFieldStorage ('@fields' , 'nosy,status,assignedto' ),
280+ cgi .MiniFieldStorage ('@verbose' , '0' )
281+ ]
282+
283+ expected = {'data' : {
284+ '@total_size' : 3 ,
285+ 'collection' : [
286+ {'assignedto' : None ,
287+ 'link' : 'http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/issue/1' ,
288+ 'status' : '9' ,
289+ 'nosy' : ['1' , '2' ],
290+ 'id' : '1' },
291+ {'assignedto' : '3' ,
292+ 'link' : 'http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/issue/2' ,
293+ 'status' : '9' ,
294+ 'nosy' : ['3' ],
295+ 'id' : '2' },
296+ {'assignedto' : None ,
297+ 'link' : 'http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/issue/3' ,
298+ 'status' : '9' ,
299+ 'nosy' : [],
300+ 'id' : '3' }]}}
301+
302+ results = self .server .get_collection ('issue' , form )
303+ print (results )
304+ self .assertDictEqual (expected , results )
305+
306+ # check users
307+ form = cgi .FieldStorage ()
308+ form .list = [
309+ cgi .MiniFieldStorage ('@fields' , 'username,queries,password' ),
310+ cgi .MiniFieldStorage ('@verbose' , '0' )
311+ ]
312+ # note this is done as user joe, so we only get queries
313+ # and password for joe.
314+ expected = {'data' : {'collection' : [
315+ {'id' : '1' ,
316+ 'username' : 'admin' ,
317+ 'link' : 'http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/user/1' },
318+ {'id' : '2' ,
319+ 'username' : 'anonymous' ,
320+ 'link' : 'http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/user/2' },
321+ {'password' : '[password hidden scheme PBKDF2]' ,
322+ 'id' : '3' ,
323+ 'queries' : [],
324+ 'username' : 'joe' ,
325+ 'link' : 'http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/user/3' }],
326+ '@total_size' : 3 }}
327+
328+ results = self .server .get_collection ('user' , form )
329+ self .assertDictEqual (expected , results )
330+
331+ ## Start testing get_element
332+ form = cgi .FieldStorage ()
333+ form .list = [
334+ cgi .MiniFieldStorage ('@fields' , 'queries,password' ),
335+ cgi .MiniFieldStorage ('@verbose' , '2' )
336+ ]
337+ expected = {'data' : {
338+ 'id' : '3' ,
339+ 'type' : 'user' ,
340+ '@etag' : '' ,
341+ 'link' : 'http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/user/3' ,
342+ 'attributes' : {
343+ 'password' : '[password hidden scheme PBKDF2]' ,
344+ 'queries' : [],
345+ 'username' : 'joe'
346+ }
347+ }}
348+
349+ results = self .server .get_element ('user' , self .joeid , form )
350+ results ['data' ]['@etag' ] = '' # etag depends on date, set to empty
351+ self .assertDictEqual (expected ,results )
352+
353+ form = cgi .FieldStorage ()
354+ form .list = [
355+ cgi .MiniFieldStorage ('@fields' , 'status:priority' ),
356+ cgi .MiniFieldStorage ('@verbose' , '1' )
357+ ]
358+ expected = {'data' : {
359+ 'type' : 'issue' ,
360+ 'id' : '3' ,
361+ 'attributes' : {
362+ 'status' : {
363+ 'id' : '9' ,
364+ 'link' : 'http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/status/9' },
365+ 'priority' : {
366+ 'id' : '1' ,
367+ 'link' : 'http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/priority/1' }},
368+ '@etag' : '' ,
369+ 'link' : 'http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/issue/3' }}
370+
371+ results = self .server .get_element ('issue' , "3" , form )
372+ results ['data' ]['@etag' ] = '' # etag depends on date, set to empty
373+ self .assertDictEqual (expected ,results )
374+
375+ form = cgi .FieldStorage ()
376+ form .list = [
377+ cgi .MiniFieldStorage ('@fields' , 'status,priority' ),
378+ cgi .MiniFieldStorage ('@verbose' , '0' )
379+ ]
380+ expected = {'data' : {
381+ 'type' : 'issue' ,
382+ 'id' : '3' ,
383+ 'attributes' : {
384+ 'status' : '9' ,
385+ 'priority' : '1' },
386+ '@etag' : '' ,
387+ 'link' : 'http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/issue/3' }}
388+
389+ results = self .server .get_element ('issue' , "3" , form )
390+ results ['data' ]['@etag' ] = '' # etag depends on date, set to empty
391+ self .assertDictEqual (expected ,results )
392+
141393 def testFilter (self ):
142394 """
143395 Retrieve all three users
0 commit comments