Skip to content

Commit

Permalink
Merge pull request #9 from hapinessjs/next
Browse files Browse the repository at this point in the history
release(version): v1.1.2
  • Loading branch information
akanass authored Feb 14, 2018
2 parents b1f8dee + e08749d commit c43acb0
Show file tree
Hide file tree
Showing 8 changed files with 732 additions and 297 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ class MyService {

## Change History

* v1.1.2 (2018-02-14)
* MingoModel now have an id exposed.
* Fix when calling the toJson method but value returned by mongo is null.
* v1.1.1 (2018-02-13)
* Fix returned object, now doesn't include mongoose metadata
* v1.1.0 (2018-01-30)
Expand Down
826 changes: 592 additions & 234 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hapiness/mingo",
"version": "1.1.1",
"version": "1.1.2",
"description": "Manage files using minio as file storage and mongodb for metadata",
"main": "commonjs/index.js",
"types": "index.d.ts",
Expand Down
1 change: 1 addition & 0 deletions src/module/interfaces/mingo-file.interface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export interface MingoFileInterface {
id?: string | any;
filename: string;
contentType: string;
created_at: Date;
Expand Down
9 changes: 5 additions & 4 deletions src/module/managers/files.manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export class FilesManager {
.createFile(input, filename, null, content_type)
.switchMap((result): Observable<MingoFileInterface> =>
Observable.of({
id: undefined,
filename: filename,
size: result.size,
contentType: result.contentType,
Expand All @@ -55,7 +56,7 @@ export class FilesManager {
this._getDocument()
.findOneAndUpdate({ filename }, _, { new: true, upsert: true })
)
.map<MingoFileDocumentInterface, MingoFileInterface>(file => file.toJSON())
.map<MingoFileDocumentInterface, MingoFileInterface>(file => file ? file.toJSON() : file)
);
}

Expand Down Expand Up @@ -116,7 +117,7 @@ export class FilesManager {
const projectionStr = projection && projection instanceof Array ? projection.join(' ') : projection;

return Observable.fromPromise(this._getDocument().find(query, projectionStr, _options))
.map<MingoFileDocumentInterface[], MingoFileInterface[]>(_ => _.map(__ => __.toJSON()));
.map<MingoFileDocumentInterface[], MingoFileInterface[]>(_ => _.map(__ => __ ? __.toJSON() : __));
}

/**
Expand All @@ -132,7 +133,7 @@ export class FilesManager {
const projectionStr = projection && projection instanceof Array ? projection.join(' ') : projection;

return Observable.fromPromise(this._getDocument().findOne({ filename }, projectionStr, options))
.map<MingoFileDocumentInterface, MingoFileInterface>(_ => _.toJSON());
.map<MingoFileDocumentInterface, MingoFileInterface>(_ => _ ? _.toJSON() : _);
}

/**
Expand Down Expand Up @@ -180,7 +181,7 @@ export class FilesManager {
{ new: true, upsert: false }
))
)
.map(_ => _.toJSON());
.map(_ => _ ? _.toJSON() : _);
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/module/models/mingo-file.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export class MingoFileModel extends Model {
}
},
{
id: false,
versionKey: false
}
);
Expand Down
3 changes: 2 additions & 1 deletion test/functional/mingo.module.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export class MingoModuleFunctionalTest {
@test('Mingo module load successfuly and run several commands')
mingoRunSuccess(done) {
const fileProperties = {
id: null,
filename: 'package.json',
contentType: 'json',
size: fs.lstatSync('./package.json').size,
Expand Down Expand Up @@ -55,7 +56,7 @@ export class MingoModuleFunctionalTest {
}

fb().create(fs.createReadStream('./package.json'), 'package.json', 'json', null)
.do(_ => Object.assign(fileProperties, { created_at: _.created_at, updated_at: _.updated_at }))
.do(_ => Object.assign(fileProperties, { id: _.id, created_at: _.created_at, updated_at: _.updated_at }))
.do(_ => unit
.object(_)
.is(fileProperties)
Expand Down
184 changes: 128 additions & 56 deletions test/unit/managers/file.manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ export class FilesManagerUnitTest {
unit.function(this._filesManager.exists);
unit.stub(this._bucketManager, 'fileStat').returns(Observable.of({}));
unit.stub(this._filesManager, 'findByFilename').returns(Observable.of({ filename }));
const obs = this._filesManager.exists(filename);
obs.subscribe(_ => {
unit.bool(_).isTrue();
done();
}, err => done(err));
this._filesManager.exists(filename)
.subscribe(_ => {
unit.bool(_).isTrue();
done();
}, err => done(err));
}

@test('- exists: no')
Expand All @@ -84,34 +84,55 @@ export class FilesManagerUnitTest {
unit.function(this._filesManager.exists);
unit.stub(this._bucketManager, 'fileStat').returns(Observable.of({}));
unit.stub(this._filesManager, 'findByFilename').returns(Observable.of(null));
const obs = this._filesManager.exists(filename);
obs.subscribe(_ => {
unit.bool(_).isFalse();
done();
}, err => done(err));
this._filesManager.exists(filename)
.subscribe(_ => {
unit.bool(_).isFalse();
done();
}, err => done(err));
}

@test('- exists: no 2')
existsNo2(done) {
const filename = 'helloworld.txt';
unit.function(this._filesManager.exists);
unit.stub(this._bucketManager, 'fileStat').returns(Observable.throw({ code: 'NotFound' }));
const obs = this._filesManager.exists(filename);
obs.subscribe(_ => {
unit.bool(_).isFalse();
done();
}, err => done(err));
this._filesManager.exists(filename)
.subscribe(_ => {
unit.bool(_).isFalse();
done();
}, err => done(err));
}

@test('- exists: filestat throw another error than "NotFound"')
existsNo3(done) {
const filename = 'helloworld.txt';
unit.function(this._filesManager.exists);
unit.stub(this._bucketManager, 'fileStat').returns(Observable.throw({ code: 'AnotherError' }));
const obs = this._filesManager.exists(filename);
obs.subscribe(_ => {
done(new Error('Should not be here!'));
}, err => done());
this._filesManager.exists(filename)
.subscribe(_ => {
done(new Error('Should not be here!'));
}, err => done());
}

@test('- when upload returns null')
uploadReturnsNull(done) {
const input = Buffer.from('helloworld');
const filename = 'helloworld.txt';
const contentType = 'text/plain';
const metadata = { lupulus: { stock: 99, empty: 10 } };
unit.stub(this._bucketManager, 'createFile').returns(Observable.of({
size: input.length,
contentType: contentType,
etag: new Date().getTime(),
lastModified: new Date(),
}));
this._modelMock.findOneAndUpdate = unit.stub().returns(Promise.resolve(null));
unit.function(this._filesManager.upload);
this._filesManager.upload(input, filename, contentType, metadata)
.subscribe(_ => {
unit.value(_).is(null);
done();
}, err => done(err));
}

@test('- Upload')
Expand Down Expand Up @@ -139,11 +160,11 @@ export class FilesManagerUnitTest {
toJSON: () => file
}));
unit.function(this._filesManager.upload);
const obs = this._filesManager.upload(input, filename, contentType, metadata);
obs.subscribe(_ => {
unit.object(_).is(file);
done();
}, err => done(err));
this._filesManager.upload(input, filename, contentType, metadata)
.subscribe(_ => {
unit.object(_).is(file);
done();
}, err => done(err));
}

@test('- Create')
Expand All @@ -162,11 +183,11 @@ export class FilesManagerUnitTest {
};
unit.stub(this._filesManager, 'exists').returns(Observable.of(false));
unit.stub(this._filesManager, 'upload').returns(Observable.of(file));
const obs = this._filesManager.create(input, filename);
obs.subscribe(_ => {
unit.object(_).is(file);
done();
}, err => done(err));
this._filesManager.create(input, filename)
.subscribe(_ => {
unit.object(_).is(file);
done();
}, err => done(err));
}

@test('- Create error: already exists')
Expand All @@ -175,14 +196,37 @@ export class FilesManagerUnitTest {
const input = Buffer.from('helloworld');
const filename = 'helloworld.txt';
unit.stub(this._filesManager, 'exists').returns(Observable.of(true));
const obs = this._filesManager.create(input, filename);
obs.subscribe(_ => done(new Error('Cannot be here')),
err => {
unit.object(err)
.isInstanceOf(Error)
.hasProperty('message', 'File helloworld.txt already exists');
this._filesManager.create(input, filename)
.subscribe(_ => done(new Error('Cannot be here')),
err => {
unit.object(err)
.isInstanceOf(Error)
.hasProperty('message', 'File helloworld.txt already exists');
done();
});
}

@test('- find returns null')
findReturnsNull(done) {
const input = Buffer.from('helloworld');
const filename = 'helloworld.txt';
const file = Object.assign(Object.create(this._classMock), {
filename,
contentType: 'application/octet-stream',
metadata: {},
size: input.length,
created_at: new Date(),
updated_at: new Date(),
md5: new Date().getTime()
});

this._modelMock.find.returns(Promise.resolve([null, null, file, null]));
unit.function(this._filesManager.find);
this._filesManager.find()
.subscribe(_ => {
unit.array(_).is([null, null, file, null]);
done();
});
}, err => done(err));
}

@test('- find')
Expand Down Expand Up @@ -221,9 +265,21 @@ export class FilesManagerUnitTest {
});
this._modelMock.find.returns(Promise.resolve([file]));
unit.function(this._filesManager.find);
const obs = this._filesManager.find(null, ['filename', 'contentType', 'size', 'created_at', 'updated_at']);
obs.subscribe(_ => {
unit.array(_).is([file]);
this._filesManager.find(null, ['filename', 'contentType', 'size', 'created_at', 'updated_at'])
.subscribe(_ => {
unit.array(_).is([file]);
done();
}, err => done(err));
}

@test('- findByFilename returns null')
findByFilenameReturnsNull(done) {
const filename = 'helloworld.txt';
this._modelMock.findOne.returns(Promise.resolve(null));
unit.function(this._filesManager.findByFilename);
this._filesManager.findByFilename(filename)
.subscribe(_ => {
unit.value(_).is(null);
done();
}, err => done(err));
}
Expand All @@ -243,11 +299,11 @@ export class FilesManagerUnitTest {
});
this._modelMock.findOne.returns(Promise.resolve(file));
unit.function(this._filesManager.findByFilename);
const obs = this._filesManager.findByFilename(filename);
obs.subscribe(_ => {
unit.object(_).is(file);
done();
}, err => done(err));
this._filesManager.findByFilename(filename)
.subscribe(_ => {
unit.object(_).is(file);
done();
}, err => done(err));
}

@test('- findByFilename: with projection as an array')
Expand All @@ -261,11 +317,11 @@ export class FilesManagerUnitTest {
});
this._modelMock.findOne.returns(Promise.resolve(file));
unit.function(this._filesManager.findByFilename);
const obs = this._filesManager.findByFilename(filename, ['filename', 'contentType', 'size']);
obs.subscribe(_ => {
unit.object(_).is(file);
done();
}, err => done(err));
this._filesManager.findByFilename(filename, ['filename', 'contentType', 'size'])
.subscribe(_ => {
unit.object(_).is(file);
done();
}, err => done(err));
}

@test('- download')
Expand All @@ -278,11 +334,11 @@ export class FilesManagerUnitTest {
});
const filename = 'helloworld.txt';
unit.stub(this._filesManager, 'findByFilename').returns(Observable.of({ filename }));
const obs = this._filesManager.download(filename);
obs.subscribe(_ => {
unit.object(_).isInstanceOf(Readable);
done();
}, err => done(err));
this._filesManager.download(filename)
.subscribe(_ => {
unit.object(_).isInstanceOf(Readable);
done();
}, err => done(err));
}

@test('- _prepareUpdateObject')
Expand Down Expand Up @@ -330,12 +386,28 @@ export class FilesManagerUnitTest {
}, err => done(err));
}

@test('- updateByFilename returns null')
updateByFilenameReturnsNull(done) {
const filename = 'helloworld.txt';

this._modelMock.findOneAndUpdate.returns(Promise.resolve(null));

unit.stub(this._filesManager, 'exists').returns(Observable.of(true));

this._filesManager.updateByFilename(filename, { meta1: 'meta1' })
.subscribe(_ => {
unit.value(_).is(null);
unit.assert(this._modelMock.findOneAndUpdate.calledOnce);
done();
}, err => done(err));
}

@test('- updateByFilename: given file to update does not exist')
updateByFilenameWithNonexistantFile(done) {
const filename = 'helloworld.txt';
unit.stub(this._filesManager, 'exists').returns(Observable.of(false));
const obs = this._filesManager.updateByFilename(filename, {});
obs.subscribe(_ => done(new Error('Should fail')), err => done());
this._filesManager.updateByFilename(filename, {})
.subscribe(_ => done(new Error('Should fail')), err => done());
}

@test('- removeByFilename')
Expand Down

0 comments on commit c43acb0

Please sign in to comment.