Permalink
Browse files

Merge remote-tracking branch 'upstream/master'

  • Loading branch information...
2 parents 22712f9 + bc2a670 commit 1a6be4ee76ba22bc5634fa0a039bddb5fb653328 David Cordero committed Feb 4, 2014
View
@@ -1,4 +1,4 @@
-**Copyright (c) 2012 - 2013, GitHub, Inc.**
+**Copyright (c) 2012 - 2014, GitHub, Inc.**
**All rights reserved.**
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
@@ -15,7 +15,7 @@
const NSInteger MTLJSONAdapterErrorInvalidJSONDictionary = 3;
// An exception was thrown and caught.
-static const NSInteger MTLJSONAdapterErrorExceptionThrown = 1;
+const NSInteger MTLJSONAdapterErrorExceptionThrown = 1;
// Associated with the NSException that was caught.
static NSString * const MTLJSONAdapterThrownExceptionErrorKey = @"MTLJSONAdapterThrownException";
@@ -34,9 +34,6 @@ static id performInContext(NSManagedObjectContext *context, id (^block)(void)) {
return result;
}
-// An exception was thrown and caught.
-static const NSInteger MTLManagedObjectAdapterErrorExceptionThrown = 1;
-
@interface MTLManagedObjectAdapter ()
// The MTLModel subclass being serialized or deserialized.
@@ -308,7 +305,7 @@ - (id)managedObjectFromModel:(MTLModel<MTLManagedObjectSerializing> *)model inse
Class fetchRequestClass = NSClassFromString(@"NSFetchRequest");
NSAssert(fetchRequestClass != nil, @"CoreData.framework must be linked to use MTLManagedObjectAdapter");
- // If a uniquing predicate is provided, perform a fetch request to guarentee a unique managed object.
+ // If a uniquing predicate is provided, perform a fetch request to guarantee a unique managed object.
__block NSManagedObject *managedObject = nil;
NSPredicate *uniquingPredicate = [self uniquingPredicateForModel:model];
@@ -392,7 +389,7 @@ - (id)managedObjectFromModel:(MTLModel<MTLManagedObjectSerializing> *)model inse
NSValueTransformer *attributeTransformer = [self entityAttributeTransformerForKey:propertyKey];
if (attributeTransformer != nil) transformedValue = [attributeTransformer transformedValue:transformedValue];
- if (![managedObject validateValue:&transformedValue forKey:managedObjectKey error:error]) return NO;
+ if (![managedObject validateValue:&transformedValue forKey:managedObjectKey error:&tmpError]) return NO;
[managedObject setValue:transformedValue forKey:managedObjectKey];
return YES;
@@ -502,8 +499,8 @@ - (id)managedObjectFromModel:(MTLModel<MTLManagedObjectSerializing> *)model inse
}
}];
- if (managedObject != nil && ![managedObject validateForInsert:error]) {
- performInContext(context, ^ id {
+ if (managedObject != nil && ![managedObject validateForInsert:&tmpError]) {
+ managedObject = performInContext(context, ^ id {
[context deleteObject:managedObject];
return nil;
});
@@ -121,7 +121,7 @@ + (NSValueTransformer *)mtl_valueMappingTransformerWithDictionary:(NSDictionary
NSParameterAssert(dictionary.count == [[NSSet setWithArray:dictionary.allValues] count]);
return [MTLValueTransformer reversibleTransformerWithForwardBlock:^(id<NSCopying> key) {
- return dictionary[key];
+ return dictionary[key ?: NSNull.null];
} reverseBlock:^(id object) {
__block id result = nil;
[dictionary enumerateKeysAndObjectsUsingBlock:^(id key, id anObject, BOOL *stop) {
@@ -23,6 +23,11 @@
@end
+// Model for Parent entity which doesn't serialize required properties
+@interface MTLParentIncorrectTestModel : MTLModel <MTLManagedObjectSerializing>
+
+@end
+
// Corresponds to the `Child` entity.
@interface MTLChildTestModel : MTLModel <MTLManagedObjectSerializing>
@@ -42,6 +42,18 @@ + (NSDictionary *)relationshipModelClassesByPropertyKey {
@end
+@implementation MTLParentIncorrectTestModel
+
++ (NSString *)managedObjectEntityName {
+ return @"Parent";
+}
+
++ (NSDictionary *)managedObjectKeysByPropertyKey {
+ return @{};
+}
+
+@end
+
@implementation MTLChildTestModel
+ (NSString *)managedObjectEntityName {
@@ -216,6 +216,26 @@
expect(error).notTo.beNil();
});
+ it(@"should return an error if model doesn't validate for attribute description", ^{
+ MTLParentTestModel *parentModel = [MTLParentTestModel modelWithDictionary:@{} error:NULL];
+
+ NSError *error;
+ NSManagedObject *managedObject = [MTLManagedObjectAdapter managedObjectFromModel:parentModel insertingIntoContext:context error:&error];
+
+ expect(managedObject).to.beNil();
+ expect(error).notTo.beNil();
+ });
+
+ it(@"should return an error if model doesn't validate for insert", ^{
+ MTLParentIncorrectTestModel *parentModel = [MTLParentIncorrectTestModel modelWithDictionary:@{} error:NULL];
+
+ NSError *error;
+ NSManagedObject *managedObject = [MTLManagedObjectAdapter managedObjectFromModel:parentModel insertingIntoContext:context error:&error];
+
+ expect(managedObject).to.beNil();
+ expect(error).notTo.beNil();
+ });
+
it(@"should respect the uniqueness constraint", ^{
NSError *errorOne;
MTLParent *parentOne = [MTLManagedObjectAdapter managedObjectFromModel:parentModel insertingIntoContext:context error:&errorOne];
View
@@ -272,7 +272,7 @@ be invoked if overridden, giving you a convenient hook to upgrade old data.
In order to serialize your model objects from or into JSON, you need to
implement `<MTLJSONSerializing>` in your `MTLModel` subclass. This allows you to
-use `MTLJSONAdapter` convert your model objects from JSON and back:
+use `MTLJSONAdapter` to convert your model objects from JSON and back:
```objc
NSError *error = nil;

0 comments on commit 1a6be4e

Please sign in to comment.