|
| 1 | +var MongoClient = require("mongodb").MongoClient; |
| 2 | +var GridStore = require("mongodb").GridStore; |
| 3 | + |
| 4 | +var GridStoreAdapter = require("../src/Adapters/Files/GridStoreAdapter").GridStoreAdapter; |
| 5 | +var Config = require("../src/Config"); |
| 6 | +var FilesController = require('../src/Controllers/FilesController').default; |
| 7 | + |
| 8 | + |
| 9 | +// Small additional tests to improve overall coverage |
| 10 | +describe("GridStoreAdapter",() =>{ |
| 11 | + it("should properly instanciate the GridStore when deleting a file", (done) => { |
| 12 | + |
| 13 | + var databaseURI = 'mongodb://localhost:27017/parse'; |
| 14 | + var config = new Config(Parse.applicationId); |
| 15 | + var gridStoreAdapter = new GridStoreAdapter(databaseURI); |
| 16 | + var filesController = new FilesController(gridStoreAdapter); |
| 17 | + |
| 18 | + // save original unlink before redefinition |
| 19 | + var originalUnlink = GridStore.prototype.unlink; |
| 20 | + |
| 21 | + var gridStoreMode; |
| 22 | + |
| 23 | + // new unlink method that will capture the mode in which GridStore was opened |
| 24 | + GridStore.prototype.unlink = function() { |
| 25 | + |
| 26 | + // restore original unlink during first call |
| 27 | + GridStore.prototype.unlink = originalUnlink; |
| 28 | + |
| 29 | + gridStoreMode = this.mode; |
| 30 | + |
| 31 | + return originalUnlink.call(this); |
| 32 | + }; |
| 33 | + |
| 34 | + |
| 35 | + filesController.createFile(config, 'myFilename.txt', 'my file content', 'text/plain') |
| 36 | + .then(myFile => { |
| 37 | + |
| 38 | + return MongoClient.connect(databaseURI) |
| 39 | + .then(database => { |
| 40 | + |
| 41 | + // Verify the existance of the fs.files document |
| 42 | + return database.collection('fs.files').count().then(count => { |
| 43 | + expect(count).toEqual(1); |
| 44 | + return database; |
| 45 | + }); |
| 46 | + }) |
| 47 | + .then(database => { |
| 48 | + |
| 49 | + // Verify the existance of the fs.files document |
| 50 | + return database.collection('fs.chunks').count().then(count => { |
| 51 | + expect(count).toEqual(1); |
| 52 | + return database.close(); |
| 53 | + }); |
| 54 | + }) |
| 55 | + .then(() => { |
| 56 | + return filesController.deleteFile(config, myFile.name); |
| 57 | + }); |
| 58 | + }) |
| 59 | + .then(() => { |
| 60 | + return MongoClient.connect(databaseURI) |
| 61 | + .then(database => { |
| 62 | + |
| 63 | + // Verify the existance of the fs.files document |
| 64 | + return database.collection('fs.files').count().then(count => { |
| 65 | + expect(count).toEqual(0); |
| 66 | + return database; |
| 67 | + }); |
| 68 | + }) |
| 69 | + .then(database => { |
| 70 | + |
| 71 | + // Verify the existance of the fs.files document |
| 72 | + return database.collection('fs.chunks').count().then(count => { |
| 73 | + expect(count).toEqual(0); |
| 74 | + return database.close(); |
| 75 | + }); |
| 76 | + }); |
| 77 | + }) |
| 78 | + .then(() => { |
| 79 | + // Verify that gridStore was opened in read only mode |
| 80 | + expect(gridStoreMode).toEqual('r'); |
| 81 | + |
| 82 | + done(); |
| 83 | + }) |
| 84 | + .catch(fail); |
| 85 | + |
| 86 | + }) |
| 87 | +}); |
0 commit comments