-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from resource-watch/improvements-to-tests
Improvements to tests
- Loading branch information
Showing
10 changed files
with
812 additions
and
729 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,150 +1,168 @@ | ||
// /* eslint-disable no-unused-vars,no-undef,no-await-in-loop */ | ||
// const nock = require('nock'); | ||
// const chai = require('chai'); | ||
// const amqp = require('amqplib'); | ||
// const config = require('config'); | ||
// const appConstants = require('app.constants'); | ||
// const Task = require('models/task.model'); | ||
// const RabbitMQConnectionError = require('errors/rabbitmq-connection.error'); | ||
// const { task, execution } = require('rw-doc-importer-messages'); | ||
// const sleep = require('sleep'); | ||
// const { getTestServer } = require('./test-server'); | ||
// const { createTask } = require('./utils'); | ||
// | ||
// const should = chai.should(); | ||
// | ||
// let requester; | ||
// let rabbitmqConnection = null; | ||
// let channel; | ||
// | ||
// nock.disableNetConnect(); | ||
// nock.enableNetConnect(process.env.HOST_IP); | ||
// | ||
// describe('STATUS_INDEX_CREATED handling process', () => { | ||
// | ||
// before(async () => { | ||
// if (process.env.NODE_ENV !== 'test') { | ||
// throw Error(`Running the test suite with NODE_ENV ${process.env.NODE_ENV} may result in permanent data loss. Please use NODE_ENV=test.`); | ||
// } | ||
// | ||
// let connectAttempts = 10; | ||
// while (connectAttempts >= 0 && rabbitmqConnection === null) { | ||
// try { | ||
// rabbitmqConnection = await amqp.connect(config.get('rabbitmq.url')); | ||
// } catch (err) { | ||
// connectAttempts -= 1; | ||
// await sleep.sleep(5); | ||
// } | ||
// } | ||
// if (!rabbitmqConnection) { | ||
// throw new RabbitMQConnectionError(); | ||
// } | ||
// | ||
// channel = await rabbitmqConnection.createConfirmChannel(); | ||
// | ||
// await channel.assertQueue(config.get('queues.status')); | ||
// await channel.assertQueue(config.get('queues.tasks')); | ||
// await channel.assertQueue(config.get('queues.executorTasks')); | ||
// | ||
// requester = await getTestServer(); | ||
// | ||
// Task.remove({}).exec(); | ||
// }); | ||
// | ||
// beforeEach(async () => { | ||
// await channel.purgeQueue(config.get('queues.status')); | ||
// await channel.purgeQueue(config.get('queues.tasks')); | ||
// await channel.purgeQueue(config.get('queues.executorTasks')); | ||
// | ||
// const statusQueueStatus = await channel.checkQueue(config.get('queues.status')); | ||
// statusQueueStatus.messageCount.should.equal(0); | ||
// | ||
// }); | ||
// | ||
// it('Consume a STATUS_INDEX_CREATED message and update dataset tableName and task (happy case)', async () => { | ||
// const timestamp = new Date().getTime(); | ||
// | ||
// const fakeTask1 = await new Task(createTask(appConstants.TASK_STATUS.INIT, task.MESSAGE_TYPES.TASK_CREATE)).save(); | ||
// | ||
// const message = { | ||
// id: 'db96457a-f083-4bda-a428-73ae974f5f22', | ||
// type: 'STATUS_INDEX_CREATED', | ||
// taskId: fakeTask1.id, | ||
// index: 'index_1552479168458_1552479168503' | ||
// }; | ||
// | ||
// nock(process.env.CT_URL) | ||
// .patch(`/v1/dataset/${fakeTask1.datasetId}`, { | ||
// status: 0, | ||
// tableName: 'index_1552479168458_1552479168503' | ||
// }) | ||
// .once() | ||
// .reply(200); | ||
// | ||
// const preStatusQueueStatus = await channel.assertQueue(config.get('queues.status')); | ||
// preStatusQueueStatus.messageCount.should.equal(0); | ||
// const emptyTaskList = await Task.find({}).exec(); | ||
// emptyTaskList.should.be.an('array').and.have.lengthOf(1); | ||
// | ||
// await channel.sendToQueue(config.get('queues.status'), Buffer.from(JSON.stringify(message))); | ||
// | ||
// // Give the code 3 seconds to do its thing | ||
// await new Promise(resolve => setTimeout(resolve, 3000)); | ||
// | ||
// const postQueueStatus = await channel.assertQueue(config.get('queues.status')); | ||
// postQueueStatus.messageCount.should.equal(0); | ||
// | ||
// const validateMessage = async (msg) => { | ||
// const content = JSON.parse(msg.content.toString()); | ||
// content.should.have.property('datasetId').and.equal(timestamp); | ||
// content.should.have.property('id'); | ||
// content.should.have.property('fileUrl'); | ||
// content.should.have.property('provider').and.equal('csv'); | ||
// content.should.have.property('type').and.equal(execution.MESSAGE_TYPES.EXECUTION_CREATE); | ||
// content.should.have.property('taskId').and.equal(message.id); | ||
// | ||
// await channel.ack(msg); | ||
// }; | ||
// | ||
// await channel.consume(config.get('queues.status'), validateMessage); | ||
// | ||
// const createdTasks = await Task.find({}).exec(); | ||
// | ||
// createdTasks.should.be.an('array').and.have.lengthOf(1); | ||
// const createdTask = createdTasks[0]; | ||
// createdTask.should.have.property('status').and.equal(appConstants.TASK_STATUS.INDEX_CREATED); | ||
// createdTask.should.have.property('reads').and.equal(0); | ||
// createdTask.should.have.property('writes').and.equal(0); | ||
// createdTask.should.have.property('logs').and.be.an('array').and.have.lengthOf(1); | ||
// createdTask.should.have.property('_id').and.equal(fakeTask1.id); | ||
// createdTask.should.have.property('type').and.equal(task.MESSAGE_TYPES.TASK_CREATE); | ||
// createdTask.should.have.property('message').and.be.an('object'); | ||
// createdTask.should.have.property('datasetId').and.equal(fakeTask1.datasetId); | ||
// createdTask.should.have.property('createdAt').and.be.a('date'); | ||
// createdTask.should.have.property('updatedAt').and.be.a('date'); | ||
// | ||
// process.on('unhandledRejection', (error) => { | ||
// should.fail(error); | ||
// }); | ||
// }); | ||
// | ||
// afterEach(async () => { | ||
// await channel.assertQueue(config.get('queues.status')); | ||
// await channel.purgeQueue(config.get('queues.status')); | ||
// const statusQueueStatus = await channel.checkQueue(config.get('queues.status')); | ||
// statusQueueStatus.messageCount.should.equal(0); | ||
// | ||
// if (!nock.isDone()) { | ||
// const pendingMocks = nock.pendingMocks(); | ||
// nock.cleanAll(); | ||
// throw new Error(`Not all nock interceptors were used: ${pendingMocks}`); | ||
// } | ||
// | ||
// }); | ||
// | ||
// after(async () => { | ||
// Task.remove({}).exec(); | ||
// | ||
// rabbitmqConnection.close(); | ||
// }); | ||
// }); | ||
/* eslint-disable no-unused-vars,no-undef,no-await-in-loop */ | ||
const nock = require('nock'); | ||
const chai = require('chai'); | ||
const amqp = require('amqplib'); | ||
const config = require('config'); | ||
const appConstants = require('app.constants'); | ||
const Task = require('models/task.model'); | ||
const RabbitMQConnectionError = require('errors/rabbitmq-connection.error'); | ||
const { task, execution } = require('rw-doc-importer-messages'); | ||
const sleep = require('sleep'); | ||
const { getTestServer } = require('./test-server'); | ||
const { createTask } = require('./utils'); | ||
|
||
const should = chai.should(); | ||
|
||
let requester; | ||
let rabbitmqConnection = null; | ||
let channel; | ||
|
||
nock.disableNetConnect(); | ||
nock.enableNetConnect(process.env.HOST_IP); | ||
|
||
describe('STATUS_INDEX_CREATED handling process', () => { | ||
|
||
before(async () => { | ||
if (process.env.NODE_ENV !== 'test') { | ||
throw Error(`Running the test suite with NODE_ENV ${process.env.NODE_ENV} may result in permanent data loss. Please use NODE_ENV=test.`); | ||
} | ||
|
||
let connectAttempts = 10; | ||
while (connectAttempts >= 0 && rabbitmqConnection === null) { | ||
try { | ||
rabbitmqConnection = await amqp.connect(config.get('rabbitmq.url')); | ||
} catch (err) { | ||
connectAttempts -= 1; | ||
await sleep.sleep(5); | ||
} | ||
} | ||
if (!rabbitmqConnection) { | ||
throw new RabbitMQConnectionError(); | ||
} | ||
|
||
channel = await rabbitmqConnection.createConfirmChannel(); | ||
|
||
await channel.assertQueue(config.get('queues.status')); | ||
await channel.assertQueue(config.get('queues.tasks')); | ||
await channel.assertQueue(config.get('queues.executorTasks')); | ||
|
||
requester = await getTestServer(); | ||
|
||
Task.remove({}).exec(); | ||
}); | ||
|
||
beforeEach(async () => { | ||
await channel.purgeQueue(config.get('queues.status')); | ||
await channel.purgeQueue(config.get('queues.tasks')); | ||
await channel.purgeQueue(config.get('queues.executorTasks')); | ||
|
||
const statusQueueStatus = await channel.checkQueue(config.get('queues.status')); | ||
statusQueueStatus.messageCount.should.equal(0); | ||
|
||
const tasksQueueStatus = await channel.checkQueue(config.get('queues.tasks')); | ||
tasksQueueStatus.messageCount.should.equal(0); | ||
|
||
const executorTasksQueueStatus = await channel.checkQueue(config.get('queues.executorTasks')); | ||
executorTasksQueueStatus.messageCount.should.equal(0); | ||
|
||
Task.remove({}).exec(); | ||
}); | ||
|
||
it('Consume a STATUS_INDEX_CREATED message and update dataset tableName and task (happy case)', async () => { | ||
const timestamp = new Date().getTime(); | ||
|
||
const fakeTask1 = await new Task(createTask(appConstants.TASK_STATUS.INIT, task.MESSAGE_TYPES.TASK_CREATE)).save(); | ||
|
||
const message = { | ||
id: 'db96457a-f083-4bda-a428-73ae974f5f22', | ||
type: 'STATUS_INDEX_CREATED', | ||
taskId: fakeTask1.id, | ||
index: 'index_1552479168458_1552479168503' | ||
}; | ||
|
||
nock(process.env.CT_URL) | ||
.patch(`/v1/dataset/${fakeTask1.datasetId}`, { | ||
status: 0, | ||
tableName: 'index_1552479168458_1552479168503' | ||
}) | ||
.once() | ||
.reply(200); | ||
|
||
const preStatusQueueStatus = await channel.assertQueue(config.get('queues.status')); | ||
preStatusQueueStatus.messageCount.should.equal(0); | ||
const emptyTaskList = await Task.find({}).exec(); | ||
emptyTaskList.should.be.an('array').and.have.lengthOf(1); | ||
|
||
await channel.sendToQueue(config.get('queues.status'), Buffer.from(JSON.stringify(message))); | ||
|
||
// Give the code 3 seconds to do its thing | ||
await new Promise(resolve => setTimeout(resolve, 3000)); | ||
|
||
const postQueueStatus = await channel.assertQueue(config.get('queues.status')); | ||
postQueueStatus.messageCount.should.equal(0); | ||
|
||
const validateMessage = async (msg) => { | ||
const content = JSON.parse(msg.content.toString()); | ||
content.should.have.property('datasetId').and.equal(timestamp); | ||
content.should.have.property('id'); | ||
content.should.have.property('fileUrl'); | ||
content.should.have.property('provider').and.equal('csv'); | ||
content.should.have.property('type').and.equal(execution.MESSAGE_TYPES.EXECUTION_CREATE); | ||
content.should.have.property('taskId').and.equal(message.id); | ||
|
||
await channel.ack(msg); | ||
}; | ||
|
||
await channel.consume(config.get('queues.status'), validateMessage); | ||
|
||
const createdTasks = await Task.find({}).exec(); | ||
|
||
createdTasks.should.be.an('array').and.have.lengthOf(1); | ||
const createdTask = createdTasks[0]; | ||
createdTask.should.have.property('status').and.equal(appConstants.TASK_STATUS.INDEX_CREATED); | ||
createdTask.should.have.property('reads').and.equal(0); | ||
createdTask.should.have.property('writes').and.equal(0); | ||
createdTask.should.have.property('logs').and.be.an('array').and.have.lengthOf(1); | ||
createdTask.should.have.property('_id').and.equal(fakeTask1.id); | ||
createdTask.should.have.property('type').and.equal(task.MESSAGE_TYPES.TASK_CREATE); | ||
createdTask.should.have.property('message').and.be.an('object'); | ||
createdTask.should.have.property('datasetId').and.equal(fakeTask1.datasetId); | ||
createdTask.should.have.property('createdAt').and.be.a('date'); | ||
createdTask.should.have.property('updatedAt').and.be.a('date'); | ||
|
||
process.on('unhandledRejection', (error) => { | ||
should.fail(error); | ||
}); | ||
}); | ||
|
||
afterEach(async () => { | ||
Task.remove({}).exec(); | ||
|
||
await channel.assertQueue(config.get('queues.status')); | ||
await channel.purgeQueue(config.get('queues.status')); | ||
const statusQueueStatus = await channel.checkQueue(config.get('queues.status')); | ||
statusQueueStatus.messageCount.should.equal(0); | ||
|
||
await channel.assertQueue(config.get('queues.executorTasks')); | ||
await channel.purgeQueue(config.get('queues.executorTasks')); | ||
const executorQueueStatus = await channel.checkQueue(config.get('queues.executorTasks')); | ||
executorQueueStatus.messageCount.should.equal(0); | ||
|
||
await channel.assertQueue(config.get('queues.tasks')); | ||
await channel.purgeQueue(config.get('queues.tasks')); | ||
const tasksQueueStatus = await channel.checkQueue(config.get('queues.tasks')); | ||
tasksQueueStatus.messageCount.should.equal(0); | ||
|
||
if (!nock.isDone()) { | ||
const pendingMocks = nock.pendingMocks(); | ||
nock.cleanAll(); | ||
throw new Error(`Not all nock interceptors were used: ${pendingMocks}`); | ||
} | ||
}); | ||
|
||
after(async () => { | ||
Task.remove({}).exec(); | ||
|
||
rabbitmqConnection.close(); | ||
}); | ||
}); |
Oops, something went wrong.