Adding more tests for adding a poll if it doesn't exist on get, Adding code to update votes

This commit is contained in:
2025-11-06 13:38:56 -05:00
parent 8fa1947214
commit ff365cec0d
2 changed files with 165 additions and 3 deletions

View File

@@ -30,8 +30,10 @@ func TestCreatePoll(t *testing.T) {
{RandString(10) + "2", 2},
{RandString(10) + "3", 3},
{"\"" + RandString(10) + "4", 4},
{"'" + RandString(10) + "5", 5},
{";" + RandString(10) + "6", 6},
{"\\\"" + RandString(10) + "5", 5},
{"'" + RandString(10) + "6", 6},
{";" + RandString(10) + "7", 7},
{"\\" + RandString(10) + "8", 8},
}
tmp_db, err := os.CreateTemp("", "vote_test.*.db")
@@ -117,7 +119,7 @@ func TestAlreadyExists(t *testing.T) {
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)
@@ -160,3 +162,55 @@ func TestGetPollByQuestion(t *testing.T) {
t.Fatalf(`Questions don't match: expected %s: recieved %s`, question, get_poll.Question)
}
}
func TestGetCreatePollByQuestion(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 {
new_poll, err := GetAndCreatePollByQuestion(parameters[i].question)
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)
}
if new_poll.Question != parameters[i].question {
t.Fatalf(`Incorrect question returned: Expected %s != %s`, parameters[i].question, new_poll.Question)
}
}
}