From 71d0d557bc89adcf889588cc86e6b8e8e3af9574 Mon Sep 17 00:00:00 2001 From: D-Sketon <2055272094@qq.com> Date: Fri, 1 Sep 2023 00:59:58 +0800 Subject: [PATCH] test: improve coverage (#195) * test: improve coverage * ci: bump version --------- Co-authored-by: uiolee <22849383+uiolee@users.noreply.github.com> --- .github/workflows/ci.yml | 23 +++++---- src/database.ts | 2 +- test/scripts/database.ts | 15 ++++++ test/scripts/model.ts | 44 ++++++++++++++++ test/scripts/query.ts | 102 +++++++++++++++++++++++++++++++++++++ test/scripts/schematype.ts | 5 ++ test/scripts/util.ts | 4 ++ 7 files changed, 183 insertions(+), 12 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5cb12cca..2e7ecd26 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 @@ -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 @@ -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 diff --git a/src/database.ts b/src/database.ts index 0b9053b6..c5cf7a5d 100644 --- a/src/database.ts +++ b/src/database.ts @@ -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; +const pipelineAsync = Bluebird.promisify(pipeline) as unknown as (...args: Stream[]) => Bluebird; let _writev: (handle: fsPromises.FileHandle, buffers: Buffer[]) => Promise; diff --git a/test/scripts/database.ts b/test/scripts/database.ts index 16be4655..a062ea5e 100644 --- a/test/scripts/database.ts +++ b/test/scripts/database.ts @@ -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()); + }); + }); }); diff --git a/test/scripts/model.ts b/test/scripts/model.ts index 87cfead4..88589338 100644 --- a/test/scripts/model.ts +++ b/test/scripts/model.ts @@ -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]); @@ -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; diff --git a/test/scripts/query.ts b/test/scripts/query.ts index b448882d..81192205 100644 --- a/test/scripts/query.ts +++ b/test/scripts/query.ts @@ -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'} @@ -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}, @@ -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}, @@ -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'); + } + }); }); diff --git a/test/scripts/schematype.ts b/test/scripts/schematype.ts index a935779f..a49090e5 100644 --- a/test/scripts/schematype.ts +++ b/test/scripts/schematype.ts @@ -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); }); diff --git a/test/scripts/util.ts b/test/scripts/util.ts index f823a0a8..b56f4b80 100644 --- a/test/scripts/util.ts +++ b/test/scripts/util.ts @@ -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', () => { @@ -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}); }); });