More robust testing
This commit is contained in:
@@ -8,6 +8,8 @@ import (
|
||||
"go-sjles-pta-vote/server/models"
|
||||
)
|
||||
|
||||
var ErrQuestionAlreadyExists = errors.New("Question already exists")
|
||||
|
||||
func CreatePoll(poll *models.Poll) (*models.Poll, error) {
|
||||
new_poll := models.Poll{}
|
||||
|
||||
@@ -33,7 +35,7 @@ func CreatePoll(poll *models.Poll) (*models.Poll, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return nil, errors.New("Already exists")
|
||||
return nil, ErrQuestionAlreadyExists
|
||||
}
|
||||
|
||||
stmt, err := db_conn.Prepare(`
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -10,7 +11,71 @@ import (
|
||||
"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)
|
||||
@@ -29,17 +94,23 @@ func TestCreatePoll(t *testing.T) {
|
||||
}
|
||||
|
||||
create_poll := &models.Poll{
|
||||
Question: "TestQuestion",
|
||||
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.Errorf(`Failed to insert into table: %v`, err)
|
||||
t.Fatalf(`Failed to create new poll %s: %v`, question, err)
|
||||
}
|
||||
|
||||
if new_poll == nil {
|
||||
t.Errorf(`Failed to insert into table`)
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user