-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathCTPersistanceTestUpdate.m
313 lines (273 loc) · 14.6 KB
/
CTPersistanceTestUpdate.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
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
//
// CTPersistanceTestUpdate.m
// CTPersistance
//
// Created by casa on 2017/7/30.
// Copyright © 2017年 casa. All rights reserved.
//
#import <XCTest/XCTest.h>
#import "TestTable.h"
#import "TestRecord.h"
@interface CTPersistanceTestUpdate : XCTestCase
@property (nonatomic, strong) TestTable *testTable;
@property (nonatomic, strong) TestRecord *recordToUpdate;
@property (nonatomic, strong) NSMutableArray <TestRecord *> *recordList;
@property (nonatomic, strong) NSMutableArray *primaryKeyList;
@end
@implementation CTPersistanceTestUpdate
- (void)setUp {
[super setUp];
self.testTable = [[TestTable alloc] init];
self.recordList = [[NSMutableArray alloc] init];
NSInteger count = 5;
while (count --> 0) {
TestRecord *record = [[TestRecord alloc] init];
record.name = @"casa";
record.avatar = [@"avatar" dataUsingEncoding:NSUTF8StringEncoding];
record.progress = @(0.0f);
record.isCelebrity = @(NO);
record.nilValue = @"nilValue";
NSDate *now = [NSDate date];
long long millisecond = [now timeIntervalSince1970] * 1000;
record.timeStamp = millisecond;
[self.testTable insertRecord:record error:NULL];
[self.recordList addObject:record];
[self.primaryKeyList addObject:record.primaryKey];
}
self.recordToUpdate = [self.recordList firstObject];;
}
- (void)tearDown {
[self.testTable truncate];
[super tearDown];
}
- (void)testUpdateRecord
{
self.recordToUpdate.name = @"testUpdateRecord";
self.recordToUpdate.avatar = [@"newAvatar" dataUsingEncoding:NSUTF8StringEncoding];
self.recordToUpdate.progress = @(0.1f);
self.recordToUpdate.isCelebrity = @(YES);
self.recordToUpdate.nilValue = nil;
NSError *error = nil;
[self.testTable updateRecord:self.recordToUpdate error:&error];
XCTAssertNil(error);
TestRecord *record = (TestRecord *)[self.testTable findWithPrimaryKey:self.recordToUpdate.primaryKey error:NULL];
XCTAssert([record.name isEqualToString:@"testUpdateRecord"]);
NSString *newAvatarString = [[NSString alloc] initWithData:record.avatar encoding:NSUTF8StringEncoding];
XCTAssert([newAvatarString isEqualToString:@"newAvatar"]);
XCTAssertEqual([record.progress doubleValue], 0.1f);
XCTAssertEqual([record.isCelebrity boolValue], YES);
XCTAssertNil(record.nilValue);
}
- (void)testUpdateRecordList
{
self.recordToUpdate.name = @"testUpdateRecordList";
self.recordToUpdate.avatar = [@"newAvatar" dataUsingEncoding:NSUTF8StringEncoding];
self.recordToUpdate.progress = @(0.1f);
self.recordToUpdate.nilValue = nil;
NSError *error = nil;
[self.testTable updateRecordList:@[self.recordToUpdate] error:&error];
XCTAssertNil(error);
TestRecord *record = (TestRecord *)[self.testTable findWithPrimaryKey:self.recordToUpdate.primaryKey error:NULL];
XCTAssert([record.name isEqualToString:@"testUpdateRecordList"]);
NSString *newAvatarString = [[NSString alloc] initWithData:record.avatar encoding:NSUTF8StringEncoding];
XCTAssert([newAvatarString isEqualToString:@"newAvatar"]);
XCTAssertEqual([record.progress doubleValue], 0.1f);
XCTAssertNil(record.nilValue);
}
- (void)testUpdateRecordListShowError
{
TestRecord *testRecord = [[TestRecord alloc] init];
testRecord.uniqueString = @"uniqueString";
[self.testTable insertRecord:testRecord error:NULL];
self.recordToUpdate.uniqueString = @"uniqueString";
NSError *error = nil;
[self.testTable updateRecordList:@[self.recordToUpdate] error:&error];
XCTAssertNotNil(error);
}
- (void)testUpdateRecordList_multiRecord
{
[self.recordList enumerateObjectsUsingBlock:^(TestRecord * _Nonnull record, NSUInteger idx, BOOL * _Nonnull stop) {
record.name = @"testUpdateRecordList_multiRecord";
record.avatar = [@"newAvatar" dataUsingEncoding:NSUTF8StringEncoding];
record.progress = @(0.1f);
record.nilValue = nil;
}];
NSError *error = nil;
[self.testTable updateRecordList:self.recordList error:&error];
XCTAssertNil(error);
NSArray <TestRecord *> *recordListToCheck = (NSArray <TestRecord *> *)[self.testTable findAllWithPrimaryKey:self.primaryKeyList error:NULL];
XCTAssertEqual(recordListToCheck.count, self.primaryKeyList.count);
[recordListToCheck enumerateObjectsUsingBlock:^(TestRecord * _Nonnull recordToCheck, NSUInteger idx, BOOL * _Nonnull stop) {
XCTAssert([recordToCheck.name isEqualToString:@"testUpdateRecordList_multiRecord"]);
NSString *newAvatarString = [[NSString alloc] initWithData:recordToCheck.avatar encoding:NSUTF8StringEncoding];
XCTAssert([newAvatarString isEqualToString:@"newAvatar"]);
XCTAssertEqual([recordToCheck.progress doubleValue], 0.1f);
XCTAssertNil(recordToCheck.nilValue);
}];
}
- (void)testUpdateValueForKeyPrimaryKeyValue
{
NSError *error = nil;
[self.testTable updateValue:@"testUpdateValueForKeyPrimaryKeyValue" forKey:@"name" primaryKeyValue:self.recordToUpdate.primaryKey error:&error];
XCTAssertNil(error);
[self.testTable updateValue:[@"newAvatar" dataUsingEncoding:NSUTF8StringEncoding] forKey:@"avatar" primaryKeyValue:self.recordToUpdate.primaryKey error:&error];
XCTAssertNil(error);
[self.testTable updateValue:@(0.1f) forKey:@"progress" primaryKeyValue:self.recordToUpdate.primaryKey error:&error];
XCTAssertNil(error);
[self.testTable updateValue:nil forKey:@"nilValue" primaryKeyValue:self.recordToUpdate.primaryKey error:&error];
XCTAssertNil(error);
TestRecord *record = (TestRecord *)[self.testTable findWithPrimaryKey:self.recordToUpdate.primaryKey error:NULL];
XCTAssert([record.name isEqualToString:@"testUpdateValueForKeyPrimaryKeyValue"]);
NSString *newAvatarString = [[NSString alloc] initWithData:record.avatar encoding:NSUTF8StringEncoding];
XCTAssert([newAvatarString isEqualToString:@"newAvatar"]);
XCTAssertEqual([record.progress doubleValue], 0.1f);
XCTAssertNil(record.nilValue);
}
- (void)testUpdateValueForKeyWhereKeyInList
{
NSError *error = nil;
[self.testTable updateValue:@"testUpdateValueForKeyWhereKeyInList" forKey:@"name" whereKey:self.testTable.primaryKeyName inList:self.primaryKeyList error:&error];
XCTAssertNil(error);
[self.testTable updateValue:[@"newAvatar" dataUsingEncoding:NSUTF8StringEncoding] forKey:@"avatar" whereKey:self.testTable.primaryKeyName inList:self.primaryKeyList error:&error];
XCTAssertNil(error);
[self.testTable updateValue:@(0.1f) forKey:@"progress" whereKey:self.testTable.primaryKeyName inList:self.primaryKeyList error:&error];
XCTAssertNil(error);
[self.testTable updateValue:nil forKey:@"nilValue" whereKey:self.testTable.primaryKeyName inList:self.primaryKeyList error:&error];
XCTAssertNil(error);
NSArray <TestRecord *> *recordListToCheck = (NSArray <TestRecord *> *)[self.testTable findAllWithPrimaryKey:self.primaryKeyList error:NULL];
XCTAssertEqual(recordListToCheck.count, self.primaryKeyList.count);
[recordListToCheck enumerateObjectsUsingBlock:^(TestRecord * _Nonnull recordToCheck, NSUInteger idx, BOOL * _Nonnull stop) {
XCTAssert([recordToCheck.name isEqualToString:@"testUpdateValueForKeyWhereKeyInList"]);
NSString *newAvatarString = [[NSString alloc] initWithData:recordToCheck.avatar encoding:NSUTF8StringEncoding];
XCTAssert([newAvatarString isEqualToString:@"newAvatar"]);
XCTAssertEqual([recordToCheck.progress doubleValue], 0.1f);
XCTAssertNil(recordToCheck.nilValue);
}];
}
- (void)testUpdateKeyValueListWhereConditionWhereConditionParams
{
NSError *error = nil;
[self.testTable updateKeyValueList:@{
@"name":@"testUpdateKeyValueListWhereConditionWhereConditionParams",
@"avatar":[@"newAvatar" dataUsingEncoding:NSUTF8StringEncoding],
@"progress":@(0.1f),
@"nilValue":[NSNull null],
}
whereCondition:@"primaryKey > :primaryKeyValue"
whereConditionParams:@{@":primaryKeyValue":@0,}
error:&error];
XCTAssertNil(error);
NSArray <TestRecord *> *recordListToCheck = (NSArray <TestRecord *> *)[self.testTable findAllWithPrimaryKey:self.primaryKeyList error:NULL];
XCTAssertEqual(recordListToCheck.count, self.primaryKeyList.count);
[recordListToCheck enumerateObjectsUsingBlock:^(TestRecord * _Nonnull recordToCheck, NSUInteger idx, BOOL * _Nonnull stop) {
XCTAssert([recordToCheck.name isEqualToString:@"testUpdateKeyValueListWhereConditionWhereConditionParams"]);
NSString *newAvatarString = [[NSString alloc] initWithData:recordToCheck.avatar encoding:NSUTF8StringEncoding];
XCTAssert([newAvatarString isEqualToString:@"newAvatar"]);
XCTAssertEqual([recordToCheck.progress doubleValue], 0.1f);
XCTAssertNil(recordToCheck.nilValue);
}];
}
- (void)testUpdateKeyValueListWithLongLongTypeWhereCondition {
NSError *error = nil;
NSDate *now = [NSDate date];
long long millisecond = [now timeIntervalSince1970] * 1000;
[self.testTable updateKeyValueList:@{
@"name":@"testUpdateKeyValueListWhereConditionWhereConditionParams",
@"avatar":[@"newAvatar" dataUsingEncoding:NSUTF8StringEncoding],
@"progress":@(0.1f)
}
whereCondition:@"timeStamp < :timeStamp"
whereConditionParams:@{@":timeStamp":@(millisecond + 1),}
error:&error];
XCTAssertNil(error);
NSArray <TestRecord *> *recordListToCheck = (NSArray <TestRecord *> *)[self.testTable findAllWithPrimaryKey:self.primaryKeyList error:NULL];
XCTAssertEqual(recordListToCheck.count, self.primaryKeyList.count);
[recordListToCheck enumerateObjectsUsingBlock:^(TestRecord * _Nonnull recordToCheck, NSUInteger idx, BOOL * _Nonnull stop) {
XCTAssert([recordToCheck.name isEqualToString:@"testUpdateKeyValueListWhereConditionWhereConditionParams"]);
NSString *newAvatarString = [[NSString alloc] initWithData:recordToCheck.avatar encoding:NSUTF8StringEncoding];
XCTAssert([newAvatarString isEqualToString:@"newAvatar"]);
XCTAssertEqual([recordToCheck.progress doubleValue], 0.1f);
}];
}
- (void)testUpdateValueForKeyPrimaryKeyValueList
{
NSError *error = nil;
[self.testTable updateValue:@"testUpdateValueForKeyPrimaryKeyValueList"
forKey:@"name"
primaryKeyValueList:self.primaryKeyList
error:&error];
XCTAssertNil(error);
[self.testTable updateValue:[@"newAvatar" dataUsingEncoding:NSUTF8StringEncoding]
forKey:@"avatar"
primaryKeyValueList:self.primaryKeyList
error:&error];
XCTAssertNil(error);
[self.testTable updateValue:@(0.1f)
forKey:@"progress"
primaryKeyValueList:self.primaryKeyList
error:&error];
XCTAssertNil(error);
[self.testTable updateValue:nil
forKey:@"nilValue"
primaryKeyValueList:self.primaryKeyList
error:&error];
XCTAssertNil(error);
NSArray <TestRecord *> *recordListToCheck = (NSArray <TestRecord *> *)[self.testTable findAllWithPrimaryKey:self.primaryKeyList error:NULL];
XCTAssertEqual(recordListToCheck.count, self.primaryKeyList.count);
[recordListToCheck enumerateObjectsUsingBlock:^(TestRecord * _Nonnull recordToCheck, NSUInteger idx, BOOL * _Nonnull stop) {
XCTAssert([recordToCheck.name isEqualToString:@"testUpdateValueForKeyPrimaryKeyValueList"]);
NSString *newAvatarString = [[NSString alloc] initWithData:recordToCheck.avatar encoding:NSUTF8StringEncoding];
XCTAssert([newAvatarString isEqualToString:@"newAvatar"]);
XCTAssertEqual([recordToCheck.progress doubleValue], 0.1f);
XCTAssertNil(recordToCheck.nilValue);
}];
}
- (void)testUpdateKeyValueList_PrimaryKeyValueList
{
NSError *error = nil;
[self.testTable updateKeyValueList:@{
@"age":@1,
@"name":@"testUpdateKeyValueList_PrimaryKeyValueList",
@"avatar":[@"newAvatar" dataUsingEncoding:NSUTF8StringEncoding],
@"progress":@(0.1f),
@"nilValue":[NSNull null],
}
primaryKeyValueList:self.primaryKeyList
error:&error];
XCTAssertNil(error);
NSArray <TestRecord *> *recordListToCheck = (NSArray <TestRecord *> *)[self.testTable findAllWithPrimaryKey:self.primaryKeyList error:NULL];
XCTAssertEqual(recordListToCheck.count, self.primaryKeyList.count);
[recordListToCheck enumerateObjectsUsingBlock:^(TestRecord * _Nonnull recordToCheck, NSUInteger idx, BOOL * _Nonnull stop) {
XCTAssert([recordToCheck.name isEqualToString:@"testUpdateKeyValueList_PrimaryKeyValueList"]);
XCTAssertEqual([recordToCheck.age integerValue], 1);
NSString *newAvatarString = [[NSString alloc] initWithData:recordToCheck.avatar encoding:NSUTF8StringEncoding];
XCTAssert([newAvatarString isEqualToString:@"newAvatar"]);
XCTAssertEqual([recordToCheck.progress doubleValue], 0.1f);
XCTAssertNil(recordToCheck.nilValue);
}];
}
- (void)testUpdateWithDefaultValue {
NSError *error = nil;
TestRecord *record = (TestRecord *)[self.testTable findWithPrimaryKey:@(1) error:&error];
XCTAssertNil(error);
// Default String
record.defaultStr = nil;
[self.testTable updateRecord:record error:&error];
XCTAssertNil(error);
TestRecord *existRecord = (TestRecord *)[self.testTable findWithPrimaryKey:@(1) error:&error];
XCTAssertNil(error);
XCTAssert([existRecord.defaultStr isEqualToString:@""]);
// Default Int
[self.testTable updateValue:nil forKey:@"defaultInt" primaryKeyValue:@(1) error:&error];
XCTAssertNil(error);
existRecord = (TestRecord *)[self.testTable findWithPrimaryKey:@(1) error:&error];
XCTAssertNil(error);
XCTAssert(existRecord.defaultInt == 1);
// Default Int
[self.testTable updateValue:nil forKey:@"defaultDouble" primaryKeyValue:@(1) error:&error];
XCTAssertNil(error);
existRecord = (TestRecord *)[self.testTable findWithPrimaryKey:@(1) error:&error];
XCTAssertNil(error);
XCTAssert(existRecord.defaultDouble == 1);
}
@end