Skip to content

Commit

Permalink
test: improve coverage (#195)
Browse files Browse the repository at this point in the history
* test: improve coverage

* ci: bump version

---------

Co-authored-by: uiolee <[email protected]>
  • Loading branch information
D-Sketon and uiolee authored Aug 31, 2023
1 parent 5ccff01 commit 71d0d55
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 12 deletions.
23 changes: 12 additions & 11 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ jobs:
linter:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Use Node.js 16.x
uses: actions/setup-node@v1
- uses: actions/checkout@v3
- name: Use Node.js 16
uses: actions/setup-node@v3
with:
node-version: '16.x'
node-version: 16
- name: Cache NPM dependencies
uses: actions/cache@v1
uses: actions/cache@v3
with:
path: node_modules
key: ${{ runner.OS }}-npm-cache
Expand All @@ -24,16 +24,16 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
node-version: ['14.x', '16.x', '18.x']
node-version: [14, 16, 18]
fail-fast: false
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- name: Cache NPM dependencies
uses: actions/cache@v1
uses: actions/cache@v3
with:
path: node_modules
key: ${{ runner.os }}-npm-cache
Expand All @@ -45,17 +45,18 @@ jobs:
env:
CI: true
- name: Coveralls
uses: coverallsapp/github-action@master
uses: coverallsapp/github-action@v2
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
parallel: true
flag-name: ${{ runner.os }}-node-${{ matrix.node-version }}
coveralls:
needs: tester
if: ${{ always() }}
runs-on: ubuntu-latest
steps:
- name: Coveralls Finished
uses: coverallsapp/github-action@master
uses: coverallsapp/github-action@v2
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
parallel-finished: true
2 changes: 1 addition & 1 deletion src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { logger } from 'hexo-log';
const log = logger();
const pkg = require('../package.json');
const { open } = fsPromises;
const pipelineAsync = Bluebird.promisify(pipeline) as (...args: Stream[]) => Bluebird<unknown>;
const pipelineAsync = Bluebird.promisify(pipeline) as unknown as (...args: Stream[]) => Bluebird<unknown>;

let _writev: (handle: fsPromises.FileHandle, buffers: Buffer[]) => Promise<unknown>;

Expand Down
15 changes: 15 additions & 0 deletions test/scripts/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,19 @@ describe('Database', () => {
]
});
}));

it('toJSON()', () => {
const db = new Database({
path: DB_PATH,
version: 0
});

return db.load().then(() => {
const model = db.model('Test');
const json = db.toJSON();
json.meta.version.should.eql(0);
(json.models as any).Test.should.eql(model);
console.log(db.toJSON());
});
});
});
44 changes: 44 additions & 0 deletions test/scripts/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,10 @@ describe('Model', () => {
return data;
}).map(item => Post.removeById(item._id)));

it('eq() - no data', () => {
(typeof Post.eq(1)).should.eql('undefined');
});

