1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
//
// BUYPropertyDescriptionAdditionsTests.m
// Mobile Buy SDK
//
// Created by Shopify.
// Copyright (c) 2015 Shopify 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:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#import <XCTest/XCTest.h>
#import <CoreData/CoreData.h>
#import "BUYFlatCollectionTransformer.h"
#import "BUYDateTransformer.h"
#import "BUYIdentityTransformer.h"
#import "NSEntityDescription+BUYAdditions.h"
#import "NSPropertyDescription+BUYAdditions.h"
#import "TestModel.h"
static NSString * const BirdEntity = @"Bird";
static NSString * const BranchEntity = @"Branch";
static NSString * const LeafEntity = @"Leaf";
static NSString * const NestEntity = @"Nest";
static NSString * const ResearchEntity = @"Researcher";
static NSString * const RootEntity = @"Root";
@interface NSIndexSet (BUYTestAdditions)
+ (instancetype)buy_indexSetWithIndexes:(NSArray *)indexes;
@end
@interface BUYCoreDataModelAdditionsTests : XCTestCase
@property (nonatomic) NSManagedObjectModel *model;
@property (nonatomic) TestModelManager *modelManager;
@end
@implementation BUYCoreDataModelAdditionsTests
+ (void)initialize
{
if (self == [BUYCoreDataModelAdditionsTests class]) {
[NSValueTransformer setValueTransformer:[BUYFlatCollectionTransformer arrayTransformer] forName:@"Array"];
[NSValueTransformer setValueTransformer:[BUYFlatCollectionTransformer setTransformer] forName:@"Set"];
}
}
- (instancetype)initWithInvocation:(NSInvocation *)invocation
{
self = [super initWithInvocation:invocation];
if (self) {
self.modelManager = [[TestModelManager alloc] init];
self.model = self.modelManager.model;
}
return self;
}
- (NSEntityDescription *)entityForName:(NSString *)entityName
{
return self.model.entitiesByName[entityName];
}
- (NSAttributeDescription *)attributeWithName:(NSString *)attributeName forEntity:(NSString *)entityName
{
return [[self entityForName:entityName] attributesByName][attributeName];
}
- (NSRelationshipDescription *)relationshipWithName:(NSString *)propertyName forEntity:(NSString *)entityName
{
return [[self entityForName:entityName] relationshipsByName][propertyName];
}
- (void)testJSONTransformerName
{
XCTAssertEqualObjects(BUYDateTransformerName, [self attributeWithName:@"date" forEntity:LeafEntity].JSONValueTransformerName);
XCTAssertEqualObjects(BUYIdentityTransformerName, [self attributeWithName:@"identifier" forEntity:RootEntity].JSONValueTransformerName);
}
- (void)testJSONPropertyKey
{
XCTAssertEqualObjects(@"createDate", [self attributeWithName:@"date" forEntity:LeafEntity].JSONPropertyKey);
}
- (void)testJSONValueTransformer
{
XCTAssertEqualObjects([BUYFlatCollectionTransformer class], [[self attributeWithName:@"tags" forEntity:LeafEntity].JSONValueTransformer class]);
}
- (void)testNilAttribute
{
NSAttributeDescription *idAttribute = [self attributeWithName:@"identifier" forEntity:RootEntity];
XCTAssertNil([idAttribute buy_JSONForValue:nil]);
XCTAssertEqualObjects([idAttribute buy_valueForJSON:nil object:nil], [NSNull null]);
}
- (void)testNullAttribute
{
NSAttributeDescription *idAttribute = [self attributeWithName:@"identifier" forEntity:RootEntity];
XCTAssertEqualObjects([idAttribute buy_JSONForValue:[NSNull null]], [NSNull null]);
XCTAssertEqualObjects([idAttribute buy_valueForJSON:[NSNull null] object:nil], [NSNull null]);
}
- (void)testInteger
{
NSAttributeDescription *idAttribute = [self attributeWithName:@"identifier" forEntity:RootEntity];
NSNumber *identifier = @10001;
XCTAssertEqualObjects(identifier, [idAttribute buy_JSONForValue:identifier]);
XCTAssertEqualObjects(identifier, [idAttribute buy_valueForJSON:identifier object:nil]);
}
- (void)testString
{
NSAttributeDescription *stringAttribute = [self attributeWithName:@"name" forEntity:RootEntity];
NSString *name = @"MyRoot";
XCTAssertEqualObjects(name, [stringAttribute buy_JSONForValue:name]);
XCTAssertEqualObjects(name, [stringAttribute buy_valueForJSON:name object:nil]);
}
- (void)testDecimalNumber
{
NSAttributeDescription *decimalAttribute = [self attributeWithName:@"age" forEntity:RootEntity];
NSString *ageString = @"145";
NSDecimalNumber *age = [NSDecimalNumber decimalNumberWithString:ageString];
XCTAssertEqualObjects(ageString, [decimalAttribute buy_JSONForValue:age]);
XCTAssertEqualObjects(age, [decimalAttribute buy_valueForJSON:ageString object:nil]);
}
- (void)testDate
{
NSAttributeDescription *dateAttribute = [self attributeWithName:@"date" forEntity:LeafEntity];
NSString *dateString = @"1970-01-01T01:17:59+0000";
NSDate *date = [NSDate dateWithTimeIntervalSince1970:4679.0];
XCTAssertEqualObjects(dateString, [dateAttribute buy_JSONForValue:date]);
XCTAssertEqualObjects(date, [dateAttribute buy_valueForJSON:dateString object:nil]);
}
- (void)testURL
{
// the Root.url attribute declares "attributeValueClass = NSURL; JSONTransformerName = BUYURL"
NSAttributeDescription *urlAttribute = [self attributeWithName:@"url" forEntity:RootEntity];
NSString *urlString = @"https://www.example.com/api/model.json?id=100";
NSURL *url = [NSURL URLWithString:urlString];
XCTAssertEqualObjects(urlString, [urlAttribute buy_JSONForValue:url]);
XCTAssertEqualObjects(url, [urlAttribute buy_valueForJSON:urlString object:nil]);
}
- (void)testFlatArray
{
NSAttributeDescription *arrayDescription = [self attributeWithName:@"ornaments" forEntity:BranchEntity];
NSString *ornamentsString = @"one two three two one";
NSArray *ornaments = @[@"one", @"two", @"three", @"two", @"one"];
XCTAssertEqualObjects(ornamentsString, [arrayDescription buy_JSONForValue:ornaments]);
XCTAssertEqualObjects(ornaments, [arrayDescription buy_valueForJSON:ornamentsString object:nil]);
}
- (void)testFlatSet
{
NSAttributeDescription *setDescription = [self attributeWithName:@"tags" forEntity:LeafEntity];
NSString *tagsString = @"blue green red";
NSSet *tags = [NSSet setWithArray:@[@"red", @"green", @"blue"]];
XCTAssertEqualObjects(tags, [setDescription buy_valueForJSON:tagsString object:nil]);
NSString *jsonString = [setDescription buy_JSONForValue:tags];
NSSet *actual = [NSSet setWithArray:[jsonString componentsSeparatedByString:@" "]];
XCTAssertEqualObjects(actual, tags);
}
- (void)testNilRelationship
{
Branch *branch = [self.modelManager buy_objectWithEntityName:BranchEntity JSONDictionary:nil];
NSRelationshipDescription *nestRelationship = [self relationshipWithName:@"nest" forEntity:BranchEntity];
XCTAssertNil([nestRelationship buy_valueForJSON:nil object:branch]);
}
- (void)testNullRelationship
{
Branch *branch = [self.modelManager buy_objectWithEntityName:BranchEntity JSONDictionary:nil];
NSRelationshipDescription *nestRelationship = [self relationshipWithName:@"nest" forEntity:BranchEntity];
XCTAssertNil([nestRelationship buy_valueForJSON:[NSNull null] object:branch]);
}
- (void)testRelationship
{
Branch *branch = [self.modelManager buy_objectWithEntityName:BranchEntity JSONDictionary:nil];
NSRelationshipDescription *nestRelationship = [self relationshipWithName:@"nest" forEntity:BranchEntity];
NSDictionary *expected = @{ @"egg_count" : @2 };
id<BUYObject> object = [nestRelationship buy_valueForJSON:expected object:branch];
NSDictionary *actual = [nestRelationship buy_JSONForValue:object];
XCTAssertEqualObjects(actual, expected);
}
- (void)testRecursiveRelationship
{
Branch *branch = [self.modelManager buy_objectWithEntityName:BranchEntity JSONDictionary:nil];
NSRelationshipDescription *nestRelationship = [self relationshipWithName:@"nest" forEntity:BranchEntity];
NSDictionary *json = @{ @"bird_id" : @501 };
id object = [nestRelationship buy_valueForJSON:json object:branch];
XCTAssertEqualObjects(@501, [[object bird] identifier]);
id actual = [nestRelationship buy_JSONForValue:object];
XCTAssertEqualObjects(actual, json);
}
- (void)testToManyRelationship
{
Branch *branch = [self.modelManager buy_objectWithEntityName:BranchEntity JSONDictionary:nil];
NSRelationshipDescription *leafRelationship = [self relationshipWithName:@"leaves" forEntity:BranchEntity];
// Semi-random leaf objects
branch.leaves = [NSSet setWithArray:@[[self leafWithDate:[self dateWithComponents:[self november4_1605]] tags:[self tagsWithIndexes:@[@1, @5, @11]]],
[self leafWithDate:[self dateWithComponents:[self june21_1970]] tags:[self tagsWithIndexes:@[@9]]],
[self leafWithDate:[self dateWithComponents:[self jan1_2000]] tags:[self tagsWithIndexes:@[@12, @0, @8, @4]]]]];
id json = [leafRelationship buy_JSONForValue:branch.leaves];
id actual = [leafRelationship buy_valueForJSON:json object:branch];
XCTAssertEqualObjects(actual, branch.leaves);
}
- (void)testManyToManyRelationship
{
Researcher *researcher = [self.modelManager buy_objectWithEntityName:ResearchEntity JSONDictionary:nil];
NSRelationshipDescription *researchersRelationship = [self relationshipWithName:@"researchers" forEntity:BirdEntity];
XCTAssertNil([researchersRelationship buy_JSONForValue:[NSSet setWithObject:researcher]]);
}
- (void)testEntityIsPrivate
{
NSEntityDescription *forestEntity = [self entityForName:[Forest entityName]];
XCTAssertTrue([forestEntity buy_isPrivate]);
}
- (void)testFetchedProperty
{
NSFetchedPropertyDescription *fetchedProperty = [[NSFetchedPropertyDescription alloc] init];
XCTAssertNil([fetchedProperty buy_valueForJSON:nil object:nil]);
XCTAssertNil([fetchedProperty buy_JSONForValue:nil]);
}
- (Leaf *)leafWithDate:(NSDate *)date tags:(NSSet *)tags
{
Leaf *leaf = [self.modelManager buy_objectWithEntityName:[Leaf entityName] JSONDictionary:nil];
leaf.date = date;
leaf.tags = tags;
return leaf;
}
- (NSSet *)tagsWithIndexes:(NSArray *)indexes
{
static NSArray *tags;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
tags = @[@"one", @"two", @"three", @"hot", @"urgent", @"important", @"red", @"green", @"blue", @"animal", @"vegetable", @"mineral", @"fungus"];
});
return [NSSet setWithArray:[tags objectsAtIndexes:[NSIndexSet buy_indexSetWithIndexes:indexes]]];
}
- (NSDate *)dateWithComponents:(NSDateComponents *)components
{
return [[NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian] dateFromComponents:components];
}
- (NSDateComponents *)november4_1605
{
NSDateComponents *components = [[NSDateComponents alloc] init];
components.year = 1605;
components.month = 11;
components.day = 4;
return components;
}
- (NSDateComponents *)june21_1970
{
NSDateComponents *components = [[NSDateComponents alloc] init];
components.year = 1970;
components.month = 6;
components.day = 21;
return components;
}
- (NSDateComponents *)jan1_2000
{
NSDateComponents *components = [[NSDateComponents alloc] init];
components.year = 2000;
components.month = 1;
components.day = 1;
components.second = 1;
return components;
}
@end
@implementation NSIndexSet (BUYTestAdditions)
+ (instancetype)buy_indexSetWithIndexes:(NSArray *)indexes
{
NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
for (NSNumber *index in indexes) {
[indexSet addIndex:index.unsignedIntegerValue];
}
return indexSet;
}
@end