This repository has been archived by the owner on Oct 31, 2024. It is now read-only.
generated from slack-samples/deno-starter-template
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Gather all logged runs in a standard date format (#23)
- Loading branch information
Showing
9 changed files
with
239 additions
and
28 deletions.
There are no files selected for viewing
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
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,47 @@ | ||
import * as mf from "mock-fetch/mod.ts"; | ||
import { SlackFunctionTester } from "deno-slack-sdk/mod.ts"; | ||
import { assertEquals } from "std/testing/asserts.ts"; | ||
import CollectRunnerStatsFunction from "./collect_runner_stats.ts"; | ||
import { DatastoreItem } from "deno-slack-api/types.ts"; | ||
import RunningDatastore from "../datastores/run_data.ts"; | ||
|
||
// Mocked date for stable testing | ||
Date.now = () => new Date("2023-01-06").getTime(); | ||
|
||
// Collection of runs stored in the mocked datastore | ||
const mockRuns: DatastoreItem<typeof RunningDatastore.definition>[] = [ | ||
{ id: "R006", runner: "U0123456", distance: 4, rundate: "2023-01-06" }, | ||
{ id: "R005", runner: "U0123456", distance: 2, rundate: "2023-01-06" }, | ||
{ id: "R004", runner: "U7777777", distance: 2, rundate: "2023-01-03" }, | ||
{ id: "R003", runner: "U0123456", distance: 4, rundate: "2022-12-31" }, | ||
{ id: "R002", runner: "U7777777", distance: 1, rundate: "2022-12-10" }, | ||
{ id: "R001", runner: "U0123456", distance: 2, rundate: "2022-11-11" }, | ||
]; | ||
|
||
// Replaces globalThis.fetch with the mocked copy | ||
mf.install(); | ||
|
||
mf.mock("POST@/api/apps.datastore.query", () => { | ||
return new Response(JSON.stringify({ ok: true, items: mockRuns })); | ||
}); | ||
|
||
const { createContext } = SlackFunctionTester("collect_runner_stats"); | ||
|
||
Deno.test("Collect runner stats", async () => { | ||
const { outputs, error } = await CollectRunnerStatsFunction( | ||
createContext({ inputs: {} }), | ||
); | ||
|
||
const expectedStats = [{ | ||
runner: "U0123456", | ||
weekly_distance: 10, | ||
total_distance: 12, | ||
}, { | ||
runner: "U7777777", | ||
weekly_distance: 2, | ||
total_distance: 3, | ||
}]; | ||
|
||
assertEquals(error, undefined); | ||
assertEquals(outputs?.runner_stats, expectedStats); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import * as mf from "mock-fetch/mod.ts"; | ||
import { assertEquals } from "std/testing/asserts.ts"; | ||
import { SlackFunctionTester } from "deno-slack-sdk/mod.ts"; | ||
import { DatastoreItem } from "deno-slack-api/types.ts"; | ||
import CollectTeamStatsFunction from "./collect_team_stats.ts"; | ||
import RunningDatastore from "../datastores/run_data.ts"; | ||
|
||
// Mocked date for stable testing | ||
Date.now = () => new Date("2023-01-06").getTime(); | ||
|
||
// Collection of runs stored in the mocked datastore | ||
let mockRuns: DatastoreItem<typeof RunningDatastore.definition>[]; | ||
|
||
// Replaces globalThis.fetch with the mocked copy | ||
mf.install(); | ||
|
||
mf.mock("POST@/api/apps.datastore.query", async (args) => { | ||
const body = await args.formData(); | ||
const dates = JSON.parse(body.get("expression_values") as string); | ||
const runs = mockRuns.filter((run) => ( | ||
run.rundate >= dates[":start_date"] && run.rundate <= dates[":end_date"] | ||
)); | ||
return new Response(JSON.stringify({ ok: true, items: runs })); | ||
}); | ||
|
||
const { createContext } = SlackFunctionTester("collect_team_stats"); | ||
|
||
Deno.test("Retrieve the empty set", async () => { | ||
mockRuns = []; | ||
const { outputs, error } = await CollectTeamStatsFunction( | ||
createContext({ inputs: {} }), | ||
); | ||
assertEquals(error, undefined); | ||
assertEquals(outputs?.weekly_distance, 0); | ||
assertEquals(outputs?.percent_change, 0); | ||
}); | ||
|
||
Deno.test("Count only runs from the past week", async () => { | ||
mockRuns = [ | ||
{ id: "R006", runner: "U0123456", distance: 8, rundate: "2023-01-07" }, | ||
{ id: "R005", runner: "U0123456", distance: 4, rundate: "2023-01-06" }, | ||
{ id: "R004", runner: "U7777777", distance: 2, rundate: "2023-01-02" }, | ||
{ id: "R003", runner: "U0123456", distance: 4, rundate: "2022-12-31" }, | ||
{ id: "R002", runner: "U7777777", distance: 6, rundate: "2022-12-31" }, | ||
{ id: "R001", runner: "U8888888", distance: 1, rundate: "2022-12-30" }, | ||
]; | ||
const { outputs, error } = await CollectTeamStatsFunction( | ||
createContext({ inputs: {} }), | ||
); | ||
assertEquals(error, undefined); | ||
assertEquals(outputs?.weekly_distance, 16); | ||
assertEquals(outputs?.percent_change, 1500); | ||
}); | ||
|
||
Deno.test("Handle the infinite change", async () => { | ||
mockRuns = [ | ||
{ id: "R001", runner: "U0123456", distance: 10, rundate: "2023-01-05" }, | ||
]; | ||
const { outputs, error } = await CollectTeamStatsFunction( | ||
createContext({ inputs: {} }), | ||
); | ||
assertEquals(error, undefined); | ||
assertEquals(outputs?.weekly_distance, 10); | ||
assertEquals(outputs?.percent_change, 0); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { SlackFunctionTester } from "deno-slack-sdk/mod.ts"; | ||
import { assertEquals, assertStringIncludes } from "std/testing/asserts.ts"; | ||
import FormatLeaderboardFunction from "./format_leaderboard.ts"; | ||
|
||
const { createContext } = SlackFunctionTester("format_leaderboard"); | ||
|
||
Deno.test("Collect team stats", async () => { | ||
const inputs = { | ||
team_distance: 11, | ||
percent_change: 50, | ||
runner_stats: [{ | ||
runner: "U0123456", | ||
weekly_distance: 4, | ||
total_distance: 8, | ||
}, { | ||
runner: "U7777777", | ||
weekly_distance: 2, | ||
total_distance: 3, | ||
}], | ||
}; | ||
|
||
const { outputs, error } = await FormatLeaderboardFunction( | ||
createContext({ inputs }), | ||
); | ||
|
||
assertEquals(error, undefined); | ||
assertStringIncludes(outputs?.teamStatsFormatted || "", "11 miles"); | ||
assertStringIncludes(outputs?.teamStatsFormatted || "", "50%"); | ||
|
||
assertStringIncludes( | ||
outputs?.runnerStatsFormatted || "", | ||
"<@U0123456> ran 4 miles last week (8 total)", | ||
); | ||
assertStringIncludes( | ||
outputs?.runnerStatsFormatted || "", | ||
"<@U7777777> ran 2 miles last week (3 total)", | ||
); | ||
}); |
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
Oops, something went wrong.