it('first()', () => Post.insert(Array(5).fill({})).then(data => {
Post.first().should.eql(data[0]);

Expand Down Expand Up @@ -945,6 +949,46 @@ describe('Model', () => {
return data;
}).map(item => User.removeById(item._id)));

it('populate() - error', () => {
try {
Post.populate();
} catch (err) {
err.message.should.eql('path is required');
}
});

it('populate() - not exist', () => {
let posts, user;

return Post.insert([
{title: 'ABCD'},
{title: 'ACD'},
{title: 'CDE'},
{title: 'XYZ'}
]).then(posts_ => {
posts = posts_;

return User.insert({
posts: posts.map(post => post._id)
});
}).then(user_ => {
user = user_;
return User.populate({
path: 'posts',
model: 'ppp'
});
}).catch(err => {
err.message.should.eql('Model `ppp` does not exist');
return Promise.all([
User.removeById(user._id),
Post.removeById(posts[0]._id),
Post.removeById(posts[1]._id),
Post.removeById(posts[2]._id),
Post.removeById(posts[3]._id)
]);
});
});

it('populate() - object', () => {
let user, post;

Expand Down
102 changes: 102 additions & 0 deletions test/scripts/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ describe('Query', () => {
comments: [{type: Schema.Types.CUID, ref: 'Comment'}]
});

const Loop = db.model('Loop', {
age: {
age: Number
}
});

const Comment = db.model('Comment', {
content: String,
author: {type: Schema.Types.CUID, ref: 'User'}
Expand Down Expand Up @@ -262,6 +268,38 @@ describe('Query', () => {
return data;
}).map(item => User.removeById(item._id)));

it('find() - abnormal - 1', () => User.insert([
{name: 'John', age: 20},
{name: 'John', age: 25},
{name: 'Jack', age: 30}
]).then(data => {
const query = User.find({}).find({
$and: [
{name: 'Jack'},
{age: {gt: 20}}
]
});
query.toArray().length.should.eql(0);

return data;
}).map(item => User.removeById(item._id)));

it('find() - abnormal - 2', () => User.insert([
{name: 'John', age: 20},
{name: 'John', age: 25},
{name: 'Jack', age: 30}
]).then(data => {
const query = User.find({}).find({
$and: [
{name: 'Jack'},
{age: {gt: {}}}
]
});
query.toArray().should.eql([data[2]]);

return data;
}).map(item => User.removeById(item._id)));

it('findOne()', () => User.insert([
{age: 10},
{age: 20},
Expand Down Expand Up @@ -298,6 +336,18 @@ describe('Query', () => {
return data;
}).map(item => User.removeById(item._id)));

it('sort() - object', () => Loop.insert([
{age: {age: 15}},
{age: {age: 35}},
{age: {age: 10}}
]).then(data => {
const query = Loop.find({}).sort('age', { age: 1 });
query.data[0].should.eql(data[2]);
query.data[1].should.eql(data[0]);
query.data[2].should.eql(data[1]);
return data;
}).map(item => Loop.removeById(item._id)));

it('sort() - descending', () => User.insert([
{age: 15},
{age: 35},
Expand Down Expand Up @@ -552,4 +602,56 @@ describe('Query', () => {
]);
});
});

it('populate() - array expr - string', () => {
let user, comment;

return User.insert({}).then(user_ => {
user = user_;

return Comment.insert({
author: user._id
});
}).then(comment_ => {
comment = comment_;
return Comment.find({}).populate(['author']);
}).then(result => {
result.first().author.should.eql(user);

return Promise.all([
User.removeById(user._id),
Comment.removeById(comment._id)
]);
});
});

it('populate() - array expr - object', () => {
let user, comment;

return User.insert({}).then(user_ => {
user = user_;

return Comment.insert({
author: user._id
});
}).then(comment_ => {
comment = comment_;
return Comment.find({}).populate([{ path: 'author' }]);
}).then(result => {
result.first().author.should.eql(user);

return Promise.all([
User.removeById(user._id),
Comment.removeById(comment._id)
]);
});
});

it('populate() - path is required', () => {
try {
Comment.find({}).populate({});
} catch (err) {
err.message.should.eql('path is required');
}
});
});
5 changes: 5 additions & 0 deletions test/scripts/schematype.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ describe('SchemaType', () => {
type.cast().should.eql('foo');
});

it('cast() - default - function', () => {
const type = new SchemaType('test', {default: () => 'foo'});
type.cast().should.eql('foo');
});

it('validate()', () => {
type.validate(123).should.eql(123);
});
Expand Down
4 changes: 4 additions & 0 deletions test/scripts/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ describe('util', () => {

util.delProp(obj, 'd.e.f');
should.not.exist(obj.d.e.f);

util.delProp(obj, 'd.f.g');
should.exist(obj.d.e);
});

it('delProp() - obj must be an object', () => {
Expand Down Expand Up @@ -172,5 +175,6 @@ describe('util', () => {
util.parseArgs('name', -1).should.eql({name: -1});
util.parseArgs('name -date').should.eql({name: 1, date: -1});
util.parseArgs('name -date +priority').should.eql({name: 1, date: -1, priority: 1});
util.parseArgs({name: 1}).should.eql({name: 1});
});
});

0 comments on commit 71d0d55

Please sign in to comment.