Skip to content

Commit

Permalink
insert also department code on geo params with postGis and public_dep…
Browse files Browse the repository at this point in the history
…artement_region
  • Loading branch information
bbohec committed Dec 5, 2024
1 parent 2f393e6 commit c0f97e8
Show file tree
Hide file tree
Showing 2 changed files with 270 additions and 118 deletions.
Original file line number Diff line number Diff line change
@@ -1,28 +1,20 @@
import { Pool } from "pg";
import {
AppellationCode,
WithAcquisition,
expectObjectsToMatch,
expectToEqual,
} from "shared";
import { AppellationCode, expectToEqual } from "shared";
import { KyselyDb, makeKyselyDb } from "../../../config/pg/kysely/kyselyUtils";
import { getTestPgPool, optional } from "../../../config/pg/pgUtils";
import { SearchMadeEntity } from "../entities/SearchMadeEntity";
import { GeoParams, SearchMadeEntity } from "../entities/SearchMadeEntity";
import { PgSearchMadeRepository } from "./PgSearchMadeRepository";

const defaultSearchMade: SearchMadeEntity = {
id: "9f6dad2c-6f02-11ec-90d6-0242ac120003",
distanceKm: 30,
lat: 48.119146,
lon: 4.17602,
needsToBeSearched: true,
sortedBy: "distance",
place: "Nantes",
voluntaryToImmersion: true,
numberOfResults: 1,
};

