1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-24 13:48:49 +08:00

NOISSUE - Fix count when search by name is performed (#767)

* Fix total field when search by name

Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com>

* Fix tests to check total count

Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com>
This commit is contained in:
Dušan Borovčanin 2019-06-24 14:03:48 +02:00 committed by Manuel Imperiale
parent f27cb1d019
commit aa219b6f99
4 changed files with 29 additions and 10 deletions

View File

@ -11,6 +11,7 @@ import (
"database/sql"
"encoding/json"
"fmt"
"strings"
"github.com/gofrs/uuid"
"github.com/jmoiron/sqlx"
@ -108,10 +109,11 @@ func (cr channelRepository) RetrieveByID(owner, id string) (things.Channel, erro
}
func (cr channelRepository) RetrieveAll(owner string, offset, limit uint64, name string) (things.ChannelsPage, error) {
name = strings.ToLower(name)
nq := ""
if name != "" {
name = fmt.Sprintf(`%%%s%%`, name)
nq = `AND name LIKE :name`
nq = `AND LOWER(name) LIKE :name`
}
q := fmt.Sprintf(`SELECT id, name, metadata FROM channels
@ -145,7 +147,7 @@ func (cr channelRepository) RetrieveAll(owner string, offset, limit uint64, name
cq := ""
if name != "" {
cq = `AND name = $2`
cq = `AND LOWER(name) LIKE $2`
}
q = fmt.Sprintf(`SELECT COUNT(*) FROM channels WHERE owner = $1 %s;`, cq)

View File

@ -204,7 +204,8 @@ func TestMultiChannelRetrieval(t *testing.T) {
Owner: email,
}
if i == 0 {
// Create first two Channels with name.
if i < 2 {
c.Name = channelName
}
@ -217,31 +218,36 @@ func TestMultiChannelRetrieval(t *testing.T) {
limit uint64
name string
size uint64
total uint64
}{
"retrieve all channels with existing owner": {
owner: email,
offset: 0,
limit: n,
size: n,
total: n,
},
"retrieve subset of channels with existing owner": {
owner: email,
offset: n / 2,
limit: n,
size: n / 2,
total: n,
},
"retrieve channels with non-existing owner": {
owner: wrongValue,
offset: n / 2,
limit: n,
size: 0,
total: 0,
},
"retrieve all channels with existing name": {
"retrieve channels with existing name": {
owner: email,
offset: 0,
offset: 1,
limit: n,
name: channelName,
size: 1,
total: 2,
},
"retrieve all channels with non-existing name": {
owner: email,
@ -249,6 +255,7 @@ func TestMultiChannelRetrieval(t *testing.T) {
limit: n,
name: "wrong",
size: 0,
total: 0,
},
}
@ -256,6 +263,7 @@ func TestMultiChannelRetrieval(t *testing.T) {
page, err := chanRepo.RetrieveAll(tc.owner, tc.offset, tc.limit, tc.name)
size := uint64(len(page.Channels))
assert.Equal(t, tc.size, size, fmt.Sprintf("%s: expected %d got %d\n", desc, tc.size, size))
assert.Equal(t, tc.total, page.Total, fmt.Sprintf("%s: expected %d got %d\n", desc, tc.total, page.Total))
assert.Nil(t, err, fmt.Sprintf("%s: expected no error got %d\n", desc, err))
}
}

View File

@ -11,6 +11,7 @@ import (
"database/sql"
"encoding/json"
"fmt"
"strings"
"github.com/gofrs/uuid"
"github.com/jmoiron/sqlx"
@ -170,10 +171,11 @@ func (tr thingRepository) RetrieveByKey(key string) (string, error) {
}
func (tr thingRepository) RetrieveAll(owner string, offset, limit uint64, name string) (things.ThingsPage, error) {
name = strings.ToLower(name)
nq := ""
if name != "" {
name = fmt.Sprintf(`%%%s%%`, name)
nq = `AND name LIKE :name`
nq = `AND LOWER(name) LIKE :name`
}
q := fmt.Sprintf(`SELECT id, name, key, metadata FROM things
@ -209,7 +211,7 @@ func (tr thingRepository) RetrieveAll(owner string, offset, limit uint64, name s
cq := ""
if name != "" {
cq = `AND name = $2`
cq = `AND LOWER(name) LIKE $2`
}
q = fmt.Sprintf(`SELECT COUNT(*) FROM things WHERE owner = $1 %s;`, cq)

View File

@ -379,8 +379,8 @@ func TestMultiThingRetrieval(t *testing.T) {
Key: thkey,
}
// Create first Thing with name
if i == 0 {
// Create first two Things with name.
if i < 2 {
th.Name = name
}
@ -393,31 +393,36 @@ func TestMultiThingRetrieval(t *testing.T) {
limit uint64
name string
size uint64
total uint64
}{
"retrieve all things with existing owner": {
owner: email,
offset: 0,
limit: n,
size: n,
total: n,
},
"retrieve subset of things with existing owner": {
owner: email,
offset: n / 2,
limit: n,
size: n / 2,
total: n,
},
"retrieve things with non-existing owner": {
owner: wrongValue,
offset: 0,
limit: n,
size: 0,
total: 0,
},
"retrieve things with existing name": {
owner: email,
offset: 0,
offset: 1,
limit: n,
name: name,
size: 1,
total: 2,
},
"retrieve things with non-existing name": {
owner: email,
@ -425,6 +430,7 @@ func TestMultiThingRetrieval(t *testing.T) {
limit: n,
name: "wrong",
size: 0,
total: 0,
},
}
@ -432,6 +438,7 @@ func TestMultiThingRetrieval(t *testing.T) {
page, err := thingRepo.RetrieveAll(tc.owner, tc.offset, tc.limit, tc.name)
size := uint64(len(page.Things))
assert.Equal(t, tc.size, size, fmt.Sprintf("%s: expected %d got %d\n", desc, tc.size, size))
assert.Equal(t, tc.total, page.Total, fmt.Sprintf("%s: expected %d got %d\n", desc, tc.total, page.Total))
assert.Nil(t, err, fmt.Sprintf("%s: expected no error got %d\n", desc, err))
}
}