Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rjtokenring committed Sep 17, 2024
1 parent cbed141 commit edb6212
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/importer/importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func StartImport(ctx context.Context, logger *logrus.Entry, key, secret, orgid s
return err
}

if writer, from, err := tsextractorClient.ExportTSToFile(ctx, timeWindowMinutes, thingsMap, resolution, destinationS3Bucket, aggregationStat); err != nil {
if writer, from, err := tsextractorClient.ExportTSToFile(ctx, timeWindowMinutes, thingsMap, resolution, aggregationStat); err != nil {
logger.Error("Error aligning time series samples: ", err)
return err
} else {
Expand Down
1 change: 0 additions & 1 deletion business/tsextractor/tsextractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ func (a *TsExtractor) ExportTSToFile(
timeWindowInMinutes int,
thingsMap map[string]iotclient.ArduinoThing,
resolution int,
destinationS3Bucket string,
aggregationStat string) (*csv.CsvWriter, time.Time, error) {

// Truncate time to given resolution
Expand Down
79 changes: 79 additions & 0 deletions business/tsextractor/tsextractor_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
package tsextractor

import (
"context"
"fmt"
"io"
"os"
"testing"
"time"

iotMocks "github.com/arduino/aws-s3-integration/internal/iot/mocks"
iotclient "github.com/arduino/iot-client-go/v2"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)

func TestTimeAlignment_HourlyTimeWindows(t *testing.T) {
Expand Down Expand Up @@ -35,3 +44,73 @@ func TestTimeAlignment_raw_HourlyTimeWindows(t *testing.T) {
from, to := computeTimeAlignment(-1, 60)
assert.Equal(t, int64(3600), to.Unix()-from.Unix())
}

func toPtr(val string) *string {
return &val
}

func TestExtractionFlow_defaultAggregation(t *testing.T) {
logger := logrus.NewEntry(logrus.New())
ctx := context.Background()

thingId := "91f30213-2bd7-480a-b1dc-f31b01840e7e"
propertyId := "c86f4ed9-7f52-4bd3-bdc6-b2936bec68ac"

// Init client
iotcl := iotMocks.NewAPI(t)

now := time.Now()
responses := []iotclient.ArduinoSeriesResponse{
{
Aggregation: toPtr("AVG"),
Query: fmt.Sprintf("property.%s", propertyId),
Times: []time.Time{now.Add(-time.Minute * 1), now},
Values: []float64{1.0, 2.0},
CountValues: 2,
},
}
samples := iotclient.ArduinoSeriesBatch{
Responses: responses,
}
iotcl.On("GetTimeSeriesByThing", ctx, thingId, mock.Anything, mock.Anything, int64(300), "AVG").Return(&samples, false, nil)

tsextractorClient := New(iotcl, logger)

propCount := int64(1)
thingsMap := make(map[string]iotclient.ArduinoThing)
thingsMap[thingId] = iotclient.ArduinoThing{
Id: thingId,
Name: "test",
Properties: []iotclient.ArduinoProperty{
{
Name: "ptest",
Id: propertyId,
Type: "FLOAT",
},
},
PropertiesCount: &propCount,
}

writer, from, err := tsextractorClient.ExportTSToFile(ctx, 60, thingsMap, 300, "AVG")
assert.NoError(t, err)
assert.NotNil(t, writer)
assert.NotNil(t, from)

writer.Close()
defer writer.Delete()

outF, err := os.Open(writer.GetFilePath())
assert.NoError(t, err)
defer outF.Close()
content, err := io.ReadAll(outF)
assert.NoError(t, err)
entries := []string{
"timestamp,thing_id,thing_name,property_id,property_name,property_type,value",
"91f30213-2bd7-480a-b1dc-f31b01840e7e,test,c86f4ed9-7f52-4bd3-bdc6-b2936bec68ac,ptest,FLOAT,1,AVG",
"91f30213-2bd7-480a-b1dc-f31b01840e7e,test,c86f4ed9-7f52-4bd3-bdc6-b2936bec68ac,ptest,FLOAT,2,AVG",
}
for _, entry := range entries {
assert.Contains(t, string(content), entry)
}
fmt.Println(string(content))
}

0 comments on commit edb6212

Please sign in to comment.