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

* Fix empty protobuf values Update Normalizer service and .proto files. Reader and Writer services needs to be updated due to message format change. Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Update HTTP adapter to use gogo protobuf Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Update Reader services API Update API or Reader services to match Message changes due to switching to gogo/proto. Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Update InfluxDB services Update InfluxDB Reader and Writer services to match new Message format. Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Update MongoDB services Update MongoDB Reader and Writer services to match new message format. Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Update Cassandra services Update Cassandra Reader and Writer service to match new Message format. Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Fix InfluxDB Reader test Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Update Makefile and docs accordingly Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Fix possible data race in InfluxDB writer Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Update InfluxDB Writer tests Raise test coverage. Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Fix InfluxDB reader Fix wrong ValueSum readings. Upadete tests and raise coverage. Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Update MongoDB services tests Raise test coverage for MongoDB Reader and Writer services. Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Update Readers API tests Raise test coverage. Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Raise test coverage Update Cassandra Reader and Writer services tests. Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Use gogo/protobuf in CoAP adapter Add gogo/protobuf to Gopkg.toml and update dependencies. Update Dockerfile to run `make proto`. Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Update Cassandra Reader tests Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Refactor code Improve code style and comments to improve readability. Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Rename Sum to SumValue Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Rename Values to Value Since message contains only single value (or possibly no value at all), `Values` name could be misleading. Rename simple double value from `Value` to `FloatValue` accordingly. Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Fix InfluxDB Reader logging Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Replace exclusive if statements with switch-case Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Update Cassandra services tests Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>
111 lines
2.3 KiB
Go
111 lines
2.3 KiB
Go
//
|
|
// Copyright (c) 2018
|
|
// Mainflux
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
|
|
package mongodb_test
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"testing"
|
|
|
|
readers "github.com/mainflux/mainflux/readers/mongodb"
|
|
writers "github.com/mainflux/mainflux/writers/mongodb"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/mainflux/mainflux"
|
|
|
|
log "github.com/mainflux/mainflux/logger"
|
|
"github.com/mongodb/mongo-go-driver/mongo"
|
|
)
|
|
|
|
const (
|
|
testDB = "test"
|
|
collection = "mainflux"
|
|
chanID = 1
|
|
msgsNum = 42
|
|
valueFields = 6
|
|
)
|
|
|
|
var (
|
|
port string
|
|
addr string
|
|
msg = mainflux.Message{
|
|
Channel: chanID,
|
|
Publisher: 1,
|
|
Protocol: "mqtt",
|
|
}
|
|
testLog, _ = log.New(os.Stdout, log.Info.String())
|
|
)
|
|
|
|
func TestReadAll(t *testing.T) {
|
|
client, err := mongo.Connect(context.Background(), addr, nil)
|
|
require.Nil(t, err, fmt.Sprintf("Creating new MongoDB client expected to succeed: %s.\n", err))
|
|
|
|
db := client.Database(testDB)
|
|
|
|
writer := writers.New(db)
|
|
|
|
messages := []mainflux.Message{}
|
|
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{5}
|
|
case 1:
|
|
msg.Value = &mainflux.Message_BoolValue{false}
|
|
case 2:
|
|
msg.Value = &mainflux.Message_StringValue{"value"}
|
|
case 3:
|
|
msg.Value = &mainflux.Message_DataValue{"base64data"}
|
|
case 4:
|
|
msg.ValueSum = nil
|
|
case 5:
|
|
msg.ValueSum = &mainflux.SumValue{Value: 45}
|
|
}
|
|
|
|
err := writer.Save(msg)
|
|
require.Nil(t, err, fmt.Sprintf("failed to store message to Cassandra: %s", err))
|
|
messages = append(messages, msg)
|
|
}
|
|
|
|
reader := readers.New(db)
|
|
|
|
cases := map[string]struct {
|
|
chanID uint64
|
|
offset uint64
|
|
limit uint64
|
|
messages []mainflux.Message
|
|
}{
|
|
"read message page for existing channel": {
|
|
chanID: chanID,
|
|
offset: 0,
|
|
limit: 10,
|
|
messages: messages[0:10],
|
|
},
|
|
"read message page for non-existent channel": {
|
|
chanID: 2,
|
|
offset: 0,
|
|
limit: 10,
|
|
messages: []mainflux.Message{},
|
|
},
|
|
"read message last page": {
|
|
chanID: chanID,
|
|
offset: 40,
|
|
limit: 10,
|
|
messages: messages[40:42],
|
|
},
|
|
}
|
|
|
|
for desc, tc := range cases {
|
|
result := reader.ReadAll(tc.chanID, tc.offset, tc.limit)
|
|
assert.ElementsMatch(t, tc.messages, result, fmt.Sprintf("%s: expected %v got %v", desc, tc.messages, result))
|
|
}
|
|
}
|