import {expect} from 'chai'; import {suite, test} from 'mocha'; import { prisma, dbDateToday, firstDayOfMonth, lastDayOfMonth, monthsAgo, dbDateFromString, } from './db.js'; suite('Server', () => { // Kind of a lame test, but not sure how else to do it, and it // actually caught a bug (month off by one)! test('dbToday', async () => { const today = await dbDateToday(); const dbNow: [{date: string}] = await prisma.$queryRaw`select now()::date as date;`; expect(today.toISOString().slice(0, 10)).to.equal(dbNow[0].date); }); test('firstDayOfMonth', () => { function expectEq(input: string, expected: string) { const got = firstDayOfMonth(dbDateFromString(input)) .toISOString() .slice(0, 10); expect(got).to.equal(expected); } expectEq('2020-01-01', '2020-01-01'); expectEq('2020-01-23', '2020-01-01'); expectEq('2022-02-02', '2022-02-01'); }); test('lastDayOfMonth', () => { function expectEq(input: string, expected: string) { const got = lastDayOfMonth(dbDateFromString(input)) .toISOString() .slice(0, 10); expect(got).to.equal(expected); } expectEq('2020-01-01', '2020-01-31'); expectEq('2020-12-01', '2020-12-31'); expectEq('2020-12-31', '2020-12-31'); expectEq('2020-02-01', '2020-02-29'); // leap year }); test('monthsAgo', () => { function expectEq(input: string, n: number, expected: string) { const got = monthsAgo(dbDateFromString(input), n) .toISOString() .slice(0, 10); expect(got).to.equal(expected); } expectEq('2020-01-01', 0, '2020-01-01'); expectEq('2020-01-02', 0, '2020-01-02'); expectEq('2020-02-03', 1, '2020-01-03'); expectEq('2020-01-01', 1, '2019-12-01'); expectEq('2020-01-01', 2, '2019-11-01'); expectEq('2020-05-31', 1, '2020-04-30'); // april has only 30 days expectEq('2020-03-31', 1, '2020-02-29'); // leap year }); });