59 lines
1.5 KiB
TypeScript
59 lines
1.5 KiB
TypeScript
import { test, expect } from "bun:test"
|
|
import * as db from "./database.ts"
|
|
import _ from "lodash"
|
|
import { None, Ok, Option, Some } from "ts-results-es"
|
|
|
|
test("DataBase", () => {
|
|
const allTables = db.allTables()
|
|
expect(allTables).toBeArray()
|
|
expect(allTables).toEqual(["Users", "Boards"])
|
|
expect(db.BoardTable.countAll()).toBe(0)
|
|
expect(db.UserTable.countAll()).toBe(0)
|
|
})
|
|
|
|
test("Board Table", () => {
|
|
const boardsNumber = 10
|
|
const rooms = ["A1", "A1", "A1", "A1", "A1", "A2", "A2", "A2", "A2", "A2"]
|
|
|
|
// Try to find something empty
|
|
const findEmptyByID = db.BoardTable.find(_.random(0, boardsNumber))
|
|
expect(findEmptyByID).toEqual(Ok(None))
|
|
const findEmptyByName = db.BoardTable.find("Hello", "World")
|
|
expect(findEmptyByName).toEqual(Ok(None))
|
|
|
|
|
|
const boardsArray: Array<db.Board> = []
|
|
for (let i = 0; i < boardsNumber; i++) {
|
|
boardsArray.push({
|
|
id: i,
|
|
name: `Board ${i}`,
|
|
room: rooms[i],
|
|
ipv4: `192.168.172.${i}`,
|
|
port: i,
|
|
})
|
|
}
|
|
db.BoardTable.addFromArray(boardsArray)
|
|
|
|
// Test Find
|
|
expect(db.BoardTable.find(1)).toEqual(Ok(Some({
|
|
id: 1,
|
|
name: `Board ${1}`,
|
|
room: rooms[1],
|
|
ipv4: `192.168.172.${1}`,
|
|
port: 1,
|
|
} as db.Board)))
|
|
expect(db.BoardTable.find("Board 3", "A1")).toEqual(Ok(Some({
|
|
id: 3,
|
|
name: `Board ${3}`,
|
|
room: rooms[3],
|
|
ipv4: `192.168.172.${3}`,
|
|
port: 3,
|
|
} as db.Board)))
|
|
|
|
// Test Count
|
|
expect(db.BoardTable.countByName("Board 1")).toBe(1)
|
|
expect(db.BoardTable.countByRoom("A1")).toBe(5)
|
|
|
|
|
|
})
|