-
Notifications
You must be signed in to change notification settings - Fork 154
/
CTPersistanceTestChangeKey.m
147 lines (111 loc) · 5.36 KB
/
CTPersistanceTestChangeKey.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
//
// CTPersistanceTestChangeKey.m
// CTPersistanceTests
//
// Created by casa on 2018/5/8.
// Copyright © 2018年 casa. All rights reserved.
//
#import <XCTest/XCTest.h>
#import <sqlite3.h>
#import "CTPersistanceDataBase.h"
#import "CTPersistanceQueryCommand+SchemaManipulations.h"
extern SQLITE_API int sqlite3_key(sqlite3 *db, const void *pKey, int nKey);
@interface CTPersistanceTestChangeKey : XCTestCase
@property (nonatomic, unsafe_unretained) sqlite3 *database;
@property (nonatomic, strong) NSString *databaseFilePath;
@end
@implementation CTPersistanceTestChangeKey
- (void)setUp {
[super setUp];
// Put setup code here. This method is called before the invocation of each test method in the class.
if ([[NSFileManager defaultManager] fileExistsAtPath:self.databaseFilePath]) {
[[NSFileManager defaultManager] removeItemAtPath:self.databaseFilePath error:NULL];
}
}
- (void)tearDown {
// Put teardown code here. This method is called after the invocation of each test method in the class.
sqlite3_close_v2(_database);
_database = NULL;
_databaseFilePath = nil;
[super tearDown];
}
- (void)testKey0ToKey4 {
[self createDatabaseWithKey:@"0"];
NSError *error = nil;
CTPersistanceDataBase *database = [[CTPersistanceDataBase alloc] initWithDatabaseName:@"TestDatabase.sqlite" swiftModuleName:nil error:&error];
[database closeDatabase];
database = [[CTPersistanceDataBase alloc] initWithDatabaseName:@"TestDatabase.sqlite" swiftModuleName:nil error:&error];
CTPersistanceQueryCommand *queryCommand = [[CTPersistanceQueryCommand alloc] initWithDatabase:database];
NSArray *result = [[queryCommand showTablesWithError:&error] fetchWithError:&error];
XCTAssertGreaterThan(result.count, 0);
[database closeDatabase];
}
- (void)testKey1ToKey4 {
[self createDatabaseWithKey:@"1"];
NSError *error = nil;
CTPersistanceDataBase *database = [[CTPersistanceDataBase alloc] initWithDatabaseName:@"TestDatabase.sqlite" swiftModuleName:nil error:&error];
[database closeDatabase];
database = [[CTPersistanceDataBase alloc] initWithDatabaseName:@"TestDatabase.sqlite" swiftModuleName:nil error:&error];
CTPersistanceQueryCommand *queryCommand = [[CTPersistanceQueryCommand alloc] initWithDatabase:database];
NSArray *result = [[queryCommand showTablesWithError:&error] fetchWithError:&error];
XCTAssertGreaterThan(result.count, 0);
[database closeDatabase];
}
- (void)testKey2ToKey4 {
[self createDatabaseWithKey:@"2"];
NSError *error = nil;
CTPersistanceDataBase *database = [[CTPersistanceDataBase alloc] initWithDatabaseName:@"TestDatabase.sqlite" swiftModuleName:nil error:&error];
[database closeDatabase];
database = [[CTPersistanceDataBase alloc] initWithDatabaseName:@"TestDatabase.sqlite" swiftModuleName:nil error:&error];
CTPersistanceQueryCommand *queryCommand = [[CTPersistanceQueryCommand alloc] initWithDatabase:database];
NSArray *result = [[queryCommand showTablesWithError:&error] fetchWithError:&error];
XCTAssertGreaterThan(result.count, 0);
[database closeDatabase];
}
- (void)testKey3ToKey4 {
[self createDatabaseWithKey:@"3"];
NSError *error = nil;
CTPersistanceDataBase *database = [[CTPersistanceDataBase alloc] initWithDatabaseName:@"TestDatabase.sqlite" swiftModuleName:nil error:&error];
[database closeDatabase];
database = [[CTPersistanceDataBase alloc] initWithDatabaseName:@"TestDatabase.sqlite" swiftModuleName:nil error:&error];
CTPersistanceQueryCommand *queryCommand = [[CTPersistanceQueryCommand alloc] initWithDatabase:database];
NSArray *result = [[queryCommand showTablesWithError:&error] fetchWithError:&error];
XCTAssertGreaterThan(result.count, 0);
[database closeDatabase];
}
- (void)testNewDatabase {
NSError *error = nil;
CTPersistanceDataBase *database = [[CTPersistanceDataBase alloc] initWithDatabaseName:@"TestDatabase.sqlite" swiftModuleName:nil error:&error];
CTPersistanceQueryCommand *queryCommand = [[CTPersistanceQueryCommand alloc] initWithDatabase:database];
NSArray *result = [[queryCommand showTablesWithError:&error] fetchWithError:&error];
XCTAssertGreaterThan(result.count, 0);
}
#pragma mark - private methods
- (void)createDatabaseWithKey:(NSString *)key
{
sqlite3 *database = NULL;
const char *path = [self.databaseFilePath UTF8String];
sqlite3_open_v2(path, &(database),
SQLITE_OPEN_CREATE |
SQLITE_OPEN_READWRITE |
SQLITE_OPEN_NOMUTEX |
SQLITE_OPEN_SHAREDCACHE,
NULL);
sqlite3_key(database, [key UTF8String], (int)key.length);
NSString *sqlString = @"CREATE TABLE IF NOT EXISTS `test` (`name` TEXT);";
sqlite3_stmt *statement = nil;
sqlite3_prepare_v2(database, [sqlString UTF8String], (int)sqlString.length, &statement, NULL);
sqlite3_step(statement);
sqlite3_finalize(statement);
sqlite3_close_v2(database);
database = NULL;
}
#pragma mark - getters and setters
- (NSString *)databaseFilePath
{
if (_databaseFilePath == nil) {
_databaseFilePath = [[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"TestDatabase.sqlite"];
}
return _databaseFilePath;
}
@end