-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
36 changed files
with
1,337 additions
and
278 deletions.
There are no files selected for viewing
93 changes: 92 additions & 1 deletion
93
api.planx.uk/modules/analytics/metabase/collection/collection.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,92 @@ | ||
test.todo("should test collection check and creation"); | ||
import { $api } from "../../../../client/index.js"; | ||
import { updateMetabaseId } from "./updateMetabaseId.js"; | ||
import { getTeamIdAndMetabaseId } from "./getTeamIdAndMetabaseId.js"; | ||
|
||
describe("getTeamIdAndMetabaseId", () => { | ||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
test("successfully gets team and existing metabase id", async () => { | ||
vi.spyOn($api.client, "request").mockResolvedValue({ | ||
teams: [ | ||
{ | ||
id: 26, | ||
slug: "barnet", | ||
metabaseId: 20, | ||
}, | ||
], | ||
}); | ||
|
||
const teamAndMetabaseId = await getTeamIdAndMetabaseId("barnet"); | ||
|
||
expect(teamAndMetabaseId.id).toEqual(26); | ||
expect(teamAndMetabaseId.metabaseId).toEqual(20); | ||
}); | ||
|
||
test("handles team with null metabase id", async () => { | ||
vi.spyOn($api.client, "request").mockResolvedValue({ | ||
teams: [ | ||
{ | ||
id: 26, | ||
slug: "barnet", | ||
metabaseId: null, | ||
}, | ||
], | ||
}); | ||
|
||
const teamAndMetabaseId = await getTeamIdAndMetabaseId("Barnet"); | ||
|
||
expect(teamAndMetabaseId.id).toEqual(26); | ||
expect(teamAndMetabaseId.metabaseId).toBeNull(); | ||
}); | ||
}); | ||
|
||
describe("updateMetabaseId", () => { | ||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
test("successfully updates metabase ID", async () => { | ||
// Mock the GraphQL request | ||
vi.spyOn($api.client, "request").mockResolvedValue({ | ||
update_teams: { | ||
returning: [ | ||
{ | ||
id: 1, | ||
slug: "testteam", | ||
metabase_id: 123, | ||
}, | ||
], | ||
}, | ||
}); | ||
|
||
const result = await updateMetabaseId(1, 123); | ||
|
||
expect(result).toEqual({ | ||
update_teams: { | ||
returning: [ | ||
{ | ||
id: 1, | ||
slug: "testteam", | ||
metabase_id: 123, | ||
}, | ||
], | ||
}, | ||
}); | ||
|
||
expect($api.client.request).toHaveBeenCalledWith(expect.any(String), { | ||
id: 1, | ||
metabaseId: 123, | ||
}); | ||
}); | ||
|
||
test("handles GraphQL error", async () => { | ||
// Mock a failed GraphQL request | ||
vi.spyOn($api.client, "request").mockRejectedValue( | ||
new Error("GraphQL error"), | ||
); | ||
|
||
await expect(updateMetabaseId(1, 123)).rejects.toThrow("GraphQL error"); | ||
}); | ||
}); |
38 changes: 38 additions & 0 deletions
38
api.planx.uk/modules/analytics/metabase/collection/getTeamIdAndMetabaseId.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { gql } from "graphql-request"; | ||
import { $api } from "../../../../client/index.js"; | ||
|
||
interface GetMetabaseId { | ||
teams: { | ||
id: number; | ||
slug: string; | ||
metabaseId: number | null; | ||
}[]; | ||
} | ||
|
||
export const getTeamIdAndMetabaseId = async (slug: string) => { | ||
try { | ||
const response = await $api.client.request<GetMetabaseId>( | ||
gql` | ||
query GetTeamAndMetabaseId($slug: String!) { | ||
teams(where: { slug: { _eq: $slug } }) { | ||
id | ||
slug | ||
metabaseId: metabase_id | ||
} | ||
} | ||
`, | ||
{ | ||
slug: slug, | ||
}, | ||
); | ||
|
||
const result = response.teams[0]; | ||
return result; | ||
} catch (e) { | ||
console.error( | ||
"Error fetching team's ID / Metabase ID from PlanX DB:", | ||
(e as Error).stack, | ||
); | ||
throw e; | ||
} | ||
}; |
43 changes: 43 additions & 0 deletions
43
api.planx.uk/modules/analytics/metabase/collection/updateMetabaseId.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { gql } from "graphql-request"; | ||
import { $api } from "../../../../client/index.js"; | ||
|
||
interface UpdateMetabaseId { | ||
teams: { | ||
id: number; | ||
slug: string; | ||
metabaseId: number; | ||
}; | ||
} | ||
|
||
/** Updates column `metabase_id` in the Planx DB `teams` table */ | ||
export const updateMetabaseId = async (teamId: number, metabaseId: number) => { | ||
try { | ||
const response = await $api.client.request<UpdateMetabaseId>( | ||
gql` | ||
mutation UpdateTeamMetabaseId($id: Int!, $metabaseId: Int!) { | ||
update_teams( | ||
where: { id: { _eq: $id } } | ||
_set: { metabase_id: $metabaseId } | ||
) { | ||
returning { | ||
id | ||
slug | ||
metabase_id | ||
} | ||
} | ||
} | ||
`, | ||
{ | ||
id: teamId, | ||
metabaseId: metabaseId, | ||
}, | ||
); | ||
return response; | ||
} catch (e) { | ||
console.error( | ||
"There's been an error while updating the Metabase ID for this team", | ||
(e as Error).stack, | ||
); | ||
throw e; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,87 @@ | ||
import { expect, Page } from "@playwright/test"; | ||
import { Feature } from "geojson"; | ||
|
||
export const checkGeoJsonContent = async (page: Page, geoJson: Feature) => { | ||
export const waitForMapComponent = async (page: Page) => { | ||
await page.waitForFunction(() => { | ||
const map = document.getElementById("draw-boundary-map"); | ||
return map; | ||
}); | ||
}; | ||
|
||
export const getMapProperties = async ( | ||
page: Page, | ||
attribute: "geojsondata" | "drawgeojsondata", | ||
) => { | ||
const mapComponent = await page.waitForSelector("my-map"); | ||
return await mapComponent.getAttribute(attribute); | ||
}; | ||
|
||
export const alterDrawGeoJson = async (page: Page) => { | ||
const map = page.getByTestId("map-test-id"); | ||
|
||
await map.click({ button: "left", position: { x: 100, y: 200 } }); | ||
await map.click({ button: "left", position: { x: 150, y: 250 } }); | ||
await map.click({ button: "left", position: { x: 200, y: 250 } }); | ||
await map.click({ button: "left", position: { x: 100, y: 200 } }); | ||
}; | ||
|
||
export const resetMapBoundary = async (page: Page) => { | ||
const resetButton = page.getByLabel("Reset map view"); | ||
await resetButton.click(); | ||
|
||
const resetGeoJson = await getMapProperties(page, "drawgeojsondata"); | ||
|
||
expect(resetGeoJson, "drawGeoJsonData should be reset").toEqual(null); | ||
}; | ||
|
||
export const checkGeoJsonContent = async ( | ||
page: Page, | ||
attribute: "geojsondata" | "drawgeojsondata", | ||
geoJson: Feature, | ||
) => { | ||
// Wait for the map component to be present | ||
const mapComponent = await page.waitForSelector("my-map"); | ||
|
||
await page.waitForFunction(() => customElements.get("my-map")); | ||
await expect( | ||
page.getByTestId("map-test-id"), | ||
"Check we can see the map", | ||
).toBeVisible(); | ||
|
||
// Get the geojsonData attribute | ||
const geojsonData = await mapComponent.getAttribute("geojsondata"); | ||
const geojsonData = await mapComponent.getAttribute(attribute); | ||
|
||
expect( | ||
JSON.parse(geojsonData!), | ||
"map attribute matches expected mock attribute", | ||
).toEqual(geoJson); | ||
}; | ||
|
||
export const checkUploadFileAltRoute = async (page: Page) => { | ||
const uploadButton = page.getByTestId("upload-file-button"); | ||
|
||
await expect( | ||
uploadButton, | ||
"We can see a button to upload a file instead", | ||
).toBeVisible(); | ||
|
||
await uploadButton.click(); | ||
|
||
await expect( | ||
page.getByRole("heading", { name: "Upload a location plan" }), | ||
"Should be in a page for uploading a file", | ||
).toBeVisible(); | ||
|
||
await expect( | ||
page.getByRole("button", { name: "Drop file here or choose" }), | ||
"A button for uploading files is visible", | ||
).toBeVisible(); | ||
|
||
await page.getByTestId("continue-button").click(); | ||
|
||
await page.getByTestId("error-message-upload-location-plan").isVisible(); | ||
|
||
const useMapButton = page.getByTestId("use-map-button"); | ||
|
||
expect(JSON.parse(geojsonData!)).toEqual(geoJson); | ||
await useMapButton.click(); | ||
}; |
Oops, something went wrong.