Skip to content

Commit

Permalink
Add tests for wildcard state save
Browse files Browse the repository at this point in the history
Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com>
  • Loading branch information
darkodraskovic committed Jul 8, 2020
1 parent 35fc34c commit a9798df
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
16 changes: 12 additions & 4 deletions twins/mocks/states.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,11 @@ func (srm *stateRepositoryMock) RetrieveAll(ctx context.Context, offset uint64,
srm.mu.Lock()
defer srm.mu.Unlock()

items := make([]twins.State, 0)

if limit <= 0 {
return twins.StatesPage{}, nil
}

items := make([]twins.State, 0)
for k, v := range srm.states {
if (uint64)(len(items)) >= limit {
break
Expand All @@ -78,11 +77,10 @@ func (srm *stateRepositoryMock) RetrieveAll(ctx context.Context, offset uint64,
return items[i].ID < items[j].ID
})

total := uint64(len(srm.states))
page := twins.StatesPage{
States: items,
PageMetadata: twins.PageMetadata{
Total: total,
Total: srm.total(twinID),
Offset: offset,
Limit: limit,
},
Expand All @@ -91,6 +89,16 @@ func (srm *stateRepositoryMock) RetrieveAll(ctx context.Context, offset uint64,
return page, nil
}

func (srm *stateRepositoryMock) total(twinID string) uint64 {
var total uint64
for k := range srm.states {
if strings.HasPrefix(k, twinID) {
total++
}
}
return total
}

// RetrieveLast returns the last state related to twin spec by id
func (srm *stateRepositoryMock) RetrieveLast(ctx context.Context, twinID string) (twins.State, error) {
srm.mu.Lock()
Expand Down
6 changes: 1 addition & 5 deletions twins/mocks/twins.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,7 @@ func (trm *twinRepositoryMock) RetrieveByAttribute(ctx context.Context, channel,
}
}
}

if len(ids) > 0 {
return ids, nil
}
return ids, twins.ErrNotFound
return ids, nil
}

func (trm *twinRepositoryMock) RetrieveAll(_ context.Context, owner string, offset uint64, limit uint64, name string, metadata twins.Metadata) (twins.Page, error) {
Expand Down
10 changes: 9 additions & 1 deletion twins/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,10 @@ func TestSaveStates(t *testing.T) {
tw, err := svc.AddTwin(context.Background(), token, twin, def)
require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err))

defWildcard := mocks.CreateDefinition(channels[0:2], []string{twins.SubtopicWildcard, twins.SubtopicWildcard})
twWildcard, err := svc.AddTwin(context.Background(), token, twin, defWildcard)
require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err))

var recs = make([]senml.Record, numRecs)
mocks.CreateSenML(numRecs, recs)

Expand Down Expand Up @@ -300,12 +304,16 @@ func TestSaveStates(t *testing.T) {
require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err))

err = svc.SaveStates(message)
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err))

ttlAdded += tc.size
page, err := svc.ListStates(context.TODO(), token, 0, 10, tw.ID)
require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err))
assert.Equal(t, ttlAdded, page.Total, fmt.Sprintf("%s: expected %d total got %d total\n", tc.desc, ttlAdded, page.Total))

page, err = svc.ListStates(context.TODO(), token, 0, 10, twWildcard.ID)
require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err))
assert.Equal(t, ttlAdded, page.Total, fmt.Sprintf("%s: expected %d total got %d total\n", tc.desc, ttlAdded, page.Total))
}
}

Expand Down

0 comments on commit a9798df

Please sign in to comment.