Skip to content

Commit

Permalink
#36: Add tests TestDB_WhereNotExists, Fix len for First
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurkushman committed Sep 8, 2019
1 parent 61149f6 commit 1a49151
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
5 changes: 4 additions & 1 deletion advanced.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ func (r *DB) First() (map[string]interface{}, error) {
return nil, err
}

return res[0], nil
if len(res) > 0 {
return res[0], nil
}
return nil, fmt.Errorf("no records were produced by query: %s", r.Builder.buildSelect())
}

// Value gets the value of column in first query resulting row
Expand Down
18 changes: 15 additions & 3 deletions builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,10 +352,22 @@ func TestDB_WhereExists(t *testing.T) {
db.Table(UsersTable).Select("name").Where("points", ">=", int64(12345)),
).First()
assert.NoError(t, er)
assert.Equal(t, TestUserName, res["name"])

if res["name"] != TestUserName {
t.Fatalf("want %s, got: %s", TestUserName, res["name"])
}
db.Truncate(UsersTable)
}

func TestDB_WhereNotExists(t *testing.T) {
db.Truncate(UsersTable)

err := db.Table(UsersTable).InsertBatch(batchUsers)
assert.NoError(t, err)

res, er := db.Table(UsersTable).Select("name").WhereNotExists(
db.Table(UsersTable).Select("name").Where("points", ">=", int64(12345)),
).First()
assert.NoError(t, er)
assert.Equal(t, TestUserName, res["name"])

db.Truncate(UsersTable)
}
Expand Down

0 comments on commit 1a49151

Please sign in to comment.