Skip to content

Commit

Permalink
GH-96 Add to-one indirect recursive spec
Browse files Browse the repository at this point in the history
  • Loading branch information
Dima Zen committed May 20, 2017
1 parent e138899 commit 68c058f
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 11 deletions.
6 changes: 6 additions & 0 deletions FastEasyMapping.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@
AD5BB53D1ECF507600E914EB /* Object+Mapping.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD5BB5391ECF507600E914EB /* Object+Mapping.swift */; };
AD5BB53F1ECF5CB600E914EB /* RecursiveChatMessages.json in Resources */ = {isa = PBXBuildFile; fileRef = AD5BB53E1ECF5CB600E914EB /* RecursiveChatMessages.json */; };
AD5BB5401ECF5CB600E914EB /* RecursiveChatMessages.json in Resources */ = {isa = PBXBuildFile; fileRef = AD5BB53E1ECF5CB600E914EB /* RecursiveChatMessages.json */; };
AD5BB5421ECF605400E914EB /* RecursiveChatLastMessage.json in Resources */ = {isa = PBXBuildFile; fileRef = AD5BB5411ECF605400E914EB /* RecursiveChatLastMessage.json */; };
AD5BB5431ECF605400E914EB /* RecursiveChatLastMessage.json in Resources */ = {isa = PBXBuildFile; fileRef = AD5BB5411ECF605400E914EB /* RecursiveChatLastMessage.json */; };
AD90A4C01EBB4B93006751E9 /* FEMObjectRef.h in Headers */ = {isa = PBXBuildFile; fileRef = AD90A4BE1EBB4B93006751E9 /* FEMObjectRef.h */; };
AD90A4C11EBB4B93006751E9 /* FEMObjectRef.h in Headers */ = {isa = PBXBuildFile; fileRef = AD90A4BE1EBB4B93006751E9 /* FEMObjectRef.h */; };
AD90A4C21EBB4B93006751E9 /* FEMObjectRef.h in Headers */ = {isa = PBXBuildFile; fileRef = AD90A4BE1EBB4B93006751E9 /* FEMObjectRef.h */; };
Expand Down Expand Up @@ -473,6 +475,7 @@
AD5BB5381ECF507600E914EB /* ManagedObject+Mapping.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "ManagedObject+Mapping.swift"; sourceTree = "<group>"; };
AD5BB5391ECF507600E914EB /* Object+Mapping.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Object+Mapping.swift"; sourceTree = "<group>"; };
AD5BB53E1ECF5CB600E914EB /* RecursiveChatMessages.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = RecursiveChatMessages.json; sourceTree = "<group>"; };
AD5BB5411ECF605400E914EB /* RecursiveChatLastMessage.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = RecursiveChatLastMessage.json; sourceTree = "<group>"; };
AD90A4BE1EBB4B93006751E9 /* FEMObjectRef.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FEMObjectRef.h; sourceTree = "<group>"; };
AD90A4BF1EBB4B93006751E9 /* FEMObjectRef.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FEMObjectRef.m; sourceTree = "<group>"; };
AD90A4C81EBB4EB1006751E9 /* FEMRelationshipMappingSpec.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FEMRelationshipMappingSpec.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -871,6 +874,7 @@
AD94006B1E8AA93700558F37 /* ManagedPersonWithRecursiveRelationship.json */,
AD94006C1E8AA93700558F37 /* PersonWithRecursiveRelationship.json */,
1D6A30F341766BD719DB5C9F /* RecursiveCyclicRelationship.json */,
AD5BB5411ECF605400E914EB /* RecursiveChatLastMessage.json */,
AD5BB53E1ECF5CB600E914EB /* RecursiveChatMessages.json */,
1D6A39199C892D7ABCEB1EF7 /* ManagedObjectSupportedTypes.json */,
1D6A3D85D370CEE526FC4741 /* ManagedObjectSupportedTypesNull.json */,
Expand Down Expand Up @@ -1250,6 +1254,7 @@
files = (
2C375F3318CCD40200FCB8EA /* Native.json in Resources */,
AD94006E1E8AA93700558F37 /* PersonWithRecursiveRelationship.json in Resources */,
AD5BB5421ECF605400E914EB /* RecursiveChatLastMessage.json in Resources */,
2C375F3918CCD40200FCB8EA /* Male.json in Resources */,
2C375F3B18CCD40200FCB8EA /* Person.json in Resources */,
2C375F3518CCD40200FCB8EA /* Plane.json in Resources */,
Expand Down Expand Up @@ -1293,6 +1298,7 @@
files = (
ADCD43FE1E9640C7006C8106 /* Address.json in Resources */,
ADCD43FB1E9640C7006C8106 /* CarWithDate.json in Resources */,
AD5BB5431ECF605400E914EB /* RecursiveChatLastMessage.json in Resources */,
ADCD43FF1E9640C7006C8106 /* Person.json in Resources */,
ADCD43F51E9640C7006C8106 /* PersonWithDifferentNaming.json in Resources */,
ADCD43FD1E9640C7006C8106 /* Male.json in Resources */,
Expand Down
8 changes: 8 additions & 0 deletions FastEasyMappingTests/Fixtures/RecursiveChatLastMessage.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"primaryKey": 1,

"lastMessage": {
"primaryKey": 300,
"chat": { "primaryKey": 1 }
}
}
2 changes: 1 addition & 1 deletion FastEasyMappingTests/Fixtures/RecursiveChatMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"primaryKey": 1,

"messages": [{
"primaryKey": 345,
"primaryKey": 300,
"chat": { "primaryKey": 1 }
}]
}
3 changes: 2 additions & 1 deletion FastEasyMappingTests/Mapping Provider/Chat+Mapping.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
@interface Chat (Mapping)

