|
| 1 | +package reversertransformer |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "io" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/apache/arrow/go/v16/arrow" |
| 9 | + "github.com/apache/arrow/go/v16/arrow/array" |
| 10 | + "github.com/apache/arrow/go/v16/arrow/memory" |
| 11 | + pb "github.com/cloudquery/plugin-pb-go/pb/plugin/v3" |
| 12 | + internalPlugin "github.com/cloudquery/plugin-sdk/v4/internal/servers/plugin/v3" |
| 13 | + "github.com/cloudquery/plugin-sdk/v4/plugin" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | + "google.golang.org/grpc" |
| 16 | + "google.golang.org/grpc/metadata" |
| 17 | +) |
| 18 | + |
| 19 | +var mem = memory.NewGoAllocator() |
| 20 | + |
| 21 | +func TestReverserTransformer(t *testing.T) { |
| 22 | + p := plugin.NewPlugin("test", "development", GetNewClient()) |
| 23 | + s := internalPlugin.Server{ |
| 24 | + Plugin: p, |
| 25 | + } |
| 26 | + _, err := s.Init(context.Background(), &pb.Init_Request{ |
| 27 | + Spec: []byte("{}"), |
| 28 | + NoConnection: true, |
| 29 | + InvocationId: "26b550f9-c6f8-4b4b-9ec4-773bab288ee6", |
| 30 | + }) |
| 31 | + require.NoError(t, err) |
| 32 | + requests := makeRequestsFromStrings("hello", "world") |
| 33 | + stream := mockTransformServer{incomingMessages: requests} |
| 34 | + require.NoError(t, s.Transform(&stream)) |
| 35 | + require.Equal(t, 2, len(stream.outgoingMessages)) |
| 36 | + |
| 37 | + record1, err := pb.NewRecordFromBytes(stream.outgoingMessages[0].Record) |
| 38 | + require.NoError(t, err) |
| 39 | + record2, err := pb.NewRecordFromBytes(stream.outgoingMessages[1].Record) |
| 40 | + require.NoError(t, err) |
| 41 | + |
| 42 | + require.Equal(t, "olleh", record1.Column(0).ValueStr(0)) |
| 43 | + require.Equal(t, "dlrow", record2.Column(0).ValueStr(0)) |
| 44 | +} |
| 45 | + |
| 46 | +func makeRequestsFromStrings(s ...string) []*pb.Transform_Request { |
| 47 | + requests := make([]*pb.Transform_Request, len(s)) |
| 48 | + for i, str := range s { |
| 49 | + requests[i] = makeRequestFromString(str) |
| 50 | + } |
| 51 | + return requests |
| 52 | +} |
| 53 | + |
| 54 | +func makeRequestFromString(s string) *pb.Transform_Request { |
| 55 | + record := makeRecordFromString(s) |
| 56 | + bs, _ := pb.RecordToBytes(record) |
| 57 | + return &pb.Transform_Request{Record: bs} |
| 58 | +} |
| 59 | + |
| 60 | +func makeRecordFromString(s string) arrow.Record { |
| 61 | + str := array.NewStringBuilder(mem) |
| 62 | + str.AppendString(s) |
| 63 | + arr := str.NewStringArray() |
| 64 | + schema := arrow.NewSchema([]arrow.Field{{Name: "col1", Type: arrow.BinaryTypes.String}}, nil) |
| 65 | + |
| 66 | + return array.NewRecord(schema, []arrow.Array{arr}, 1) |
| 67 | +} |
| 68 | + |
| 69 | +type mockTransformServer struct { |
| 70 | + grpc.ServerStream |
| 71 | + incomingMessages []*pb.Transform_Request |
| 72 | + outgoingMessages []*pb.Transform_Response |
| 73 | +} |
| 74 | + |
| 75 | +func (*mockTransformServer) SendAndClose(*pb.Transform_Response) error { |
| 76 | + return nil |
| 77 | +} |
| 78 | +func (s *mockTransformServer) Recv() (*pb.Transform_Request, error) { |
| 79 | + if len(s.incomingMessages) > 0 { |
| 80 | + msg := s.incomingMessages[0] |
| 81 | + s.incomingMessages = s.incomingMessages[1:] |
| 82 | + return msg, nil |
| 83 | + } |
| 84 | + return nil, io.EOF |
| 85 | +} |
| 86 | +func (s *mockTransformServer) Send(resp *pb.Transform_Response) error { |
| 87 | + s.outgoingMessages = append(s.outgoingMessages, resp) |
| 88 | + return nil |
| 89 | +} |
| 90 | +func (*mockTransformServer) SetHeader(metadata.MD) error { |
| 91 | + return nil |
| 92 | +} |
| 93 | +func (*mockTransformServer) SendHeader(metadata.MD) error { |
| 94 | + return nil |
| 95 | +} |
| 96 | +func (*mockTransformServer) SetTrailer(metadata.MD) { |
| 97 | +} |
| 98 | +func (mockTransformServer) Context() context.Context { |
| 99 | + return context.Background() |
| 100 | +} |
| 101 | +func (mockTransformServer) SendMsg(any) error { |
| 102 | + return nil |
| 103 | +} |
| 104 | +func (mockTransformServer) RecvMsg(any) error { |
| 105 | + return nil |
| 106 | +} |
0 commit comments