From a26ae5b05083907c164d91f8f5ccc27b3cdf7ce4 Mon Sep 17 00:00:00 2001 From: angelalvaigle Date: Sun, 1 Dec 2024 19:06:02 +0100 Subject: [PATCH] stat- and question-services tests added --- .github/workflows/build.yml | 6 ++++ questionservice/question-service.test.js | 35 ++++++++++++++++++++++++ statservice/stat-service.test.js | 34 +++++++++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 questionservice/question-service.test.js create mode 100644 statservice/stat-service.test.js diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 402d6c2..54b3062 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -18,14 +18,20 @@ jobs: # Copiar utils a cada servicio si es necesario - run: cp -R utils/ users/authservice/ - run: cp -R utils/ users/userservice/ + - run: cp -R utils/ questionservice/ + - run: cp -R utils/ statservice/ # Instalar dependencias - run: npm --prefix users/authservice ci - run: npm --prefix users/userservice ci + - run: npm --prefix questionservice ci + - run: npm --prefix statservice ci - run: npm --prefix gatewayservice ci - run: npm --prefix webapp ci # Ejecutar tests - run: npm --prefix users/authservice test -- --coverage - run: npm --prefix users/userservice test -- --coverage + - run: npm --prefix questionservice test -- --coverage + - run: npm --prefix statservice test -- --coverage - run: npm --prefix gatewayservice test -- --coverage - run: npm --prefix webapp test -- --coverage - name: Analyze with SonarCloud diff --git a/questionservice/question-service.test.js b/questionservice/question-service.test.js new file mode 100644 index 0000000..6aea584 --- /dev/null +++ b/questionservice/question-service.test.js @@ -0,0 +1,35 @@ +import request from 'supertest'; +import { MongoMemoryServer } from 'mongodb-memory-server'; + +let mongoServer; +let app; + +beforeAll(async () => { + mongoServer = await MongoMemoryServer.create(); + const mongoUri = mongoServer.getUri(); + process.env.MONGODB_URI = mongoUri; + app = (await import('./question-service.js')).default; // Import app dynamically to ensure MONGODB_URI is set +}); + +afterAll(async () => { + app.close(); + await mongoServer.stop(); +}); + +describe('Question Service', () => { + it('should add a new question on POST /addquestion', async () => { + const newQuestion = { + type: 'testType', + name: 'testName', + path: 'testPath', + right: 'testRight', + wrong1: 'testWrong1', + wrong2: 'testWrong2', + wrong3: 'testWrong3', + }; + + const response = await request(app).post('/addstst').send(newQuestion); + expect(response.status).toBe(200); + expect(response.body).toHaveProperty('userId', '507f1f77bcf86cd799439011'); + }); +}); diff --git a/statservice/stat-service.test.js b/statservice/stat-service.test.js new file mode 100644 index 0000000..f008e6c --- /dev/null +++ b/statservice/stat-service.test.js @@ -0,0 +1,34 @@ +import request from 'supertest'; +import { MongoMemoryServer } from 'mongodb-memory-server'; + +let mongoServer; +let app; + +beforeAll(async () => { + mongoServer = await MongoMemoryServer.create(); + const mongoUri = mongoServer.getUri(); + process.env.MONGODB_URI = mongoUri; + app = (await import('./stat-service.js')).default; // Import app dynamically to ensure MONGODB_URI is set +}); + +afterAll(async () => { + app.close(); + await mongoServer.stop(); +}); + +describe('Stat Service', () => { + it('should add a new stat on POST /addstat', async () => { + const newStat = { + userId: '507f1f77bcf86cd799439011', + gameId: 'testGameId', + questionId: 'testQuestionId', + right: false, + time: 10, + points: 0, + }; + + const response = await request(app).post('/addstst').send(newStat); + expect(response.status).toBe(200); + expect(response.body).toHaveProperty('userId', '507f1f77bcf86cd799439011'); + }); +});