/Source/externals/GData/Source/Tests/GDataQueryTest.m

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