This repository has been archived by the owner on Dec 12, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadapter_test.js
170 lines (149 loc) · 4.37 KB
/
adapter_test.js
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
// deno-lint-ignore-file no-unused-vars
import { adapter } from "./adapter.js";
import { assertEquals } from "./dev_deps.js";
function Datastore(config) {
return Object.freeze({
insert: (doc) => Promise.resolve(doc),
findOne: ({ _id }) => {
if (_id === "1") {
return Promise.resolve({ _id: "1", hello: "world" });
} else if (_id === "movie-4") {
return Promise.resolve({
_id: "movie-4",
type: "movie",
title: "what about bob?",
});
} else {
return Promise.resolve(null);
}
},
updateOne: (criteria, action) => {
return Promise.resolve(action.$set);
},
removeOne: (o) => Promise.resolve(o),
find: () =>
Promise.resolve([
{ _id: "movie-1", type: "movie", title: "ghostbusters" },
{ _id: "movie-2", type: "movie", title: "great outdoors" },
{ _id: "movie-3", type: "movie", title: "groundhog day" },
{ _id: "movie-4", type: "movie", title: "what about bob?" },
{ _id: "movie-5", type: "movie", title: "spaceballs" },
]),
update: (criteria, action) => Promise.resolve(action.$set),
remove: (critera) => Promise.resolve({ ok: true }),
});
}
const test = Deno.test;
const a = adapter({ filename: "./test.db" }, Datastore);
test("create database", async () => {
const result = await a.createDatabase("foo");
console.log(result);
assertEquals(result.ok, true);
});
test("remove database", async () => {
// Error will throw if db file is not found, we want to return true
// because the fact it is not found means that it does not exist, therefore removed.
const result = await a.removeDatabase("foo").catch((_) => ({ ok: true }));
assertEquals(result.ok, true);
});
test("create document", async () => {
const result = await a.createDocument({
db: "foo",
id: "2",
doc: { hello: "world" },
});
assertEquals(result.ok, true);
});
test("retrieve document", async () => {
const result = await a.retrieveDocument({
db: "foo",
id: "1",
});
assertEquals(result._id, "1");
});
test("update document", async () => {
const result = await a.updateDocument({
db: "foo",
id: "1",
doc: { _id: "1", hello: "moon" },
});
assertEquals(result.ok, true);
});
test("remove document", async () => {
const result = await a.removeDocument({
db: "foo",
id: "1",
});
assertEquals(result.ok, true);
});
test("list documents", async () => {
const result = await a.listDocuments({ db: "foo" });
assertEquals(result.ok, true);
});
test("query documents", async () => {
await a.createDocument({
db: "foo",
id: "movie-1",
doc: { _id: "movie-1", type: "movie", title: "Great Outdoors" },
});
const result = await a.queryDocuments({
db: "foo",
query: {
selector: { type: "movie" },
},
});
assertEquals(result.ok, true);
});
test("query documents - with fields", async () => {
await a.createDocument({
db: "foo",
id: "movie-1",
doc: { _id: "movie-1", type: "movie", title: "Great Outdoors" },
});
const result = await a.queryDocuments({
db: "foo",
query: {
selector: { type: "movie" },
fields: ["title"],
},
});
assertEquals(result.ok, true);
assertEquals(result.docs[0].title, "ghostbusters");
});
test("query documents - with sort", async () => {
await a.createDocument({
db: "foo",
id: "movie-1",
doc: { _id: "movie-1", type: "movie", title: "Great Outdoors" },
});
const result = await a.queryDocuments({
db: "foo",
query: {
selector: { type: "movie" },
sort: [{ type: "ASC" }, { title: "DESC" }],
},
});
assertEquals(result.ok, true);
assertEquals(result.docs[0].title, "what about bob?");
});
test("index documents", async () => {
const result = await a.indexDocuments({
db: "foo",
name: "fooIndex",
fields: ["type"],
});
assertEquals(result.ok, true);
});
test("bulk update/insert/remove documents", async () => {
const result = await a.bulkDocuments({
db: "foo",
docs: [
{ _id: "movie-1", type: "movie", title: "ghostbusters", _deleted: true },
{ _id: "movie-2", type: "movie", title: "great outdoors" },
{ _id: "movie-3", type: "movie", title: "groundhog day" },
{ _id: "movie-4", type: "movie", title: "what about bob?" },
{ _id: "movie-5", type: "movie", title: "spaceballs" },
],
});
assertEquals(result.ok, true);
});