Skip to content

Commit

Permalink
NOISSUE - Fix count when search by name is performed (#767)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
dborovcanin authored and manuio committed Jun 24, 2019
1 parent 93ec2d3 commit 95c5a80
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 10 deletions.
6 changes: 4 additions & 2 deletions things/postgres/channels.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"database/sql"
"encoding/json"
"fmt"
"strings"

"github.com/gofrs/uuid"
"github.com/jmoiron/sqlx"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 11 additions & 3 deletions things/postgres/channels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -217,45 +218,52 @@ 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,
offset: 0,
limit: n,
name: "wrong",
size: 0,
total: 0,
},
}

for desc, tc := range cases {
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))
}
}
Expand Down
6 changes: 4 additions & 2 deletions things/postgres/things.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"database/sql"
"encoding/json"
"fmt"
"strings"

"github.com/gofrs/uuid"
"github.com/jmoiron/sqlx"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
13 changes: 10 additions & 3 deletions things/postgres/things_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -393,45 +393,52 @@ 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,
offset: 0,
limit: n,
name: "wrong",
size: 0,
total: 0,
},
}

for desc, tc := range cases {
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))
}
}
Expand Down

0 comments on commit 95c5a80

Please sign in to comment.