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

* Commit for mainflux-596 Modified and tested: - cli - http - mqtt - normalizer - all readers - sdk messages - all writers - ws Missing: - coap - lora Signed-off-by: ale <ale@metaverso.org> * - fix subtopic name in, when starting with dot, http/ws/mqtt - add some test on readers Signed-off-by: ale <ale@metaverso.org> * - fix string concatenation - update http/transport regexp to match subtopic names with only \w- - update ws/transport regexp like http ones with also the wildcard * and > Signed-off-by: ale <ale@metaverso.org> * added subtopic support to coap adapter Signed-off-by: ale <ale@metaverso.org> * - update replace functions with replaceall when needed - renamed getDestChannel to fmtSubject - update api/transport and ws/transport route to be more readable - fix mqtt syntax - renamed func andQuery to query as suggested by @anovakovic01 - have a nice we :) Signed-off-by: ale <ale@metaverso.org> * - fix error declaration on ws/nat/publisher - fix regexp added missing allowed chars - and _ on coap/api/transport - fix subtopic clean suffix / if present on coap/api/transport - improve regexp on http and ws /api/transport, now does not accept url that do not strictly match - add some ws subtopic tests Signed-off-by: ale <ale@metaverso.org> * - enabled wildcard chars on coap/api/transport - allow use special chars on http and ws api/transport Signed-off-by: ale <ale@metaverso.org> * - use strings.Replace() insted ReplaceAll() Signed-off-by: ale <ale@metaverso.org> * - allow every chars on subtopics - fix replace error on mqtt Signed-off-by: ale <ale@metaverso.org> * fix cassandra test Signed-off-by: ale <ale@metaverso.org> * fix ws test with invalid subtopic Signed-off-by: ale <ale@metaverso.org> * fix invalid GOCACHE in go1.12, replaced by -count 1, see https://golang.org/doc/go1.10#test Signed-off-by: ale <ale@metaverso.org> * - improve regexp on http/ws api/transport - minor changes Signed-off-by: ale <ale@metaverso.org> * - add generic function parseSubtopic on ws/http adapters Signed-off-by: ale <ale@metaverso.org> * - add generic function fmtSubtopic on coap adapter Signed-off-by: ale <ale@metaverso.org>
137 lines
3.3 KiB
Go
137 lines
3.3 KiB
Go
package influxdb_test
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
influxdata "github.com/influxdata/influxdb/client/v2"
|
|
"github.com/mainflux/mainflux"
|
|
reader "github.com/mainflux/mainflux/readers/influxdb"
|
|
writer "github.com/mainflux/mainflux/writers/influxdb"
|
|
|
|
log "github.com/mainflux/mainflux/logger"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
const (
|
|
testDB = "test"
|
|
chanID = "1"
|
|
subtopic = "topic"
|
|
msgsNum = 101
|
|
valueFields = 6
|
|
)
|
|
|
|
var (
|
|
port string
|
|
client influxdata.Client
|
|
testLog, _ = log.New(os.Stdout, log.Info.String())
|
|
|
|
clientCfg = influxdata.HTTPConfig{
|
|
Username: "test",
|
|
Password: "test",
|
|
}
|
|
|
|
msg = mainflux.Message{
|
|
Channel: chanID,
|
|
Publisher: "1",
|
|
Protocol: "mqtt",
|
|
Name: "name",
|
|
Unit: "U",
|
|
Value: &mainflux.Message_FloatValue{FloatValue: 5},
|
|
ValueSum: &mainflux.SumValue{Value: 45},
|
|
Time: 123456,
|
|
UpdateTime: 1234,
|
|
Link: "link",
|
|
}
|
|
)
|
|
|
|
func TestReadAll(t *testing.T) {
|
|
writer, err := writer.New(client, testDB, 1, time.Second)
|
|
require.Nil(t, err, fmt.Sprintf("Creating new InfluxDB writer expected to succeed: %s.\n", err))
|
|
|
|
messages := []mainflux.Message{}
|
|
now := time.Now().Unix()
|
|
for i := 0; i < msgsNum; i++ {
|
|
// Mix possible values as well as value sum.
|
|
count := i % valueFields
|
|
switch count {
|
|
case 0:
|
|
msg.Subtopic = subtopic
|
|
msg.Value = &mainflux.Message_FloatValue{FloatValue: 5}
|
|
case 1:
|
|
msg.Value = &mainflux.Message_BoolValue{BoolValue: false}
|
|
case 2:
|
|
msg.Value = &mainflux.Message_StringValue{StringValue: "value"}
|
|
case 3:
|
|
msg.Value = &mainflux.Message_DataValue{DataValue: "base64data"}
|
|
case 4:
|
|
msg.ValueSum = nil
|
|
case 5:
|
|
msg.ValueSum = &mainflux.SumValue{Value: 45}
|
|
}
|
|
msg.Time = float64(now - int64(i))
|
|
|
|
err := writer.Save(msg)
|
|
require.Nil(t, err, fmt.Sprintf("failed to store message to InfluxDB: %s", err))
|
|
messages = append(messages, msg)
|
|
}
|
|
|
|
reader, err := reader.New(client, testDB)
|
|
require.Nil(t, err, fmt.Sprintf("Creating new InfluxDB reader expected to succeed: %s.\n", err))
|
|
|
|
cases := map[string]struct {
|
|
chanID string
|
|
offset uint64
|
|
limit uint64
|
|
query map[string]string
|
|
messages []mainflux.Message
|
|
}{
|
|
"read message page for existing channel": {
|
|
chanID: chanID,
|
|
offset: 0,
|
|
limit: 10,
|
|
messages: messages[0:10],
|
|
},
|
|
"read message page for too large limit": {
|
|
chanID: chanID,
|
|
offset: 0,
|
|
limit: 101,
|
|
messages: messages[0:100],
|
|
},
|
|
"read message page for non-existent channel": {
|
|
chanID: "2",
|
|
offset: 0,
|
|
limit: 10,
|
|
messages: []mainflux.Message{},
|
|
},
|
|
"read message last page": {
|
|
chanID: chanID,
|
|
offset: 95,
|
|
limit: 10,
|
|
messages: messages[95:101],
|
|
},
|
|
"read message with non-existent subtopic": {
|
|
chanID: chanID,
|
|
offset: 0,
|
|
limit: msgsNum,
|
|
query: map[string]string{"subtopic": "not-present"},
|
|
messages: []mainflux.Message{},
|
|
},
|
|
"read message with subtopic": {
|
|
chanID: chanID,
|
|
offset: 0,
|
|
limit: 10,
|
|
query: map[string]string{"subtopic": subtopic},
|
|
messages: messages[0:10],
|
|
},
|
|
}
|
|
|
|
for desc, tc := range cases {
|
|
result := reader.ReadAll(tc.chanID, tc.offset, tc.limit, tc.query)
|
|
assert.ElementsMatch(t, tc.messages, result, fmt.Sprintf("%s: expected: %v \n-------------\n got: %v", desc, tc.messages, result))
|
|
}
|
|
}
|