diff options
| author | eug-vs <eug-vs@keemail.me> | 2020-12-02 04:10:15 +0300 | 
|---|---|---|
| committer | eug-vs <eug-vs@keemail.me> | 2020-12-02 04:10:36 +0300 | 
| commit | 6f52408e7bf79730df9eb7b487dcacf468e409e1 (patch) | |
| tree | 0bc1caf00ad8a95ac4faa438ff2f848529d36a2b /test | |
| parent | 40ffcc5e78f5ee41353e383ca25b81134084ab9f (diff) | |
| download | mongo-cronjob-6f52408e7bf79730df9eb7b487dcacf468e409e1.tar.gz | |
test: Event model
Diffstat (limited to 'test')
| -rw-r--r-- | test/event.model.test.ts | 34 | ||||
| -rw-r--r-- | test/model.ts | 16 | ||||
| -rw-r--r-- | test/utils/dbConnection.ts | 26 | 
3 files changed, 76 insertions, 0 deletions
| diff --git a/test/event.model.test.ts b/test/event.model.test.ts new file mode 100644 index 0000000..05afb7a --- /dev/null +++ b/test/event.model.test.ts @@ -0,0 +1,34 @@ +import { expect } from 'chai'; +import Model from './model'; + +describe('Event model', () => { +  it('Should assign status and nextRunAt on creation', async () => { +    const event = await Model.create({ +      type: 'test', +      schedule: '* * * * *', +      context: { +        message: 'Hello, world!' +      }, +    }); +    expect(event.status).equal('notStarted'); +    expect(event).to.have.property('nextRunAt'); +  }); + +  describe('Methods', () => { +    it('log', async () => { +      const event = await Model.findOne(); +      const log = await event?.log('Test message'); +      expect(log).to.have.property('message').equal('Test message'); +    }); + +    it('start'); + +    it('complete'); + +    it('fail'); + +    it('getLogs') + +    it('computeNextRunAt'); +  }) +}); diff --git a/test/model.ts b/test/model.ts new file mode 100644 index 0000000..4d189dc --- /dev/null +++ b/test/model.ts @@ -0,0 +1,16 @@ +import { Schema } from 'mongoose'; +import model from '../lib/event.model'; + +interface Context { +  message: string; +} + +const contextSchema = new Schema({ +  message: String +}); + +const CustomEventModel = model<Context>('CustomEvent', contextSchema); + + +export default CustomEventModel; + diff --git a/test/utils/dbConnection.ts b/test/utils/dbConnection.ts new file mode 100644 index 0000000..ee1e9e0 --- /dev/null +++ b/test/utils/dbConnection.ts @@ -0,0 +1,26 @@ +import mongoose from 'mongoose'; +import Promise from 'bluebird'; + +mongoose.Promise = Promise; + +const MONGODB_URL = process.env.MONGODB_URI || 'mongodb://localhost:27017/mongo-cronjob-test'; +const { MONGODB_USER, MONGODB_PASSWORD } = process.env; + +mongoose.connect(MONGODB_URL, { +  user: MONGODB_USER, +  pass: MONGODB_PASSWORD, +  useNewUrlParser: true, +  useUnifiedTopology: true, +  useCreateIndex: true, +  useFindAndModify: false, +  family: 4 // Use IPv4, skip trying IPv6 +}); + +const db = mongoose.connection; +db.on('error', console.error.bind(console, 'connection error:')); +db.once('open', () => { +  console.log('Connection to MongoDB successful'); +}); + +export default db; + | 
