Skip to content
This repository has been archived by the owner on Sep 12, 2023. It is now read-only.

Commit

Permalink
unit(manager): prepare tests to check if model belongs to resource
Browse files Browse the repository at this point in the history
  • Loading branch information
KennethTrecy committed Sep 11, 2022
1 parent a25e10c commit f05f1dc
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 74 deletions.
16 changes: 16 additions & 0 deletions database/managers/chat_message.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Model from "%/models/chat_message"
import UserFactory from "~/factories/user"
import Factory from "~/factories/chat_message"
import AttachedChatFile from "%/models/attached_chat_file"
import ConsultationFactory from "~/factories/consultation"
Expand All @@ -8,6 +9,21 @@ import ChatMessageActivityFactory from "~/factories/chat_message_activity"
import Manager from "./chat_message"

describe("Database Manager: Chat message read operations", () => {
it("can check if model belongs to user", async() => {
const manager = new Manager()
const user = await new UserFactory().insertOne()
const chatMessageActivityModel = await new ChatMessageActivityFactory()
.user(() => Promise.resolve(user))
.insertOne()
const model = await new Factory()
.chatMessageActivity(() => Promise.resolve(chatMessageActivityModel))
.insertOne()

const doesBelong = await manager.isModelBelongsTo(model.id, user.id, manager.modelChainToUser)

expect(doesBelong).toBeTruthy()
})

it("can read preview messages", async() => {
const consultationModels = await new ConsultationFactory().insertMany(2)
const chatMessageActivityModelA = await new ChatMessageActivityFactory()
Expand Down
17 changes: 17 additions & 0 deletions database/managers/consultation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,27 @@ import Factory from "~/factories/consultation"
import ChatMessage from "%/models/chat_message"
import AttachedRoleFactory from "~/factories/attached_role"
import ChatMessageActivity from "%/models/chat_message_activity"
import ChatMessageActivityFactory from "~/factories/chat_message_activity"

import type { UserIdentifierDocument } from "$/types/documents/user"
import Manager from "./consultation"

describe("Database Manager: Consultation read operations", () => {
it("can check if model belongs to user", async() => {
const manager = new Manager()
const user = await new UserFactory().insertOne()
const model = await new Factory().insertOne()
await new ChatMessageActivityFactory()
.user(() => Promise.resolve(user))
.consultation(() => Promise.resolve(model))
.insertOne()

const doesBelong = await manager.isModelBelongsTo(model.id, user.id, manager.modelChainToUser)

expect(doesBelong).toBeTruthy()
})
})

describe("Database Manager: Consultation create operations", () => {
it("can create resource", async() => {
const attachedRole = await new AttachedRoleFactory().insertOne()
Expand Down
47 changes: 30 additions & 17 deletions database/managers/department.spec.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,50 @@
import DepartmentFactory from "~/factories/department"
import UserFactory from "~/factories/user"
import Factory from "~/factories/department"
import DatabaseError from "$!/errors/database"

import DeparmentManager from "./department"
import Manager from "./department"

describe("Database Manager: Department read operations", () => {
it("can count single department", async() => {
const manager = new DeparmentManager()
const department = await new DepartmentFactory().insertOne()
await new UserFactory().in(department).insertOne()
await new UserFactory().in(department).insertOne()
it("can check if model belongs to user", async() => {
const manager = new Manager()
const model = await new Factory().insertOne()
const user = await new UserFactory().in(model).insertOne()

try {
await manager.isModelBelongsTo(model.id, user.id, manager.modelChainToUser)
} catch (error) {
expect(error).toBeInstanceOf(DatabaseError)
}
})

it("can count single model", async() => {
const manager = new Manager()
const model = await new Factory().insertOne()
await new UserFactory().in(model).insertOne()
await new UserFactory().in(model).insertOne()

const counts = await manager.countUsers([ department.id ])
const counts = await manager.countUsers([ model.id ])

expect(counts).toHaveProperty("data.0.id", String(department.id))
expect(counts).toHaveProperty("data.0.id", String(model.id))
expect(counts).toHaveProperty("data.0.type", "department")
expect(counts).toHaveProperty("data.0.meta.userCount", 2)
})

it("can count single department with zero users", async() => {
const manager = new DeparmentManager()
const department = await new DepartmentFactory().insertOne()
it("can count single model with zero users", async() => {
const manager = new Manager()
const model = await new Factory().insertOne()

const counts = await manager.countUsers([ department.id ])
const counts = await manager.countUsers([ model.id ])

expect(counts).toHaveProperty("data.0.id", String(department.id))
expect(counts).toHaveProperty("data.0.id", String(model.id))
expect(counts).toHaveProperty("data.0.type", "department")
expect(counts).toHaveProperty("data.0.meta.userCount", 0)
})

it("can count multiple departments", async() => {
const manager = new DeparmentManager()
const departmentA = await new DepartmentFactory().insertOne()
const departmentB = await new DepartmentFactory().insertOne()
const manager = new Manager()
const departmentA = await new Factory().insertOne()
const departmentB = await new Factory().insertOne()
await new UserFactory().in(departmentA).insertOne()
await new UserFactory().in(departmentA).insertOne()
await new UserFactory().in(departmentA).insertOne()
Expand Down
51 changes: 32 additions & 19 deletions database/managers/role.spec.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,52 @@
import RoleFactory from "~/factories/role"
import Factory from "~/factories/role"
import UserFactory from "~/factories/user"
import DatabaseError from "$!/errors/database"
import AttachedRole from "%/models/attached_role"
import Condition from "%/managers/helpers/condition"

import RoleManager from "./role"
import Manager from "./role"

describe("Database Manager: Role read operations", () => {
it("can count single role", async() => {
const manager = new RoleManager()
const role = await new RoleFactory().insertOne()
await new UserFactory().attach(role).insertOne()
await new UserFactory().attach(role).insertOne()
it("can check if model belongs to user", async() => {
const manager = new Manager()
const model = await new Factory().insertOne()
const user = await new UserFactory().attach(model).insertOne()

const counts = await manager.countUsers([ role.id ])
try {
await manager.isModelBelongsTo(model.id, user.id, manager.modelChainToUser)
} catch (error) {
expect(error).toBeInstanceOf(DatabaseError)
}
})

it("can count single model", async() => {
const manager = new Manager()
const model = await new Factory().insertOne()
await new UserFactory().attach(model).insertOne()
await new UserFactory().attach(model).insertOne()

const counts = await manager.countUsers([ model.id ])

expect(counts).toHaveProperty("data.0.id", String(role.id))
expect(counts).toHaveProperty("data.0.id", String(model.id))
expect(counts).toHaveProperty("data.0.type", "role")
expect(counts).toHaveProperty("data.0.meta.userCount", 2)
})

it("can count single role with zero users", async() => {
const manager = new RoleManager()
const role = await new RoleFactory().insertOne()
it("can count single model with zero users", async() => {
const manager = new Manager()
const model = await new Factory().insertOne()

const counts = await manager.countUsers([ role.id ])
const counts = await manager.countUsers([ model.id ])

expect(counts).toHaveProperty("data.0.id", String(role.id))
expect(counts).toHaveProperty("data.0.id", String(model.id))
expect(counts).toHaveProperty("data.0.type", "role")
expect(counts).toHaveProperty("data.0.meta.userCount", 0)
})

it("can count multiple roles", async() => {
const manager = new RoleManager()
const roleA = await new RoleFactory().insertOne()
const roleB = await new RoleFactory().insertOne()
const manager = new Manager()
const roleA = await new Factory().insertOne()
const roleB = await new Factory().insertOne()
await new UserFactory().attach(roleA).insertOne()
await new UserFactory().attach(roleA).insertOne()
await new UserFactory().attach(roleA).insertOne()
Expand All @@ -53,8 +66,8 @@ describe("Database Manager: Role read operations", () => {

describe("Database Manager: Role update operations", () => {
it("can reattach roles", async() => {
const roles = await new RoleFactory().insertMany(4)
const manager = new RoleManager()
const roles = await new Factory().insertMany(4)
const manager = new Manager()
const user = await new UserFactory().attach(roles[0]).attach(roles[1]).insertOne()

await manager.reattach(user.id, [
Expand Down
Loading

0 comments on commit f05f1dc

Please sign in to comment.