|
| 1 | +import { join as joinPath } from 'path'; |
| 2 | +import { createReadStream } from 'fs'; |
| 3 | +import { getType as mimeTypeOf } from 'mime'; |
1 | 4 | import { ObjectId } from '@lykmapipo/mongoose-common';
|
2 | 5 | import { expect } from '@lykmapipo/mongoose-test-helpers';
|
3 | 6 | import { Buckets, FileTypes, createModels } from '../src/file.model';
|
4 | 7 |
|
5 |
| -describe('Common', () => { |
| 8 | +const readStreamFor = filename => { |
| 9 | + return createReadStream(joinPath(__dirname, 'fixtures', filename)); |
| 10 | +}; |
| 11 | + |
| 12 | +describe('File', () => { |
6 | 13 | it('should expose bucket definitions', () => {
|
7 | 14 | expect(Buckets).to.exist;
|
8 | 15 |
|
@@ -91,4 +98,114 @@ describe('Common', () => {
|
91 | 98 | expect(Document.modelName).to.be.equal('Document');
|
92 | 99 | expect(Document.collection.name).to.be.equal('documents.files');
|
93 | 100 | });
|
| 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 | + }); |
94 | 211 | });
|
0 commit comments