-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.test.js
More file actions
210 lines (166 loc) · 4.92 KB
/
index.test.js
File metadata and controls
210 lines (166 loc) · 4.92 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
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
import dotenv from 'dotenv'
import { randomBytes } from 'crypto'
import ormjs from './index'
import moment from 'moment'
import { isEqual, sortBy, isEmpty } from 'lodash'
dotenv.config()
const { NAME } = process.env
const username = `test${randomBytes(8).toString('hex')}`
const db = ormjs.connect({
database: NAME || 'test'
})
class User extends db.Model {
static schema = {
name: { $type: String, index: true, unique: true },
profile: {
vegan: { $type: Boolean, optional: true }
}
}
}
class Post extends db.Model {
static schema = {
body: { $type: String, optional: true },
creator: { $type: User, optional: false }
}
}
class Likes extends db.Edge {}
test('initialize ormjs', () => {
expect(db).toHaveProperty('Edge')
expect(db).toHaveProperty('Model')
})
test('create new model instance', async () => {
const newUser = await User.add({ name: username })
expect(newUser._id).toBeTruthy()
})
test('check name must be unique', async () => {
try {
await User.add({ name: username })
} catch (error) {
expect(error)
.not.toHaveProperty('_id')
}
})
test('create test post', async () => {
const user = await User.findOne({ where: { name: username } })
const post = await Post.add({ body: username, creator: user })
expect(post.body === username).toBeTruthy()
expect(post.body.length > 4).toBeTruthy()
})
test('create edge collection relationship', async () => {
const user = await User.findOne({ where: { name: username } })
const post = await Post.findOne({ where: { body: username } })
const like = await Likes.add(user, post)
expect(like._from).toBe(user._id)
})
test('find collections by example', async () => {
const user = await User.findOne({ where: { name: username } })
expect(user).toHaveProperty('_key')
})
test('find edge collections by example', async () => {
const user = await User.findOne({ where: { name: username } })
const edge = await Likes.findOne({ where: { _from: user._id } })
expect(edge._from).toBe(user._id)
})
test('find only certain attributes', async () => {
const user = await User.findOne({
where: { name: username },
attributes: ['_id', 'createdAt']
})
expect(user.name).toBe(undefined)
expect(user._id).not.toBe(undefined)
})
test('make sure all documents have createdAt field', async () => {
const user = await User.findOne({
where: { name: username }
})
expect(user).toHaveProperty('createdAt')
expect(moment(user.createdAt).isValid()).toBe(true)
})
test('make sure all documents have updatedAt field', async () => {
const post = await Post.findOne({
body: username
})
post.body = 'Updated!'
await post.save()
expect(post).toHaveProperty('updatedAt')
expect(moment(post.updatedAt).isValid()).toBe(true)
})
test('use aql query', async () => {
const { aql, _database } = await db.Model
const cursor = await _database.query(aql`for u in User limit 10 return u`)
const users = await cursor.all()
expect(users.length).toBe(10)
})
test('check list is sorted', async () => {
const posts = await Post.find({
sort: 'createdAt asc',
limit: 100
})
const sorted = await sortBy(posts, ['createdAt'])
expect(isEqual(posts, sorted)).toBe(true)
expect(isEqual(posts, sorted.reverse())).not.toBe(true)
})
test('find with include', async () => {
const posts = await Post.find({
include: {
model: User,
as: 'creator'
}
})
expect(posts[0]).toHaveProperty('creator')
expect(posts[0]).toHaveProperty('_key')
expect(posts[0]).toHaveProperty('body')
})
test('find and count include', async () => {
const included = await Post.findAndCount({
include: {
model: User,
as: 'creator'
}
})
const { data: posts, meta: { count } } = included
expect(posts[0]).toHaveProperty('creator')
expect(posts[0]).toHaveProperty('_key')
expect(posts[0]).toHaveProperty('body')
expect(typeof count === 'number').toBeTruthy()
})
// test('remove item', async () => {
// const userToRemove = await User.findOne({
// where: { name: username }
// })
// await userToRemove.remove()
// const user = await User.findOne({
// where: { name: username }
// })
// expect(user).toBe(null)
// })
test('do not allow injection', async () => {
const users = await User.find({
where: { name: `123" || user._id == 'User/4627980' || user.name == "hi` },
limit: 100
})
expect(isEmpty(users)).toBe(true)
// expect(statuses.includes(true) !== true).toBe(true)
})
test('find by include where', async () => {
const post = await Post.findOne({
include: {
as: 'creator',
where: {name: username}
}
})
expect(post.creator.name).toBe(username)
})
test('complex query', async () => {
const posts = await Post.findAndCount({
limit: 5,
skip: 0,
sort: 'createdAt DESC',
attributes: ['creator', 'createdAt'],
include: {
as: 'creator',
where: {name: username}
}
})
expect(posts.data[0].creator.name).toBe(username)
})