2024-02-12 12:40:49 +01:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2024-02-27 05:51:16 +01:00
|
|
|
// Tests are not guaranteed to be sequential
|
2024-02-28 03:21:13 +01:00
|
|
|
|
2024-02-29 20:33:20 +01:00
|
|
|
func setupState() (Database, error) {
|
2024-02-28 03:21:13 +01:00
|
|
|
db := DbConnect(":memory:")
|
|
|
|
err := db.Migrate("../../migrations")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return db, nil
|
|
|
|
}
|
2024-02-27 05:51:16 +01:00
|
|
|
|
2024-02-12 12:40:49 +01:00
|
|
|
func TestDbConnect(t *testing.T) {
|
2024-02-28 03:21:13 +01:00
|
|
|
db := DbConnect(":memory:")
|
2024-02-12 12:40:49 +01:00
|
|
|
_ = db
|
|
|
|
}
|
2024-02-27 05:00:04 +01:00
|
|
|
|
|
|
|
func TestDbAddUser(t *testing.T) {
|
2024-02-28 03:21:13 +01:00
|
|
|
db, err := setupState()
|
|
|
|
if err != nil {
|
|
|
|
t.Error("setupState failed:", err)
|
|
|
|
}
|
|
|
|
err = db.AddUser("test", "password")
|
2024-02-27 05:00:04 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Error("AddUser failed:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-27 05:51:16 +01:00
|
|
|
func TestDbGetUserId(t *testing.T) {
|
2024-02-28 03:21:13 +01:00
|
|
|
db, err := setupState()
|
|
|
|
if err != nil {
|
|
|
|
t.Error("setupState failed:", err)
|
|
|
|
}
|
2024-03-02 04:29:50 +01:00
|
|
|
if db.AddUser("test", "password") != nil {
|
|
|
|
t.Error("AddUser failed")
|
|
|
|
}
|
2024-02-27 05:51:16 +01:00
|
|
|
|
|
|
|
var id int
|
|
|
|
|
2024-02-28 03:21:13 +01:00
|
|
|
id, err = db.GetUserId("test")
|
2024-02-27 05:51:16 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Error("GetUserId failed:", err)
|
|
|
|
}
|
|
|
|
if id != 1 {
|
|
|
|
t.Error("GetUserId failed: expected 1, got", id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-27 07:59:42 +01:00
|
|
|
func TestDbAddProject(t *testing.T) {
|
2024-02-28 03:21:13 +01:00
|
|
|
db, err := setupState()
|
|
|
|
if err != nil {
|
|
|
|
t.Error("setupState failed:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = db.AddProject("test", "description", "test")
|
2024-02-27 07:59:42 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Error("AddProject failed:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-27 05:00:04 +01:00
|
|
|
func TestDbRemoveUser(t *testing.T) {
|
2024-02-28 03:21:13 +01:00
|
|
|
db, err := setupState()
|
|
|
|
if err != nil {
|
|
|
|
t.Error("setupState failed:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = db.RemoveUser("test")
|
2024-02-27 05:00:04 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Error("RemoveUser failed:", err)
|
|
|
|
}
|
|
|
|
}
|