clndr/api/test/models/user_test.go

77 lines
1.3 KiB
Go

package models_test
import (
"magmise/models"
"magmise/test"
"testing"
)
func TestUserCreate(t *testing.T) {
cleanup := test.Setup(t, "user create")
defer cleanup()
user := models.User{Username: "Bob", Password: "a"}
if user.Create() != nil {
t.Fail()
}
}
func TestUserRead(t *testing.T) {
cleanup := test.Setup(t, "user read")
defer cleanup()
user := models.User{Username: "fred"}
if user.Read() != nil {
t.Fatal("Faied to read application user")
}
if user.Password != "a" {
t.Log(user)
}
}
func TestUserReadAll(t *testing.T) {
cleanup := test.Setup(t, "user readall")
defer cleanup()
user := models.User{}
users, err := user.ReadAll(0, 10)
if err != nil {
t.Fatal(err)
}
if users[0].Username != "fred" {
t.Log(users)
t.Fail()
}
}
func TestUserUpdate(t *testing.T) {
cleanup := test.Setup(t, "user update")
defer cleanup()
user := models.User{Username: "fred"}
user.Read()
user.Password = "b"
if user.Update() != nil {
t.Fail()
}
user.Read()
if user.Password != "b" {
t.Fail()
}
}
func TestUserDelete(t *testing.T) {
cleanup := test.Setup(t, "user delete")
defer cleanup()
user := models.User{Username: "fred"}
if err := user.Delete(); err != nil {
t.Fatal(err)
}
t.Log()
t.Log("\033[33m\033[1mShould error with \033[95mrecord not found\033[0m")
if user.Read() == nil {
t.Fatal(user)
}
}