This repository has been archived by the owner on Mar 9, 2022. It is now read-only.
/
TDDatabase_Tests.m
313 lines (255 loc) · 13 KB
/
TDDatabase_Tests.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
//
// TDDatabase_Tests.m
// TouchDB
//
// Created by Jens Alfke on 12/7/11.
// Copyright 2011 Couchbase, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
// except in compliance with the License. You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed under the
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
// either express or implied. See the License for the specific language governing permissions
// and limitations under the License.
#import "TDDatabase.h"
#import "TDBody.h"
#import "TDRevision.h"
#import "TDBlobStore.h"
#import "TDBase64.h"
#import "TDInternal.h"
#import "Test.h"
#if DEBUG
NSString* kPath = @"/tmp/touchdb_test.sqlite3";
static TDDatabase* createDB(void) {
TDDatabase *db = [TDDatabase createEmptyDBAtPath: kPath];
CAssert([db open]);
return db;
}
static NSDictionary* userProperties(NSDictionary* dict) {
NSMutableDictionary* user = $mdict();
for (NSString* key in dict) {
if (![key hasPrefix: @"_"])
[user setObject: [dict objectForKey: key] forKey: key];
}
return user;
}
TestCase(TDDatabase_CRUD) {
// Start with a fresh database in /tmp:
TDDatabase* db = createDB();
// Create a document:
NSMutableDictionary* props = $mdict({@"foo", $object(1)}, {@"bar", $false});
TDBody* doc = [[[TDBody alloc] initWithProperties: props] autorelease];
TDRevision* rev1 = [[[TDRevision alloc] initWithBody: doc] autorelease];
TDStatus status;
rev1 = [db putRevision: rev1 prevRevisionID: nil status: &status];
CAssertEq(status, 201);
Log(@"Created: %@", rev1);
CAssert(rev1.docID.length >= 10);
CAssert([rev1.revID hasPrefix: @"1-"]);
// Read it back:
TDRevision* readRev = [db getDocumentWithID: rev1.docID];
CAssert(readRev != nil);
CAssertEqual(userProperties(readRev.properties), userProperties(doc.properties));
// Now update it:
props = [[readRev.properties mutableCopy] autorelease];
[props setObject: @"updated!" forKey: @"status"];
doc = [TDBody bodyWithProperties: props];
TDRevision* rev2 = [[[TDRevision alloc] initWithBody: doc] autorelease];
TDRevision* rev2Input = rev2;
rev2 = [db putRevision: rev2 prevRevisionID: rev1.revID status: &status];
CAssertEq(status, 201);
Log(@"Updated: %@", rev2);
CAssertEqual(rev2.docID, rev1.docID);
CAssert([rev2.revID hasPrefix: @"2-"]);
// Read it back:
readRev = [db getDocumentWithID: rev2.docID];
CAssert(readRev != nil);
CAssertEqual(userProperties(readRev.properties), userProperties(doc.properties));
// Try to update the first rev, which should fail:
CAssertNil([db putRevision: rev2Input prevRevisionID: rev1.revID status: &status]);
CAssertEq(status, 409);
// Delete it:
TDRevision* revD = [[[TDRevision alloc] initWithDocID: rev2.docID revID: nil deleted: YES] autorelease];
revD = [db putRevision: revD prevRevisionID: rev2.revID status: &status];
CAssertEq(status, 200);
CAssertEqual(revD.docID, rev2.docID);
CAssert([revD.revID hasPrefix: @"3-"]);
// Read it back (should fail):
readRev = [db getDocumentWithID: revD.docID];
CAssertNil(readRev);
TDRevisionList* changes = [db changesSinceSequence: 0 options: NULL];
Log(@"Changes = %@", changes);
CAssertEq(changes.count, 1u);
NSArray* history = [db getRevisionHistory: revD];
Log(@"History = %@", history);
CAssertEqual(history, $array(revD, rev2, rev1));
CAssert([db close]);
}
static void verifyHistory(TDDatabase* db, TDRevision* rev, NSArray* history) {
TDRevision* gotRev = [db getDocumentWithID: rev.docID];
CAssertEqual(gotRev, rev);
CAssertEqual(gotRev.properties, rev.properties);
NSArray* revHistory = [db getRevisionHistory: gotRev];
CAssertEq(revHistory.count, history.count);
for (NSUInteger i=0; i<history.count; i++) {
TDRevision* hrev = [revHistory objectAtIndex: i];
CAssertEqual(hrev.docID, rev.docID);
CAssertEqual(hrev.revID, [history objectAtIndex: i]);
CAssert(!hrev.deleted);
}
}
TestCase(TDDatabase_RevTree) {
RequireTestCase(TDDatabase_CRUD);
// Start with a fresh database in /tmp:
TDDatabase* db = createDB();
TDRevision* rev = [[[TDRevision alloc] initWithDocID: @"MyDocID" revID: @"4-foxy" deleted: NO] autorelease];
rev.properties = $dict({@"_id", rev.docID}, {@"_rev", rev.revID}, {@"message", @"hi"});
NSArray* history = $array(rev.revID, @"3-thrice", @"2-too", @"1-won");
TDStatus status = [db forceInsert: rev revisionHistory: history source: nil];
CAssertEq(status, 201);
CAssertEq(db.documentCount, 1u);
verifyHistory(db, rev, history);
TDRevision* conflict = [[[TDRevision alloc] initWithDocID: @"MyDocID" revID: @"5-epsilon" deleted: NO] autorelease];
conflict.properties = $dict({@"_id", conflict.docID}, {@"_rev", conflict.revID},
{@"message", @"yo"});
history = $array(conflict.revID, @"4-delta", @"3-gamma", @"2-too", @"1-won");
status = [db forceInsert: conflict revisionHistory: history source: nil];
CAssertEq(status, 201);
CAssertEq(db.documentCount, 1u);
verifyHistory(db, conflict, history);
// Fetch one of those phantom revisions with no body:
TDRevision* rev2 = [db getDocumentWithID: rev.docID revisionID: @"2-too"];
CAssertEqual(rev2.docID, rev.docID);
CAssertEqual(rev2.revID, @"2-too");
CAssertEqual(rev2.body, nil);
// Make sure no duplicate rows were inserted for the common revisions:
CAssertEq(db.lastSequence, 7u);
// Make sure the revision with the higher revID wins the conflict:
TDRevision* current = [db getDocumentWithID: rev.docID];
CAssertEqual(current, conflict);
}
TestCase(TDDatabase_Attachments) {
RequireTestCase(TDDatabase_CRUD);
// Start with a fresh database in /tmp:
TDDatabase* db = createDB();
TDBlobStore* attachments = db.attachmentStore;
CAssertEq(attachments.count, 0u);
CAssertEqual(attachments.allKeys, $array());
// Add a revision and an attachment to it:
TDRevision* rev1;
TDStatus status;
rev1 = [db putRevision: [TDRevision revisionWithProperties:$dict({@"foo", $object(1)},
{@"bar", $false})]
prevRevisionID: nil status: &status];
CAssertEq(status, 201);
NSData* attach1 = [@"This is the body of attach1" dataUsingEncoding: NSUTF8StringEncoding];
CAssert([db insertAttachment: attach1 forSequence: rev1.sequence
named: @"attach" type: @"text/plain"
revpos: rev1.generation]);
NSString* type;
CAssertEqual([db getAttachmentForSequence: rev1.sequence named: @"attach"
type: &type status: &status], attach1);
CAssertEq(status, 200);
CAssertEqual(type, @"text/plain");
// Check the attachment dict:
NSDictionary* attachmentDict = $dict({@"attach", $dict({@"content_type", @"text/plain"},
{@"digest", @"sha1-gOHUOBmIMoDCrMuGyaLWzf1hQTE="},
{@"length", $object(27)},
{@"stub", $true},
{@"revpos", $object(1)})});
CAssertEqual([db getAttachmentDictForSequence: rev1.sequence withContent: NO], attachmentDict);
TDRevision* gotRev1 = [db getDocumentWithID: rev1.docID revisionID: rev1.revID];
CAssertEqual([gotRev1.properties objectForKey: @"_attachments"], attachmentDict);
// Add a second revision that doesn't update the attachment:
TDRevision* rev2;
rev2 = [db putRevision: [TDRevision revisionWithProperties:$dict({@"_id", rev1.docID},
{@"foo", $object(2)},
{@"bazz", $false})]
prevRevisionID: rev1.revID status: &status];
CAssertEq(status, 201);
[db copyAttachmentNamed: @"attach" fromSequence: rev1.sequence toSequence: rev2.sequence];
// Add a third revision of the same document:
TDRevision* rev3;
rev3 = [db putRevision: [TDRevision revisionWithProperties:$dict({@"_id", rev2.docID},
{@"foo", $object(2)},
{@"bazz", $false})]
prevRevisionID: rev2.revID status: &status];
CAssertEq(status, 201);
NSData* attach2 = [@"<html>And this is attach2</html>" dataUsingEncoding: NSUTF8StringEncoding];
CAssert([db insertAttachment: attach2 forSequence: rev3.sequence
named: @"attach" type: @"text/html" revpos: rev2.generation]);
// Check the 2nd revision's attachment:
type = nil;
CAssertEqual([db getAttachmentForSequence: rev2.sequence
named: @"attach"
type: &type
status: &status], attach1);
CAssertEq(status, 200);
CAssertEqual(type, @"text/plain");
// Check the 3rd revision's attachment:
CAssertEqual([db getAttachmentForSequence: rev3.sequence
named: @"attach"
type: &type
status: &status], attach2);
CAssertEq(status, 200);
CAssertEqual(type, @"text/html");
// Examine the attachment store:
CAssertEq(attachments.count, 2u);
NSSet* expected = [NSSet setWithObjects: [TDBlobStore keyDataForBlob: attach1],
[TDBlobStore keyDataForBlob: attach2], nil];
CAssertEqual([NSSet setWithArray: attachments.allKeys], expected);
CAssertEq([db compact], 200); // This clears the body of the first revision
CAssertEq(attachments.count, 1u);
CAssertEqual(attachments.allKeys, $array([TDBlobStore keyDataForBlob: attach2]));
}
TestCase(TDDatabase_PutAttachment) {
RequireTestCase(TDDatabase_Attachments);
// Start with a fresh database in /tmp:
TDDatabase* db = createDB();
NSData* attach1 = [@"This is the body of attach1" dataUsingEncoding: NSUTF8StringEncoding];
NSString* base64 = [TDBase64 encode: attach1];
NSDictionary* attachmentDict = $dict({@"attach", $dict({@"content_type", @"text/plain"},
{@"data", base64})});
NSDictionary* props = $dict({@"foo", $object(1)},
{@"bar", $false},
{@"_attachments", attachmentDict});
TDRevision* rev1;
TDStatus status;
rev1 = [db putRevision: [TDRevision revisionWithProperties: props]
prevRevisionID: nil status: &status];
CAssertEq(status, 201);
// Examine the attachment store:
CAssertEq(db.attachmentStore.count, 1u);
TDRevision* gotRev1 = [db getDocumentWithID: rev1.docID revisionID: rev1.revID];
attachmentDict = [gotRev1.properties objectForKey: @"_attachments"];
CAssertEqual(attachmentDict, $dict({@"attach", $dict({@"content_type", @"text/plain"},
{@"digest", @"sha1-gOHUOBmIMoDCrMuGyaLWzf1hQTE="},
{@"length", $object(27)},
{@"stub", $true},
{@"revpos", $object(1)})}));
}
TestCase(TDDatabase_ReplicatorSequences) {
RequireTestCase(TDDatabase_CRUD);
TDDatabase* db = createDB();
NSURL* remote = [NSURL URLWithString: @"http://iriscouch.com/"];
CAssertNil([db lastSequenceWithRemoteURL: remote push: NO]);
CAssertNil([db lastSequenceWithRemoteURL: remote push: YES]);
[db setLastSequence: @"lastpull" withRemoteURL: remote push: NO];
CAssertEqual([db lastSequenceWithRemoteURL: remote push: NO], @"lastpull");
CAssertNil([db lastSequenceWithRemoteURL: remote push: YES]);
[db setLastSequence: @"newerpull" withRemoteURL: remote push: NO];
CAssertEqual([db lastSequenceWithRemoteURL: remote push: NO], @"newerpull");
CAssertNil([db lastSequenceWithRemoteURL: remote push: YES]);
[db setLastSequence: @"lastpush" withRemoteURL: remote push: YES];
CAssertEqual([db lastSequenceWithRemoteURL: remote push: NO], @"newerpull");
CAssertEqual([db lastSequenceWithRemoteURL: remote push: YES], @"lastpush");
}
TestCase(TDDatabase) {
RequireTestCase(TDDatabase_CRUD);
RequireTestCase(TDDatabase_RevTree);
RequireTestCase(TDDatabase_Attachments);
RequireTestCase(TDDatabase_PutAttachment);
RequireTestCase(TDDatabase_ReplicatorSequences);
}
#endif //DEBUG