Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #220 from kylef/bug/managed-object-adapter/silent-…

…error

MTLManagedObjectAdapter: Fix a bug where errors are silently handled
  • Loading branch information...
commit 61b3f667b7997763a93bb5fe3c32d7a20d7f3700 2 parents eb2178a + 027e8c1
@robb robb authored
View
6 Mantle/MTLManagedObjectAdapter.m
@@ -392,7 +392,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 +502,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;
});
View
5 MantleTests/MTLCoreDataTestModels.h
@@ -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>
View
12 MantleTests/MTLCoreDataTestModels.m
@@ -42,6 +42,18 @@ + (NSDictionary *)relationshipModelClassesByPropertyKey {
@end
+@implementation MTLParentIncorrectTestModel
+
++ (NSString *)managedObjectEntityName {
+ return @"Parent";
+}
+
++ (NSDictionary *)managedObjectKeysByPropertyKey {
+ return @{};
+}
+
+@end
+
@implementation MTLChildTestModel
+ (NSString *)managedObjectEntityName {
View
20 MantleTests/MTLManagedObjectAdapterSpec.m
@@ -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];
Please sign in to comment.
Something went wrong with that request. Please try again.