ActiveResourceKit  v1.2 (498.0)
 All Classes Files Functions Variables Typedefs Enumerator Properties Macros Pages
FinderTests.m
Go to the documentation of this file.
1 // ActiveResourceKitTests FinderTests.m
2 //
3 // Copyright © 2011, 2012, Roy Ratcliffe, Pioneering Software, United Kingdom
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the “Software”), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED “AS IS,” WITHOUT WARRANTY OF ANY KIND, EITHER
16 // EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO
18 // EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
19 // OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 // DEALINGS IN THE SOFTWARE.
22 //
23 //------------------------------------------------------------------------------
24 
25 #import "FinderTests.h"
26 
27 #import "Person.h"
28 
30 
31 @implementation FinderTests
32 
33 - (void)testFindByID
34 {
35  [[Person service] findSingleWithID:[NSNumber numberWithInt:1]
36  options:nil
37  completionHandler:^(ARHTTPResponse *response,
38  ARResource *matz,
39  NSError *error) {
40  STAssertNotNil(matz, nil);
41  STAssertNil(error, nil);
42 
43  STAssertTrue([matz isKindOfClass:[Person class]], nil);
44  STAssertEqualObjects([matz valueForKey:@"name"], @"Matz", nil);
45  STAssertNotNil([[matz attributes] objectForKey:@"name"], nil);
46 
47  [self setStop:YES];
48  }];
49  [self runUntilStop];
50 }
51 
53 {
54  // The following tests sends exactly the same GET request as above, only it
55  // also carries a query string as follows.
56  //
57  // /people/1.json?auth_token=XYZ
58  //
59  [[Person service] findSingleWithID:[NSNumber numberWithInt:1]
60  options:[NSDictionary dictionaryWithObject:@"XYZ" forKey:@"auth_token"]
61  completionHandler:^(ARHTTPResponse *response, ARResource *matz, NSError *error) {
62  STAssertNotNil(matz, nil);
63  STAssertNil(error, nil);
64 
65  STAssertTrue([matz isKindOfClass:[Person class]], nil);
66  STAssertEqualObjects([matz valueForKey:@"name"], @"Matz", nil);
67  STAssertNotNil([[matz attributes] objectForKey:@"name"], nil);
68 
69  [self setStop:YES];
70  }];
71  [self runUntilStop];
72 }
73 
75 {
76  NSUInteger __block pending = 0;
77 
78  ARService *postService = [[ARService alloc] initWithSite:ActiveResourceKitTestsBaseURL() elementName:@"post"];
79  ARService *commentService = [postService serviceForSubelementNamed:@"comment"];
80  // Derive a site URL for the nested resource.// Stop the run loop when pending equals zero, but not// before. Completion handlers execute when the request// responds. Finding a post increments pending. Finding all a// post's comments decrements pending. Pending therefore finally// becomes zero after finding all posts and finding each one's// comments.[postService findAllWithOptions:nil completionHandler:^(ARHTTPResponse *response, NSArray *posts, NSError *error) {
81  STAssertNotNil(posts, nil);
82  for (ARResource *post in posts)
83  {
84 
85  [commentService findAllWithOptions:[post optionsForSubelement] completionHandler:^(ARHTTPResponse *response, NSArray *comments, NSError *error) {
86  STAssertNotNil(comments, nil);
87  NSLog(@"%@", [post valueForKey:@"title"]);
88  for (ARResource *comment in comments)
89  {
90  NSLog(@"%@", [comment valueForKey:@"text"]);
91  }
92 
93 
94 
95 
96 
97 
98 
99  if (--pending == 0) [self setStop:YES];
100  }];
101  ++pending;
102  }
103  if (pending == 0) [self setStop:YES];
104  }];
105  [self runUntilStop];
106 }
107 
108 @end