Skip to content

Commit 9005bd5

Browse files
committed
Add test coverage and fixes for RKMappingTest. closes RestKit#1086
1 parent bf63a77 commit 9005bd5

File tree

15 files changed

+295
-11
lines changed

15 files changed

+295
-11
lines changed

Code/ObjectMapping/RKMappingOperation.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,6 @@ - (BOOL)applyRelationshipMappings
603603
{
604604
NSAssert(self.dataSource, @"Cannot perform relationship mapping without a data source");
605605
NSMutableArray *mappingsApplied = [NSMutableArray array];
606-
id destinationObject = nil;
607606

608607
for (RKRelationshipMapping *relationshipMapping in [self relationshipMappings]) {
609608
if ([self isCancelled]) return NO;
@@ -702,7 +701,8 @@ - (BOOL)applyRelationshipMappings
702701

703702
// Notify the delegate
704703
if ([self.delegate respondsToSelector:@selector(mappingOperation:didSetValue:forKeyPath:usingMapping:)]) {
705-
[self.delegate mappingOperation:self didSetValue:destinationObject forKeyPath:relationshipMapping.destinationKeyPath usingMapping:relationshipMapping];
704+
id setValue = [self.destinationObject valueForKeyPath:relationshipMapping.destinationKeyPath];
705+
[self.delegate mappingOperation:self didSetValue:setValue forKeyPath:relationshipMapping.destinationKeyPath usingMapping:relationshipMapping];
706706
}
707707

708708
// Fail out if a validation error has occurred

Code/ObjectMapping/RKObjectMapping.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@
376376
/**
377377
Returns the preferred date formatter to use when generating NSString representations from NSDate attributes. This type of transformation occurs when RestKit is mapping local objects into JSON or form encoded serializations that do not have a native time construct.
378378
379-
Defaults to an instance of the `RKISO8601DateFormatter` configured with the UTC time-zone. The format string is equal to "YYYY-MM-DDThh:mm:ssTZD"
379+
Defaults to an instance of the `RKISO8601DateFormatter` configured with the UTC time-zone. The format string is equal to "yyyy-MM-DDThh:mm:ssTZD"
380380
381381
For details about the ISO-8601 format, see http://www.w3.org/TR/NOTE-datetime
382382

Code/Support/RestKit-Prefix.pch

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@
1313
#import <SystemConfiguration/SystemConfiguration.h>
1414
#import <CoreServices/CoreServices.h>
1515
#endif
16+
#import <CoreData/CoreData.h>
1617
#endif

Code/Testing.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@
1111
#import "RKTestFactory.h"
1212
#import "RKTestHelpers.h"
1313
#import "RKMappingTest.h"
14+
15+
#ifdef _COREDATADEFINES_H
16+
#import "RKConnectionTestExpectation.h"
17+
#endif

Code/Testing/RKConnectionTestExpectation.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
// limitations under the License.
1919
//
2020

21+
#ifdef _COREDATADEFINES_H
22+
2123
#import <Foundation/Foundation.h>
2224

2325
/**
@@ -81,3 +83,5 @@
8183
- (NSString *)summary;
8284

8385
@end
86+
87+
#endif

Code/Testing/RKConnectionTestExpectation.m

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
// limitations under the License.
1919
//
2020

21-
#import <CoreData/CoreData.h>
21+
#ifdef _COREDATADEFINES_H
22+
2223
#import "RKConnectionTestExpectation.h"
2324
#import "RKObjectUtilities.h"
2425

@@ -64,3 +65,5 @@ - (NSString *)description
6465
}
6566

6667
@end
68+
69+
#endif

Code/Testing/RKMappingTest.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
//
2020

2121
#import <Foundation/Foundation.h>
22-
#import <CoreData/CoreData.h>
2322
#import "RKMappingOperation.h"
2423
#import "RKPropertyMappingTestExpectation.h"
2524

@@ -195,6 +194,8 @@ extern NSString * const RKMappingTestExpectationErrorKey;
195194
*/
196195
@property (nonatomic, strong, readonly) id destinationObject;
197196

197+
#ifdef _COREDATADEFINES_H
198+
198199
///----------------------------
199200
/// @name Core Data Integration
200201
///----------------------------
@@ -213,4 +214,6 @@ extern NSString * const RKMappingTestExpectationErrorKey;
213214
*/
214215
@property (nonatomic, strong) id<RKManagedObjectCaching> managedObjectCache;
215216

217+
#endif // _COREDATADEFINES_H
218+
216219
@end

Code/Testing/RKMappingTest.m

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ - (BOOL)event:(RKMappingTestEvent *)event satisfiesExpectation:(id)expectation e
242242
NSString *reason = [NSString stringWithFormat:@"expected to %@, but instead got %@ '%@'",
243243
expectation, [event.value class], event.value];
244244
if (error) *error = [self errorForExpectation:expectation
245-
withCode:RKMappingTestEvaluationBlockError
245+
withCode:RKMappingTestValueInequalityError
246246
userInfo:userInfo
247247
description:description
248248
reason:reason];
@@ -258,7 +258,7 @@ - (BOOL)event:(RKMappingTestEvent *)event satisfiesExpectation:(id)expectation e
258258
NSString *reason = [NSString stringWithFormat:@"expected to %@, but was instead mapped using: %@",
259259
expectation, relationshipMapping];
260260
if (error) *error = [self errorForExpectation:expectation
261-
withCode:RKMappingTestValueInequalityError
261+
withCode:RKMappingTestMappingMismatchError
262262
userInfo:userInfo
263263
description:description
264264
reason:reason];
@@ -319,6 +319,7 @@ - (BOOL)event:(RKMappingTestEvent *)event satisfiesExpectation:(id)expectation e
319319
// If we have been given an explicit data source, use it
320320
if (self.mappingOperationDataSource) return self.mappingOperationDataSource;
321321

322+
#ifdef _COREDATADEFINES_H
322323
if ([self.mapping isKindOfClass:[RKEntityMapping class]]) {
323324
NSAssert(self.managedObjectContext, @"Cannot test an `RKEntityMapping` with a nil managed object context.");
324325
id<RKManagedObjectCaching> managedObjectCache = self.managedObjectCache ?: [RKFetchRequestManagedObjectCache new];
@@ -332,6 +333,9 @@ - (BOOL)event:(RKMappingTestEvent *)event satisfiesExpectation:(id)expectation e
332333
} else {
333334
return [RKObjectMappingOperationDataSource new];
334335
}
336+
#else
337+
return [RKObjectMappingOperationDataSource new];
338+
#endif
335339
}
336340

337341
- (void)performMapping
@@ -350,7 +354,8 @@ - (void)performMapping
350354
}
351355

