-
Notifications
You must be signed in to change notification settings - Fork 126
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
1 parent
f2025d9
commit dd3abe0
Showing
6 changed files
with
126 additions
and
27 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
60 changes: 60 additions & 0 deletions
60
web-admin/src/features/scheduled-reports/metadata/metadata.spec.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,60 @@ | ||
import { formatRefreshSchedule } from "@rilldata/web-admin/features/scheduled-reports/metadata/utils"; | ||
import { | ||
convertFormValuesToCronExpression, | ||
getFrequencyFromCronExpression, | ||
ReportFrequency, | ||
} from "@rilldata/web-common/features/scheduled-reports/time-utils"; | ||
import { describe, it, expect } from "vitest"; | ||
|
||
describe("Reports metadata", () => { | ||
describe("formatRefreshSchedule", () => { | ||
const TestCase: [string, string][] = [ | ||
[ | ||
"* * * 21 * *", | ||
"Every second, every minute, every hour, on the 21st of each month", | ||
], | ||
// TODO: we need a follow up to better match what is expected. | ||
// perhaps we should replace cronstrue | ||
["0 0 * * 1", "At 12:00 AM, only on Monday"], | ||
]; | ||
for (const [cron, formattedSchedule] of TestCase) { | ||
it(`format(${cron})=${formattedSchedule}`, () => { | ||
expect(formatRefreshSchedule(cron)).toEqual(formattedSchedule); | ||
}); | ||
} | ||
}); | ||
|
||
describe("frequency extraction and conversion", () => { | ||
const TestCase: [ | ||
frequency: ReportFrequency, | ||
dayOfWeek: string, | ||
timeOfDay: string, | ||
dayOfMonth: number, | ||
expectedCron: string, | ||
][] = [ | ||
[ReportFrequency.Daily, "Monday", "11:20", 1, "20 11 * * *"], | ||
[ReportFrequency.Weekly, "Tuesday", "11:20", 1, "20 11 * * 2"], | ||
[ReportFrequency.Weekdays, "Tuesday", "11:20", 1, "20 11 * * 1-5"], | ||
[ReportFrequency.Monthly, "Tuesday", "11:20", 11, "20 11 11 * *"], | ||
]; | ||
|
||
for (const [ | ||
frequency, | ||
dayOfWeek, | ||
timeOfDay, | ||
dayOfMonth, | ||
expectedCron, | ||
] of TestCase) { | ||
it(expectedCron, () => { | ||
const actualCron = convertFormValuesToCronExpression( | ||
frequency, | ||
dayOfWeek, | ||
timeOfDay, | ||
dayOfMonth, | ||
); | ||
expect(actualCron).toEqual(expectedCron); | ||
expect(getFrequencyFromCronExpression(actualCron)).toEqual(frequency); | ||
}); | ||
} | ||
}); | ||
}); |
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,34 @@ | ||
import { ordinal } from "@rilldata/web-common/lib/ordinal"; | ||
import { describe, it, expect } from "vitest"; | ||
|
||
describe("ordinal", () => { | ||
const TestCases: [number, string][] = [ | ||
[0, "0th"], | ||
[1, "1st"], | ||
[2, "2nd"], | ||
[3, "3rd"], | ||
[4, "4th"], | ||
[10, "10th"], | ||
[11, "11th"], | ||
[12, "12th"], | ||
[13, "13th"], | ||
[20, "20th"], | ||
[21, "21st"], | ||
[22, "22nd"], | ||
[23, "23rd"], | ||
[110, "110th"], | ||
[111, "111th"], | ||
[112, "112th"], | ||
[113, "113th"], | ||
[120, "120th"], | ||
[121, "121st"], | ||
[122, "122nd"], | ||
[123, "123rd"], | ||
]; | ||
|
||
for (const [num, expected] of TestCases) { | ||
it(`ordinal(${num})=${expected}`, () => { | ||
expect(ordinal(num)).toEqual(expected); | ||
}); | ||
} | ||
}); |
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,14 @@ | ||
function indicator(num: number): string { | ||
num = Math.abs(num); | ||
const cent = num % 100; | ||
if (cent >= 10 && cent <= 20) return "th"; | ||
const dec = num % 10; | ||
if (dec === 1) return "st"; | ||
if (dec === 2) return "nd"; | ||
if (dec === 3) return "rd"; | ||
return "th"; | ||
} | ||
|
||
export function ordinal(num: number): string { | ||
return `${num}${indicator(num)}`; | ||
} |