Skip to content

Commit

Permalink
DATA-3445 Update ExportTabularData to return a slice (#4640)
Browse files Browse the repository at this point in the history
  • Loading branch information
katiepeters authored Dec 18, 2024
1 parent ff045cd commit 15d8098
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
37 changes: 18 additions & 19 deletions app/data_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"time"
Expand Down Expand Up @@ -203,21 +204,6 @@ type ExportTabularDataResponse struct {
Payload map[string]interface{}
}

// ExportTabularDataStream is a stream that returns ExportTabularDataResponses.
type ExportTabularDataStream struct {
Stream pb.DataService_ExportTabularDataClient
}

// Next gets the next ExportTabularDataResponse.
func (e *ExportTabularDataStream) Next() (*ExportTabularDataResponse, error) {
streamResp, err := e.Stream.Recv()
if err != nil {
return nil, err
}

return exportTabularDataResponseFromProto(streamResp), nil
}

// DataSyncClient structs

// SensorMetadata contains the time the sensor data was requested and was received.
Expand Down Expand Up @@ -503,7 +489,7 @@ func (d *DataClient) GetLatestTabularData(ctx context.Context, partID, resourceN
// ExportTabularData returns a stream of ExportTabularDataResponses.
func (d *DataClient) ExportTabularData(
ctx context.Context, partID, resourceName, resourceSubtype, method string, interval CaptureInterval,
) (*ExportTabularDataStream, error) {
) ([]*ExportTabularDataResponse, error) {
stream, err := d.dataClient.ExportTabularData(ctx, &pb.ExportTabularDataRequest{
PartId: partID,
ResourceName: resourceName,
Expand All @@ -514,9 +500,22 @@ func (d *DataClient) ExportTabularData(
if err != nil {
return nil, err
}
return &ExportTabularDataStream{
Stream: stream,
}, nil

var responses []*ExportTabularDataResponse

for {
response, err := stream.Recv()
if errors.Is(err, io.EOF) {
break
}
if err != nil {
return nil, err
}

responses = append(responses, exportTabularDataResponseFromProto(response))
}

return responses, nil
}

// BinaryDataByFilter queries binary data and metadata based on given filters.
Expand Down
13 changes: 9 additions & 4 deletions app/data_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package app

import (
"context"
"io"
"os"
"testing"
"time"
Expand Down Expand Up @@ -392,8 +393,14 @@ func TestDataClient(t *testing.T) {
})

t.Run("ExportTabularData", func(t *testing.T) {
sentOnce := false
mockStream := &inject.DataServiceExportTabularDataClient{
RecvFunc: func() (*pb.ExportTabularDataResponse, error) {
if sentOnce {
return nil, io.EOF
}

sentOnce = true
return exportTabularResponse, nil
},
}
Expand All @@ -409,11 +416,9 @@ func TestDataClient(t *testing.T) {
return mockStream, nil
}

stream, err := client.ExportTabularData(context.Background(), partID, componentName, componentType, method, captureInterval)
test.That(t, err, test.ShouldBeNil)
resp, err := stream.Next()
responses, err := client.ExportTabularData(context.Background(), partID, componentName, componentType, method, captureInterval)
test.That(t, err, test.ShouldBeNil)
test.That(t, resp, test.ShouldResemble, exportTabularDataResponseFromProto(exportTabularResponse))
test.That(t, responses[0], test.ShouldResemble, exportTabularDataResponseFromProto(exportTabularResponse))
})

t.Run("BinaryDataByFilter", func(t *testing.T) {
Expand Down

0 comments on commit 15d8098

Please sign in to comment.