More robust testing
This commit is contained in:
@@ -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 (
|
||||
|
||||
@@ -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`)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user