mirror of
https://gitlab.com/mobicoop/v3/service/user.git
synced 2026-01-11 22:02:39 +00:00
integration tests
This commit is contained in:
166
src/modules/users/tests/integration/users.repository.spec.ts
Normal file
166
src/modules/users/tests/integration/users.repository.spec.ts
Normal file
@@ -0,0 +1,166 @@
|
||||
import { TestingModule, Test } from '@nestjs/testing';
|
||||
import { DatabaseModule } from '../../../database/database.module';
|
||||
import { PrismaService } from '../../../database/src/adapters/secondaries/prisma-service';
|
||||
import { DatabaseException } from '../../../database/src/exceptions/DatabaseException';
|
||||
import { UsersRepository } from '../../adapters/secondaries/users.repository';
|
||||
import { User } from '../../domain/entities/user';
|
||||
|
||||
describe('UsersRepository', () => {
|
||||
let prismaService: PrismaService;
|
||||
let usersRepository: UsersRepository;
|
||||
|
||||
const createUsers = async (nbToCreate = 10) => {
|
||||
for (let i = 0; i < nbToCreate; i++) {
|
||||
await prismaService.user.create({
|
||||
data: {
|
||||
firstName: `firstName-${i}`,
|
||||
},
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
beforeAll(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
imports: [DatabaseModule],
|
||||
providers: [UsersRepository, PrismaService],
|
||||
}).compile();
|
||||
|
||||
prismaService = module.get<PrismaService>(PrismaService);
|
||||
usersRepository = module.get<UsersRepository>(UsersRepository);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await prismaService.$disconnect();
|
||||
});
|
||||
|
||||
beforeEach(async () => {
|
||||
await prismaService.user.deleteMany();
|
||||
});
|
||||
|
||||
describe('findAll', () => {
|
||||
it('should return an empty data array', async () => {
|
||||
const res = await usersRepository.findAll();
|
||||
expect(res).toEqual({
|
||||
data: [],
|
||||
total: 0,
|
||||
});
|
||||
});
|
||||
|
||||
it('should return an data array with users', async () => {
|
||||
await createUsers(10);
|
||||
const users = await usersRepository.findAll();
|
||||
expect(users.data.length).toBe(10);
|
||||
});
|
||||
});
|
||||
|
||||
describe('findOneByUuid', () => {
|
||||
it('should return a user', async () => {
|
||||
const userToFind = await prismaService.user.create({
|
||||
data: {
|
||||
firstName: 'test',
|
||||
},
|
||||
});
|
||||
|
||||
const user = await usersRepository.findOneByUuid(userToFind.uuid);
|
||||
expect(user.uuid).toBe(userToFind.uuid);
|
||||
});
|
||||
|
||||
it('should return null', async () => {
|
||||
const user = await usersRepository.findOneByUuid(
|
||||
'544572be-11fb-4244-8235-587221fc9104',
|
||||
);
|
||||
expect(user).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('findOne', () => {
|
||||
it('should return a user according to its email', async () => {
|
||||
const userToFind = await prismaService.user.create({
|
||||
data: {
|
||||
email: 'test@test.com',
|
||||
},
|
||||
});
|
||||
|
||||
const user = await usersRepository.findOne({
|
||||
email: 'test@test.com',
|
||||
});
|
||||
|
||||
expect(user.uuid).toBe(userToFind.uuid);
|
||||
});
|
||||
|
||||
it('should return null with unknown email', async () => {
|
||||
const user = await usersRepository.findOne({
|
||||
email: 'wrong@email.com',
|
||||
});
|
||||
expect(user).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('create', () => {
|
||||
it('should create a user', async () => {
|
||||
const beforeCount = await prismaService.user.count();
|
||||
|
||||
const userToCreate: User = new User();
|
||||
userToCreate.firstName = 'test';
|
||||
const user = await usersRepository.create(userToCreate);
|
||||
|
||||
const afterCount = await prismaService.user.count();
|
||||
|
||||
expect(afterCount - beforeCount).toBe(1);
|
||||
expect(user.uuid).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('update', () => {
|
||||
it('should update user firstName', async () => {
|
||||
const userToUpdate = await prismaService.user.create({
|
||||
data: {
|
||||
firstName: 'test',
|
||||
},
|
||||
});
|
||||
|
||||
const toUpdate: User = new User();
|
||||
toUpdate.firstName = 'updated';
|
||||
const updateduser = await usersRepository.update(
|
||||
userToUpdate.uuid,
|
||||
toUpdate,
|
||||
);
|
||||
|
||||
expect(updateduser.uuid).toBe(userToUpdate.uuid);
|
||||
expect(updateduser.firstName).toBe('updated');
|
||||
});
|
||||
|
||||
it('should throw DatabaseException', async () => {
|
||||
const toUpdate: User = new User();
|
||||
toUpdate.firstName = 'updated';
|
||||
|
||||
await expect(
|
||||
usersRepository.update(
|
||||
'544572be-11fb-4244-8235-587221fc9104',
|
||||
toUpdate,
|
||||
),
|
||||
).rejects.toBeInstanceOf(DatabaseException);
|
||||
});
|
||||
});
|
||||
|
||||
describe('delete', () => {
|
||||
it('should delete a user', async () => {
|
||||
const userToRemove = await prismaService.user.create({
|
||||
data: {
|
||||
firstName: 'test',
|
||||
},
|
||||
});
|
||||
|
||||
await usersRepository.delete(userToRemove.uuid);
|
||||
|
||||
const count = await prismaService.user.count();
|
||||
expect(count).toBe(0);
|
||||
});
|
||||
|
||||
it('should throw DatabaseException', async () => {
|
||||
await expect(
|
||||
usersRepository.delete('544572be-11fb-4244-8235-587221fc9104'),
|
||||
).rejects.toBeInstanceOf(DatabaseException);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user