I'm creating a crud application and I'm testing the database with an in-memory DB. The only test and functions that are breaking are the toggleTask func and test.
function:
func (r *Repository) ToggleTask(task Task) (Task, error) {
query := "UPDATE Tasks SET completed = NOT completed WHERE id = (?)"
_, err := r.db.Exec(query, task.ID)
if err != nil {
return task, err
}
query = "SELECT id, txt, completed FROM Tasks WHERE id = (?) RETURNING *"
err = r.db.QueryRow(query, task.ID).Scan(&task.ID, &task.Text, &task.ListID, &task.Completed)
if err != nil {
return task, err
}
return task, nil
}
test:
const (
ToggleTask = "SELECT id, txt, completed FROM Tasks WHERE id = (?) RETURNING *"
)
func TestToggleTask(t *testing.T) {
repo := mockDbRepo()
list := List{Name: "Test List"}
repo.db.Exec(CreateList, list.Name)
task := Task{Text: "Test Task", ListID: list.ID}
repo.db.Exec(CreateTask, task.Text, task.ListID)
completedTask,err := repo.ToggleTask(task)
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(completedTask, task) {
t.Errorf("Expected %v, got %v", task, completedTask)
}
}
The test returns to me:
SQL logic error: near "RETURNING": syntax error (1)
CodePudding user response:
Your SQL query is incorrect. As stated in the SQL Lite docs, SELECT
does not accept a RETURNING
clause. The SELECT
clause by itself returns data.
Remove your RETURNING
clauses in your queries and you should be good.
SQL Lite Ref: https://www.sqlite.org/lang_select.html