45 lines
928 B
Go
45 lines
928 B
Go
package services
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"go-sjles-pta-vote/server/config"
|
|
"go-sjles-pta-vote/server/db"
|
|
"go-sjles-pta-vote/server/models"
|
|
)
|
|
|
|
func TestCreatePoll(t *testing.T) {
|
|
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: "TestQuestion",
|
|
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)
|
|
}
|
|
|
|
if new_poll.ID != 1 {
|
|
t.Errorf(`Failed to insert into table: Index %d: error %v`, new_poll.ID, err)
|
|
}
|
|
} |