Adding tests for getting poll data

This commit is contained in:
2025-11-05 16:18:06 -05:00
parent 711e77e8b8
commit 8fa1947214
3 changed files with 114 additions and 1 deletions

View File

@@ -113,4 +113,50 @@ func TestAlreadyExists(t *testing.T) {
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)
}
}