TTime/backend/internal/database/db_test.go
2024-03-14 14:29:37 +01:00

127 lines
2.3 KiB
Go

package database
import (
"testing"
)
// Tests are not guaranteed to be sequential
func setupState() (Database, error) {
db := DbConnect(":memory:")
err := db.Migrate("../../migrations")
if err != nil {
return nil, err
}
return db, nil
}
func TestDbConnect(t *testing.T) {
db := DbConnect(":memory:")
_ = db
}
func TestDbAddUser(t *testing.T) {
db, err := setupState()
if err != nil {
t.Error("setupState failed:", err)
}
err = db.AddUser("test", "password")
if err != nil {
t.Error("AddUser failed:", err)
}
}
func TestDbGetUserId(t *testing.T) {
db, err := setupState()
if err != nil {
t.Error("setupState failed:", err)
}
if db.AddUser("test", "password") != nil {
t.Error("AddUser failed")
}
var id int
id, err = db.GetUserId("test")
if err != nil {
t.Error("GetUserId failed:", err)
}
if id != 1 {
t.Error("GetUserId failed: expected 1, got", id)
}
}
func TestDbAddProject(t *testing.T) {
db, err := setupState()
if err != nil {
t.Error("setupState failed:", err)
}
err = db.AddProject("test", "description", "test")
if err != nil {
t.Error("AddProject failed:", err)
}
}
func TestDbRemoveUser(t *testing.T) {
db, err := setupState()
if err != nil {
t.Error("setupState failed:", err)
}
err = db.RemoveUser("test")
if err != nil {
t.Error("RemoveUser failed:", err)
}
}
func TestPromoteToAdmin(t *testing.T) {
db, err := setupState()
if err != nil {
t.Error("setupState failed:", err)
}
err = db.AddUser("test", "password")
if err != nil {
t.Error("AddUser failed:", err)
}
err = db.PromoteToAdmin("test")
if err != nil {
t.Error("PromoteToAdmin failed:", err)
}
}
func TestDbChangeUserRole(t *testing.T) {
// Set up the initial state
db, err := setupState()
if err != nil {
t.Error("setupState failed:", err)
}
// Add a user
err = db.AddUser("test", "password")
if err != nil {
t.Error("AddUser failed:", err)
}
// Add a project
err = db.AddProject("test_project", "project description", "test")
if err != nil {
t.Error("AddProject failed:", err)
}
// Change user role
err = db.ChangeUserRole("test", "test_project", "project_manager")
if err != nil {
t.Error("ChangeUserRole failed:", err)
}
}
// func TestAddTimeReport(t *testing.T) {
// }
// func TestAddUserToProject(t *testing.T) {
// }