Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
Add tests for gridfs
Browse files Browse the repository at this point in the history
  • Loading branch information
paolobueno committed Nov 8, 2017
1 parent aeb2d44 commit 6a32ae9
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 11 deletions.
2 changes: 1 addition & 1 deletion cloud/filestore/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"clean": "del coverage_report src/**/*.js src/**/*.map test/**/*.js test/**/*.map",
"build": "tsc",
"start": "ts-node src/index.ts",
"test": "npm run clean && nyc mocha"
"test": "nyc mocha"
},
"nyc": {
"include": [
Expand Down
2 changes: 1 addition & 1 deletion cloud/filestore/src/impl/GridFsStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const connectAsync = Promise.promisify<mongo.Db, string>(MongoClient.connect);
export class GridFsStorage implements FileStorage {

public gridFileSystem: gridfs.Grid;
private fileSystemPromise: Promise<gridfs>;
private fileSystemPromise: Promise<gridfs.Grid>;

/**
* Creates instance of the GridFsStorage that will connect to specified mongo url
Expand Down
2 changes: 1 addition & 1 deletion cloud/filestore/src/services/FileService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export function writeStreamToFile(metadata: FileMetadata, stream: Stream): Promi
* @param fileName Name of the file to build a path for, usually the file's id
*/
export function buildFilePath(fileName: string, root: string = FILE_STORAGE_DIRECTORY) {
return path.join(FILE_STORAGE_DIRECTORY, fileName);
return path.join(root, fileName);
}

const diskStorageDefaultOptions = {
Expand Down
1 change: 1 addition & 0 deletions cloud/filestore/test/fixtures/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello world
48 changes: 40 additions & 8 deletions cloud/filestore/test/impl/GridFsStorage-spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,44 @@
import * as Promise from 'bluebird';
import { expect } from 'chai';
import * as chai from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import { readFileSync } from 'fs';
import * as mongo from 'mongodb';
import { MongoClient } from 'mongodb';
import * as path from 'path';
import { FileMetadata } from '../../src/file-api/FileMetadata';
import { GridFsStorage } from '../../src/impl/GridFsStorage';

const connectAsync = Promise.promisify<mongo.Db, string>(MongoClient.connect);

chai.use(chaiAsPromised);

function readStream(stream: NodeJS.ReadableStream): Promise<string> {
let data = '';
stream.setEncoding('utf8');
stream.on('data', chunk => data += chunk);
return new Promise(resolve => stream.on('end', () => resolve(data)));
}

describe('GridFsStorage', function() {
const db = new mongo.Db('testdb', new mongo.Server('127.0.0.1', 27017));
const mongoUrl = 'mongodb://127.0.0.1:27017/testdb';
const dbFactory = () => connectAsync(mongoUrl);
let db: mongo.Db;
const metadata: FileMetadata = {
id: 'test-file'
};
const fileLocation = path.resolve(__dirname, '../fixtures/test.txt');
const fileContents = readFileSync(fileLocation, {
encoding: 'utf8'
});

before(function() {
return dbFactory().then(database => db = database);
});
after(function() {
db.close();
});

describe('constructor', function() {
it('should accept a mongodb connection', function() {
const storage = new GridFsStorage(db);
Expand All @@ -19,12 +49,14 @@ describe('GridFsStorage', function() {
const storage = new GridFsStorage(mongoUrl);
});
});
describe('writeFile', function() {
it('should write a file to the storage', function() {
const storage = new GridFsStorage(db);
});
});
describe('readFile', function() {
it('should read back a file from the storage');

it('should write and read back a file from storage', function() {
const storage = new GridFsStorage(db);
const write = storage.writeFile(metadata, fileLocation);
const read = write
.then(() => storage.readFile(metadata.id))
.then(readStream);
return expect(write).to.eventually.be.fulfilled &&
expect(read).to.eventually.equal(fileContents);
});
});

0 comments on commit 6a32ae9

Please sign in to comment.