+ (FEMMapping *)defaultMapping;
+ (FEMMapping *)recursiveMapping;
+ (FEMMapping *)chatLastMessageMapping;
+ (FEMMapping *)chatMessagesMapping;

@end
15 changes: 12 additions & 3 deletions FastEasyMappingTests/Mapping Provider/Chat+Mapping.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,23 @@ + (FEMMapping *)defaultMapping {
return mapping;
}

+ (FEMMapping *)recursiveMapping {
+ (FEMMapping *)chatLastMessageMapping {
FEMMapping *chat = [self defaultMapping];

FEMMapping *message = [ChatMessage defaultMapping];
[message addRelationshipMapping:[Chat defaultMapping] forProperty:@"chat" keyPath:@"chat"];
[message addRelationshipMapping:[self defaultMapping] forProperty:@"chat" keyPath:@"chat"];
[chat addRelationshipMapping:message forProperty:@"lastMessage" keyPath:@"lastMessage"];

[chat addToManyRelationshipMapping:message forProperty:@"messages" keyPath:@"messages"];
return chat;
}

+ (FEMMapping *)chatMessagesMapping {
FEMMapping *chat = [self defaultMapping];

FEMMapping *message = [ChatMessage defaultMapping];
[message addRelationshipMapping:[self defaultMapping] forProperty:@"chat" keyPath:@"chat"];
[chat addToManyRelationshipMapping:message forProperty:@"messages" keyPath:@"messages"];

return chat;
}

Expand Down
3 changes: 2 additions & 1 deletion FastEasyMappingTests/Models/CoreData.xcdatamodel/contents
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
</entity>
<entity name="Chat" representedClassName="Chat" syncable="YES" codeGenerationType="class">
<attribute name="primaryKey" optional="YES" attributeType="Integer 32" defaultValueString="0" usesScalarValueType="NO" syncable="YES"/>
<relationship name="lastMessage" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="ChatMessage" syncable="YES"/>
<relationship name="messages" optional="YES" toMany="YES" deletionRule="Cascade" destinationEntity="ChatMessage" syncable="YES"/>
</entity>
<entity name="ChatMessage" representedClassName="ChatMessage" syncable="YES" codeGenerationType="class">
Expand Down Expand Up @@ -63,7 +64,7 @@
</entity>
<elements>
<element name="Car" positionX="243" positionY="-990" width="128" height="120"/>
<element name="Chat" positionX="45" positionY="-828" width="128" height="75"/>
<element name="Chat" positionX="45" positionY="-828" width="128" height="90"/>
<element name="ChatMessage" positionX="54" positionY="-819" width="128" height="75"/>
<element name="ManagedObject" positionX="54" positionY="-900" width="128" height="315"/>
<element name="ManagedObjectChild" positionX="63" positionY="-810" width="128" height="75"/>
Expand Down
2 changes: 1 addition & 1 deletion FastEasyMappingTests/Specs/FEMAttributeMappingSpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#import "FEMAttribute.h"

SPEC_BEGIN(FEMAttributeMappingSpec)
SPEC_BEGIN(FEMAttributeSpec)

describe(@"FEMAttribute", ^{
FEMMapBlock map = ^id (id value) {
Expand Down
32 changes: 28 additions & 4 deletions FastEasyMappingTests/Specs/FEMDeserializerSpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#import "Chat+Mapping.h"
#import "ChatMessage+Mapping.h"

SPEC_BEGIN(FEMDeserializerOptionsSpec)
SPEC_BEGIN(FEMDeserializerSpec)
describe(@"FEMDeserializer", ^{
context(@"NSManagedObject", ^{
__block NSManagedObjectContext *moc;
Expand Down Expand Up @@ -386,23 +386,47 @@
[[child.child should] equal:object];
});
});

context(@"indirect to-one recursive relationship", ^{
__block Chat *chat;
__block Chat *indirectChat;
__block ChatMessage *message;

beforeEach(^{
NSDictionary *fixture = [Fixture buildUsingFixture:@"RecursiveChatLastMessage"];
FEMMapping *mapping = [Chat chatLastMessageMapping];
chat = [FEMDeserializer objectFromRepresentation:fixture mapping:mapping context:moc];
message = chat.lastMessage;
indirectChat = message.chat;
});

it(@"should map values", ^{
[[chat.primaryKey should] equal:@1];
[[message.primaryKey should] equal:@300];
[[indirectChat.primaryKey should] equal:@1];
});

it(@"should not duplicate chat", ^{
[[chat should] equal:indirectChat];
});
});

context(@"indirect recursive relationship", ^{
context(@"indirect to-many recursive relationship", ^{
__block Chat *chat;
__block Chat *indirectChat;
__block ChatMessage *message;

beforeEach(^{
NSDictionary *fixture = [Fixture buildUsingFixture:@"RecursiveChatMessages"];
FEMMapping *mapping = [Chat recursiveMapping];
FEMMapping *mapping = [Chat chatMessagesMapping];
chat = [FEMDeserializer objectFromRepresentation:fixture mapping:mapping context:moc];
message = [chat.messages anyObject];
indirectChat = message.chat;
});

it(@"should map values", ^{
[[chat.primaryKey should] equal:@1];
[[message.primaryKey should] equal:@345];
[[message.primaryKey should] equal:@300];
[[indirectChat.primaryKey should] equal:@1];
});

Expand Down

0 comments on commit 68c058f

Please sign in to comment.