Skip to content

Commit

Permalink
Add repo tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dyedwiper committed Nov 9, 2023
1 parent 2ce8f7a commit 7c31b58
Showing 1 changed file with 119 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { NotFoundError } from '@mikro-orm/core';
import { EntityManager, ObjectId } from '@mikro-orm/mongodb';
import { Test, TestingModule } from '@nestjs/testing';
import { SortOrder } from '@shared/domain';
import { SchoolEntity } from '@shared/domain/entity/school.entity';
import { MongoMemoryDatabaseModule } from '@shared/infra/database';
import { cleanupCollections, schoolFactory } from '@shared/testing';
import { cleanupCollections, federalStateFactory, schoolFactory, systemFactory } from '@shared/testing';
import { SCHOOL_REPO } from '../../domain';
import { SchoolEntityMapper } from './mapper';
import { SchoolMikroOrmRepo } from './school.repo';
Expand Down Expand Up @@ -36,24 +37,132 @@ describe('SchoolMikroOrmRepo', () => {
expect(repo.entityName).toBe(SchoolEntity);
});

describe('getAllSchools', () => {});
describe('getAllSchools', () => {
describe('when no query and options are given', () => {
const setup = async () => {
const entities = schoolFactory.buildList(3);
await em.persistAndFlush(entities);
em.clear();
const schools = entities.map((entity) => SchoolEntityMapper.mapToDo(entity));

return { schools };
};

it('should return all schools', async () => {
const { schools } = await setup();

const result = await repo.getAllSchools({});

expect(result).toEqual(schools);
});
});

describe('when query is given', () => {
const setup = async () => {
const someFederalStateId = new ObjectId().toHexString();
const federalState = federalStateFactory.buildWithId({}, someFederalStateId);
const entity1 = schoolFactory.build({ federalState });
const entity2 = schoolFactory.build();
await em.persistAndFlush([entity1, entity2]);
em.clear();
const schoolDo1 = SchoolEntityMapper.mapToDo(entity1);
const schoolDo2 = SchoolEntityMapper.mapToDo(entity2);

const query = { federalStateId: someFederalStateId };

return { schoolDo1, schoolDo2, query };
};

it('should return all schools matching query', async () => {
const { schoolDo1, schoolDo2, query } = await setup();

const result = await repo.getAllSchools(query);

expect(result).toContainEqual(schoolDo1);
expect(result).not.toContainEqual(schoolDo2);
});
});

describe('when pagination option is given', () => {
const setup = async () => {
const entities = schoolFactory.buildList(3);
await em.persistAndFlush(entities);
em.clear();
const schoolDos = entities.map((entity) => SchoolEntityMapper.mapToDo(entity));

const options = {
pagination: {
skip: 1,
limit: 1,
},
};

return { schoolDos, options };
};

it('should return schools matching pagination', async () => {
const { schoolDos, options } = await setup();

const result = await repo.getAllSchools({}, options);

expect(result).toEqual([schoolDos[1]]);
});
});

describe('when order option is given', () => {
const setup = async () => {
const entity1 = schoolFactory.build({ name: 'bbb' });
const entity2 = schoolFactory.build({ name: 'aaa' });
await em.persistAndFlush([entity1, entity2]);
em.clear();
const schoolDo1 = SchoolEntityMapper.mapToDo(entity1);
const schoolDo2 = SchoolEntityMapper.mapToDo(entity2);

const options = {
order: {
name: SortOrder.asc,
},
};

return { schoolDo1, schoolDo2, options };
};

it('should return schools in given order', async () => {
const { schoolDo1, schoolDo2, options } = await setup();

const result = await repo.getAllSchools({}, options);

expect(result).toEqual([schoolDo2, schoolDo1]);
});
});
});

describe('getSchool', () => {
it('should throw NotFound if entity is not found', async () => {
const someId = new ObjectId().toString();
const someId = new ObjectId().toHexString();

await expect(() => repo.getSchool(someId)).rejects.toThrow(NotFoundError);
});

it('should return school', async () => {
const entity = schoolFactory.build();
await em.persistAndFlush([entity]);
em.clear();
const school = SchoolEntityMapper.mapToDo(entity);
describe('when entity is found', () => {
const setup = async () => {
const systems = systemFactory.buildList(2);
const someId = new ObjectId().toHexString();
const entity = schoolFactory.buildWithId({ systems }, someId);
await em.persistAndFlush([entity]);
em.clear();
const schoolDo = SchoolEntityMapper.mapToDo(entity);

return { schoolDo, someId };
};

it('should return school with all refs populated', async () => {
const { schoolDo, someId } = await setup();

const result = await repo.getSchool(entity.id);
const result = await repo.getSchool(someId);

expect(result).toEqual(school);
expect(result).toEqual(schoolDo);
});
});
});
});

0 comments on commit 7c31b58

Please sign in to comment.