2022-01-18 19:08:54 +08:00
|
|
|
// Copyright (c) Mainflux
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package timescale_test
|
|
|
|
|
|
|
|
import (
|
2023-07-22 20:38:16 +08:00
|
|
|
"context"
|
2022-01-18 19:08:54 +08:00
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2023-08-11 17:30:25 +08:00
|
|
|
"github.com/gofrs/uuid"
|
2022-01-18 19:08:54 +08:00
|
|
|
"github.com/mainflux/mainflux/consumers/writers/timescale"
|
|
|
|
"github.com/mainflux/mainflux/pkg/transformers/json"
|
|
|
|
"github.com/mainflux/mainflux/pkg/transformers/senml"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
msgsNum = 42
|
|
|
|
valueFields = 5
|
|
|
|
subtopic = "topic"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
v float64 = 5
|
|
|
|
stringV = "value"
|
|
|
|
boolV = true
|
|
|
|
dataV = "base64"
|
|
|
|
sum float64 = 42
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSaveSenml(t *testing.T) {
|
|
|
|
repo := timescale.New(db)
|
|
|
|
|
|
|
|
chid, err := uuid.NewV4()
|
2023-04-12 23:30:01 +08:00
|
|
|
assert.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err))
|
2022-01-18 19:08:54 +08:00
|
|
|
|
|
|
|
msg := senml.Message{}
|
|
|
|
msg.Channel = chid.String()
|
|
|
|
|
|
|
|
pubid, err := uuid.NewV4()
|
2023-04-12 23:30:01 +08:00
|
|
|
assert.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err))
|
2022-01-18 19:08:54 +08:00
|
|
|
msg.Publisher = pubid.String()
|
|
|
|
|
|
|
|
now := time.Now().Unix()
|
|
|
|
var msgs []senml.Message
|
|
|
|
|
|
|
|
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 = &v
|
|
|
|
case 1:
|
|
|
|
msg.BoolValue = &boolV
|
|
|
|
case 2:
|
|
|
|
msg.StringValue = &stringV
|
|
|
|
case 3:
|
|
|
|
msg.DataValue = &dataV
|
|
|
|
case 4:
|
|
|
|
msg.Sum = &sum
|
|
|
|
}
|
|
|
|
|
|
|
|
msg.Time = float64(now + int64(i))
|
|
|
|
msgs = append(msgs, msg)
|
|
|
|
}
|
|
|
|
|
2023-07-22 20:38:16 +08:00
|
|
|
err = repo.ConsumeBlocking(context.TODO(), msgs)
|
2022-01-18 19:08:54 +08:00
|
|
|
assert.Nil(t, err, fmt.Sprintf("expected no error got %s\n", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSaveJSON(t *testing.T) {
|
|
|
|
repo := timescale.New(db)
|
|
|
|
|
|
|
|
chid, err := uuid.NewV4()
|
2023-04-12 23:30:01 +08:00
|
|
|
assert.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err))
|
2022-01-18 19:08:54 +08:00
|
|
|
pubid, err := uuid.NewV4()
|
2023-04-12 23:30:01 +08:00
|
|
|
assert.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err))
|
2022-01-18 19:08:54 +08:00
|
|
|
|
|
|
|
msg := json.Message{
|
|
|
|
Channel: chid.String(),
|
|
|
|
Publisher: pubid.String(),
|
|
|
|
Created: time.Now().Unix(),
|
|
|
|
Subtopic: "subtopic/format/some_json",
|
|
|
|
Protocol: "mqtt",
|
|
|
|
Payload: map[string]interface{}{
|
|
|
|
"field_1": 123,
|
|
|
|
"field_2": "value",
|
|
|
|
"field_3": false,
|
|
|
|
"field_4": 12.344,
|
|
|
|
"field_5": map[string]interface{}{
|
|
|
|
"field_1": "value",
|
|
|
|
"field_2": 42,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
now := time.Now().Unix()
|
|
|
|
msgs := json.Messages{
|
|
|
|
Format: "some_json",
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < msgsNum; i++ {
|
|
|
|
msg.Created = now + int64(i)
|
|
|
|
msgs.Data = append(msgs.Data, msg)
|
|
|
|
}
|
|
|
|
|
2023-07-22 20:38:16 +08:00
|
|
|
err = repo.ConsumeBlocking(context.TODO(), msgs)
|
2022-01-18 19:08:54 +08:00
|
|
|
assert.Nil(t, err, fmt.Sprintf("expected no error got %s\n", err))
|
|
|
|
}
|