-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.js
128 lines (91 loc) · 3.57 KB
/
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
/* eslint-env mocha */
const fs = require('fs')
const mongoose = require('mongoose')
const { MongoMemoryServer } = require('mongodb-memory-server')
const { expect } = require('chai')
const MemoryStream = require('memorystream')
const { mongo } = mongoose
describe('Schema', () => {
let GridFile
let GridFileSchema
let connection
let mongodb
before(async () => {
// create mongo connection
mongodb = await MongoMemoryServer.create()
const connectionUri = await mongodb.getUri()
connection = await mongoose.connect(connectionUri)
GridFileSchema = require('./src/gridFile.schema')
GridFile = connection.model('GridFile', GridFileSchema)
})
after(async () => {
// close mongo connection
await mongoose.connection.close()
await mongodb.stop()
})
describe('GridFS Bucket', () => {
it('should create a GridFS Bucket', () => {
const bucket = GridFile.getBucket()
expect(bucket).instanceOf(mongo.GridFSBucket)
})
it('should create a GridFS Bucket with custom name', () => {
const CustomGridFile = connection.model('CustomGridFile', GridFileSchema, 'attachment.files')
const bucket = CustomGridFile.getBucket()
expect(bucket).instanceOf(mongo.GridFSBucket)
expect(bucket.s.options.bucketName).equals('attachment')
})
it('should throw error when collection name doesn\'t end with .files', () => {
try {
const CustomGridFile = connection.model('CustomGridFile1', GridFileSchema, 'attachment.files1')
const bucket = CustomGridFile.getBucket()
expect(bucket).equals(false)
} catch (error) {
expect(error.message).equals('Collection Name doesn\'t end with .files')
}
})
})
describe('GridFS Files', () => {
it('should upload a file to GridFS', async () => {
const file = new GridFile()
file.filename = 'package.json'
file.metadata = 'test'
file.aliases = ['test-alieas1', 'alias-2']
file.contentType = 'application/json'
const fileStream = fs.createReadStream('./package.json')
const uploadedFile = await file.upload(fileStream)
expect(uploadedFile).instanceOf(GridFile)
})
it('should find recently uploaded file by id', async () => {
const file = new GridFile()
file.filename = 'package.json'
const fileStream = fs.createReadStream('./package.json')
const uploadedFile = await file.upload(fileStream)
const foundFile = await GridFile.findById(uploadedFile.id)
expect(foundFile.md5).equals(uploadedFile.md5)
})
it('should download a file from GridFS', async () => {
const file = new GridFile()
file.filename = 'package.json'
const fileStream = fs.createReadStream('./package.json')
const uploadedFile = await file.upload(fileStream)
const foundFile = await GridFile.findById(uploadedFile.id)
// download file as in-memory stream
const stream = new MemoryStream()
await foundFile.download(stream)
expect(stream._readableState.length).equals(uploadedFile.length)
})
it('should delete a file from GridFS', async () => {
const file = new GridFile()
file.filename = 'package.json'
const fileStream = fs.createReadStream('./package.json')
const uploadedFile = await file.upload(fileStream)
const foundFile = await GridFile.findByIdAndDelete(uploadedFile.id)
expect(foundFile.md5).equals(uploadedFile.md5)
const search = await GridFile.findOne()
expect(search).to.equal(null)
})
afterEach(async () => {
await GridFile.deleteMany()
})
})
})