mirror of
https://github.com/mainflux/mainflux.git
synced 2025-05-01 13:48:56 +08:00

* Add mongodb reader service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add tests for mongodb reader service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add documentation for mongodb reader service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix test function name Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update comment in docker-compose for mongodb-reader service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
58 lines
1.4 KiB
Go
58 lines
1.4 KiB
Go
package mongodb_test
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/mainflux/mainflux"
|
|
"github.com/mainflux/mainflux/writers/mongodb"
|
|
|
|
log "github.com/mainflux/mainflux/logger"
|
|
"github.com/mongodb/mongo-go-driver/mongo"
|
|
)
|
|
|
|
var (
|
|
port string
|
|
addr string
|
|
testLog = log.New(os.Stdout)
|
|
testDB = "test"
|
|
collection = "mainflux"
|
|
db mongo.Database
|
|
)
|
|
|
|
func TestSave(t *testing.T) {
|
|
msg := mainflux.Message{
|
|
Channel: 45,
|
|
Publisher: 2580,
|
|
Protocol: "http",
|
|
Name: "test name",
|
|
Unit: "km",
|
|
Value: 24,
|
|
StringValue: "24",
|
|
BoolValue: false,
|
|
DataValue: "dataValue",
|
|
ValueSum: 24,
|
|
Time: 13451312,
|
|
UpdateTime: 5456565466,
|
|
Link: "link",
|
|
}
|
|
|
|
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)
|
|
repo := mongodb.New(db)
|
|
|
|
err = repo.Save(msg)
|
|
assert.Nil(t, err, fmt.Sprintf("Save operation expected to succeed: %s.\n", err))
|
|
|
|
count, err := db.Collection(collection).Count(context.Background(), nil)
|
|
assert.Nil(t, err, fmt.Sprintf("Querying database expected to succeed: %s.\n", err))
|
|
assert.Equal(t, int64(1), count, fmt.Sprintf("Expected to have 1 value, found %d instead.\n", count))
|
|
}
|