aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreug-vs <eug-vs@keemail.me>2020-12-02 04:10:15 +0300
committereug-vs <eug-vs@keemail.me>2020-12-02 04:10:36 +0300
commit6f52408e7bf79730df9eb7b487dcacf468e409e1 (patch)
tree0bc1caf00ad8a95ac4faa438ff2f848529d36a2b
parent40ffcc5e78f5ee41353e383ca25b81134084ab9f (diff)
downloadmongo-cronjob-6f52408e7bf79730df9eb7b487dcacf468e409e1.tar.gz
test: Event model
-rw-r--r--.mocharc.json6
-rw-r--r--lib/event.model.ts2
-rw-r--r--lib/event.schema.ts2
-rw-r--r--test/event.model.test.ts34
-rw-r--r--test/model.ts16
-rw-r--r--test/utils/dbConnection.ts26
-rw-r--r--tsconfig.json3
7 files changed, 85 insertions, 4 deletions
diff --git a/.mocharc.json b/.mocharc.json
index 5b98d75..dbf2db2 100644
--- a/.mocharc.json
+++ b/.mocharc.json
@@ -1,3 +1,7 @@
{
- "require": ["ts-node/register", "test/dbConnection.ts"]
+ "require": [
+ "chai/register-should",
+ "ts-node/register",
+ "test/utils/dbConnection.ts"
+ ]
}
diff --git a/lib/event.model.ts b/lib/event.model.ts
index 7768ffc..1b6d324 100644
--- a/lib/event.model.ts
+++ b/lib/event.model.ts
@@ -5,7 +5,7 @@ import { LogDocument } from './log.schema';
import LogModel from './log.model';
export interface Event<Context> extends EventDocument<Context> {
- log(message: string): void;
+ log(message: string): Promise<LogDocument>;
start(): void;
complete(): void;
fail(error: Error | string): void;
diff --git a/lib/event.schema.ts b/lib/event.schema.ts
index 0f428fd..8b9db8b 100644
--- a/lib/event.schema.ts
+++ b/lib/event.schema.ts
@@ -3,7 +3,7 @@ import { Schema, Document } from 'mongoose';
export interface EventDocument<Context> extends Document {
type: string;
schedule: string;
- status: 'notStarted' | 'running' | 'complete' | 'failed';
+ status?: 'notStarted' | 'running' | 'complete' | 'failed';
error?: string;
context: Context;
nextRunAt?: Date;
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;
+
diff --git a/tsconfig.json b/tsconfig.json
index 301894b..adf45f2 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -7,5 +7,6 @@
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"noImplicitThis": false
- }
+ },
+ "exclude": ["./test"]
}