1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-24 13:48:49 +08:00
b1ackd0t 1f21541a27
NOISSUE - Fix Twins Tests (#1931)
* feat(twins/mocks): Add new service constructor with mock dependencies

This commit adds a new service constructor to the `twins/mocks/service.go` file. The new constructor, `NewService`, takes no arguments and returns an instance of `twins.Service` along with a mock instance of `authmocks.Service`. This allows for the creation of a real twins service using mock dependencies.

Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>

* feat(api): Add tests for endpoint states

This commit adds tests for the endpoint states API in the `endpoint_states_test.go` file. The tests cover the functionality of the `GET` and `POST` methods for retrieving and updating the states of endpoints respectively. The tests use the `mainflux` and `authmocks` packages for mocking and testing the API endpoints. Additionally, the `testsutil` package is imported for utility functions used in the tests.

Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>

---------

Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>
2023-10-20 23:06:03 +02:00

80 lines
2.0 KiB
Go

// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0
package mocks
import (
"encoding/json"
"strconv"
"time"
authmocks "github.com/mainflux/mainflux/auth/mocks"
"github.com/mainflux/mainflux/pkg/messaging"
"github.com/mainflux/mainflux/pkg/uuid"
"github.com/mainflux/mainflux/twins"
"github.com/mainflux/senml"
)
const publisher = "twins"
var id = 0
// NewService use mock dependencies to create real twins service.
func NewService() (twins.Service, *authmocks.Service) {
auth := new(authmocks.Service)
twinsRepo := NewTwinRepository()
twinCache := NewTwinCache()
statesRepo := NewStateRepository()
idProvider := uuid.NewMock()
subs := map[string]string{"chanID": "chanID"}
broker := NewBroker(subs)
return twins.New(broker, auth, twinsRepo, twinCache, statesRepo, idProvider, "chanID", nil), auth
}
// CreateDefinition creates twin definition.
func CreateDefinition(channels []string, subtopics []string) twins.Definition {
var def twins.Definition
for i := range channels {
attr := twins.Attribute{
Channel: channels[i],
Subtopic: subtopics[i],
PersistState: true,
}
def.Attributes = append(def.Attributes, attr)
}
return def
}
// CreateTwin creates twin.
func CreateTwin(channels []string, subtopics []string) twins.Twin {
id++
return twins.Twin{
ID: strconv.Itoa(id),
Definitions: []twins.Definition{CreateDefinition(channels, subtopics)},
}
}
// CreateSenML creates SenML record array.
func CreateSenML(recs []senml.Record) {
for i, rec := range recs {
rec.BaseTime = float64(time.Now().Unix())
rec.Time = float64(i)
rec.Value = nil
}
}
// CreateMessage creates Mainflux message using SenML record array.
func CreateMessage(attr twins.Attribute, recs []senml.Record) (*messaging.Message, error) {
mRecs, err := json.Marshal(recs)
if err != nil {
return nil, err
}
return &messaging.Message{
Channel: attr.Channel,
Subtopic: attr.Subtopic,
Payload: mRecs,
Publisher: publisher,
}, nil
}