-
Notifications
You must be signed in to change notification settings - Fork 0
/
OTRestModelMapperTest.m
232 lines (200 loc) · 9.52 KB
/
OTRestModelMapperTest.m
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
//
// OTRestModelMapperTest.m
// OTRestFramework
//
// Created by Jeremy Ellison on 8/17/09.
// Copyright 2009 Two Toasters. All rights reserved.
//
#import "OTRestModelMapperTest.h"
#import "OTRestModelMapper.h"
// Because we are going to test private methods
#import "OTRestModelMapper_Private.h"
#import "TestSerialization.h"
@interface OTRestModelMapperTest (Private)
- (NSString*)jsonString;
- (NSString*)jsonCollectionString;
- (NSString*)xmlString;
- (NSString*)xmlCollectionString;
@end
@implementation OTRestModelMapperTest
- (void)testIsParentSelector {
OTRestModelMapper* mapper = [[OTRestModelMapper alloc] init];
STAssertTrue([mapper isParentSelector:@"blah > blah"], nil);
STAssertFalse([mapper isParentSelector:@"blah"], nil);
[mapper release];
}
- (void)testContainingElementNameForSelector {
OTRestModelMapper* mapper = [[OTRestModelMapper alloc] init];
STAssertTrue([[mapper containingElementNameForSelector:@"blahs > blah"] isEqualToString:@"blahs"], nil);
[mapper release];
}
- (void)testChildElementNameForSelector {
OTRestModelMapper* mapper = [[OTRestModelMapper alloc] init];
STAssertTrue([[mapper childElementNameForSelelctor:@"blahs > blah"] isEqualToString:@"blah"], nil);
[mapper release];
}
- (void)testJSONMapping {
OTRestModelMapper* mapper = [[OTRestModelMapper alloc] init];
mapper.format = OTRestMappingFormatJSON;
[mapper registerModel:[TestSerialization class] forElementNamed:@"test_serialization_class"];
[mapper registerModel:[TestSerialization class] forElementNamed:@"test_serialization_class"];
[mapper registerModel:[TestSerializationAssociation class] forElementNamed:@"has_many"];
[mapper registerModel:[TestSerializationAssociation class] forElementNamed:@"has_one"];
id result = [mapper buildModelFromString:[self jsonString]];
STAssertNotNil(result, @"Result should not be nil, %@, mapper: %@, jsonstr: %@", result, mapper, [self jsonString]);
STAssertNotNil([result dateTest], @"dateTest should not be nil");
STAssertTrue([[result numberTest] isEqualToNumber:[NSNumber numberWithInt:2]], @"numberTest should be 2");
STAssertTrue([[result stringTest] isEqualToString:@"SomeString"], @"stringTest should == SomeString, is %@", [result stringTest]);
STAssertNotNil([result hasOne], @"has one association should not be nil");
STAssertTrue([[[result hasOne] testString] isEqualToString:@"A String"], @"has one association (%@) stringTest should be 'A String', is %@", [result hasOne], [[result hasOne] testString]);
STAssertNotNil([result hasMany], @"has many association should not be nil");
STAssertTrue([[result hasMany] count] == 2, @"there should be 2 has many associations");
}
- (void)testJSONCollectionMapping {
OTRestModelMapper* mapper = [[OTRestModelMapper alloc] init];
mapper.format = OTRestMappingFormatJSON;
[mapper registerModel:[TestSerialization class] forElementNamed:@"test_serialization_class"];
[mapper registerModel:[TestSerialization class] forElementNamed:@"test_serialization_class"];
[mapper registerModel:[TestSerializationAssociation class] forElementNamed:@"has_many"];
[mapper registerModel:[TestSerializationAssociation class] forElementNamed:@"has_one"];
NSArray* results = [mapper buildModelsFromString:[self jsonCollectionString]];
for(TestSerialization* result in results) {
STAssertNotNil(result, @"Result should not be nil, %@, mapper: %@, jsonstr: %@", result, mapper, [self jsonString]);
STAssertNotNil([result dateTest], @"dateTest should not be nil");
STAssertTrue([[result numberTest] isEqualToNumber:[NSNumber numberWithInt:2]], @"numberTest should be 2");
STAssertTrue([[result stringTest] isEqualToString:@"SomeString"], @"stringTest should == SomeString, is %@", [result stringTest]);
STAssertNotNil([result hasOne], @"has one association should not be nil");
STAssertTrue([[[result hasOne] testString] isEqualToString:@"A String"], @"has one association (%@) stringTest should be 'A String', is %@", [result hasOne], [[result hasOne] testString]);
STAssertNotNil([result hasMany], @"has many association should not be nil");
STAssertTrue([[result hasMany] count] == 2, @"there should be 2 has many associations");
}
}
- (void)testXMLMapping {
OTRestModelMapper* mapper = [[OTRestModelMapper alloc] init];
mapper.format = OTRestMappingFormatXML;
[mapper registerModel:[TestSerialization class] forElementNamed:@"test_serialization_class"];
[mapper registerModel:[TestSerializationAssociation class] forElementNamed:@"has_many"];
[mapper registerModel:[TestSerializationAssociation class] forElementNamed:@"has_one"];
TestSerialization* result = [mapper buildModelFromString:[self xmlString]];
STAssertNotNil(result, @"Result should not be nil");
STAssertNotNil([result dateTest], @"dateTest should not be nil");
STAssertTrue([[result numberTest] isEqualToNumber:[NSNumber numberWithInt:2]], @"numberTest should be 2");
STAssertTrue([[result stringTest] isEqualToString:@"SomeString"], @"stringTest should == SomeString, is %@", [result stringTest]);
STAssertNotNil([result hasOne], @"has one association should not be nil");
STAssertTrue([[[result hasOne] testString] isEqualToString:@"A String"], @"has one association (%@) stringTest should be 'A String', is %@", [result hasOne], [[result hasOne] testString]);
STAssertNotNil([result hasMany], @"has many association should not be nil");
STAssertTrue([[result hasMany] count] == 2, @"there should be 2 has many associations");
// TODO: test values of testString properties here
}
- (void)testXMLCollectionMapping {
OTRestModelMapper* mapper = [[OTRestModelMapper alloc] init];
mapper.format = OTRestMappingFormatXML;
[mapper registerModel:[TestSerialization class] forElementNamed:@"test_serialization_class"];
[mapper registerModel:[TestSerializationAssociation class] forElementNamed:@"has_many"];
[mapper registerModel:[TestSerializationAssociation class] forElementNamed:@"has_one"];
NSArray* results = [mapper buildModelsFromString:[self xmlCollectionString]];
for (TestSerialization* result in results) {
STAssertNotNil(result, @"Result should not be nil");
STAssertNotNil([result dateTest], @"dateTest should not be nil");
STAssertTrue([[result numberTest] isEqualToNumber:[NSNumber numberWithInt:2]], @"numberTest should be 2");
STAssertTrue([[result stringTest] isEqualToString:@"SomeString"], @"stringTest should == SomeString, is %@", [result stringTest]);
STAssertNotNil([result hasOne], @"has one association should not be nil");
STAssertTrue([[[result hasOne] testString] isEqualToString:@"A String"], @"has one association (%@) stringTest should be 'A String', is %@", [result hasOne], [[result hasOne] testString]);
STAssertNotNil([result hasMany], @"has many association should not be nil");
STAssertTrue([[result hasMany] count] == 2, @"there should be 2 has many associations");
// TODO: test values of testString properties here
}
}
@end
@implementation OTRestModelMapperTest (Private)
- (NSString*)jsonString {
NSString* json = @"{\"test_serialization_class\": "
@"{\"date_test\": \"2009-08-17T19:24:40Z\", "
@"\"number_test\": 2, "
@"\"string_test\": \"SomeString\", "
@"\"has_one\": {\"test_string\": \"A String\"}, "
@"\"has_manys\": [{\"test_string\": \"A String 2\"}, "
@"{\"test_string\": \"A String 3\"}]"
@"}}";
return json;
}
- (NSString*)jsonCollectionString {
NSString* json = @"["
@"{\"test_serialization_class\": "
@"{\"date_test\": \"2009-08-17T19:24:40Z\", "
@"\"number_test\": 2, "
@"\"string_test\": \"SomeString\", "
@"\"has_one\": {\"test_string\": \"A String\"}, "
@"\"has_manys\": [{\"test_string\": \"A String 2\"}, "
@"{\"test_string\": \"A String 3\"}]"
@"}"
@"}, "
@"{\"test_serialization_class\": "
@"{\"date_test\": \"2009-08-17T19:24:40Z\", "
@"\"number_test\": 2, "
@"\"string_test\": \"SomeString\", "
@"\"has_one\": {\"test_string\": \"A String\"}, "
@"\"has_manys\": [{\"test_string\": \"A String 2\"}, "
@"{\"test_string\": \"A String 3\"}]"
@"}"
@"}"
@"]";
return json;
}
- (NSString*)xmlString {
NSString* xml = @"<test_serialization_class>"
@"<date_test type='datetime'>2009-08-17T19:24:40Z</date_test>"
@"<number_test type='integer'>2</number_test>"
@"<string_test>SomeString</string_test>"
@"<has_one>"
@"<test_string>A String</test_string>"
@"</has_one>"
@"<has_manys>"
@"<has_many>"
@"<test_string>A String 2</test_string>"
@"</has_many>"
@"<has_many>"
@"<test_string>A String 3</test_string>"
@"</has_many>"
@"</has_manys>"
@"</test_serialization_class>";
return xml;
}
- (NSString*)xmlCollectionString {
NSString* xml = @"<test_serialization_classes>"
@"<test_serialization_class>"
@"<date_test type='datetime'>2009-08-17T19:24:40Z</date_test>"
@"<number_test type='integer'>2</number_test>"
@"<string_test>SomeString</string_test>"
@"<has_one>"
@"<test_string>A String</test_string>"
@"</has_one>"
@"<has_manys>"
@"<has_many>"
@"<test_string>A String 2</test_string>"
@"</has_many>"
@"<has_many>"
@"<test_string>A String 3</test_string>"
@"</has_many>"
@"</has_manys>"
@"</test_serialization_class>"
@"<test_serialization_class>"
@"<date_test type='datetime'>2009-08-17T19:24:40Z</date_test>"
@"<number_test type='integer'>2</number_test>"
@"<string_test>SomeString</string_test>"
@"<has_one>"
@"<test_string>A String</test_string>"
@"</has_one>"
@"<has_manys>"
@"<has_many>"
@"<test_string>A String 2</test_string>"
@"</has_many>"
@"<has_many>"
@"<test_string>A String 3</test_string>"
@"</has_many>"
@"</has_manys>"
@"</test_serialization_class>"
@"</test_serialization_classes>";
return xml;
}
@end