Skip to content

Commit f8da520

Browse files
committed
test: implement File, Image, Audio, Video, Document write specs
1 parent 6f146f0 commit f8da520

File tree

3 files changed

+141
-12
lines changed

3 files changed

+141
-12
lines changed

package-lock.json

+16-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+7-8
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"build": "rollup -c",
1111
"lint": "eslint --fix --ext .js src/ test/ rollup.config.js",
1212
"pretest": "npm run lint",
13-
"test": "NODE_ENV=test mocha --require @babel/register test/**/*.spec.js",
13+
"test": "NODE_ENV=test mocha --timeout 8000 --require @babel/register test/**/*.spec.js",
1414
"test:unit": "npm run pretest && NODE_ENV=test mocha --require @babel/register test/unit/**/*.spec.js",
1515
"test:integration": "npm run pretest && NODE_ENV=test mocha --require @babel/register test/integration/**/*.spec.js",
1616
"docs": "doxdox 'lib/**/*.js' -p package.json -l markdown -o DOCUMENTATION.md",
@@ -46,13 +46,11 @@
4646
"email": "lallyelias87@gmail.com",
4747
"url": "https://github.com/lykmapipo"
4848
},
49-
"contributors": [
50-
{
51-
"name": "lykmapipo",
52-
"email": "lallyelias87@gmail.com",
53-
"url": "https://github.com/lykmapipo"
54-
}
55-
],
49+
"contributors": [{
50+
"name": "lykmapipo",
51+
"email": "lallyelias87@gmail.com",
52+
"url": "https://github.com/lykmapipo"
53+
}],
5654
"license": "MIT",
5755
"bugs": {
5856
"url": "https://github.com/lykmapipo/file/issues",
@@ -103,6 +101,7 @@
103101
"@lykmapipo/mongoose-common": ">=0.23.4",
104102
"async": ">=3.0.1",
105103
"lodash": ">=4.17.11",
104+
"mime": ">=2.4.3",
106105
"mongoose-geojson-schemas": ">=0.10.5",
107106
"mongoose-gridfs": ">=1.0.6",
108107
"mongoose-rest-actions": ">=0.28.1"

test/file.model.spec.js

+118-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1+
import { join as joinPath } from 'path';
2+
import { createReadStream } from 'fs';
3+
import { getType as mimeTypeOf } from 'mime';
14
import { ObjectId } from '@lykmapipo/mongoose-common';
25
import { expect } from '@lykmapipo/mongoose-test-helpers';
36
import { Buckets, FileTypes, createModels } from '../src/file.model';
47

5-
describe('Common', () => {
8+
const readStreamFor = filename => {
9+
return createReadStream(joinPath(__dirname, 'fixtures', filename));
10+
};
11+
12+
describe('File', () => {
613
it('should expose bucket definitions', () => {
714
expect(Buckets).to.exist;
815

@@ -91,4 +98,114 @@ describe('Common', () => {
9198
expect(Document.modelName).to.be.equal('Document');
9299
expect(Document.collection.name).to.be.equal('documents.files');
93100
});
101+
102+
it('should write file to File bucket', done => {
103+
const filename = 'file.txt';
104+
const contentType = mimeTypeOf('.txt');
105+
const options = { filename, contentType };
106+
const readStream = readStreamFor(filename);
107+
108+
const { File } = createModels();
109+
110+
File.write(options, readStream, (error, file) => {
111+
expect(error).to.not.exist;
112+
expect(file).to.exist;
113+
expect(file._id).to.exist;
114+
expect(file.filename).to.exist;
115+
expect(file.contentType).to.exist;
116+
expect(file.length).to.exist;
117+
expect(file.chunkSize).to.exist;
118+
expect(file.uploadDate).to.exist;
119+
expect(file.md5).to.exist;
120+
done(error, file);
121+
});
122+
});
123+
124+
it('should write image to Image bucket', done => {
125+
const filename = 'image.png';
126+
const contentType = mimeTypeOf('.png');
127+
const options = { filename, contentType };
128+
const readStream = readStreamFor(filename);
129+
130+
const { Image } = createModels();
131+
132+
Image.write(options, readStream, (error, image) => {
133+
expect(error).to.not.exist;
134+
expect(image).to.exist;
135+
expect(image._id).to.exist;
136+
expect(image.filename).to.exist;
137+
expect(image.contentType).to.exist;
138+
expect(image.length).to.exist;
139+
expect(image.chunkSize).to.exist;
140+
expect(image.uploadDate).to.exist;
141+
expect(image.md5).to.exist;
142+
done(error, image);
143+
});
144+
});
145+
146+
it('should write audio to Audio bucket', done => {
147+
const filename = 'audio.mp3';
148+
const contentType = mimeTypeOf('.mp3');
149+
const options = { filename, contentType };
150+
const readStream = readStreamFor(filename);
151+
152+
const { Audio } = createModels();
153+
154+
Audio.write(options, readStream, (error, audio) => {
155+
expect(error).to.not.exist;
156+
expect(audio).to.exist;
157+
expect(audio._id).to.exist;
158+
expect(audio.filename).to.exist;
159+
expect(audio.contentType).to.exist;
160+
expect(audio.length).to.exist;
161+
expect(audio.chunkSize).to.exist;
162+
expect(audio.uploadDate).to.exist;
163+
expect(audio.md5).to.exist;
164+
done(error, audio);
165+
});
166+
});
167+
168+
it('should write video to Video bucket', done => {
169+
const filename = 'video.mp4';
170+
const contentType = mimeTypeOf('.mp4');
171+
const options = { filename, contentType };
172+
const readStream = readStreamFor(filename);
173+
174+
const { Video } = createModels();
175+
176+
Video.write(options, readStream, (error, video) => {
177+
expect(error).to.not.exist;
178+
expect(video).to.exist;
179+
expect(video._id).to.exist;
180+
expect(video.filename).to.exist;
181+
expect(video.contentType).to.exist;
182+
expect(video.length).to.exist;
183+
expect(video.chunkSize).to.exist;
184+
expect(video.uploadDate).to.exist;
185+
expect(video.md5).to.exist;
186+
done(error, video);
187+
});
188+
});
189+
190+
it('should write document to Document bucket', done => {
191+
const filename = 'document.doc';
192+
const contentType = mimeTypeOf('.doc');
193+
const options = { filename, contentType };
194+
const readStream = readStreamFor(filename);
195+
196+
const { Document } = createModels();
197+
198+
Document.write(options, readStream, (error, doc) => {
199+
expect(error).to.not.exist;
200+
expect(doc).to.exist;
201+
expect(doc._id).to.exist;
202+
expect(doc.filename).to.exist;
203+
expect(doc.contentType).to.exist;
204+
expect(doc.length).to.exist;
205+
expect(doc.chunkSize).to.exist;
206+
expect(doc.uploadDate).to.exist;
207+
expect(doc.md5).to.exist;
208+
done(error, doc);
209+
});
210+
});
94211
});

0 commit comments

Comments
 (0)