#Why is this not entering row.Next()

4 messages · Page 1 of 1 (latest)

amber vigil
#
func (s *WordService) GetSynonyms(word string) (Synonyms, error) {
    // make sure word exists
    wordRow, err := s.GetWord(word)
    if err != nil {
        return Synonyms{}, fmt.Errorf("word %s does not exist", word)
    }
    synonyms := Synonyms{
        Word:     wordRow.Word,
        Synonyms: make([]string, 0),
    }

    rows, err := s.DB.QueryContext(context.Background(), GetSynonyms, wordRow.ID)
    if err != nil {
        return synonyms, err
    }

    defer rows.Close()

    for rows.Next() {
        fmt.Print("hello")
        var synonymsResult GetSynonymsResult
        if err := rows.Scan(&synonymsResult.SynonymID, &synonymsResult.WordID, &synonymsResult.Word); err != nil {
            return synonyms, err
        }
        synonyms.Synonyms = append(synonyms.Synonyms, synonymsResult.Word)
    }

    if err := rows.Err(); err != nil {
        return synonyms, fmt.Errorf("error iterating over rows: %v", err)
    }

    return synonyms, nil
}
#
func TestAddSynonym(t *testing.T) {
    db := setupTestDB(t)
    defer db.Close()

    wordService := &words.WordService{
        DB: db,
    }
    word := "dark"
    synonym := "shadowy"
    result, err := wordService.AddSynonym(word, synonym)
    if err != nil {
        t.Errorf("Failed to add synonym: %v", err)
    }
    if result != 1 {
        t.Errorf("expected row id to be %v got %v", 1, result)
    }
    synonyms, err := wordService.GetSynonyms(word)
    if err != nil {
        t.Errorf("Failed to get synonyms: %v", err)
    }
    js, err := json.Marshal(synonyms)
    if err != nil {
        t.Errorf("Failed to add synonym: %v", err)
    }
    fmt.Println(string(js))
}
#
=== RUN   TestAdd
--- PASS: TestAdd (0.00s)
=== RUN   TestGetWord
--- PASS: TestGetWord (0.00s)
=== RUN   TestAddSynonym
{"Word":"dark","Synonyms":[]}
--- PASS: TestAddSynonym (0.00s)
PASS
south oxide
#

Obvious things to check:

  • the AddSynonym() query actually works,
  • the insert and the select are in the same transaction,
  • they are in different transactions, but the insert is committed before the select
  • the sql of both sql statements are correct
    ...