-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod_test.ts
More file actions
149 lines (127 loc) · 3.16 KB
/
Copy pathmod_test.ts
File metadata and controls
149 lines (127 loc) · 3.16 KB
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
import {
assertArrayIncludes,
assertEquals,
assertExists,
assertFalse,
assertGreater,
assertNotEquals,
} from "@std/assert";
import { Database } from "./mod.ts";
type Book = {
title: string;
author: string;
};
const Books: Book[] = [
{
title: "The Great Gatsby",
author: "F. Scott Fitzgerald",
},
{
title: "Moby",
author: "Herman Melville",
},
{
title: "War and Peace",
author: "Leo Tolstoy",
},
{
title: "The Catcher in the Rye",
author: "J.D. Salinger",
},
{
title: "To Kill a Mockingbird",
author: "Harper Lee",
},
];
const bookDb = new Database<Book, string>("books");
Deno.test("Seed data", () => {
bookDb.seed(Books);
const books = bookDb.findAll();
const bookWithoutId = books.map(({ _id, ...e }) => ({ ...e }));
assertEquals(bookWithoutId, Books);
});
Deno.test("Find all without filter", () => {
const books = bookDb.findAll();
assertEquals(books.length, 5);
});
Deno.test("Find all with filter", () => {
const books = bookDb.findAll({
title: "The Great Gatsby",
});
assertEquals(books.length, 1);
assertEquals(books[0].title, "The Great Gatsby");
});
Deno.test("Find one data", () => {
const book = bookDb.findOne({
title: "The Catcher in the Rye",
});
assertExists(book);
assertEquals(book.title, "The Catcher in the Rye");
});
Deno.test("Insert one data", () => {
bookDb.insertOne({
title: "1984",
author: "George Orwell",
});
const book = bookDb.findOne({ title: "1984" });
assertExists(book);
assertEquals(book.title, "1984");
});
Deno.test("Bulk insert data", () => {
bookDb.bulkInsert([
{
author: "J.R.R. Tolkien",
title: "The Hobbit",
},
{
author: "H.G. Wells",
title: "The War of the Worlds",
},
{
author: "Jules Verne",
title: "Twenty Thousand Leagues Under the Sea",
},
]);
const books = bookDb.findAll();
assertGreater(books.length, 6);
const book1 = bookDb.findOne({ author: "J.R.R. Tolkien" });
assertExists(book1);
assertArrayIncludes(books, [book1]);
const book2 = bookDb.findOne({ title: "The War of the Worlds" });
assertExists(book2);
assertArrayIncludes(books, [book2]);
const book3 = bookDb.findOne({
title: "Twenty Thousand Leagues Under the Sea",
});
assertExists(book3);
assertArrayIncludes(books, [book3]);
});
Deno.test("Update one data", () => {
const book = bookDb.findOne({ title: "The Catcher in the Rye" });
assertExists(book);
bookDb.updateOne({ title: "The Catcher in the Rye" }, {
title: "The Catcher in the Rye (Updated)",
});
const newBook = bookDb.findOne({
title: "The Catcher in the Rye (Updated)",
});
assertExists(newBook);
assertNotEquals(book.title, newBook.title);
});
Deno.test("Update all data", () => {
bookDb.updateAll({ author: "J.R.R. Tolkien" }, {
title: "The Lord of the Rings",
});
const books = bookDb.findAll();
const book = bookDb.findOne({ title: "The Lord of the Rings" });
assertExists(book);
assertArrayIncludes(books, [book]);
});
Deno.test("Delete data", () => {
const book = bookDb.findOne({ title: "The Catcher in the Rye (Updated)" });
assertExists(book);
bookDb.delete({ title: "The Catcher in the Rye (Updated)" });
assertFalse(
!!bookDb.findOne({ title: "The Catcher in the Rye (Updated)" }),
);
});