2019-10-07 08:14:47 -06:00
|
|
|
// Copyright (c) Mainflux
|
2018-08-26 13:15:48 +02:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2018-05-21 16:28:52 +02:00
|
|
|
package influxdb_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"testing"
|
2018-09-23 01:53:03 +02:00
|
|
|
"time"
|
2018-05-21 16:28:52 +02:00
|
|
|
|
|
|
|
influxdata "github.com/influxdata/influxdb/client/v2"
|
|
|
|
"github.com/mainflux/mainflux"
|
|
|
|
log "github.com/mainflux/mainflux/logger"
|
|
|
|
writer "github.com/mainflux/mainflux/writers/influxdb"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2018-06-16 02:30:46 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-05-21 16:28:52 +02:00
|
|
|
)
|
|
|
|
|
2018-11-05 19:18:51 +01:00
|
|
|
const valueFields = 6
|
|
|
|
|
2018-05-21 16:28:52 +02:00
|
|
|
var (
|
2019-10-31 14:04:47 +01:00
|
|
|
port string
|
|
|
|
testLog, _ = log.New(os.Stdout, log.Info.String())
|
|
|
|
testDB = "test"
|
|
|
|
streamsSize = 250
|
|
|
|
selectMsgs = "SELECT * FROM test..messages"
|
|
|
|
dropMsgs = "DROP SERIES FROM messages"
|
|
|
|
client influxdata.Client
|
|
|
|
clientCfg = influxdata.HTTPConfig{
|
2018-05-21 16:28:52 +02:00
|
|
|
Username: "test",
|
|
|
|
Password: "test",
|
|
|
|
}
|
2018-09-23 01:53:03 +02:00
|
|
|
|
|
|
|
msg = mainflux.Message{
|
2018-12-05 13:09:25 +01:00
|
|
|
Channel: "45",
|
|
|
|
Publisher: "2580",
|
2018-11-05 19:18:51 +01:00
|
|
|
Protocol: "http",
|
|
|
|
Name: "test name",
|
|
|
|
Unit: "km",
|
2018-12-05 13:09:25 +01:00
|
|
|
Value: &mainflux.Message_FloatValue{FloatValue: 24},
|
2018-11-05 19:18:51 +01:00
|
|
|
ValueSum: &mainflux.SumValue{Value: 22},
|
|
|
|
UpdateTime: 5456565466,
|
|
|
|
Link: "link",
|
2018-09-23 01:53:03 +02:00
|
|
|
}
|
2018-05-21 16:28:52 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// This is utility function to query the database.
|
2018-09-23 01:53:03 +02:00
|
|
|
func queryDB(cmd string) ([][]interface{}, error) {
|
2018-05-21 16:28:52 +02:00
|
|
|
q := influxdata.Query{
|
|
|
|
Command: cmd,
|
|
|
|
Database: testDB,
|
|
|
|
}
|
|
|
|
response, err := client.Query(q)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if response.Error() != nil {
|
|
|
|
return nil, response.Error()
|
|
|
|
}
|
2018-09-23 01:53:03 +02:00
|
|
|
if len(response.Results[0].Series) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2018-05-21 16:28:52 +02:00
|
|
|
// There is only one query, so only one result and
|
|
|
|
// all data are stored in the same series.
|
2018-09-23 01:53:03 +02:00
|
|
|
return response.Results[0].Series[0].Values, nil
|
2018-05-21 16:28:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSave(t *testing.T) {
|
2019-10-31 14:04:47 +01:00
|
|
|
repo := writer.New(client, testDB)
|
2018-09-23 01:53:03 +02:00
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
desc string
|
2018-11-05 19:18:51 +01:00
|
|
|
msgsNum int
|
2018-09-23 01:53:03 +02:00
|
|
|
expectedSize int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "save a single message",
|
2018-11-05 19:18:51 +01:00
|
|
|
msgsNum: 1,
|
2018-09-23 01:53:03 +02:00
|
|
|
expectedSize: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "save a batch of messages",
|
2018-11-05 19:18:51 +01:00
|
|
|
msgsNum: streamsSize,
|
2019-10-31 14:04:47 +01:00
|
|
|
expectedSize: streamsSize,
|
2018-09-23 01:53:03 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
// Clean previously saved messages.
|
2018-12-05 13:09:25 +01:00
|
|
|
_, err := queryDB(dropMsgs)
|
2018-09-23 01:53:03 +02:00
|
|
|
require.Nil(t, err, fmt.Sprintf("Cleaning data from InfluxDB expected to succeed: %s.\n", err))
|
|
|
|
|
2018-11-18 16:42:39 +01:00
|
|
|
now := time.Now().Unix()
|
2019-10-31 14:04:47 +01:00
|
|
|
var msgs []mainflux.Message
|
2018-11-05 19:18:51 +01:00
|
|
|
for i := 0; i < tc.msgsNum; i++ {
|
|
|
|
// Mix possible values as well as value sum.
|
|
|
|
count := i % valueFields
|
|
|
|
switch count {
|
|
|
|
case 0:
|
2018-12-05 13:09:25 +01:00
|
|
|
msg.Value = &mainflux.Message_FloatValue{FloatValue: 5}
|
2018-11-05 19:18:51 +01:00
|
|
|
case 1:
|
2018-12-05 13:09:25 +01:00
|
|
|
msg.Value = &mainflux.Message_BoolValue{BoolValue: false}
|
2018-11-05 19:18:51 +01:00
|
|
|
case 2:
|
2018-12-05 13:09:25 +01:00
|
|
|
msg.Value = &mainflux.Message_StringValue{StringValue: "value"}
|
2018-11-05 19:18:51 +01:00
|
|
|
case 3:
|
2018-12-05 13:09:25 +01:00
|
|
|
msg.Value = &mainflux.Message_DataValue{DataValue: "base64data"}
|
2018-11-05 19:18:51 +01:00
|
|
|
case 4:
|
|
|
|
msg.ValueSum = nil
|
|
|
|
case 5:
|
2019-10-31 14:04:47 +01:00
|
|
|
msg.ValueSum = &mainflux.SumValue{Value: 42}
|
2018-11-05 19:18:51 +01:00
|
|
|
}
|
2018-11-18 16:42:39 +01:00
|
|
|
msg.Time = float64(now + int64(i))
|
2019-10-31 14:04:47 +01:00
|
|
|
msgs = append(msgs, msg)
|
2018-09-23 01:53:03 +02:00
|
|
|
}
|
|
|
|
|
2019-10-31 14:04:47 +01:00
|
|
|
err = repo.Save(msgs...)
|
|
|
|
assert.Nil(t, err, fmt.Sprintf("Save operation expected to succeed: %s.\n", err))
|
|
|
|
|
2018-12-05 13:09:25 +01:00
|
|
|
row, err := queryDB(selectMsgs)
|
2018-09-23 01:53:03 +02:00
|
|
|
assert.Nil(t, err, fmt.Sprintf("Querying InfluxDB to retrieve data expected to succeed: %s.\n", err))
|
2018-05-21 16:28:52 +02:00
|
|
|
|
2018-09-23 01:53:03 +02:00
|
|
|
count := len(row)
|
|
|
|
assert.Equal(t, tc.expectedSize, count, fmt.Sprintf("Expected to have %d messages saved, found %d instead.\n", tc.expectedSize, count))
|
|
|
|
}
|
2018-05-21 16:28:52 +02:00
|
|
|
}
|