ActiveResourceKit  v1.2 (498.0)
 All Classes Files Functions Variables Typedefs Enumerator Properties Macros Pages
BaseTests.m
Go to the documentation of this file.
1 // ActiveResourceKitTests BaseTests.m
2 //
3 // Copyright © 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 "BaseTests.h"
26 #import "Person.h"
27 
29 
30 @implementation BaseTests
31 
32 - (void)testCreate
33 {
34  [[Person service] createWithAttributes:[NSDictionary dictionaryWithObject:@"Rick" forKey:@"name"] completionHandler:^(ARHTTPResponse *response, ARResource *resource, NSError *error) {
35  STAssertNotNil(resource, nil);
36  [self setStop:YES];
37  }];
38  [self runUntilStop];
39 }
40 
41 - (void)testDestroy
42 {
43  ARService *peopleService = [Person service];
44  [peopleService createWithAttributes:[NSDictionary dictionaryWithObject:@"Roy" forKey:@"name"] completionHandler:^(ARHTTPResponse *response, ARResource *resource, NSError *error) {
45  STAssertNotNil(resource, nil);
46  [resource destroyWithCompletionHandler:^(ARHTTPResponse *response, NSError *error) {
47  STAssertEquals([response code], (NSInteger)200, nil);
48  [self setStop:YES];
49  }];
50  }];
51  [self runUntilStop];
52 }
53 
54 - (void)testExists
55 {
56  ARService *peopleService = [Person service];
57 
58  // Do not execute a run loop with nil IDs. The answer (exists equal to NO)
59  // comes back immediately. The completion block runs before the
60  // exists-with-ID method returns.
61  [peopleService existsWithID:nil options:nil completionHandler:^(ARHTTPResponse *response, BOOL exists, NSError *error) {
62  STAssertFalse(exists, nil);
63  }];
64 
65  [peopleService existsWithID:[NSNumber numberWithInt:1] options:nil completionHandler:^(ARHTTPResponse *response, BOOL exists, NSError *error) {
66  STAssertTrue(exists, nil);
67  [self setStop:YES];
68  }];
69  [self runUntilStop];
70 
71  [peopleService existsWithID:[NSNumber numberWithInt:99] options:nil completionHandler:^(ARHTTPResponse *response, BOOL exists, NSError *error) {
72  STAssertFalse(exists, nil);
73  STAssertEquals([response code], (NSInteger)404, nil);
74  STAssertEquals([error code], (NSInteger)ARResourceNotFoundErrorCode, nil);
75  [self setStop:YES];
76  }];
77  [self runUntilStop];
78 
79  Person *person = [[Person alloc] init];
80  [person existsWithCompletionHandler:^(ARHTTPResponse *response, BOOL exists, NSError *error) {
81  STAssertFalse(exists, nil);
82  }];
83 }
84 
85 - (void)testToJSON
86 {
87  [[Person service] findSingleWithID:[NSNumber numberWithInt:6]
88  options:nil
89  completionHandler:^(ARHTTPResponse *response, ARResource *joe, NSError *error) {
90  NSString *string = [[NSString alloc] initWithData:[response body] encoding:NSUTF8StringEncoding];
91  for (NSString *pattern in [NSArray arrayWithObjects:@"\"id\":6", @"\"name\":\"Joe\"", nil])
92  {
93  NSRegularExpression *re = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:NULL];
94  NSTextCheckingResult *firstMatch = [re firstMatchInString:string options:0 range:NSMakeRange(0, [string length])];
95  STAssertNotNil(firstMatch, @"%@ should find match in %@", pattern, string);
96  }
97  [self setStop:YES];
98  }];
99  [self runUntilStop];
100 }
101 
102 @end