-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcouch_test.ts
250 lines (238 loc) · 5.97 KB
/
couch_test.ts
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
import { CouchClient } from "./couch.ts";
import {
assert,
assertEquals,
} from "./vendor/https/deno.land/std/testing/asserts.ts";
import open = Deno.open;
const { test } = Deno;
const kDbName = "testdb";
const endpoint = Deno.env.get("COUCHDB_ENDPOINT") || "http://127.0.0.1:5984";
const client = new CouchClient(endpoint);
const db = client.database<any>(kDbName);
if (await client.databaseExists(kDbName)) {
await client.deleteDatabase(kDbName);
}
await client.createDatabase(kDbName);
async function useDatabase(f: (db: string) => Promise<unknown>) {
const name = "testdb-" + Math.round(Math.random() * 10000000);
return client
.databaseExists(name)
.then((ok) => (ok ? null : client.createDatabase(name)))
.then((_) => f(name))
.finally(async () => {
if (await client.databaseExists(name)) {
await client.deleteDatabase(name);
}
});
}
test({
name: "metadata",
fn: async () => {
const data = await client.metadata();
assertEquals(data.couchdb, "Welcome");
},
});
test({
name: "databaseExists",
fn: async () => {
const exists = await client.databaseExists("nodb");
assertEquals(exists, false);
},
});
test({
name: "createDatabase",
fn: async () => {
const db = "testdb1";
try {
assertEquals(await client.databaseExists(db), false);
const { ok } = await client.createDatabase(db);
assertEquals(ok, true);
} finally {
await client.deleteDatabase(db);
}
},
});
test({
name: "getDatabase",
fn: async function getDatabase() {
await useDatabase(async (db) => {
const info = await client.getDatabase(db);
assertEquals(info.db_name, db);
});
},
});
test({
name: "deleteDatabase",
fn: async function deleteDatabase() {
await useDatabase(async (db) => {
const { ok } = await client.deleteDatabase(db);
assertEquals(ok, true);
assertEquals(await client.databaseExists(db), false);
});
},
});
test({
name: "createDocument",
fn: async function createDocument() {
const obj = {
name: "deno",
nice: true,
};
await useDatabase(async (_) => {
const res = await db.insert(obj);
assertEquals(res.ok, true);
});
},
});
test({
name: "getDocument",
fn: async function getDocument() {
const obj = {
name: "deno",
nice: true,
years: [2018, 2019],
};
await useDatabase(async (_) => {
const res = await db.insert(obj);
assertEquals(res.ok, true);
const doc = await db.get(res.id);
assertEquals(doc["_id"], res.id);
assertEquals(doc["name"], "deno");
assertEquals(doc["nice"], true);
assertEquals(doc["years"], [2018, 2019]);
});
},
});
test({
name: "putDocument",
fn: async function putDocument() {
const doc = {
name: "deno",
nice: true,
years: [2018, 2019],
};
const _id = "denode";
const { id, rev, ok } = await db.put(_id, doc);
assertEquals(id, _id);
assertEquals(ok, true);
const doc2 = {
name: "node",
nice: true,
years: [2009, 2019],
};
const res = await db.put(id, doc2, { rev });
assertEquals(res.id, id);
const _doc = await db.get(id);
assertEquals(_doc["name"], "node");
assertEquals(_doc["nice"], true);
assertEquals(_doc["years"], [2009, 2019]);
},
});
test({
name: "documentInfo",
fn: async function documentInfo() {
const { id } = await db.insert({ name: "deno" });
const info = await db.info(id);
assert(info !== void 0, "info must be defined");
assertEquals(await db.info("xxx"), void 0);
},
});
test({
name: "deleteDocument",
fn: async function deleteDocument() {
const doc = {
name: "deno",
};
const { id, rev } = await db.insert(doc);
const res = await db.delete(id, rev);
assertEquals(res.id, id);
assertEquals(await db.info(id), void 0);
},
});
test({
name: "copyDocument",
fn: async function copyDocument() {
await db.put("deno", {
myNameIs: "deno",
});
await db.copy("deno", "node");
const o = await db.get("node");
assertEquals(o["myNameIs"], "deno");
},
});
test({
name: "findDocument",
fn: async function findDocument() {
await Promise.all([
db.insert({
id: 100,
name: "deno",
}),
db.insert({
id: 101,
name: "node",
}),
]);
const res = await db.find<any>({
id: 100,
});
assertEquals(res.docs.length, 1);
assertEquals(res.docs[0]["id"], 100);
assertEquals(res.docs[0]["name"], "deno");
},
});
test({
name: "putAttachment",
fn: async function putAttachment() {
const { id, rev } = await db.insert({
name: "couch.ts",
});
const data = await open("./fixtures/sample.json");
const res = await db.putAttachment(id, "fixtures/sample.json", {
contentType: "application/json",
data,
rev,
});
assertEquals(res.ok, true);
data.close();
},
});
test({
name: "getAttachment",
fn: async function getAttachment() {
const { id, rev } = await db.insert({
name: "couch.ts",
});
const data = await open("./fixtures/sample.json");
await db.putAttachment(id, "fixtures/sample.json", {
contentType: "application/json",
data,
rev,
});
const attach = await db.getAttachment(id, "fixtures/sample.json");
const json = new TextDecoder().decode(attach);
const content = JSON.parse(json);
assertEquals(content["deno"], "land");
data.close();
},
});
test({
name: "deleteAttachment",
fn: async function deleteAttachment() {
const { id, rev } = await db.insert({
name: "couch.ts",
});
const data = await open("./fixtures/sample.json");
const at = await db.putAttachment(id, "fixtures/sample.json", {
contentType: "application/json",
data,
rev,
});
await db.deleteAttachment(id, "fixtures/sample.json", at.rev);
const res = await db.attachmentInfo(id, "fixtures/sample.json", {
rev,
});
assertEquals(res, void 0);
data.close();
},
});