103 lines
2.4 KiB
Go
103 lines
2.4 KiB
Go
package db_test
|
|
|
|
import (
|
|
"JISQueueing/common"
|
|
"JISQueueing/db"
|
|
"testing"
|
|
)
|
|
|
|
func TestNewVisitor(t *testing.T) {
|
|
db.UseInMemoryDatabase()
|
|
db.NewVisitor(exampleVisitor())
|
|
db.NewVisitor(exampleVisitor2())
|
|
|
|
success, _ := db.NewVisitor(exampleDuplicateVisitor())
|
|
|
|
if success {
|
|
t.Error("Uploading duplicate email visitor caused return to be true, when expected false")
|
|
}
|
|
}
|
|
|
|
func TestGetVisitor(t *testing.T) {
|
|
db.UseInMemoryDatabase()
|
|
db.NewVisitor(exampleVisitor())
|
|
db.NewVisitor(exampleVisitor2())
|
|
|
|
visitor, err := db.GetVisitor(exampleVisitor().Email)
|
|
if err != nil {
|
|
t.Error("Expected visitor to be fetched correctly")
|
|
}
|
|
if visitor.Name != exampleVisitor().Name {
|
|
t.Errorf("Expected sample visitor to have %s as it's name, got %s instead", exampleVisitor().Name, visitor.Name)
|
|
}
|
|
|
|
visitor, err = db.GetVisitor(exampleVisitor2().Email)
|
|
if err != nil {
|
|
t.Error("Expected visitor to be fetched correctly")
|
|
}
|
|
if visitor.Name != exampleVisitor2().Name {
|
|
t.Errorf("Expected sample visitor to have %s as it's name, got %s instead", exampleVisitor2().Name, visitor.Name)
|
|
}
|
|
|
|
db.EditVisitor(exampleDuplicateVisitor())
|
|
visitor, err = db.GetVisitor(exampleDuplicateVisitor().Email)
|
|
if err != nil {
|
|
t.Error("Expected visitor to be fetched correctly")
|
|
}
|
|
if visitor.Name != exampleDuplicateVisitor().Name {
|
|
t.Errorf("Expected sample visitor to have %s as it's name, got %s instead", exampleDuplicateVisitor().Name, visitor.Name)
|
|
}
|
|
}
|
|
|
|
func TestEditVisitor(t *testing.T) {
|
|
db.UseInMemoryDatabase()
|
|
|
|
visitor := exampleVisitor()
|
|
db.NewVisitor(visitor)
|
|
visitor.Name = "New name"
|
|
success, _ := db.EditVisitor(visitor)
|
|
if !success {
|
|
t.Error("Expected visitor to be edited correctly")
|
|
}
|
|
}
|
|
|
|
func TestSetFirstTicket(t *testing.T) {
|
|
db.UseInMemoryDatabase()
|
|
|
|
visitor := exampleVisitor()
|
|
db.NewVisitor(visitor)
|
|
|
|
visitor.FirstTicket = 2
|
|
db.SetFirstTicket(visitor)
|
|
|
|
updated, err := db.GetVisitor(visitor.Email)
|
|
if err != nil {
|
|
t.Error("Expected visitor to be fetched correctly")
|
|
}
|
|
|
|
if updated.FirstTicket != 2 {
|
|
t.Error("Expected first ticket id to be updated correctly")
|
|
}
|
|
}
|
|
|
|
func exampleVisitor() common.Visitor {
|
|
return common.Visitor{
|
|
Email: "test@example.com",
|
|
Name: "Example Name",
|
|
}
|
|
}
|
|
|
|
func exampleVisitor2() common.Visitor {
|
|
return common.Visitor{
|
|
Email: "example@example.com",
|
|
Name: "John Doe",
|
|
}
|
|
}
|
|
|
|
func exampleDuplicateVisitor() common.Visitor {
|
|
return common.Visitor{
|
|
Email: "example@example.com",
|
|
Name: "Bob Smith",
|
|
}
|
|
}
|