mirror of
https://github.com/mainflux/mainflux.git
synced 2025-05-01 13:48:56 +08:00

* Move messaging to pkg Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Move errors to pkg Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Move Transformers to pkg Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Move SDK to pkg Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Remove Transformers from root Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Fix make proto Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Add copyrights header Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Fix CI Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Move Auth client to pkg Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Fix dependencies Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Update dependencies and vendors Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Fix CI Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
54 lines
1.2 KiB
Go
54 lines
1.2 KiB
Go
// Copyright (c) Mainflux
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package mocks
|
|
|
|
import (
|
|
"sync"
|
|
|
|
"github.com/mainflux/mainflux/pkg/transformers/senml"
|
|
"github.com/mainflux/mainflux/readers"
|
|
)
|
|
|
|
var _ readers.MessageRepository = (*messageRepositoryMock)(nil)
|
|
|
|
type messageRepositoryMock struct {
|
|
mutex sync.Mutex
|
|
messages map[string][]senml.Message
|
|
}
|
|
|
|
// NewMessageRepository returns mock implementation of message repository.
|
|
func NewMessageRepository(messages map[string][]senml.Message) readers.MessageRepository {
|
|
return &messageRepositoryMock{
|
|
mutex: sync.Mutex{},
|
|
messages: messages,
|
|
}
|
|
}
|
|
|
|
func (repo *messageRepositoryMock) ReadAll(chanID string, offset, limit uint64, query map[string]string) (readers.MessagesPage, error) {
|
|
repo.mutex.Lock()
|
|
defer repo.mutex.Unlock()
|
|
|
|
end := offset + limit
|
|
|
|
numOfMessages := uint64(len(repo.messages[chanID]))
|
|
if offset < 0 || offset >= numOfMessages {
|
|
return readers.MessagesPage{}, nil
|
|
}
|
|
|
|
if limit < 1 {
|
|
return readers.MessagesPage{}, nil
|
|
}
|
|
|
|
if offset+limit > numOfMessages {
|
|
end = numOfMessages
|
|
}
|
|
|
|
return readers.MessagesPage{
|
|
Total: numOfMessages,
|
|
Limit: limit,
|
|
Offset: offset,
|
|
Messages: repo.messages[chanID][offset:end],
|
|
}, nil
|
|
}
|