From 56c132d4c49afc3d13c47f348d149baeb48bdb0d Mon Sep 17 00:00:00 2001 From: mouryasujit Date: Sat, 19 Aug 2023 23:37:58 +0530 Subject: [PATCH 1/6] #127 issue no creating test casses for infrastructure endpoint done here --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index df56d06..6eb8b1c 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ "serverstart": "DEBUG=api:* npm run devstart", "serverstartWin": "SET DEBUG=api:* && npm run devstart", "test": "NODE_OPTIONS=--experimental-vm-modules npx jest", - "test:watch": "NODE_OPTIONS=--experimental-vm-modules npx jest --watch", + "test:watch": "NODE_OPTIONS=--experimental-vm-modules npx jest --wa tch", "test:openHandels": "NODE_OPTIONS=--experimental-vm-modules npx jest --detectOpenHandles", "eslint": "eslint ." }, From d279fc0076f231d6533f60e3012a9fd431a12b33 Mon Sep 17 00:00:00 2001 From: mouryasujit Date: Thu, 24 Aug 2023 21:44:50 +0530 Subject: [PATCH 2/6] #127 issue no creating test casses for infrastructure endpoint done here --- test/routes/infrastructure.test.js | 60 ++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 test/routes/infrastructure.test.js diff --git a/test/routes/infrastructure.test.js b/test/routes/infrastructure.test.js new file mode 100644 index 0000000..4315478 --- /dev/null +++ b/test/routes/infrastructure.test.js @@ -0,0 +1,60 @@ +import infrastructureController from '../controllers/infrastructure'; + +describe('Infrastructure Controller', () => { + const mockInfrastructure = { + name: 'Building A', + type: 'Office', + wing: 'East', + floor: 3, + capacity: 100, + }; + + it('should add infrastructure', async () => { + const createSpy = jest.spyOn(infrastructureController, 'create'); + createSpy.mockResolvedValue(mockInfrastructure); + + const result = await infrastructureController.addInfrastructure(mockInfrastructure); + expect(result).toEqual(mockInfrastructure); + expect(createSpy).toHaveBeenCalledWith( + mockInfrastructure.name, + mockInfrastructure.type, + mockInfrastructure.wing, + mockInfrastructure.floor, + mockInfrastructure.capacity + ); + }); + + it('should read infrastructure', async () => { + const filter = { name: 'Building A' }; + const limit = 5; + const readSpy = jest.spyOn(infrastructureController, 'read'); + readSpy.mockResolvedValue([mockInfrastructure]); + + const result = await infrastructureController.readInfrastructure(filter, limit); + expect(result).toEqual([mockInfrastructure]); + expect(readSpy).toHaveBeenCalledWith(filter, limit); + }); + + it('should update infrastructure', async () => { + const filter = { name: 'Building A' }; + const updateData = { capacity: 150 }; + const updatedInfrastructure = { ...mockInfrastructure, capacity: 150 }; + const updateSpy = jest.spyOn(infrastructureController, 'update'); + updateSpy.mockResolvedValue(updatedInfrastructure); + + const result = await infrastructureController.updateInfrastructure(filter, updateData); + expect(result).toEqual(updatedInfrastructure); + expect(updateSpy).toHaveBeenCalledWith(filter, updateData); + }); + + it('should remove infrastructure', async () => { + const filter = { name: 'Building A' }; + const removedInfrastructure = { ...mockInfrastructure }; + const removeSpy = jest.spyOn(infrastructureController, 'remove'); + removeSpy.mockResolvedValue(removedInfrastructure); + + const result = await infrastructureController.removeInfrastructure(filter); + expect(result).toEqual(removedInfrastructure); + expect(removeSpy).toHaveBeenCalledWith(filter); + }); +}); From a38491f6fc1a73ea7da377d6f11d598a42827cc6 Mon Sep 17 00:00:00 2001 From: mouryasujit Date: Thu, 24 Aug 2023 22:32:34 +0530 Subject: [PATCH 3/6] #127 issue no creating test casses for infrastructure endpoint done here --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 6eb8b1c..df56d06 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ "serverstart": "DEBUG=api:* npm run devstart", "serverstartWin": "SET DEBUG=api:* && npm run devstart", "test": "NODE_OPTIONS=--experimental-vm-modules npx jest", - "test:watch": "NODE_OPTIONS=--experimental-vm-modules npx jest --wa tch", + "test:watch": "NODE_OPTIONS=--experimental-vm-modules npx jest --watch", "test:openHandels": "NODE_OPTIONS=--experimental-vm-modules npx jest --detectOpenHandles", "eslint": "eslint ." }, From ce8b92eeb6c088d2abc860e717063424012c352d Mon Sep 17 00:00:00 2001 From: mouryasujit Date: Thu, 24 Aug 2023 22:41:31 +0530 Subject: [PATCH 4/6] #127 issue no creating test casses for infrastructure endpoint done here --- test/routes/infrastructure.test.js | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/test/routes/infrastructure.test.js b/test/routes/infrastructure.test.js index 4315478..10dc02f 100644 --- a/test/routes/infrastructure.test.js +++ b/test/routes/infrastructure.test.js @@ -1,4 +1,4 @@ -import infrastructureController from '../controllers/infrastructure'; +import { addinfrastructure } from '../controllers/infrastructure'; describe('Infrastructure Controller', () => { const mockInfrastructure = { @@ -10,10 +10,12 @@ describe('Infrastructure Controller', () => { }; it('should add infrastructure', async () => { - const createSpy = jest.spyOn(infrastructureController, 'create'); + const createSpy = jest.spyOn(addinfrastructure, 'create'); createSpy.mockResolvedValue(mockInfrastructure); - const result = await infrastructureController.addInfrastructure(mockInfrastructure); + const result = await addinfrastructure.addInfrastructure( + mockInfrastructure + ); expect(result).toEqual(mockInfrastructure); expect(createSpy).toHaveBeenCalledWith( mockInfrastructure.name, @@ -27,10 +29,10 @@ describe('Infrastructure Controller', () => { it('should read infrastructure', async () => { const filter = { name: 'Building A' }; const limit = 5; - const readSpy = jest.spyOn(infrastructureController, 'read'); + const readSpy = jest.spyOn(addinfrastructure, 'read'); readSpy.mockResolvedValue([mockInfrastructure]); - const result = await infrastructureController.readInfrastructure(filter, limit); + const result = await addinfrastructure.readInfrastructure(filter, limit); expect(result).toEqual([mockInfrastructure]); expect(readSpy).toHaveBeenCalledWith(filter, limit); }); @@ -39,10 +41,13 @@ describe('Infrastructure Controller', () => { const filter = { name: 'Building A' }; const updateData = { capacity: 150 }; const updatedInfrastructure = { ...mockInfrastructure, capacity: 150 }; - const updateSpy = jest.spyOn(infrastructureController, 'update'); + const updateSpy = jest.spyOn(addinfrastructure, 'update'); updateSpy.mockResolvedValue(updatedInfrastructure); - const result = await infrastructureController.updateInfrastructure(filter, updateData); + const result = await addinfrastructure.updateInfrastructure( + filter, + updateData + ); expect(result).toEqual(updatedInfrastructure); expect(updateSpy).toHaveBeenCalledWith(filter, updateData); }); @@ -50,10 +55,10 @@ describe('Infrastructure Controller', () => { it('should remove infrastructure', async () => { const filter = { name: 'Building A' }; const removedInfrastructure = { ...mockInfrastructure }; - const removeSpy = jest.spyOn(infrastructureController, 'remove'); + const removeSpy = jest.spyOn(addinfrastructure, 'remove'); removeSpy.mockResolvedValue(removedInfrastructure); - const result = await infrastructureController.removeInfrastructure(filter); + const result = await addinfrastructure.removeInfrastructure(filter); expect(result).toEqual(removedInfrastructure); expect(removeSpy).toHaveBeenCalledWith(filter); }); From 175943b00cfb1418cc20069d59eec2711941b600 Mon Sep 17 00:00:00 2001 From: mouryasujit Date: Thu, 24 Aug 2023 23:54:49 +0530 Subject: [PATCH 5/6] Adding the changes --- test/routes/infrastructure.test.js | 169 ++++++++++++++++++++--------- 1 file changed, 118 insertions(+), 51 deletions(-) diff --git a/test/routes/infrastructure.test.js b/test/routes/infrastructure.test.js index 10dc02f..2380f70 100644 --- a/test/routes/infrastructure.test.js +++ b/test/routes/infrastructure.test.js @@ -1,65 +1,132 @@ -import { addinfrastructure } from '../controllers/infrastructure'; - -describe('Infrastructure Controller', () => { - const mockInfrastructure = { - name: 'Building A', - type: 'Office', - wing: 'East', - floor: 3, - capacity: 100, - }; - - it('should add infrastructure', async () => { - const createSpy = jest.spyOn(addinfrastructure, 'create'); - createSpy.mockResolvedValue(mockInfrastructure); - - const result = await addinfrastructure.addInfrastructure( - mockInfrastructure +import mongoose from 'mongoose'; +import { MongoMemoryServer } from 'mongodb-memory-server'; +import { jest } from '@jest/globals'; +import databaseUtil from '#models/databaseUtil'; +import app from '#app'; + +import InfrastructureFunctions from '#models/infrastructure'; + +const request = require('supertest'); +jest.mock('#util'); +let mongoServer; +let server; +let agent; +beforeAll((done) => { + server = app.listen(5000, () => { + agent = request.agent(server); + connector.set('debug', false); + done(); + }); +}); + +function cleanUp(callback) { + accreditationModel.remove({ name: 'xyz' }).then(() => { + connector.disconnect((DBerr) => { + if (DBerr) console.log('Database dissconnnect error: ', DBerr); + server.close((serverErr) => { + if (serverErr) console.log(serverErr); + callback(); + }); + }); + }); +} + +afterAll((done) => { + cleanUp(done); +}); + +describe('Infrastructure API Endpoints', () => { + // ... (beforeEach, beforeAll, afterAll, etc.) + + test('Read Infrastructure', async () => { + await InfrastructureFunctions.create( + 'Building A', + 'Office', + 'East Wing', + 2, + 100 ); - expect(result).toEqual(mockInfrastructure); - expect(createSpy).toHaveBeenCalledWith( - mockInfrastructure.name, - mockInfrastructure.type, - mockInfrastructure.wing, - mockInfrastructure.floor, - mockInfrastructure.capacity + await InfrastructureFunctions.create( + 'Building B', + 'Residential', + 'West Wing', + 1, + 50 ); + + const response = await request(app).get('/api/infrastructures'); // Adjust the API endpoint + + expect(response.status).toBe(200); + expect(response.body).toHaveLength(2); + expect(response.body[0].name).toBe('Building A'); + expect(response.body[1].name).toBe('Building B'); }); - it('should read infrastructure', async () => { - const filter = { name: 'Building A' }; - const limit = 5; - const readSpy = jest.spyOn(addinfrastructure, 'read'); - readSpy.mockResolvedValue([mockInfrastructure]); + test('Update Infrastructure', async () => { + const createdInfrastructure = await InfrastructureFunctions.create( + 'Building A', + 'Office', + 'East Wing', + 2, + 100 + ); - const result = await addinfrastructure.readInfrastructure(filter, limit); - expect(result).toEqual([mockInfrastructure]); - expect(readSpy).toHaveBeenCalledWith(filter, limit); + const response = await request(app) + .put(`/api/infrastructures/${createdInfrastructure._id}`) // Adjust the API endpoint + .send({ capacity: 150 }); + + expect(response.status).toBe(200); + expect(response.body.capacity).toBe(150); }); - it('should update infrastructure', async () => { - const filter = { name: 'Building A' }; - const updateData = { capacity: 150 }; - const updatedInfrastructure = { ...mockInfrastructure, capacity: 150 }; - const updateSpy = jest.spyOn(addinfrastructure, 'update'); - updateSpy.mockResolvedValue(updatedInfrastructure); + test('Remove Infrastructure', async () => { + const createdInfrastructure = await InfrastructureFunctions.create( + 'Building A', + 'Office', + 'East Wing', + 2, + 100 + ); - const result = await addinfrastructure.updateInfrastructure( - filter, - updateData + const response = await request(app).delete( + `/api/infrastructures/${createdInfrastructure._id}` + ); // Adjust the API endpoint + + expect(response.status).toBe(200); + expect(response.body).toMatchObject({ + name: 'Building A', + type: 'Office', + wing: 'East Wing', + floor: 2, + capacity: 100, + }); + + const infrastructuresResponse = await request(app).get( + '/api/infrastructures' ); - expect(result).toEqual(updatedInfrastructure); - expect(updateSpy).toHaveBeenCalledWith(filter, updateData); + expect(infrastructuresResponse.body).toHaveLength(0); }); - it('should remove infrastructure', async () => { - const filter = { name: 'Building A' }; - const removedInfrastructure = { ...mockInfrastructure }; - const removeSpy = jest.spyOn(addinfrastructure, 'remove'); - removeSpy.mockResolvedValue(removedInfrastructure); + test('Get Infrastructure by ID', async () => { + const createdInfrastructure = await InfrastructureFunctions.create( + 'Building A', + 'Office', + 'East Wing', + 2, + 100 + ); + + const response = await request(app).get( + `/api/infrastructures/${createdInfrastructure._id}` + ); // Adjust the API endpoint - const result = await addinfrastructure.removeInfrastructure(filter); - expect(result).toEqual(removedInfrastructure); - expect(removeSpy).toHaveBeenCalledWith(filter); + expect(response.status).toBe(200); + expect(response.body).toMatchObject({ + name: 'Building A', + type: 'Office', + wing: 'East Wing', + floor: 2, + capacity: 100, + }); }); }); From 5c5979422576e1492f2e7b655eb130386e96c69f Mon Sep 17 00:00:00 2001 From: mouryasujit Date: Sat, 26 Aug 2023 21:49:26 +0530 Subject: [PATCH 6/6] Adding the test cases for infrastructure endpoint --- test/routes/infrastructure.test.js | 159 +++++++++++------------------ 1 file changed, 61 insertions(+), 98 deletions(-) diff --git a/test/routes/infrastructure.test.js b/test/routes/infrastructure.test.js index 2380f70..cfdd142 100644 --- a/test/routes/infrastructure.test.js +++ b/test/routes/infrastructure.test.js @@ -1,16 +1,14 @@ -import mongoose from 'mongoose'; -import { MongoMemoryServer } from 'mongodb-memory-server'; import { jest } from '@jest/globals'; -import databaseUtil from '#models/databaseUtil'; -import app from '#app'; +import request from 'supertest'; +import app from '#app'; // Update this import based on your app's structure +import connector from '#models/databaseUtil'; // Update this import +import infrastructureModel from '#models/infrastructure'; // Update this import -import InfrastructureFunctions from '#models/infrastructure'; - -const request = require('supertest'); jest.mock('#util'); -let mongoServer; + let server; let agent; + beforeAll((done) => { server = app.listen(5000, () => { agent = request.agent(server); @@ -20,113 +18,78 @@ beforeAll((done) => { }); function cleanUp(callback) { - accreditationModel.remove({ name: 'xyz' }).then(() => { - connector.disconnect((DBerr) => { - if (DBerr) console.log('Database dissconnnect error: ', DBerr); - server.close((serverErr) => { - if (serverErr) console.log(serverErr); - callback(); + infrastructureModel + .remove({ + name: 'Building A', + type: 'Office', + wing: 'East', + floor: 3, + capacity: 100, + }) + .then(() => { + connector.disconnect((DBerr) => { + if (DBerr) console.log('Database disconnect error: ', DBerr); + server.close((serverErr) => { + if (serverErr) console.log(serverErr); + callback(); + }); }); }); - }); } afterAll((done) => { cleanUp(done); }); -describe('Infrastructure API Endpoints', () => { - // ... (beforeEach, beforeAll, afterAll, etc.) - - test('Read Infrastructure', async () => { - await InfrastructureFunctions.create( - 'Building A', - 'Office', - 'East Wing', - 2, - 100 - ); - await InfrastructureFunctions.create( - 'Building B', - 'Residential', - 'West Wing', - 1, - 50 - ); - - const response = await request(app).get('/api/infrastructures'); // Adjust the API endpoint - - expect(response.status).toBe(200); - expect(response.body).toHaveLength(2); - expect(response.body[0].name).toBe('Building A'); - expect(response.body[1].name).toBe('Building B'); - }); - - test('Update Infrastructure', async () => { - const createdInfrastructure = await InfrastructureFunctions.create( - 'Building A', - 'Office', - 'East Wing', - 2, - 100 - ); - - const response = await request(app) - .put(`/api/infrastructures/${createdInfrastructure._id}`) // Adjust the API endpoint - .send({ capacity: 150 }); - - expect(response.status).toBe(200); - expect(response.body.capacity).toBe(150); - }); - - test('Remove Infrastructure', async () => { - const createdInfrastructure = await InfrastructureFunctions.create( - 'Building A', - 'Office', - 'East Wing', - 2, - 100 - ); - - const response = await request(app).delete( - `/api/infrastructures/${createdInfrastructure._id}` - ); // Adjust the API endpoint - - expect(response.status).toBe(200); - expect(response.body).toMatchObject({ +describe('Infrastructure API', () => { + it('should create infrastructure', async () => { + const response = await agent.post('/infrastructure/add').send({ name: 'Building A', type: 'Office', - wing: 'East Wing', - floor: 2, + wing: 'East', + floor: 3, capacity: 100, }); - const infrastructuresResponse = await request(app).get( - '/api/infrastructures' - ); - expect(infrastructuresResponse.body).toHaveLength(0); + expect(response.status).toBe(200); + expect(response.body.res).toMatch(/added user/); }); - test('Get Infrastructure by ID', async () => { - const createdInfrastructure = await InfrastructureFunctions.create( - 'Building A', - 'Office', - 'East Wing', - 2, - 100 - ); + describe('after adding infrastructure', () => { + beforeEach(async () => { + await agent.post('/infrastructure/add').send({ + name: 'Building A', + type: 'Office', + wing: 'East', + floor: 3, + capacity: 100, + }); + }); + + afterEach(async () => { + await infrastructureModel.remove({ + name: 'Building A', + type: 'Office', + wing: 'East', + floor: 3, + capacity: 100, + }); + }); - const response = await request(app).get( - `/api/infrastructures/${createdInfrastructure._id}` - ); // Adjust the API endpoint + it('should read infrastructure', async () => { + const response = await agent + .post('/infrastructure/list') + .send({ name: 'Building A' }); + expect(response.body.res).not.toBeNull(); + }); - expect(response.status).toBe(200); - expect(response.body).toMatchObject({ - name: 'Building A', - type: 'Office', - wing: 'East Wing', - floor: 2, - capacity: 100, + it('should update infrastructure', async () => { + const response = await agent + .post('/infrastructure/update') + .send({ name: 'Building A' }, { capacity: 150 }); + + expect(response.status).toBe(200); + expect(response.body.res).toMatch(/updated infrastructure/); }); }); });