Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: staging and test data now using addresses according to owner/housing geoCode #911

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions server/src/controllers/campaignController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,9 @@ describe('Campaign API', () => {
it('should create a new campaign', async () => {
const title = randomstring.generate();
const description = randomstring.generate();
const houses: HousingApi[] = Array.from({ length: 2 }).map(() =>
genHousingApi(oneOf(establishment.geoCodes))
);
const houses: HousingApi[] = await Promise.all(Array.from({ length: 2 }).map(async () =>
await genHousingApi(oneOf(establishment.geoCodes))
));
await Housing().insert(houses.map(formatHousingRecordApi));
const payload: CampaignCreationPayloadDTO = {
title,
Expand Down Expand Up @@ -239,16 +239,16 @@ describe('Campaign API', () => {
});
});

describe('POST /campaigns/{id}/groups', () => {
describe('POST /campaigns/{id}/groups', async () => {
const testRoute = (id: string) => `/api/campaigns/${id}/groups`;

const geoCode = oneOf(establishment.geoCodes);
const group = genGroupApi(user, establishment);
const groupHousing = [
const groupHousing = await Promise.all([
genHousingApi(geoCode),
genHousingApi(geoCode),
genHousingApi(geoCode)
];
]);
const owners = groupHousing
.map((housing) => housing.owner)
.filter(isDefined);
Expand Down Expand Up @@ -591,9 +591,9 @@ describe('Campaign API', () => {
});

it('should delete linked events and campaign housing', async () => {
const houses: HousingApi[] = Array.from({ length: 2 }).map(() =>
const houses: HousingApi[] = await Promise.all(Array.from({ length: 2 }).map(() =>
genHousingApi(oneOf(establishment.geoCodes))
);
));
await Housing().insert(houses.map(formatHousingRecordApi));
const campaignHouses: CampaignHousingDBO[] = houses.map((housing) => ({
campaign_id: campaign.id,
Expand Down
15 changes: 8 additions & 7 deletions server/src/controllers/eventController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ describe('Event API', () => {
await Users().insert(formatUserApi(user));
});

describe('GET /owners/{id}/events', () => {
describe('GET /owners/{id}/events', async () => {
const testRoute = (id: string) => `/api/owners/${id}/events`;

const owner = genOwnerApi();
const geoCode = '67268';
const owner = await genOwnerApi(geoCode);

beforeAll(async () => {
await Owners().insert(formatOwnerApi(owner));
Expand All @@ -67,9 +68,9 @@ describe('Event API', () => {
});

it('should list the owner events', async () => {
const events: OwnerEventApi[] = Array.from({ length: 3 }).map(() =>
genOwnerEventApi(owner, user)
);
const events: OwnerEventApi[] = await Promise.all(Array.from({ length: 3 }).map(() =>
genOwnerEventApi(owner, user, geoCode)
));
await Events().insert(events.map(formatEventApi));
await OwnerEvents().insert(events.map(formatOwnerEventApi));

Expand All @@ -85,10 +86,10 @@ describe('Event API', () => {
});
});

describe('GET /housing/{id}/events', () => {
describe('GET /housing/{id}/events', async () => {
const testRoute = (id: string) => `/api/housing/${id}/events`;

const housing = genHousingApi(oneOf(establishment.geoCodes));
const housing = await genHousingApi(oneOf(establishment.geoCodes));

beforeAll(async () => {
await Housing().insert(formatHousingRecordApi(housing));
Expand Down
51 changes: 28 additions & 23 deletions server/src/controllers/groupController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,15 @@ describe('Group API', () => {
});
});

describe('POST /groups', () => {
describe('POST /groups', async () => {
const testRoute = '/api/groups';
const owner = genOwnerApi();
const housingList = [
const geoCode = '67268';
const owner = await genOwnerApi(geoCode);
const housingList = await Promise.all([
genHousingApi(establishment.geoCodes[0]),
genHousingApi(establishment.geoCodes[0]),
genHousingApi(otherEstablishment.geoCodes[0])
];
]);
const payload: GroupPayloadDTO = {
title: 'Logements prioritaires',
description: 'Logements les plus énergivores',
Expand Down Expand Up @@ -286,16 +287,17 @@ describe('Group API', () => {
});

it('should create the group immediately and add housing later if the volume of housing exceeds the threshold', async () => {
const housingList = Array.from({
const housingList = await Promise.all(Array.from({
length: config.app.batchSize * 2
}).map(() => genHousingApi(oneOf(establishment.geoCodes)));
}).map(() => genHousingApi(oneOf(establishment.geoCodes))));
await async.forEach(
fp.chunk(config.app.batchSize, housingList),
async (chunk) => {
await Housing().insert(chunk.map(formatHousingRecordApi));
}
);
const owner = genOwnerApi();
const geoCode = oneOf(establishment.geoCodes);
const owner = await genOwnerApi(geoCode);
await Owners().insert(formatOwnerApi(owner));
await HousingOwners().insert(
housingList.flatMap((housing) =>
Expand Down Expand Up @@ -330,16 +332,16 @@ describe('Group API', () => {
});
});

describe('PUT /groups/{id}', () => {
describe('PUT /groups/{id}', async () => {
const testRoute = (id: string) => `/api/groups/${id}`;
const group = genGroupApi(user, establishment);
const anotherGroup = genGroupApi(otherUser, otherEstablishment);
const housingList = [
const housingList = await Promise.all([
genHousingApi(establishment.geoCodes[0]),
genHousingApi(establishment.geoCodes[0]),
genHousingApi(establishment.geoCodes[0]),
genHousingApi(otherEstablishment.geoCodes[0])
];
]);

const payload: GroupPayloadDTO = {
title: 'Logement prioritaires',
Expand Down Expand Up @@ -419,15 +421,16 @@ describe('Group API', () => {
});
});

describe('POST /groups/{id}/housing', () => {
describe('POST /groups/{id}/housing', async () => {
const testRoute = (id: string) => `/api/groups/${id}/housing`;
const owner = genOwnerApi();
const housingList = [
const geoCode = '67268';
const owner = await genOwnerApi(geoCode);
const housingList = await Promise.all([
genHousingApi(establishment.geoCodes[0]),
genHousingApi(establishment.geoCodes[0]),
genHousingApi(establishment.geoCodes[0]),
genHousingApi(otherEstablishment.geoCodes[0])
];
]);
const establishmentHousingList = housingList.filter((housing) =>
establishment.geoCodes.includes(housing.geoCode)
);
Expand Down Expand Up @@ -494,7 +497,7 @@ describe('Group API', () => {
});

it('should add the housing corresponding to the given criteria to the group', async () => {
const housing = genHousingApi(oneOf(establishment.geoCodes));
const housing = await genHousingApi(oneOf(establishment.geoCodes));
await Housing().insert(formatHousingRecordApi(housing));
await HousingOwners().insert({
owner_id: owner.id,
Expand Down Expand Up @@ -529,7 +532,7 @@ describe('Group API', () => {
});

it('should create events when some housing get added', async () => {
const housing = genHousingApi(oneOf(establishment.geoCodes));
const housing = await genHousingApi(oneOf(establishment.geoCodes));
await Housing().insert(formatHousingRecordApi(housing));
await HousingOwners().insert({
owner_id: owner.id,
Expand Down Expand Up @@ -572,15 +575,16 @@ describe('Group API', () => {
});
});

describe('DELETE /groups/{id}/housing', () => {
describe('DELETE /groups/{id}/housing', async () => {
const testRoute = (id: string) => `/api/groups/${id}/housing`;
const owner = genOwnerApi();
const housingList = [
const geoCode = '67268';
const owner = await genOwnerApi(geoCode);
const housingList = await Promise.all([
genHousingApi(establishment.geoCodes[0]),
genHousingApi(establishment.geoCodes[0]),
genHousingApi(establishment.geoCodes[0]),
genHousingApi(otherEstablishment.geoCodes[0])
];
]);
const establishmentHousingList = housingList.filter((housing) =>
establishment.geoCodes.includes(housing.geoCode)
);
Expand Down Expand Up @@ -716,10 +720,11 @@ describe('Group API', () => {
beforeEach(async () => {
group = genGroupApi(user, establishment);
anotherGroup = genGroupApi(otherUser, otherEstablishment);
housingList = Array.from({ length: 3 }).map(() =>
housingList = await Promise.all(Array.from({ length: 3 }).map(() =>
genHousingApi(oneOf(establishment.geoCodes))
);
owner = genOwnerApi();
));
const geoCode = '67268';
owner = await genOwnerApi(geoCode);

await Groups().insert(formatGroupApi(group));
await Housing().insert(housingList.map(formatHousingRecordApi));
Expand Down
15 changes: 8 additions & 7 deletions server/src/controllers/housingController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ describe('Housing API', () => {
await Users().insert([user, anotherUser].map(formatUserApi));
});

describe('GET /housing/{id}', () => {
describe('GET /housing/{id}', async () => {
const testRoute = (id: string) => `/api/housing/${id}`;

const housing = genHousingApi(oneOf(anotherEstablishment.geoCodes));
const housing = await genHousingApi(oneOf(anotherEstablishment.geoCodes));

beforeAll(async () => {
await Housing().insert(formatHousingRecordApi(housing));
Expand Down Expand Up @@ -107,12 +107,13 @@ describe('Housing API', () => {
});

it('should return the housing list for a query filter', async () => {
const housingApi = await genHousingApi(oneOf(establishment.geoCodes));
const queriedHousing = {
...genHousingApi(oneOf(establishment.geoCodes)),
...housingApi,
rawAddress: ['line1 with many spaces', 'line2'],
};
await Housing().insert(formatHousingRecordApi(queriedHousing));
const owner = genOwnerApi();
const owner = await genOwnerApi(oneOf(establishment.geoCodes));
await Owners().insert(formatOwnerApi(owner));
await HousingOwners().insert(
formatHousingOwnersApi(queriedHousing, [owner]),
Expand Down Expand Up @@ -152,7 +153,7 @@ describe('Housing API', () => {
});

it('should fail if the housing already exists', async () => {
const housing = genHousingApi(oneOf(establishment.geoCodes));
const housing = await genHousingApi(oneOf(establishment.geoCodes));
await Housing().insert(formatHousingRecordApi(housing));
const payload = {
localId: housing.localId,
Expand Down Expand Up @@ -259,7 +260,7 @@ describe('Housing API', () => {
});
});

describe('POST /housing/{id}', () => {
describe('POST /housing/{id}', async () => {
const validBody: { housingUpdate: HousingUpdateBody } = {
housingUpdate: {
statusUpdate: {
Expand All @@ -279,7 +280,7 @@ describe('Housing API', () => {

const testRoute = (housingId: string) => `/api/housing/${housingId}`;

const housing = genHousingApi(oneOf(establishment.geoCodes));
const housing = await genHousingApi(oneOf(establishment.geoCodes));

beforeAll(async () => {
await Housing().insert(formatHousingRecordApi(housing));
Expand Down
4 changes: 2 additions & 2 deletions server/src/controllers/noteController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ import {
} from '~/repositories/noteRepository';
import { NoteApi } from '~/models/NoteApi';

describe('Note API', () => {
describe('Note API', async () => {
const { app } = createServer();

const establishment = genEstablishmentApi();
const user = genUserApi(establishment.id);
const housing = genHousingApi(oneOf(establishment.geoCodes));
const housing = await genHousingApi(oneOf(establishment.geoCodes));

beforeAll(async () => {
await Establishments().insert(formatEstablishmentApi(establishment));
Expand Down
26 changes: 16 additions & 10 deletions server/src/controllers/ownerController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ describe('Owner API', () => {
await Users().insert(formatUserApi(user));
});

describe('GET /owners/housing/{id}', () => {
describe('GET /owners/housing/{id}', async () => {
const testRoute = (housingId: string) => `/api/owners/housing/${housingId}`;

const housing = genHousingApi(oneOf(establishment.geoCodes));
const owners: OwnerApi[] = Array.from({ length: 3 }, () => genOwnerApi());
const housing = await genHousingApi(oneOf(establishment.geoCodes));
const owners: OwnerApi[] = await Promise.all(Array.from({ length: 3 }, () => genOwnerApi(oneOf(establishment.geoCodes))));

beforeAll(async () => {
await Housing().insert(formatHousingRecordApi(housing));
Expand Down Expand Up @@ -102,7 +102,8 @@ describe('Owner API', () => {
let owner: OwnerApi;

beforeEach(async () => {
owner = genOwnerApi();
const geoCode = '67268';
owner = await genOwnerApi(geoCode);
await Owners().insert(formatOwnerApi(owner));
});

Expand Down Expand Up @@ -178,7 +179,8 @@ describe('Owner API', () => {
});

it('should create an event if the name or birth date has changed', async () => {
const original = genOwnerApi();
const geoCode = '67268';
const original = await genOwnerApi(geoCode);
await Owners().insert(formatOwnerApi(original));
const payload: OwnerPayloadDTO = {
...original,
Expand Down Expand Up @@ -214,7 +216,8 @@ describe('Owner API', () => {
});

it('should create an event if the email or phone has changed', async () => {
const original = genOwnerApi();
const geoCode = '67268';
const original = await genOwnerApi(geoCode);
await Owners().insert(formatOwnerApi(original));
const payload: OwnerPayloadDTO = {
...original,
Expand Down Expand Up @@ -247,7 +250,8 @@ describe('Owner API', () => {
});

it('should create an event if the address has changed', async () => {
const original = genOwnerApi();
const geoCode = '67268';
const original = await genOwnerApi(geoCode);
const originalAddress = genAddressApi(original.id, AddressKinds.Owner);
await Owners().insert(formatOwnerApi(original));
await db(banAddressesTable).insert(formatAddressApi(originalAddress));
Expand Down Expand Up @@ -290,11 +294,12 @@ describe('Owner API', () => {
let housingOwners: ReadonlyArray<HousingOwnerApi>;

beforeEach(async () => {
housing = genHousingApi(
housing = await genHousingApi(
faker.helpers.arrayElement(establishment.geoCodes)
);
await Housing().insert(formatHousingRecordApi(housing));
owners = Array.from({ length: 3 }, genOwnerApi);
const geoCode = '67268';
owners = await Promise.all(Array.from({ length: 3 }, async () => await genOwnerApi(geoCode)));
await Owners().insert(owners.map(formatOwnerApi));
housingOwners = owners.map((owner, i) => {
return {
Expand Down Expand Up @@ -329,11 +334,12 @@ describe('Owner API', () => {
});

it('should reject if one of the owners is missing', async () => {
const geoCode = '67268';
const payload: HousingOwnerPayloadDTO[] = owners
.map((owner, i) => {
return createHousingOwnerPayload(owner, i + 1);
})
.concat(createHousingOwnerPayload(genOwnerApi(), owners.length + 1));
.concat(createHousingOwnerPayload(await genOwnerApi(geoCode), owners.length + 1));

const { status } = await request(app)
.put(testRoute(housing.id))
Expand Down
Loading
Loading