2019-05-07 19:09:28 +02:00
|
|
|
//
|
|
|
|
// Copyright (c) 2019
|
|
|
|
// Mainflux
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
//
|
|
|
|
|
|
|
|
package postgres_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/mainflux/mainflux"
|
|
|
|
"github.com/mainflux/mainflux/writers/postgres"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2019-05-16 13:35:13 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-05-07 19:09:28 +02:00
|
|
|
|
2019-05-16 13:35:13 +02:00
|
|
|
"github.com/gofrs/uuid"
|
2019-05-07 19:09:28 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
msg = mainflux.Message{}
|
|
|
|
msgsNum = 42
|
|
|
|
valueFields = 6
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMessageSave(t *testing.T) {
|
|
|
|
messageRepo := postgres.New(db)
|
|
|
|
|
2019-05-16 13:35:13 +02:00
|
|
|
chid, err := uuid.NewV4()
|
|
|
|
require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err))
|
|
|
|
msg.Channel = chid.String()
|
|
|
|
|
|
|
|
pubid, err := uuid.NewV4()
|
|
|
|
require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err))
|
|
|
|
msg.Publisher = pubid.String()
|
2019-05-07 19:09:28 +02:00
|
|
|
|
|
|
|
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.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 := messageRepo.Save(msg)
|
|
|
|
assert.Nil(t, err, fmt.Sprintf("expected no error got %s\n", err))
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|