-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: basic assignment scheduler database functions unit tests (#100)
* feat: add inferred `insert` and `select` types for all tables * feat: basic testing setup for database functions * fix: don't hardcode testingDb in db function * fix: no seperate "db implementation" * fix: move mock data * wip * fix: cast time string to timestamp * feat: check for actual task, not just length of array * fix: run tests in ci * fix: use .env in CI * fix: env var db test * ci: test --------- Co-authored-by: julian-wasmeier-titanom <[email protected]>
- Loading branch information
1 parent
9264510
commit 810780b
Showing
13 changed files
with
291 additions
and
31 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,124 @@ | ||
import { | ||
dbGetTasksToAssignForCurrentInterval, | ||
TaskToAssign, | ||
} from '../functions/assignment'; | ||
import { | ||
assignmentTable, | ||
recurringTaskGroupTable, | ||
recurringTaskGroupUserTable, | ||
taskTable, | ||
taskUserGroupTable, | ||
} from '../schema'; | ||
import { client, db } from '..'; | ||
import { | ||
recurringTaskGroupWeekly, | ||
userJulian, | ||
taskVacuuming, | ||
userGroupWG1, | ||
} from '../tests/mock-data'; | ||
import { truncateAllTables, seedDatabase } from '../tests/util'; | ||
|
||
describe('dbGetTasksToAssignForCurrentInterval', () => { | ||
beforeEach(async () => { | ||
await truncateAllTables(); | ||
await seedDatabase(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await client.end(); | ||
}); | ||
|
||
async function setup() { | ||
await db.insert(recurringTaskGroupTable).values(recurringTaskGroupWeekly); | ||
await db.insert(recurringTaskGroupUserTable).values({ | ||
recurringTaskGroupId: recurringTaskGroupWeekly.id, | ||
userId: userJulian.id, | ||
}); | ||
|
||
await db.insert(taskTable).values(taskVacuuming); | ||
await db | ||
.insert(taskUserGroupTable) | ||
.values({ groupId: userGroupWG1.id, taskId: taskVacuuming.id }); | ||
} | ||
|
||
it('does return tasks where initial start date is in the past', async () => { | ||
await setup(); | ||
|
||
const result = await dbGetTasksToAssignForCurrentInterval({ | ||
currentTime: new Date('2024-07-29 13:00:00Z'), | ||
}); | ||
|
||
const expectedTask = { | ||
taskId: taskVacuuming.id, | ||
taskGroupId: recurringTaskGroupWeekly.id, | ||
isInFirstInterval: true, | ||
taskGroupInitialStartDate: recurringTaskGroupWeekly.initialStartDate, | ||
} satisfies TaskToAssign; | ||
|
||
expect(result).toHaveLength(1); | ||
const firstTask = result[0]; | ||
expect(firstTask).toStrictEqual(expectedTask); | ||
}); | ||
|
||
it('does not return tasks where the initalStartDate is in the future', async () => { | ||
await setup(); | ||
|
||
const result = await dbGetTasksToAssignForCurrentInterval({ | ||
currentTime: new Date('2024-07-28 21:59:59Z'), | ||
}); | ||
|
||
expect(result).toHaveLength(0); | ||
}); | ||
|
||
it('returns a task where there are no assignments yet', async () => { | ||
await setup(); | ||
|
||
const expectedTask = { | ||
taskId: taskVacuuming.id, | ||
taskGroupId: recurringTaskGroupWeekly.id, | ||
isInFirstInterval: true, | ||
taskGroupInitialStartDate: recurringTaskGroupWeekly.initialStartDate, | ||
} satisfies TaskToAssign; | ||
|
||
const result = await dbGetTasksToAssignForCurrentInterval({ | ||
currentTime: new Date('2024-07-31 22:00:00Z'), | ||
}); | ||
expect(result).toHaveLength(1); | ||
expect(result[0]).toStrictEqual(expectedTask); | ||
}); | ||
|
||
it('returns no tasks where there is already an assignment for the current period', async () => { | ||
await setup(); | ||
await db.insert(assignmentTable).values({ | ||
taskId: taskVacuuming.id, | ||
userId: userJulian.id, | ||
createdAt: new Date('2024-07-28 22:00:00Z'), | ||
}); | ||
const result = await dbGetTasksToAssignForCurrentInterval({ | ||
currentTime: new Date('2024-08-04 21:59:00Z'), | ||
}); | ||
expect(result).toHaveLength(0); | ||
}); | ||
|
||
it('returns a task for which there exists an assignment in the previous period', async () => { | ||
await setup(); | ||
const expectedTask = { | ||
taskId: taskVacuuming.id, | ||
taskGroupId: recurringTaskGroupWeekly.id, | ||
isInFirstInterval: false, | ||
taskGroupInitialStartDate: recurringTaskGroupWeekly.initialStartDate, | ||
} satisfies TaskToAssign; | ||
|
||
await db.insert(assignmentTable).values({ | ||
taskId: taskVacuuming.id, | ||
userId: userJulian.id, | ||
createdAt: new Date('2024-07-28 22:00:00Z'), | ||
}); | ||
const result = await dbGetTasksToAssignForCurrentInterval({ | ||
currentTime: new Date('2024-08-04 22:00:00Z'), | ||
}); | ||
|
||
expect(result).toHaveLength(1); | ||
expect(result[0]).toStrictEqual(expectedTask); | ||
}); | ||
}); |
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
Oops, something went wrong.