Skip to content

Commit

Permalink
stat- and question-services tests added
Browse files Browse the repository at this point in the history
  • Loading branch information
angelalvaigle committed Dec 1, 2024
1 parent 1eaa00f commit a26ae5b
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions questionservice/question-service.test.js
Original file line number Diff line number Diff line change
@@ -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');
});
});
34 changes: 34 additions & 0 deletions statservice/stat-service.test.js
Original file line number Diff line number Diff line change
@@ -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');
});
});

0 comments on commit a26ae5b

Please sign in to comment.