aheckmann / gridfs-stream

Easily stream files to and from MongoDB
MIT License
615 stars 120 forks source link

GridFS Unit testing with Mockgoose #136

Open bolencki13 opened 6 years ago

bolencki13 commented 6 years ago

Info

gridfs-stream ^1.1.1 mockgoose ^7.3.5 nodejs v8.11.2

Description

I am trying to write some unit tests that incorporate gridfs-stream and mockgoose. I have the database mocked out and the connection to gridfs set up as follows:

const mongoose = require('mongoose')
const Mockgoose = require('mockgoose').Mockgoose
const mockgoose = new Mockgoose(mongoose)

mockgoose.prepareStorage().then(function () {
    mongoose.connect(`mongodb://${process.env.DB_ADDRESS}/${process.env.DB_NAME}`)
})

const gridfs = require('gridfs-stream')(mockgoose.mongooseObj.connections[0], database.mongooseObj.mongo)

When inserting a file into the database by use of a stream:

let options = {
    mode: 'w',
    content_type: 'application/pdf',
    root: 'files',
    filename: 'test-file.pdf'
}
let wStream = gridfs.createWriteStream(options)

await (new Promise((resolve, reject) => {
    fs.createReadStream('./test/supportFiles/test.pdf')
        .on('error', (err) => {
            reject(err)
        })
        .on('end', () => {
            resolve()
        })
        .pipe(wStream)
}))

Content is found from the file with the rStream and the data has a length

.on('data', (data) => {
    // data has a length there it is a buffer returned
})

Problem

When attempting to retrieve the file from the database by use of the id of the file inserted, I am receiving an error that file with id 5bb62062ac13503388572732 not opened for writing. The file is retrieved from the database however the retrieved file has no length.

Error
GridReadStream {
  _readableState: 
   ReadableState {
     objectMode: false,
     highWaterMark: 16384,
     buffer: BufferList { head: null, tail: null, length: 0 },
     length: 0,
     pipes: null,
     pipesCount: 0,
     flowing: true,
     ended: false,
     endEmitted: false,
     reading: false,
     sync: true,
     needReadable: false,
     emittedReadable: false,
     readableListening: false,
     resumeScheduled: true,
     destroyed: false,
     defaultEncoding: 'utf8',
     awaitDrain: 0,
     readingMore: false,
     decoder: null,
     encoding: null },
  readable: true,
  domain: null,
  _events: { end: [Function], error: [Function], data: [Function] },
  _eventsCount: 3,
  _maxListeners: undefined,
  _opened: false,
  _opening: false,
  _closing: false,
  _end: false,
  _needToPush: false,
  _grid: 
   Grid {
     db: 
      NativeConnection {
        base: [Object],
        collections: [Object],
        models: [Object],
        config: [Object],
        replica: false,
        hosts: null,
        host: 'localhost',
        port: 27017,
        user: undefined,
        pass: undefined,
        name: 'mockgoose-temp-db-49d8417a-9bf0-43c4-b29e-87d820cfd437',
        options: [Object],
        otherDbs: [],
        states: [Object],
        _readyState: 1,
        _closeCalled: false,
        _hasOpened: true,
        _listening: false,
        _events: [Object],
        _eventsCount: 1,
        db: [Object] },
     mongo: 
      { [Function]
        MongoError: [Object],
        Admin: [Object],
        MongoClient: [Object],
        Db: [Object],
        Collection: [Object],
        Server: [Object],
        ReplSet: [Object],
        Mongos: [Object],
        ReadPreference: [Object],
        GridStore: [Object],
        Chunk: [Object],
        Logger: [Object],
        Cursor: [Object],
        GridFSBucket: [Object],
        CoreServer: [Object],
        CoreConnection: [Object],
        Binary: [Object],
        Code: [Object],
        Map: [Object],
        DBRef: [Object],
        Double: [Object],
        Int32: [Object],
        Long: [Object],
        MinKey: [Object],
        MaxKey: [Object],
        ObjectID: [Object],
        ObjectId: [Object],
        Symbol: [Object],
        Timestamp: [Object],
        BSONRegExp: [Object],
        Decimal128: [Object],
        connect: [Circular],
        instrument: [Function] },
     curCol: 'fs' },
  options: { _id: 5bb62062ac13503388572732, mode: 'r', root: 'files' },
  id: 5bb62062ac13503388572732,
  name: '',
  mode: 'r',
  _chunkSize: 261120,
  range: { startPos: 0, endPos: undefined },
  _currentPos: 0,
  _store: 
   GridStore {
     db: 
      NativeConnection {
        base: [Object],
        collections: [Object],
        models: [Object],
        config: [Object],
        replica: false,
        hosts: null,
        host: 'localhost',
        port: 27017,
        user: undefined,
        pass: undefined,
        name: 'mockgoose-temp-db-49d8417a-9bf0-43c4-b29e-87d820cfd437',
        options: [Object],
        otherDbs: [],
        states: [Object],
        _readyState: 1,
        _closeCalled: false,
        _hasOpened: true,
        _listening: false,
        _events: [Object],
        _eventsCount: 1,
        db: [Object] },
     referenceBy: 1,
     fileId: 5bb62062ac13503388572732,
     filename: '',
     mode: 'r',
     options: { _id: 5bb62062ac13503388572732, mode: 'r', root: 'files' },
     isOpen: false,
     root: 'files',
     position: 0,
     readPreference: 'primary',
     writeConcern: { w: 1 },
     internalChunkSize: 261120,
     promiseLibrary: [Function: Promise],
     chunkSize: [Getter/Setter],
     md5: [Getter],
     chunkNumber: [Getter] } }

Questions

Is gridfs-stream compatible with mockgoose or any form of mocking mongoose/mongodb? Is my configuration of gridfs-stream and/or mockgoose correct for this usage? Any help would be appreciated thanks.

AsadGilani commented 4 years ago

@bolencki13 : Did you find the solution yet. I am having the same problem. I am using the gridfs for uploading the content using await _bucket.UploadFromStreamAsync(data.FileName, source, options); but dont have idea how we can mock the method. Tried so may things using moq nuget and fake lib to mock but no success yet. if you find the solution please share.