352356
// Let the connection operations execute to completion
353-
if ([mappingOperation.dataSource isKindOfClass:[RKManagedObjectMappingOperationDataSource class]]) {
357+
Class managedObjectMappingOperationDataSourceClass = NSClassFromString(@"RKManagedObjectMappingOperationDataSource");
358+
if ([mappingOperation.dataSource isKindOfClass:managedObjectMappingOperationDataSourceClass]) {
354359
NSOperationQueue *operationQueue = [(RKManagedObjectMappingOperationDataSource *)mappingOperation.dataSource operationQueue];
355360
if (! [operationQueue isEqual:[NSOperationQueue mainQueue]]) {
356361
[operationQueue waitUntilAllOperationsAreFinished];
@@ -410,7 +415,8 @@ - (BOOL)evaluate
410415
- (BOOL)evaluateExpectation:(id)expectation error:(NSError **)error
411416
{
412417
NSParameterAssert(expectation);
413-
NSAssert([expectation isKindOfClass:[RKPropertyMappingTestExpectation class]] || [expectation isKindOfClass:[RKConnectionTestExpectation class]], @"Must be an instance of `RKPropertyMappingTestExpectation` or `RKConnectionTestExpectation`");
418+
Class connectionTestExpectation = NSClassFromString(@"RKConnectionTestExpectation");
419+
NSAssert([expectation isKindOfClass:[RKPropertyMappingTestExpectation class]] || (connectionTestExpectation && [expectation isKindOfClass:connectionTestExpectation]), @"Must be an instance of `RKPropertyMappingTestExpectation` or `RKConnectionTestExpectation`");
414420
[self performMapping];
415421

416422
RKMappingTestEvent *event = [self eventMatchingExpectation:expectation];

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,8 @@ operation.managedObjectCache = managedObjectStore.managedObjectCache;
223223
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
224224
NSLog(@"Failed with error: %@", [error localizedDescription]);
225225
}];
226+
NSOperationQueue *operationQueue = [NSOperationQueue new];
227+
[operationQueue addOperation:operation];
226228
```
227229
228230
### Map a Client Error Response to an NSError

RestKit.xcodeproj/project.pbxproj

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,8 @@
497497
25B408271491CDDC00F21111 /* RKPathUtilities.h in Headers */ = {isa = PBXBuildFile; fileRef = 25B408241491CDDB00F21111 /* RKPathUtilities.h */; settings = {ATTRIBUTES = (Public, ); }; };
498498
25B408281491CDDC00F21111 /* RKPathUtilities.m in Sources */ = {isa = PBXBuildFile; fileRef = 25B408251491CDDB00F21111 /* RKPathUtilities.m */; };
499499
25B408291491CDDC00F21111 /* RKPathUtilities.m in Sources */ = {isa = PBXBuildFile; fileRef = 25B408251491CDDB00F21111 /* RKPathUtilities.m */; };
500+
25B639CC16961EFA0065EB7B /* RKMappingTestTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 25B639CB16961EFA0065EB7B /* RKMappingTestTest.m */; };
501+
25B639CD16961EFA0065EB7B /* RKMappingTestTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 25B639CB16961EFA0065EB7B /* RKMappingTestTest.m */; };
500502
25B6E95514CF795D00B1E881 /* RKErrors.h in Headers */ = {isa = PBXBuildFile; fileRef = 25B6E95414CF795D00B1E881 /* RKErrors.h */; settings = {ATTRIBUTES = (Public, ); }; };
501503
25B6E95614CF795D00B1E881 /* RKErrors.h in Headers */ = {isa = PBXBuildFile; fileRef = 25B6E95414CF795D00B1E881 /* RKErrors.h */; settings = {ATTRIBUTES = (Public, ); }; };
502504
25B6E95814CF7A1C00B1E881 /* RKErrors.m in Sources */ = {isa = PBXBuildFile; fileRef = 25B6E95714CF7A1C00B1E881 /* RKErrors.m */; };
@@ -893,6 +895,7 @@
893895
25AFF8F015B4CF1F0051877F /* RKMappingErrors.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; lineEnding = 0; path = RKMappingErrors.h; sourceTree = "<group>"; };
894896
25B408241491CDDB00F21111 /* RKPathUtilities.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RKPathUtilities.h; sourceTree = "<group>"; };
895897
25B408251491CDDB00F21111 /* RKPathUtilities.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RKPathUtilities.m; sourceTree = "<group>"; };
898+
25B639CB16961EFA0065EB7B /* RKMappingTestTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RKMappingTestTest.m; sourceTree = "<group>"; };
896899
25B6E95414CF795D00B1E881 /* RKErrors.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RKErrors.h; sourceTree = "<group>"; };
897900
25B6E95714CF7A1C00B1E881 /* RKErrors.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RKErrors.m; sourceTree = "<group>"; };
898901
25B6E95A14CF7E3C00B1E881 /* RKObjectMappingMatcher.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RKObjectMappingMatcher.h; sourceTree = "<group>"; };
@@ -1292,6 +1295,7 @@
12921295
2516101B1456F2330060A5C5 /* ObjectMapping */,
12931296
251610511456F2330060A5C5 /* Support */,
12941297
25104F9B15C33E3A00829135 /* Search */,
1298+
25B639C816961EC70065EB7B /* Testing */,
12951299
251610471456F2330060A5C5 /* Server */,
12961300
);
12971301
path = Tests;
@@ -1561,6 +1565,15 @@
15611565
name = Testing;
15621566
sourceTree = "<group>";
15631567
};
1568+
25B639C816961EC70065EB7B /* Testing */ = {
1569+
isa = PBXGroup;
1570+
children = (
1571+
25B639CB16961EFA0065EB7B /* RKMappingTestTest.m */,
1572+
);
1573+
name = Testing;
1574+
path = Logic/Testing;
1575+
sourceTree = "<group>";
1576+
};
15641577
25BCB31715ED57D500EE84DD /* AFNetworking */ = {
15651578
isa = PBXGroup;
15661579
children = (
@@ -2358,6 +2371,7 @@
23582371
2536D1FD167270F100DF9BB0 /* RKRouterTest.m in Sources */,
23592372
2551338F167838590017E4B6 /* RKHTTPRequestOperationTest.m in Sources */,
23602373
255133CF167AC7600017E4B6 /* RKManagedObjectRequestOperationTest.m in Sources */,
2374+
25B639CC16961EFA0065EB7B /* RKMappingTestTest.m in Sources */,
23612375
);
23622376
runOnlyForDeploymentPostprocessing = 0;
23632377
};
@@ -2508,6 +2522,7 @@
25082522
2543A25E1664FD3200821D5B /* RKResponseDescriptorTest.m in Sources */,
25092523
2536D1FE167270F100DF9BB0 /* RKRouterTest.m in Sources */,
25102524
25513390167838590017E4B6 /* RKHTTPRequestOperationTest.m in Sources */,
2525+
25B639CD16961EFA0065EB7B /* RKMappingTestTest.m in Sources */,
25112526
);
25122527
runOnlyForDeploymentPostprocessing = 0;
25132528
};
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,12 @@
1-
{"human":{"name":"Blake Watters","id":null,"age":28,"favorite_cat":{"name": "Asia"}}}
1+
{
2+
"human": {
3+
"name": "Blake Watters",
4+
"id": null,
5+
"age": 28,
6+
"favorite_cat_id": 1234,
7+
"favorite_cat": {
8+
"name": "Asia",
9+
"id": 1234
10+
}
11+
}
12+
}

Tests/Fixtures/JSON/user.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,8 @@
2626
"id": 7,
2727
"name": "Rachit Shukla"
2828
}
29-
]
29+
],
30+
"created_at": "2011-02-02 07:53:08",
31+
"latitude": 12345,
32+
"longitude": 56789
3033
}

0 commit comments

Comments
 (0)