-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathdatasource.test.js
138 lines (112 loc) · 3.78 KB
/
datasource.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
import { MongoClient, ObjectId } from 'mongodb'
import mongoose, { Schema, model } from 'mongoose'
import { MongoDataSource } from '../datasource'
import { isModel, isCollectionOrModel, getCollection } from '../helpers'
mongoose.set('useFindAndModify', false)
class Users extends MongoDataSource {
initialize(config) {
super.initialize(config)
}
}
describe('MongoDataSource', () => {
it('sets up caching functions', () => {
const users = {}
const source = new Users(users)
source.initialize()
expect(source.findOneById).toBeDefined()
expect(source.findByFields).toBeDefined()
expect(source.deleteFromCacheById).toBeDefined()
expect(source.deleteFromCacheByFields).toBeDefined()
expect(source.collection).toEqual(users)
})
})
const URL = 'mongodb://localhost:27017/test-apollo-datasource'
const connectArgs = [
URL,
{
useNewUrlParser: true,
useUnifiedTopology: true
}
]
const connect = async () => {
const client = new MongoClient(...connectArgs)
await mongoose.connect(...connectArgs)
await client.connect()
return client.db()
}
const hexId = '5cf82e14a220a607eb64a7d4'
const objectID = ObjectId(hexId)
describe('Mongoose', () => {
let UserModel
let userCollection
let alice
let nestedBob
beforeAll(async () => {
const userSchema = new Schema({ name: 'string' })
UserModel = model('User', userSchema)
const db = await connect()
userCollection = db.collection('users')
alice = await UserModel.findOneAndUpdate(
{ name: 'Alice' },
{ name: 'Alice' },
{ upsert: true, new: true }
)
nestedBob = await userCollection.findOneAndReplace(
{ name: 'Bob' },
{ name: 'Bob', nested: { _id: objectID, field1: 'value1', field2: '' } },
{ new: true, upsert: true }
)
})
test('isCollectionOrModel', () => {
expect(isCollectionOrModel(userCollection)).toBe(true)
expect(isCollectionOrModel(UserModel)).toBe(true)
expect(isCollectionOrModel(Function.prototype)).toBe(false)
expect(isCollectionOrModel(undefined)).toBe(false)
})
test('isModel', () => {
expect(isModel(userCollection)).toBe(false)
expect(isModel(UserModel)).toBe(true)
expect(isCollectionOrModel(Function.prototype)).toBe(false)
expect(isCollectionOrModel(undefined)).toBe(false)
})
test('mongoose class-based components', () => {
/**
* @see https://github.com/GraphQLGuide/apollo-datasource-mongodb/issues/51
*/
const ClassModel = mongoose.model(
class ClassModel extends mongoose.Model {},
new Schema({ name: 'string' })
)
expect(isModel(ClassModel)).toBe(true)
expect(isCollectionOrModel(ClassModel)).toBe(true)
})
test('getCollectionName', () => {
expect(getCollection(userCollection).collectionName).toBe('users')
expect(getCollection(UserModel).collectionName).toBe('users')
})
test('Data Source with Model', async () => {
const users = new Users(UserModel)
users.initialize()
const user = await users.findOneById(alice._id)
expect(user.name).toBe('Alice')
expect(user.id).toBe(alice._id.toString())
})
test('Data Source with Collection', async () => {
const users = new Users(userCollection)
users.initialize()
const user = await users.findOneById(alice._id)
expect(user.name).toBe('Alice')
expect(user.id).toBeUndefined()
})
test('nested findByFields', async () => {
const users = new Users(userCollection)
users.initialize()
const [user] = await users.findByFields({ 'nested._id': objectID })
expect(user).toBeDefined()
expect(user.name).toBe('Bob')
const res1 = await users.findByFields({ 'nested.field1': 'value1' })
const res2 = await users.findByFields({ 'nested.field2': 'value1' })
expect(res1[0].name).toBe('Bob')
expect(res2[0]).toBeUndefined()
})
})