163 lines
3.7 KiB
Go
163 lines
3.7 KiB
Go
package services
|
|
|
|
import (
|
|
"math/rand"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"go-sjles-pta-vote/server/config"
|
|
"go-sjles-pta-vote/server/db"
|
|
"go-sjles-pta-vote/server/models"
|
|
)
|
|
|
|
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-=`~!@#$%^&*()_+[]\\;',./{}|:\"<>?"
|
|
|
|
func RandString(length int) string {
|
|
rand_bytes := make([]byte, length)
|
|
for rand_index := range length {
|
|
rand_bytes[rand_index] = charset[rand.Intn(len(charset))]
|
|
}
|
|
return string(rand_bytes)
|
|
}
|
|
|
|
func TestCreatePoll(t *testing.T) {
|
|
parameters := []struct{
|
|
question string
|
|
table_index int64
|
|
}{
|
|
{RandString(10) + "1", 1},
|
|
{RandString(10) + "2", 2},
|
|
{RandString(10) + "3", 3},
|
|
{"\"" + RandString(10) + "4", 4},
|
|
{"'" + RandString(10) + "5", 5},
|
|
{";" + RandString(10) + "6", 6},
|
|
}
|
|
|
|
tmp_db, err := os.CreateTemp("", "vote_test.*.db")
|
|
if err != nil {
|
|
t.Errorf(`Failed to create temporary db file: %v`, err)
|
|
}
|
|
|
|
init_conf := &config.Config{
|
|
DBPath: string(tmp_db.Name()),
|
|
}
|
|
config.SetConfig(init_conf)
|
|
|
|
defer os.Remove(tmp_db.Name())
|
|
tmp_db.Close()
|
|
|
|
if _, err := db.Connect(); err != nil {
|
|
t.Errorf(`Failed to create the database: %v`, err)
|
|
}
|
|
|
|
for i := range parameters {
|
|
create_poll := &models.Poll{
|
|
Question: parameters[i].question,
|
|
ExpiresAt: time.Now().Add(time.Hour * 10).Format("2006-01-02 15:04:05"),
|
|
}
|
|
|
|
new_poll, err := CreatePoll(create_poll)
|
|
|
|
if err != nil {
|
|
t.Fatalf(`Failed to create new poll %s: %v`, parameters[i].question, err)
|
|
}
|
|
|
|
if new_poll == nil {
|
|
t.Fatalf(`Failed to insert %s into table`, parameters[i].question)
|
|
}
|
|
|
|
if new_poll.ID != parameters[i].table_index {
|
|
t.Fatalf(`Incorrect increment in index for %s: expected %d != %d`, parameters[i].question, parameters[i].table_index, new_poll.ID)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestAlreadyExists(t *testing.T) {
|
|
question := "TestQuestion"
|
|
|
|
tmp_db, err := os.CreateTemp("", "vote_test.*.db")
|
|
if err != nil {
|
|
t.Errorf(`Failed to create temporary db file: %v`, err)
|
|
}
|
|
|
|
init_conf := &config.Config{
|
|
DBPath: string(tmp_db.Name()),
|
|
}
|
|
config.SetConfig(init_conf)
|
|
|
|
defer os.Remove(tmp_db.Name())
|
|
tmp_db.Close()
|
|
|
|
if _, err := db.Connect(); err != nil {
|
|
t.Errorf(`Failed to create the database: %v`, err)
|
|
}
|
|
|
|
create_poll := &models.Poll{
|
|
Question: question,
|
|
ExpiresAt: time.Now().Add(time.Hour * 10).Format("2006-01-02 15:04:05"),
|
|
}
|
|
|
|
new_poll, err := CreatePoll(create_poll)
|
|
|
|
if err != nil {
|
|
t.Fatalf(`Failed to create new poll %s: %v`, question, err)
|
|
}
|
|
|
|
if new_poll == nil {
|
|
t.Fatalf(`Failed to insert %s into table`, question)
|
|
}
|
|
|
|
new_poll, err = CreatePoll(create_poll)
|
|
|
|
if err != ErrQuestionAlreadyExists {
|
|
t.Fatalf(`Should have failed adding %s as it already exists`, question)
|
|
}
|
|
}
|
|
|
|
func TestGetPollByQuestion(t *testing.T) {
|
|
question := "TestQuestion"
|
|
|
|
tmp_db, err := os.CreateTemp("", "vote_test.*.db")
|
|
if err != nil {
|
|
t.Errorf(`Failed to create temporary db file: %v`, err)
|
|
}
|
|
|
|
init_conf := &config.Config{
|
|
DBPath: string(tmp_db.Name()),
|
|
}
|
|
config.SetConfig(init_conf)
|
|
|
|
defer os.Remove(tmp_db.Name())
|
|
tmp_db.Close()
|
|
|
|
if _, err := db.Connect(); err != nil {
|
|
t.Errorf(`Failed to create the database: %v`, err)
|
|
}
|
|
|
|
create_poll := &models.Poll{
|
|
Question: question,
|
|
ExpiresAt: time.Now().Add(time.Hour * 10).Format("2006-01-02 15:04:05"),
|
|
}
|
|
|
|
new_poll, err := CreatePoll(create_poll)
|
|
|
|
if err != nil {
|
|
t.Fatalf(`Failed to create new poll %s: %v`, question, err)
|
|
}
|
|
|
|
if new_poll == nil {
|
|
t.Fatalf(`Failed to insert %s into table`, question)
|
|
}
|
|
|
|
get_poll, err := GetPollByQuestion(question)
|
|
|
|
if err != nil {
|
|
t.Fatalf(`Failed to get the poll %s: %v`, question, err)
|
|
}
|
|
|
|
if get_poll.Question != question {
|
|
t.Fatalf(`Questions don't match: expected %s: recieved %s`, question, get_poll.Question)
|
|
}
|
|
}
|