2018-08-25 12:48:03 +02:00
|
|
|
package influxdb_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"testing"
|
2018-09-23 01:53:03 +02:00
|
|
|
"time"
|
2018-08-25 12:48:03 +02:00
|
|
|
|
|
|
|
influxdata "github.com/influxdata/influxdb/client/v2"
|
2019-04-25 00:18:43 +02:00
|
|
|
"github.com/mainflux/mainflux/readers"
|
2018-08-25 12:48:03 +02:00
|
|
|
reader "github.com/mainflux/mainflux/readers/influxdb"
|
2019-11-05 11:57:16 +01:00
|
|
|
"github.com/mainflux/mainflux/transformers/senml"
|
2018-08-25 12:48:03 +02:00
|
|
|
writer "github.com/mainflux/mainflux/writers/influxdb"
|
|
|
|
|
|
|
|
log "github.com/mainflux/mainflux/logger"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-11-05 11:57:16 +01:00
|
|
|
testDB = "test"
|
|
|
|
chanID = "1"
|
|
|
|
subtopic = "topic"
|
|
|
|
msgsNum = 101
|
2018-08-25 12:48:03 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-11-05 11:57:16 +01:00
|
|
|
v float64 = 5
|
|
|
|
stringV = "value"
|
|
|
|
boolV = true
|
|
|
|
dataV = "base64"
|
|
|
|
sum float64 = 42
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
valueFields = 5
|
|
|
|
port string
|
|
|
|
client influxdata.Client
|
|
|
|
testLog, _ = log.New(os.Stdout, log.Info.String())
|
2018-11-05 19:18:51 +01:00
|
|
|
|
2018-08-25 12:48:03 +02:00
|
|
|
clientCfg = influxdata.HTTPConfig{
|
|
|
|
Username: "test",
|
|
|
|
Password: "test",
|
|
|
|
}
|
2018-11-05 19:18:51 +01:00
|
|
|
|
2019-11-05 11:57:16 +01:00
|
|
|
m = senml.Message{
|
2018-11-05 19:18:51 +01:00
|
|
|
Channel: chanID,
|
2018-12-05 13:09:25 +01:00
|
|
|
Publisher: "1",
|
2018-11-05 19:18:51 +01:00
|
|
|
Protocol: "mqtt",
|
|
|
|
Name: "name",
|
|
|
|
Unit: "U",
|
|
|
|
Time: 123456,
|
2018-11-18 16:42:39 +01:00
|
|
|
UpdateTime: 1234,
|
2018-11-05 19:18:51 +01:00
|
|
|
Link: "link",
|
2018-08-25 12:48:03 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestReadAll(t *testing.T) {
|
2019-10-31 14:04:47 +01:00
|
|
|
writer := writer.New(client, testDB)
|
2018-08-25 12:48:03 +02:00
|
|
|
|
2019-11-05 11:57:16 +01:00
|
|
|
messages := []senml.Message{}
|
|
|
|
subtopicMsgs := []senml.Message{}
|
2018-11-18 16:42:39 +01:00
|
|
|
now := time.Now().Unix()
|
2018-11-05 19:18:51 +01:00
|
|
|
for i := 0; i < msgsNum; i++ {
|
|
|
|
// Mix possible values as well as value sum.
|
|
|
|
count := i % valueFields
|
2019-11-05 11:57:16 +01:00
|
|
|
msg := m
|
2018-11-05 19:18:51 +01:00
|
|
|
switch count {
|
|
|
|
case 0:
|
2019-03-15 18:38:07 +01:00
|
|
|
msg.Subtopic = subtopic
|
2019-11-05 11:57:16 +01:00
|
|
|
msg.Value = &v
|
2018-11-05 19:18:51 +01:00
|
|
|
case 1:
|
2019-11-05 11:57:16 +01:00
|
|
|
msg.BoolValue = &boolV
|
2018-11-05 19:18:51 +01:00
|
|
|
case 2:
|
2019-11-05 11:57:16 +01:00
|
|
|
msg.StringValue = &stringV
|
2018-11-05 19:18:51 +01:00
|
|
|
case 3:
|
2019-11-05 11:57:16 +01:00
|
|
|
msg.DataValue = &dataV
|
2018-11-05 19:18:51 +01:00
|
|
|
case 4:
|
2019-11-05 11:57:16 +01:00
|
|
|
msg.Sum = &sum
|
2018-11-05 19:18:51 +01:00
|
|
|
}
|
2019-11-05 11:57:16 +01:00
|
|
|
|
2018-12-18 15:17:55 +01:00
|
|
|
msg.Time = float64(now - int64(i))
|
2018-08-25 12:48:03 +02:00
|
|
|
messages = append(messages, msg)
|
2019-04-25 00:18:43 +02:00
|
|
|
if count == 0 {
|
|
|
|
subtopicMsgs = append(subtopicMsgs, msg)
|
|
|
|
}
|
2018-08-25 12:48:03 +02:00
|
|
|
}
|
|
|
|
|
2019-10-31 14:04:47 +01:00
|
|
|
err := writer.Save(messages...)
|
|
|
|
require.Nil(t, err, fmt.Sprintf("failed to store message to InfluxDB: %s", err))
|
|
|
|
|
2019-05-07 15:59:18 +02:00
|
|
|
reader := reader.New(client, testDB)
|
2018-08-25 12:48:03 +02:00
|
|
|
require.Nil(t, err, fmt.Sprintf("Creating new InfluxDB reader expected to succeed: %s.\n", err))
|
|
|
|
|
|
|
|
cases := map[string]struct {
|
2019-04-25 00:18:43 +02:00
|
|
|
chanID string
|
|
|
|
offset uint64
|
|
|
|
limit uint64
|
|
|
|
query map[string]string
|
|
|
|
page readers.MessagesPage
|
2018-08-25 12:48:03 +02:00
|
|
|
}{
|
|
|
|
"read message page for existing channel": {
|
2019-04-25 00:18:43 +02:00
|
|
|
chanID: chanID,
|
|
|
|
offset: 0,
|
|
|
|
limit: 10,
|
|
|
|
page: readers.MessagesPage{
|
|
|
|
Total: msgsNum,
|
|
|
|
Offset: 0,
|
|
|
|
Limit: 10,
|
|
|
|
Messages: messages[0:10],
|
|
|
|
},
|
2018-08-25 12:48:03 +02:00
|
|
|
},
|
|
|
|
"read message page for too large limit": {
|
2019-04-25 00:18:43 +02:00
|
|
|
chanID: chanID,
|
|
|
|
offset: 0,
|
|
|
|
limit: 101,
|
|
|
|
page: readers.MessagesPage{
|
|
|
|
Total: msgsNum,
|
|
|
|
Offset: 0,
|
|
|
|
Limit: 101,
|
|
|
|
Messages: messages[0:100],
|
|
|
|
},
|
2018-08-25 12:48:03 +02:00
|
|
|
},
|
|
|
|
"read message page for non-existent channel": {
|
2019-04-25 00:18:43 +02:00
|
|
|
chanID: "2",
|
|
|
|
offset: 0,
|
|
|
|
limit: 10,
|
|
|
|
page: readers.MessagesPage{
|
|
|
|
Total: 0,
|
|
|
|
Offset: 0,
|
|
|
|
Limit: 10,
|
2019-11-05 11:57:16 +01:00
|
|
|
Messages: []senml.Message{},
|
2019-04-25 00:18:43 +02:00
|
|
|
},
|
2018-08-25 12:48:03 +02:00
|
|
|
},
|
|
|
|
"read message last page": {
|
2019-04-25 00:18:43 +02:00
|
|
|
chanID: chanID,
|
|
|
|
offset: 95,
|
|
|
|
limit: 10,
|
|
|
|
page: readers.MessagesPage{
|
|
|
|
Total: msgsNum,
|
|
|
|
Offset: 95,
|
|
|
|
Limit: 10,
|
|
|
|
Messages: messages[95:101],
|
|
|
|
},
|
2018-08-25 12:48:03 +02:00
|
|
|
},
|
2019-03-15 18:38:07 +01:00
|
|
|
"read message with non-existent subtopic": {
|
2019-04-25 00:18:43 +02:00
|
|
|
chanID: chanID,
|
|
|
|
offset: 0,
|
|
|
|
limit: msgsNum,
|
|
|
|
query: map[string]string{"subtopic": "not-present"},
|
|
|
|
page: readers.MessagesPage{
|
|
|
|
Total: 0,
|
|
|
|
Offset: 0,
|
|
|
|
Limit: msgsNum,
|
2019-11-05 11:57:16 +01:00
|
|
|
Messages: []senml.Message{},
|
2019-04-25 00:18:43 +02:00
|
|
|
},
|
2019-03-15 18:38:07 +01:00
|
|
|
},
|
|
|
|
"read message with subtopic": {
|
2019-04-25 00:18:43 +02:00
|
|
|
chanID: chanID,
|
|
|
|
offset: 0,
|
|
|
|
limit: 10,
|
|
|
|
query: map[string]string{"subtopic": subtopic},
|
|
|
|
page: readers.MessagesPage{
|
|
|
|
Total: uint64(len(subtopicMsgs)),
|
|
|
|
Offset: 0,
|
|
|
|
Limit: 10,
|
|
|
|
Messages: subtopicMsgs[0:10],
|
|
|
|
},
|
2019-03-15 18:38:07 +01:00
|
|
|
},
|
2018-08-25 12:48:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for desc, tc := range cases {
|
2019-05-07 15:59:18 +02:00
|
|
|
result, err := reader.ReadAll(tc.chanID, tc.offset, tc.limit, tc.query)
|
|
|
|
assert.Nil(t, err, fmt.Sprintf("%s: expected no error got %s", desc, err))
|
2019-04-25 00:18:43 +02:00
|
|
|
assert.ElementsMatch(t, tc.page.Messages, result.Messages, fmt.Sprintf("%s: expected: %v \n-------------\n got: %v", desc, tc.page.Messages, result.Messages))
|
2019-11-05 11:57:16 +01:00
|
|
|
|
2019-04-25 00:18:43 +02:00
|
|
|
assert.Equal(t, tc.page.Total, result.Total, fmt.Sprintf("%s: expected %d got %d", desc, tc.page.Total, result.Total))
|
2018-08-25 12:48:03 +02:00
|
|
|
}
|
|
|
|
}
|