More robust testing

This commit is contained in:
2025-11-04 17:16:42 -05:00
parent 5e8b4e2b61
commit 5d579781a8
2 changed files with 24 additions and 2 deletions

View File

@@ -1,6 +1,9 @@
package services
import (
"database/sql"
"errors"
"go-sjles-pta-vote/server/db"
"go-sjles-pta-vote/server/models"
)
@@ -13,6 +16,25 @@ func CreatePoll(poll *models.Poll) (*models.Poll, error) {
return nil, err
}
defer db.Close()
get_stmt, err := db_conn.Prepare(`
SELECT id
FROM polls
WHERE question == $1
`)
if err != nil {
return nil, err
}
defer get_stmt.Close()
var id int
err = get_stmt.QueryRow(poll.Question).Scan(&id)
if err != sql.ErrNoRows {
if err != nil {
return nil, err
}
return nil, errors.New("Already exists")
}
stmt, err := db_conn.Prepare(`
INSERT INTO polls (

View File

@@ -39,7 +39,7 @@ func TestCreatePoll(t *testing.T) {
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)
if new_poll == nil {
t.Errorf(`Failed to insert into table`)
}
}