describe("PgSearchesMadeRepository", () => {
const searchMadeWithoutLocation: SearchMadeEntity = {
id: "9f6dad2c-6f02-11ec-90d6-0242ac120003",
needsToBeSearched: true,
sortedBy: "distance",
place: "Nantes",
voluntaryToImmersion: true,
numberOfResults: 1,
};

let pool: Pool;
let db: KyselyDb;
let pgSearchesMadeRepository: PgSearchMadeRepository;
Expand All @@ -42,94 +34,153 @@ describe("PgSearchesMadeRepository", () => {
await pool.end();
});

it("insert a search made", async () => {
await pgSearchesMadeRepository.insertSearchMade(defaultSearchMade);
const retrievedSearchMade = await getSearchMadeById(defaultSearchMade.id);
expectToEqual(retrievedSearchMade, defaultSearchMade);
describe("with geo params", () => {
it.each<GeoParams & { expectedDepartmentCode: string }>([
{
distanceKm: 30,
lat: 48.85909,
lon: 2.350196,
expectedDepartmentCode: "75",
},
{
distanceKm: 2,
lat: 47.22221,
lon: -1.54212,
expectedDepartmentCode: "44",
},
{
distanceKm: 3.2,
lat: 46.159457943708254,
lon: 0.15399270230344264,
expectedDepartmentCode: "86",
},
{
distanceKm: 9,
lat: 46.15944708515133,
lon: 0.15392947992255213,
expectedDepartmentCode: "79",
},
{
distanceKm: 9,
lat: -21.154576445369,
lon: 55.40104947929245,
expectedDepartmentCode: "974",
},
])(
"insert a search made with geo params (lat '$lat' , lon '$lon' , '$distanceKm' km) and save departement code with value '$expectedDepartmentCode'",
async ({ distanceKm, lat, lon, expectedDepartmentCode }) => {
const searchMadeWithLocation: SearchMadeEntity = {
...searchMadeWithoutLocation,
distanceKm,
lat,
lon,
};

await pgSearchesMadeRepository.insertSearchMade(searchMadeWithLocation);

expectToEqual(
await getSearchMadeById(db, searchMadeWithLocation.id),
searchMadeWithLocation,
);
expectToEqual(
await db
.selectFrom("searches_made")
.select("department_code")
.where("id", "=", searchMadeWithLocation.id)
.executeTakeFirst(),
{ department_code: expectedDepartmentCode },
);
},
);
});

it("insert a search made without location and have departement code null", async () => {
await pgSearchesMadeRepository.insertSearchMade(searchMadeWithoutLocation);

expectToEqual(
await getSearchMadeById(db, searchMadeWithoutLocation.id),
searchMadeWithoutLocation,
);
expectToEqual(
await db
.selectFrom("searches_made")
.select("department_code")
.where("id", "=", searchMadeWithoutLocation.id)
.executeTakeFirst(),
{ department_code: null },
);
});

it("insert a search made and keeps acquisition params", async () => {
const withAcquisition = {
const searchMade: SearchMadeEntity = {
...searchMadeWithoutLocation,
acquisitionKeyword: "acquisition-keyword",
acquisitionCampaign: "acquisition-campaign",
} satisfies WithAcquisition;
await pgSearchesMadeRepository.insertSearchMade({
...defaultSearchMade,
...withAcquisition,
});
};

const results = await db.selectFrom("searches_made").selectAll().execute();
await pgSearchesMadeRepository.insertSearchMade(searchMade);

expect(results).toHaveLength(1);
expectObjectsToMatch(results[0], {
id: defaultSearchMade.id,
acquisition_keyword: withAcquisition.acquisitionKeyword,
acquisition_campaign: withAcquisition.acquisitionCampaign,
});
expectToEqual(await getSearchMadeById(db, searchMade.id), searchMade);
});

it("Remove duplicated appellationCodes then insert search", async () => {
const appellationCodes: AppellationCode[] = ["19365", "19365"];
const appellationCode: AppellationCode = "19365";

const searchMade: SearchMadeEntity = {
id: "9f6dad2c-6f02-11ec-90d6-0242ac120003",
appellationCodes,
distanceKm: 30,
lat: 48.119146,
lon: 4.17602,
needsToBeSearched: true,
sortedBy: "distance",
place: "Nantes",
voluntaryToImmersion: true,
numberOfResults: 1,
...searchMadeWithoutLocation,
appellationCodes: [appellationCode, appellationCode],
};

await pgSearchesMadeRepository.insertSearchMade(searchMade);
const retrievedSearchMade = await getSearchMadeById(searchMade.id);

expect(retrievedSearchMade).toEqual({
expectToEqual(await getSearchMadeById(db, searchMade.id), {
...searchMade,
appellationCodes: [appellationCodes[0]],
appellationCodes: [appellationCode],
});
});
});

const getSearchMadeById = async (
db: KyselyDb,
id: string,
): Promise<SearchMadeEntity | undefined> => {
const searchMadeResult = await db
.selectFrom("searches_made")
.selectAll()
.where("id", "=", id)
.executeTakeFirst();

const getSearchMadeById = async (
id: string,
): Promise<SearchMadeEntity | undefined> => {
const searchMadeResult = await db
.selectFrom("searches_made")
.selectAll()
.where("id", "=", id)
.executeTakeFirst();

const appellationCodes: AppellationCode[] = await db
.selectFrom("searches_made__appellation_code")
.selectAll()
.where("search_made_id", "=", id)
.execute()
.then((rows) =>
rows
.map(({ appellation_code }) => appellation_code)
.filter(
(appellationCode): appellationCode is AppellationCode =>
appellationCode !== null,
),
);

return (
searchMadeResult && {
id: searchMadeResult.id,
distanceKm: optional(searchMadeResult.distance),
lat: optional(searchMadeResult.lat),
lon: optional(searchMadeResult.lon),
sortedBy: optional(searchMadeResult.sorted_by),
voluntaryToImmersion: optional(searchMadeResult.voluntary_to_immersion),
place: optional(searchMadeResult.address),
needsToBeSearched: searchMadeResult.needstobesearched ?? false,
appellationCodes: appellationCodes.length
? appellationCodes
: undefined,
apiConsumerName: optional(searchMadeResult.api_consumer_name),
numberOfResults: optional(searchMadeResult.number_of_results) ?? 0,
}
const appellationCodes: AppellationCode[] = await db
.selectFrom("searches_made__appellation_code")
.selectAll()
.where("search_made_id", "=", id)
.execute()
.then((rows) =>
rows
.map(({ appellation_code }) => appellation_code)
.filter(
(appellationCode): appellationCode is AppellationCode =>
appellationCode !== null,
),
);
};
});

return (
searchMadeResult && {
id: searchMadeResult.id,
distanceKm: optional(searchMadeResult.distance),
lat: optional(searchMadeResult.lat),
lon: optional(searchMadeResult.lon),
sortedBy: optional(searchMadeResult.sorted_by),
voluntaryToImmersion: optional(searchMadeResult.voluntary_to_immersion),
place: optional(searchMadeResult.address),
needsToBeSearched: searchMadeResult.needstobesearched ?? false,
appellationCodes: appellationCodes.length ? appellationCodes : undefined,
apiConsumerName: optional(searchMadeResult.api_consumer_name),
numberOfResults: optional(searchMadeResult.number_of_results) ?? 0,
acquisitionCampaign: optional(searchMadeResult.acquisition_campaign),
acquisitionKeyword: optional(searchMadeResult.acquisition_keyword),
establishmentSearchableBy: optional(searchMadeResult.searchable_by),
romeCode: optional(searchMadeResult.rome),
}
);
};
Loading

0 comments on commit c0f97e8

Please sign in to comment.