-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunit_tests.mjs
More file actions
112 lines (96 loc) · 5.66 KB
/
unit_tests.mjs
File metadata and controls
112 lines (96 loc) · 5.66 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
import { get } from 'http'
import { getClient, validateBook, findUsernameByEmail, addPendingUser, confirmRegistration, User, Book, checkCredentials, getUser, initDatabaseConnection, isUsernameAvailable, createUser, validateUserData } from './data_interface.mjs'
import assert from 'assert'
async function testUserFunctions() {
assert(!await checkCredentials("test", "password"))
assert(await isUsernameAvailable("test"))
assert(await findUsernameByEmail("test@example.com") === null)
await createUser("test", "test@example.com", "password")
assert(await checkCredentials("test", "password"))
assert(!await isUsernameAvailable("test"))
assert(await findUsernameByEmail("test@example.com") === "test")
await getUser("test").setPassword("password2")
assert(!await checkCredentials("test", "password"))
assert(await checkCredentials("test", "password2"))
await addPendingUser("test2", "test2@example.com", "password")
assert(!await isUsernameAvailable("test2"))
await confirmRegistration("test2")
await confirmRegistration("doesn't exist")
assert(!await isUsernameAvailable("test2"))
assert(!validateUserData("goodusername", "bademail", "password"))
assert(!validateUserData("goodusername", "bad@email", "password"))
assert(!validateUserData("goodusername", "bad.email", "password"))
assert(!validateUserData(undefined, undefined, undefined))
assert(!validateUserData("goodusername", "email@example.com", ""))
assert(!validateUserData("", "email@example.com", "password"))
assert(!validateUserData("username", "", "password"))
assert(validateUserData("username", "email@example.com", "password"))
console.log("User functions: passed")
}
async function testBookFunctions() {
let test = getUser("test")
let test2 = getUser("test2")
await test.addBook(new Book("title", "author", "publisher", "2021", "222-1234567890", "category"))
assert((await test.getBookCollection()).length === 1)
assert((await test2.getBookCollection()).length === 0)
assert((await test.searchBookCollection(new Book("title","","","","",""))).length === 1)
assert((await test.searchBookCollection(new Book("squirtle","","","","",""))).length === 0)
assert((await test.searchBookCollection(new Book("itl","","","","",""))).length === 1)
assert((await test.searchBookCollection(new Book("","aut","","","",""))).length === 1)
assert((await test.searchBookCollection(new Book("","","pub","","",""))).length === 1)
assert((await test.searchBookCollection(new Book("","","","2021","",""))).length === 1)
assert((await test.searchBookCollection(new Book("","","","","222-",""))).length === 1)
assert((await test.searchBookCollection(new Book("","","","","","ateg"))).length === 1)
assert((await test.searchBookCollection(new Book("","","","","","ATEG"))).length === 1)
let id = (await test.getBookCollection())[0].id
await test.editBook(id, new Book("title2", "author2", "publisher2", "2022", "222-1234567891", "category2"))
assert((await test.getBookCollection())[0].title === "title2")
assert((await test.getBookCollection())[0].author === "author2")
assert((await test.getBookCollection())[0].publisher === "publisher2")
assert((await test.getBookCollection())[0].year === 2022)
assert((await test.getBookCollection())[0].isbn === "222-1234567891")
assert((await test.getBookCollection())[0].category === "category2")
id = (await test.getBookCollection())[0].id
await test.removeById(id)
assert((await test.getBookCollection()).length === 0)
let valid = [
new Book("title", "author", "publisher", '2021', "222-1234567890", "category"),
new Book("title", "", "", '', "", ""),
new Book("title", "", "", '1002', "", "category"),
new Book("!@#$%", "_____", "*#**993", '6', "222-1234567890", "Cadd[][][]"),
]
let invalid = [
new Book(undefined, "", "", '', "", ""),
new Book("", "author", "publisher", "2021", "222-1234567890", "category"),
new Book("title", "author", "publisher", "yEER", "222-1234567890", "category"),
new Book("title", "author", "publisher", "2021", "sussy isbn", "category"),
new Book("title", "author", "publisher", "2021", "222_1234567890", "category"),
new Book("title", "author", "publisher", "2021", "2221234567890", "category"),
new Book("title", "author", "publisher", "2021", "222-123456789", "category"),
new Book("title".repeat(100), "author", "publisher", "2021", "222-123456789", "category"),
new Book("title", "author".repeat(100), "publisher", "2021", "222-123456789", "category"),
new Book("title", "author", "publisher".repeat(100), "2021", "222-123456789", "category"),
new Book("title", "author", "publisher", "2021", "222-123456789", "category".repeat(100)),
new Book("title", undefined, "publisher", "2021", "222-123456789", "category"),
new Book("title", "author", undefined, "2021", "222-123456789", "category"),
new Book("title", "author", "publisher", "2021", undefined, "category"),
new Book("title", "author", "publisher", "2021", "222-1234567890", undefined),
new Book("title", "author", "publisher", undefined, "222-1234567890", undefined),
]
for (let book of valid) {
assert(validateBook(book))
}
for (let book of invalid) {
assert(!validateBook(book))
}
console.log("Book functions: passed")
}
async function runTests() {
initDatabaseConnection()
await getClient().query('DELETE FROM Books;')
await getClient().query('DELETE FROM Users;')
await testUserFunctions()
await testBookFunctions()
console.log("finished tests!")
}
await runTests()