The backend for the AiteStory website
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

233 lines
3.8 KiB

package model
import (
"testing"
"git.aiterp.net/AiteRP/aitestory/server"
)
func TestTag(t *testing.T) {
if server.Main.Config.DB.Password == "" {
t.Skip("No database password")
return
}
var testTag *Tag
name := "Te'Eryvi (Test)"
id := ""
t.Run("Insert", func(t *testing.T) {
tag := Tag{}
tag.Name = name
tag.Type = "Organization"
err := tag.Insert()
if err != nil {
t.Log("Failed to insert:", err)
t.Fail()
}
id = tag.ID
})
t.Run("Insert_BadType", func(t *testing.T) {
tag := Tag{}
tag.Name = "Ehanis Tioran"
tag.Type = "Karakter"
err := tag.Insert()
if err == nil {
t.Log("Oops, it was inserted.")
t.Fail()
return
}
if err.Error() != `invalid tag type` {
t.Log("Wrong error:", err)
t.Fail()
}
})
t.Run("Insert_BadName", func(t *testing.T) {
tag := Tag{}
tag.Name = ""
tag.Type = "Character"
err := tag.Insert()
if err == nil {
t.Log("Oops, it was inserted.")
t.Fail()
return
}
if err.Error() != `invalid length` {
t.Log("Wrong error:", err)
t.Fail()
}
})
t.Run("List", func(t *testing.T) {
tags, err := ListTags()
if err != nil {
t.Log("Failed to get tags:", err)
t.Fail()
}
if len(tags) == 0 {
t.Log("No tags found")
t.Fail()
}
t.Logf("%d tags found", len(tags))
found := false
for _, tag := range tags {
if tag.Name == name && tag.ID == id {
t.Logf("Tag found: %+v", tag)
found = true
break
}
}
if !found {
t.Log("The tag inserted in last test wasn't found")
t.Fail()
}
})
t.Run("FindByID", func(t *testing.T) {
tag, err := FindTag("id", id)
if err != nil {
t.Log("Failed to find tag:", err)
t.Fail()
}
if tag == nil {
t.Log("No tag found")
t.Fail()
return
}
t.Logf("Tag found: %+v", tag)
if tag.Name != name || tag.ID != id {
t.Error("Incorrect tag")
t.Fail()
}
testTag = tag
})
t.Run("FindByName", func(t *testing.T) {
tag, err := FindTag("name", name)
if err != nil {
t.Log("Failed to find tag:", err)
t.Fail()
}
if tag == nil {
t.Log("No tag found")
t.Fail()
return
}
t.Logf("Tag found: %+v", tag)
if tag.Name != name || tag.ID != id {
t.Error("Incorrect tag")
t.Fail()
}
testTag = tag
})
t.Run("Update", func(t *testing.T) {
if testTag == nil {
t.Error("No tag to modify")
t.Fail()
return
}
testTag.Type = "Character"
testTag.Name = "Urdnot Sild"
err := testTag.Update()
if err != nil {
t.Error("Modify failed:", err)
t.Fail()
}
tag, err := FindTag("name", name)
if err != nil && err.Error() != "not found" {
t.Log("Failed to get tags:", err)
t.Fail()
}
if tag != nil {
t.Log("Tag found by old name")
t.Fail()
return
}
name = testTag.Name
})
t.Run("FindByName", func(t *testing.T) {
tag, err := FindTag("name", name)
if err != nil {
t.Log("Failed to get tags:", err)
t.Fail()
}
if tag == nil {
t.Log("No tag found")
t.Fail()
return
}
t.Logf("Tag found: %+v", tag)
if tag.Name != name || tag.ID != id {
t.Error("Incorrect tag")
t.Fail()
}
})
t.Run("EnsureTag", func(t *testing.T) {
tag, err := EnsureTag("Character", "Renala T'Iavay (Test)")
if err != nil {
t.Error(err)
return
}
err = tag.Delete()
if err != nil {
t.Error(err)
return
}
if tag.ID == testTag.ID {
t.Error("Ensured tag matched old tag")
return
}
tag2, err := EnsureTag(testTag.Type, testTag.Name)
if err != nil {
t.Error(err)
return
}
if tag2.ID != testTag.ID {
t.Error("Second ensured tag did not match testTag")
return
}
})
t.Run("Delete", func(t *testing.T) {
if testTag == nil {
t.Error("No tag to delete")
t.Fail()
return
}
err := testTag.Delete()
if err != nil {
t.Error("Delete failed:", err)
t.Fail()
}
})
}