PageRenderTime 118ms CodeModel.GetById 14ms app.highlight 97ms RepoModel.GetById 1ms app.codeStats 1ms

/core/externals/update-engine/externals/gdata-objectivec-client/Source/Tests/GDataQueryTest.m

http://macfuse.googlecode.com/
Objective C | 584 lines | 425 code | 109 blank | 50 comment | 0 complexity | ea40a1551988eb4aa542b1803b918d9a MD5 | raw file
  1/* Copyright (c) 2007 Google Inc.
  2*
  3* Licensed under the Apache License, Version 2.0 (the "License");
  4* you may not use this file except in compliance with the License.
  5* You may obtain a copy of the License at
  6*
  7*     http://www.apache.org/licenses/LICENSE-2.0
  8*
  9* Unless required by applicable law or agreed to in writing, software
 10* distributed under the License is distributed on an "AS IS" BASIS,
 11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12* See the License for the specific language governing permissions and
 13* limitations under the License.
 14*/
 15
 16//
 17//  GDataQueryTest.m
 18//
 19
 20#import <SenTestingKit/SenTestingKit.h>
 21
 22#import "GDataQuery.h"
 23#import "GData.h"
 24
 25#define typeof __typeof__ // fixes http://www.brethorsting.com/blog/2006/02/stupid-issue-with-ocunit.html
 26
 27@interface GDataQueryTest : SenTestCase
 28@end
 29
 30@implementation GDataQueryTest
 31
 32- (void)testGDataCalendarQuery {
 33  
 34  NSURL* feedURL = [NSURL URLWithString:@"http://www.google.com/calendar/feeds/userID/private/basic"];
 35  
 36  GDataDateTime* dateTime1 = [GDataDateTime dateTimeWithRFC3339String:@"2006-03-29T07:35:59.000Z"];
 37  GDataDateTime* dateTime2 = [GDataDateTime dateTimeWithRFC3339String:@"2006-03-30T07:35:59.000Z"];
 38  GDataDateTime* dateTime3 = [GDataDateTime dateTimeWithRFC3339String:@"2006-04-29T07:35:59.000Z"];
 39  GDataDateTime* dateTime4 = [GDataDateTime dateTimeWithRFC3339String:@"2007-06-25T13:37:54.146+07:00"];
 40  
 41  // test query with feed URL but no params
 42  GDataQuery* query1 = [GDataQuery queryWithFeedURL:feedURL];
 43  NSURL* resultURL1 = [query1 URL];
 44  STAssertEqualObjects(resultURL1, feedURL, @"Unadorned feed URL is not preserved by GDataQuery");
 45
 46  // test query with params but no categories
 47  GDataQuery* query2 = [GDataQuery queryWithFeedURL:feedURL];
 48  [query2 setAuthor:@"Fred Flintstone"];
 49  [query2 setFullTextQueryString:@"Darcy Dingo"];
 50  [query2 setIsAscendingOrder:YES];
 51  [query2 setIsStrict:YES];
 52  [query2 setLanguage:@"en"];
 53  [query2 setMaxResults:20];
 54  [query2 setOrderBy:@"random"];
 55  [query2 setProtocolVersion:@"2.0"];
 56  [query2 setShouldPrettyPrint:YES];
 57  [query2 setShouldShowDeleted:YES];
 58  [query2 setShouldRequireAllDeleted:YES];
 59  [query2 setShouldShowOnlyDeleted:YES];
 60  [query2 setStartIndex:10];
 61  [query2 setPublishedMinDateTime:dateTime1];
 62  [query2 setPublishedMaxDateTime:dateTime2];
 63  [query2 setUpdatedMinDateTime:dateTime3];
 64  [query2 setUpdatedMaxDateTime:dateTime4];
 65  [query2 addCustomParameterWithName:@"Fred" value:@"Barney"];
 66  [query2 addCustomParameterWithName:@"Wilma" value:@"Betty"];
 67  
 68  NSURL* resultURL2 = [query2 URL];
 69  
 70  NSString *expected2 = @"http://www.google.com/calendar/feeds/userID/private/basic?"
 71    "author=Fred+Flintstone&Fred=Barney&hl=en&max-results=20&onlydeleted=true"
 72    "&orderby=random&prettyprint=true&published-max=2006-03-30T07%3A35%3A59Z"
 73    "&published-min=2006-03-29T07%3A35%3A59Z&q=Darcy+Dingo"
 74    "&requirealldeleted=true&showdeleted=true"
 75    "&sortorder=ascending&start-index=10&strict=true"
 76    "&updated-max=2007-06-25T13%3A37%3A54%2B07%3A00"
 77    "&updated-min=2006-04-29T07%3A35%3A59Z&v=2.0&Wilma=Betty";
 78  
 79  STAssertEqualObjects([resultURL2 absoluteString], expected2, @"Parameter generation error");
 80  
 81  GDataCategoryFilter *categoryFilter = [GDataCategoryFilter categoryFilter];
 82  [categoryFilter addCategory:[GDataCategory categoryWithScheme:@"http://schemas.google.com/g/2005#kind" 
 83                                                           term:@"http://schemas.google.com/g/2005#event"]];
 84  [categoryFilter addCategoryWithScheme:@"MyScheme2" term:@"MyTerm2"];
 85  [categoryFilter addExcludeCategoryWithScheme:nil term:@"MyTerm3"];
 86
 87  // test a query with categories but no params
 88  GDataQuery* query3 = [GDataQuery queryWithFeedURL:feedURL];
 89  [query3 addCategoryFilter:categoryFilter];
 90  NSURL* resultURL3 = [query3 URL];
 91
 92  NSString* expected3 = @"http://www.google.com/calendar/feeds/userID/private/"
 93    "basic/-/%7Bhttp%3A%2F%2Fschemas.google.com%2Fg%2F2005%23kind%7Dhttp"
 94    "%3A%2F%2Fschemas.google.com%2Fg%2F2005%23event%7C%7BMyScheme2%7D"
 95    "MyTerm2%7C-MyTerm3";
 96  STAssertEqualObjects([resultURL3 absoluteString], expected3, @"Category filter generation error");
 97
 98
 99  // finally, add the previous category filter and another category filter
100  // to the second query's parameters
101  [query2 addCategoryFilter:categoryFilter];
102  [query2 addCategoryFilterWithScheme:nil term:@"Zonk4"];
103  
104  NSURL* resultURL2a = [query2 URL];
105
106  NSString *expected2a = @"http://www.google.com/calendar/feeds/userID/private/"
107    "basic/-/%7Bhttp%3A%2F%2Fschemas.google.com%2Fg%2F2005%23kind%7Dhttp%3A%2F%2F"
108    "schemas.google.com%2Fg%2F2005%23event%7C%7BMyScheme2%7DMyTerm2%7C-MyTerm3/"
109    "Zonk4?author=Fred+Flintstone&Fred=Barney&hl=en&max-results=20&onlydeleted=true&"
110    "orderby=random&prettyprint=true&published-max=2006-03-30T07%3A35%3A59Z&"
111    "published-min=2006-03-29T07%3A35%3A59Z&q=Darcy+Dingo&"
112    "requirealldeleted=true&showdeleted=true&"
113    "sortorder=ascending&start-index=10&strict=true&"
114    "updated-max=2007-06-25T13%3A37%3A54%2B07%3A00"
115    "&updated-min=2006-04-29T07%3A35%3A59Z&v=2.0&Wilma=Betty";
116
117  STAssertEqualObjects([resultURL2a absoluteString], expected2a, @"Category filter generation error");
118  
119  //
120  // test a calendar query
121  //
122  GDataQueryCalendar* queryCal = [GDataQueryCalendar calendarQueryWithFeedURL:feedURL];
123  [queryCal setStartIndex:10];
124  [queryCal setMaxResults:20];
125  [queryCal setMinimumStartTime:dateTime1];
126  [queryCal setMaximumStartTime:dateTime2];
127  [queryCal setShouldShowInlineComments:YES];
128  [queryCal setShouldShowHiddenEvents:NO];
129  
130  NSURL* resultURLC1 = [queryCal URL];
131  NSString *expectedC1 = @"http://www.google.com/calendar/feeds/userID/private/basic?"
132    "max-results=20&start-index=10&start-max=2006-03-30T07%3A35%3A59Z&start-min=2006-03-29T07%3A35%3A59Z";
133
134  STAssertEqualObjects([resultURLC1 absoluteString], expectedC1, @"Query error");
135  
136  GDataQueryCalendar* queryCal2 = [GDataQueryCalendar calendarQueryWithFeedURL:feedURL];
137  [queryCal2 setRecurrenceExpansionStartTime:dateTime1];
138  [queryCal2 setRecurrenceExpansionEndTime:dateTime1];
139  [queryCal2 setShouldQueryAllFutureEvents:YES];
140  [queryCal2 setShouldExpandRecurrentEvents:YES];
141  [queryCal2 setCurrentTimeZoneName:@"America/Los Angeles"];
142  [queryCal2 setShouldShowInlineComments:NO];
143  [queryCal2 setShouldShowHiddenEvents:YES];
144  [queryCal2 setShouldFormatErrorsAsXML:YES];
145  [queryCal2 setMaximumAttendees:12];
146  [queryCal2 setFieldSelection:@"entry(@gd:*,link,title)"];
147
148  NSURL* resultURLC2 = [queryCal2 URL];
149  NSString *expectedC2 = @"http://www.google.com/calendar/feeds/userID/private/basic?"
150    "ctz=America%2FLos_Angeles&err=xml&"
151    "fields=entry%28%40gd%3A%2A%2Clink%2Ctitle%29&futureevents=true&"
152    "max-attendees=12&"
153    "recurrence-expansion-end=2006-03-29T07%3A35%3A59Z&"
154    "recurrence-expansion-start=2006-03-29T07%3A35%3A59Z&"
155    "showhidden=true&showinlinecomments=false&singleevents=true";
156  STAssertEqualObjects([resultURLC2 absoluteString], expectedC2, @"Query error");
157  
158}
159
160- (void)testGDataSpreadsheetsQuery {
161  
162  NSURL* feedURL = [NSURL URLWithString:kGDataGoogleSpreadsheetsPrivateFullFeed];
163  
164  // cell feed query tests
165  GDataQuerySpreadsheet* query1 = [GDataQuerySpreadsheet spreadsheetQueryWithFeedURL:feedURL];
166  [query1 setMinimumRow:3];
167  [query1 setMaximumRow:7];
168  [query1 setMinimumColumn:2];
169  [query1 setMaximumColumn:12];
170  [query1 setRange:@"A1:B2"];
171  [query1 setShouldReturnEmpty:YES];
172  
173  NSURL* resultURL1 = [query1 URL];
174  NSString *expected1 = @"https://spreadsheets.google.com/feeds/spreadsheets/private/full?"
175    "max-col=12&max-row=7&min-col=2&min-row=3&range=A1%3AB2&return-empty=true";
176  STAssertEqualObjects([resultURL1 absoluteString], expected1, 
177                       @"Spreadsheet query 1 generation error");
178
179  // list feed query tests
180  GDataQuerySpreadsheet* query2 = [GDataQuerySpreadsheet spreadsheetQueryWithFeedURL:feedURL];
181  [query2 setSpreadsheetQuery:@"ipm<4 and hours>40"];
182  [query2 setOrderBy:@"column:foostuff"];
183  [query2 setIsReverseSort:YES];
184  
185  NSURL* resultURL2 = [query2 URL];
186  NSString *expected2 = @"https://spreadsheets.google.com/feeds/spreadsheets/private/full?"
187    "orderby=column%3Afoostuff&reverse=true&sq=ipm%3C4+and+hours%3E40";
188  STAssertEqualObjects([resultURL2 absoluteString], expected2, 
189                       @"Spreadsheet query 2 generation error");
190}
191
192- (void)testGooglePhotosQuery {
193  
194  GDataQueryGooglePhotos *pwaQuery1;
195  pwaQuery1 = [GDataQueryGooglePhotos photoQueryForUserID:@"fredflintstone"
196                                                  albumID:@"12345"
197                                                albumName:nil
198                                                  photoID:@"987654321"];
199  [pwaQuery1 setKind:kGDataGooglePhotosKindPhoto];
200  [pwaQuery1 setAccess:kGDataGooglePhotosAccessPrivate];
201  [pwaQuery1 setThumbsize:80];
202  [pwaQuery1 setImageSize:32];
203  [pwaQuery1 setTag:@"dog"];
204  
205  NSURL* resultURL1 = [pwaQuery1 URL];
206  NSString *expected1 = @"https://photos.googleapis.com/data/feed/api/"
207    "user/fredflintstone/albumid/12345/photoid/987654321?"
208    "access=private&imgmax=32&kind=photo&tag=dog&thumbsize=80";
209  STAssertEqualObjects([resultURL1 absoluteString], expected1, 
210                       @"PWA query 1 generation error");
211  STAssertEquals([pwaQuery1 imageSize], (NSInteger) 32, @"image size error");
212  
213  GDataQueryGooglePhotos *pwaQuery2; 
214  pwaQuery2 = [GDataQueryGooglePhotos photoQueryForUserID:@"fredflintstone"
215                                                  albumID:nil
216                                                albumName:@"froggy photos"
217                                                  photoID:nil];  
218  [pwaQuery2 setImageSize:kGDataGooglePhotosImageSizeDownloadable];
219  
220  NSURL* resultURL2 = [pwaQuery2 URL];
221  NSString *expected2 = @"https://photos.googleapis.com/data/feed/api/user/"
222    "fredflintstone/album/froggy%20photos?imgmax=d";
223  STAssertEqualObjects([resultURL2 absoluteString], expected2, 
224                       @"PWA query 2 generation error");
225  
226  // image size special cases mapping -1 to "d" and back; test that we get back
227  // -1
228  STAssertEquals([pwaQuery2 imageSize], kGDataGooglePhotosImageSizeDownloadable,
229                 @"image size error (2)");
230  
231  // test the generator for photo contact feed URLs
232  NSURL *contactsURL = [GDataServiceGooglePhotos photoContactsFeedURLForUserID:@"fred@example.com"];
233  NSString *contactsURLString = [contactsURL absoluteString];
234  NSString *expectedContactsURLString = @"https://photos.googleapis.com/data/feed/api/user/fred%40example.com/contacts?kind=user";
235  STAssertEqualObjects(contactsURLString, expectedContactsURLString, 
236                       @"contacts URL error");
237
238  // test service document request
239  GDataQueryGooglePhotos *pwaQuery3;
240  pwaQuery3 = [GDataQueryGooglePhotos photoQueryForUserID:@"fredflintstone"
241                                                  albumID:nil
242                                                albumName:nil
243                                                  photoID:nil];
244  [pwaQuery3 setResultFormat:kGDataQueryResultServiceDocument];
245  NSURL *resultURL3 = [pwaQuery3 URL];
246  NSString *expected3 = @"https://photos.googleapis.com/data/feed/api/user/fredflintstone?alt=atom-service";
247  STAssertEqualObjects([resultURL3 absoluteString], expected3,
248                       @"PWA query 3 generation error");
249}
250
251- (void)testMapsQuery {
252  NSURL *feedURL = [GDataServiceGoogleMaps mapsFeedURLForUserID:kGDataServiceDefaultUser
253                                                     projection:kGDataMapsProjectionFull];
254  GDataQueryMaps *mapsQuery1, *mapsQuery2;
255
256  mapsQuery1 = [GDataQueryMaps mapsQueryWithFeedURL:feedURL];
257  [mapsQuery1 setPrevID:@"pid"];
258  [mapsQuery1 setAttributeQueryString:@"[pool:true][price:budget]"];
259  [mapsQuery1 setBoxString:@"1,2,3,4"];
260  [mapsQuery1 setLatitude:12.3];
261  [mapsQuery1 setLongitude:23.4];
262  [mapsQuery1 setRadius:33.3];
263  [mapsQuery1 setSortBy:@"distance"];
264  NSURL *resultURL1 = [mapsQuery1 URL];
265  NSString *expected1 = @"https://maps.google.com/maps/feeds/maps/default/full?"
266    "box=1%2C2%2C3%2C4&lat=12.300000&lng=23.400000&"
267    "mq=%5Bpool%3Atrue%5D%5Bprice%3Abudget%5D&previd=pid&"
268    "radius=33.300000&sortby=distance";
269  STAssertEqualObjects([resultURL1 absoluteString], expected1,
270                       @"Maps Query 1 generation error");
271
272  mapsQuery2 = [GDataQueryMaps mapsQueryWithFeedURL:feedURL];
273  [mapsQuery2 setBoxWithWest:10 south:20 east:30 north:40];
274  NSURL *resultURL2 = [mapsQuery2 URL];
275  NSString *expected2 = @"https://maps.google.com/maps/feeds/maps/default/full?"
276    "box=10.000000%2C20.000000%2C30.000000%2C40.000000";
277  STAssertEqualObjects([resultURL2 absoluteString], expected2,
278                       @"Maps Query 2 generation error");
279}
280
281- (void)testYouTubeQuery {
282  
283  NSURL *feedURL = [GDataServiceGoogleYouTube youTubeURLForUserID:@"fred"
284                                                       userFeedID:kGDataYouTubeUserFeedIDFavorites];
285  
286  GDataQueryYouTube *ytQuery1;  
287  ytQuery1 = [GDataQueryYouTube youTubeQueryWithFeedURL:feedURL];
288  
289  [ytQuery1 setVideoQuery:@"\"Fred Flintstone\""];
290  [ytQuery1 setFormat:@"0,5,6"];
291  [ytQuery1 setCaptionTrackFormat:kGDataYouTubeCaptionTrackFormatSubviewer];
292  [ytQuery1 setTimePeriod:kGDataYouTubePeriodThisWeek];
293  [ytQuery1 setOrderBy:kGDataYouTubeOrderByRelevance];
294  [ytQuery1 setRestriction:@"127.0.0.1"];
295  [ytQuery1 setLanguageRestriction:@"en"];
296  [ytQuery1 setLocation:@"Canada"];
297  [ytQuery1 setLocationRadius:@"2km"];
298  [ytQuery1 setSafeSearch:kGDataYouTubeSafeSearchStrict];
299  [ytQuery1 setHasCaptions:YES];
300  [ytQuery1 setShouldInline:YES];
301  [ytQuery1 setShouldRequire3D:YES];
302  [ytQuery1 setUploader:@"foo"];
303  
304  NSURL* resultURL1 = [ytQuery1 URL];
305  NSString *expected1 = @"https://gdata.youtube.com/feeds/api/users/fred/"
306    "favorites?3d=true&caption=true&fmt=sbv&format=0%2C5%2C6&inline=true&location=Canada&"
307    "location-radius=2km&lr=en&orderby=relevance&q=%22Fred+Flintstone%22&"
308    "restriction=127.0.0.1&safeSearch=strict&time=this_week&uploader=foo";
309
310  STAssertEqualObjects([resultURL1 absoluteString], expected1, 
311                       @"YouTube query 1 generation error");
312}
313
314- (void)testContactQuery {
315  
316  GDataQueryContact *query1;
317  query1 = [GDataQueryContact contactQueryForUserID:@"user@example.com"];
318  
319  [query1 setGroupIdentifier:@"http://www.google.com/m8/feeds/groups/user%40example.com/base/6"];
320  
321  NSURL *resultURL1 = [query1 URL];
322  NSString *expected1 = @"https://www.google.com/m8/feeds/contacts/user%40example.com/full?group=http%3A%2F%2Fwww.google.com%2Fm8%2Ffeeds%2Fgroups%2Fuser%2540example.com%2Fbase%2F6";
323  STAssertEqualObjects([resultURL1 absoluteString], expected1, 
324                       @"Contacts query 1 generation error");
325}
326
327- (void)testFinanceQuery {
328  
329  NSURL *feedURL = [GDataServiceGoogleFinance portfolioFeedURLForUserID:@"user@example.com"];
330
331  GDataQueryFinance *query1;
332  query1 = [GDataQueryFinance financeQueryWithFeedURL:feedURL];
333  
334  [query1 setShouldIncludeReturns:NO];
335  [query1 setShouldIncludePositions:NO];
336  [query1 setShouldIncludeTransactions:NO];
337
338  NSURL *resultURL1 = [query1 URL];
339  NSString *expected1 = @"https://finance.google.com/finance/feeds/user%40example.com/portfolios";
340  STAssertEqualObjects([resultURL1 absoluteString], expected1, 
341                       @"Finance query 1 generation error");
342  
343  GDataQueryFinance *query2;
344  query2 = [GDataQueryFinance financeQueryWithFeedURL:feedURL];
345  
346  [query2 setShouldIncludeReturns:YES];
347  [query2 setShouldIncludePositions:YES];
348  [query2 setShouldIncludeTransactions:YES];
349  
350  NSURL *resultURL2 = [query2 URL];
351  NSString *expected2 = @"https://finance.google.com/finance/feeds/user%40example.com/portfolios?positions=true&returns=true&transactions=true";
352  
353  STAssertEqualObjects([resultURL2 absoluteString], expected2, 
354                       @"Finance query 2 generation error");
355}
356
357- (void)testBooksQuery {
358
359  NSURL *feedURL = [NSURL URLWithString:kGDataGoogleBooksVolumeFeed];
360  GDataQueryBooks *query1 = [GDataQueryBooks booksQueryWithFeedURL:feedURL];
361
362  [query1 setMinimumViewability:kGDataGoogleBooksMinViewabilityFull];
363  [query1 setEBook:@"frogchild"];
364
365  NSURL *resultURL1 = [query1 URL];
366  NSString *expected1 = @"http://books.google.com/books/feeds/volumes?ebook=frogchild&min-viewability=full";
367  STAssertEqualObjects([resultURL1 absoluteString], expected1,
368                       @"Books query 1 generation error");
369}
370
371- (void)testDocsQuery {
372  NSURL *feedURL = [GDataServiceGoogleDocs docsFeedURL];
373  GDataQueryDocs *query1 = [GDataQueryDocs documentQueryWithFeedURL:feedURL];
374
375  [query1 setTitleQuery:@"King Of Oceania"];
376  [query1 setIsTitleQueryExact:YES];
377  [query1 setParentFolderName:@"Major Folder"];
378  [query1 setShouldShowFolders:YES];
379
380  NSURL *resultURL1 = [query1 URL];
381  NSString *expected1 = @"https://docs.google.com/feeds/default/private/full?"
382    "folder=Major+Folder&showfolders=true&title=King+Of+Oceania&title-exact=true";
383  STAssertEqualObjects([resultURL1 absoluteString], expected1,
384                       @"Docs query 1 generation error");
385
386  GDataQueryDocs *query2 = [GDataQueryDocs documentQueryWithFeedURL:feedURL];
387  [query2 setOwner:@"fred@flintstone.com"];
388  [query2 setReader:@"wilma@flintstone.com,pebbles@flintstone.com"];
389  [query2 setWriter:@"barney@rubble.com,betty@rubble.com"];
390  NSURL *resultURL2 = [query2 URL];
391  NSString *expected2 = @"https://docs.google.com/feeds/default/private/full?"
392    "owner=fred%40flintstone.com&reader=wilma%40flintstone.com%2C"
393    "pebbles%40flintstone.com&writer=barney%40rubble.com%2Cbetty%40rubble.com";
394  STAssertEqualObjects([resultURL2 absoluteString], expected2,
395                       @"Docs query 2 generation error");
396
397  GDataDateTime* minDate = [GDataDateTime dateTimeWithRFC3339String:@"2006-03-29T07:35:59.000Z"];
398  GDataDateTime* maxDate = [GDataDateTime dateTimeWithRFC3339String:@"2006-03-30T07:35:59.000Z"];
399  GDataDateTime* minDate2 = [GDataDateTime dateTimeWithRFC3339String:@"2006-04-29T07:35:59.000Z"];
400  GDataDateTime* maxDate2 = [GDataDateTime dateTimeWithRFC3339String:@"2006-04-30T07:35:59.000Z"];
401
402  GDataQueryDocs *query3 = [GDataQueryDocs documentQueryWithFeedURL:feedURL];
403  [query3 setOpenedMinDateTime:minDate];
404  [query3 setOpenedMaxDateTime:maxDate];
405  [query3 setEditedMinDateTime:minDate2];
406  [query3 setEditedMaxDateTime:maxDate2];
407  NSURL *resultURL3 = [query3 URL];
408  NSString *expected3 = @"https://docs.google.com/feeds/default/private/full?"
409    "edited-max=2006-04-30T07%3A35%3A59Z&edited-min=2006-04-29T07%3A35%3A59Z&"
410    "opened-max=2006-03-30T07%3A35%3A59Z&opened-min=2006-03-29T07%3A35%3A59Z";
411  STAssertEqualObjects([resultURL3 absoluteString], expected3,
412                       @"Docs query 3 generation error");
413
414  GDataQueryDocs *query4 = [GDataQueryDocs documentQueryWithFeedURL:feedURL];
415  [query4 setShouldActuallyDelete:YES];
416  [query4 setShouldShowRootParentLink:YES];
417  [query4 setShouldConvertUpload:NO];
418  [query4 setSourceLanguage:@"en"];
419  [query4 setTargetLanguage:@"de"];
420  [query4 setShouldOCRUpload:YES];
421  [query4 setShouldCreateNewRevision:YES];
422  NSURL *resultURL4 = [query4 URL];
423  NSString *expected4 = @"https://docs.google.com/feeds/default/private/full?"
424    "convert=false&delete=true&new-revision=true&ocr=true&showroot=true&"
425    "sourceLanguage=en&targetLanguage=de";
426  STAssertEqualObjects([resultURL4 absoluteString], expected4,
427                       @"Docs query 4 generation error");
428}
429
430- (void)testAnalyticsQuery {
431  GDataQueryAnalytics *query1;
432
433  query1 = [GDataQueryAnalytics analyticsDataQueryWithTableID:@"9876"
434                                              startDateString:@"2001-01-01"
435                                                endDateString:@"2001-12-01"];
436
437  [query1 setDimensions:@"ga:browser,ga:country"];
438  [query1 setMetrics:@"ga:pageviews"];
439  [query1 setFilters:@"ga:country==United States,ga:country==Canada"];
440  [query1 setSort:@"ga:browser,ga:pageviews"];
441  [query1 setSegment:@"gaid::3"];
442
443  NSURL *resultURL1 = [query1 URL];
444  NSString *expected1 = @"https://www.google.com/analytics/feeds/data?"
445    "dimensions=ga%3Abrowser%2Cga%3Acountry&end-date=2001-12-01&"
446    "filters=ga%3Acountry%3D%3DUnited+States%2Cga%3Acountry%3D%3DCanada&"
447    "ids=9876&metrics=ga%3Apageviews&segment=gaid%3A%3A3&"
448    "sort=ga%3Abrowser%2Cga%3Apageviews&start-date=2001-01-01";
449  STAssertEqualObjects([resultURL1 absoluteString], expected1,
450                       @"Analytics query 1 generation error");
451}
452
453- (void)testMixedCategoryParamQueries {
454
455  GDataCategory *cat = [GDataCategory categoryWithScheme:nil
456                                                    term:@"ferret"];
457  GDataCategory *excludeCat = [GDataCategory categoryWithScheme:nil
458                                                           term:@"iguana"];
459
460  GDataCategory *catB = [GDataCategory categoryWithScheme:nil
461                                                     term:@"monkey"];
462
463  GDataCategoryFilter *categoryFilterA = [GDataCategoryFilter categoryFilter];
464  [categoryFilterA addCategory:cat];
465  [categoryFilterA addExcludeCategory:excludeCat];
466
467  // test a copy of the category filter
468  categoryFilterA = [[categoryFilterA copy] autorelease];
469
470  GDataCategoryFilter *categoryFilterB = [GDataCategoryFilter categoryFilter];
471  [categoryFilterB addCategory:catB];
472
473  // four flavors of URL: with and without trailing /, with and without params
474  NSURL *url1 = [NSURL URLWithString:@"http://domain.net/?x=y"];
475  NSURL *url2 = [NSURL URLWithString:@"http://domain.net?x=y"];
476  NSURL *url3 = [NSURL URLWithString:@"http://domain.net/"];
477  NSURL *url4 = [NSURL URLWithString:@"http://domain.net"];
478
479  // URL 1
480  //
481  // changes to the URL made by the query
482  //
483  // category filters
484  GDataQuery *query1 = [GDataQuery queryWithFeedURL:url1];
485  [query1 addCategoryFilter:categoryFilterA];
486  [query1 addCategoryFilter:categoryFilterB];
487
488  // new parameter
489  [query1 setStartIndex:10];
490  
491  // test a copy of the query
492  query1 = [[query1 copy] autorelease];
493
494  NSURL *resultURL1 = [query1 URL];
495  NSString *expectedStr1 = @"http://domain.net/-/ferret%7C-iguana/monkey?x=y&start-index=10";
496  STAssertEqualObjects([resultURL1 absoluteString], expectedStr1,
497                       @"Mixed query generation error");
498
499  // URL 2
500  GDataQuery *query2 = [GDataQuery queryWithFeedURL:url2];
501  [query2 addCategoryFilter:categoryFilterA];
502  [query2 addCategoryFilterWithCategory:catB];
503  [query2 setStartIndex:10];
504
505  NSURL *resultURL2 = [query2 URL];
506  NSString *expectedStr2 = @"http://domain.net/-/ferret%7C-iguana/monkey?x=y&start-index=10";
507  STAssertEqualObjects([resultURL2 absoluteString], expectedStr2,
508                       @"Mixed query generation error");
509
510  // URL 3
511  GDataQuery *query3 = [GDataQuery queryWithFeedURL:url3];
512  [query3 addCategoryFilter:categoryFilterA];
513  [query3 addCategoryFilter:categoryFilterB];
514  [query3 setStartIndex:10];
515
516  NSURL *resultURL3 = [query3 URL];
517  NSString *expectedStr3 = @"http://domain.net/-/ferret%7C-iguana/monkey?start-index=10";
518  STAssertEqualObjects([resultURL3 absoluteString], expectedStr3,
519                       @"Mixed query generation error");
520
521  // URL 4
522  GDataQuery *query4 = [GDataQuery queryWithFeedURL:url4];
523  [query4 addCategoryFilter:categoryFilterA];
524  [query4 addCategoryFilter:categoryFilterB];
525  [query4 setStartIndex:10];
526
527  NSURL *resultURL4 = [query4 URL];
528  NSString *expectedStr4 = @"http://domain.net/-/ferret%7C-iguana/monkey?start-index=10";
529  STAssertEqualObjects([resultURL4 absoluteString], expectedStr4,
530                       @"Mixed query generation error");
531
532  // URL 1 again
533
534  // repeat the first test, but with no category filters added to the query
535  GDataQuery *query5 = [GDataQuery queryWithFeedURL:url1];
536  [query5 setStartIndex:10];
537
538  NSURL *resultURL5 = [query5 URL];
539  NSString *expectedStr5 = @"http://domain.net/?x=y&start-index=10";
540  STAssertEqualObjects([resultURL5 absoluteString], expectedStr5,
541                       @"Mixed query generation error");
542
543  // repeat the first test, but with no params added to the query
544  GDataQuery *query6 = [GDataQuery queryWithFeedURL:url1];
545  [query6 addCategoryFilter:categoryFilterA];
546  [query6 addCategoryFilter:categoryFilterB];
547
548  NSURL *resultURL6 = [query6 URL];
549  NSString *expectedStr6 = @"http://domain.net/-/ferret%7C-iguana/monkey?x=y";
550  STAssertEqualObjects([resultURL6 absoluteString], expectedStr6,
551                       @"Mixed query generation error");
552}
553
554- (void)testURLParameterEncoding {
555  
556  // test all characters between 0x20 and 0x7f
557  NSString *fullAsciiParam = @" !\"#$%&'()*+,-./"
558    "0123456789:;<=>?@"
559    "ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`"
560    "abcdefghijklmnopqrstuvwxyz{|}~";
561  
562  // full URL encoding leaves +, =, and other URL-legal symbols intact
563  NSString *fullEncoded = @"%20!%22%23$%25&'()*+,-./"
564    "0123456789:;%3C=%3E?@"
565    "ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60"
566    "abcdefghijklmnopqrstuvwxyz%7B%7C%7D~%7F";
567  
568  // parameter encoding encodes these too: "!*'();:@&=+$,/?%#[]"
569  // and encodes a space as a plus
570  NSString *paramEncoded = @"+%21%22%23%24%25%26%27%28%29%2A%2B%2C-.%2F"
571    "0123456789%3A%3B%3C%3D%3E%3F%40"
572    "ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60"
573    "abcdefghijklmnopqrstuvwxyz%7B%7C%7D~%7F";
574
575  NSString *resultFull, *resultParam;
576  
577  resultFull = [GDataUtilities stringByURLEncodingString:fullAsciiParam];
578  STAssertEqualObjects(resultFull, fullEncoded, @"URL full encoding error");
579  
580  resultParam = [GDataUtilities stringByURLEncodingStringParameter:fullAsciiParam];
581  STAssertEqualObjects(resultParam, paramEncoded, @"URL param encoding error");
582}
583
584@end