Skip to content

Commit

Permalink
Add run_tests.sh script for generating mock and run tests
Browse files Browse the repository at this point in the history
  • Loading branch information
valterfrancisco-dremio committed Jan 3, 2025
1 parent 70f2bd8 commit fb3fcaa
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 420 deletions.
8 changes: 7 additions & 1 deletion go/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,13 @@ Here we're querying for a dataset called `NYC-taxi-trips`, in a source called `S

## Tests

This example Go client also has unit tests that can be run using the following command:
To run the tests, you'll need a flight client mock class. This class is generated using mockgen. To aid in this process,
we created a script that generates the mock class and runs all tests. You can run the script using the following command:
```bash
./run_tests.sh
```

If the mock class is already generated you can alternatively use the following command:
```go
go test -v
```
18 changes: 5 additions & 13 deletions go/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@
package main

import (
"arrow-flight-client-example/interfaces"
"context"
"crypto/tls"
"fmt"
"github.com/apache/arrow-go/v18/arrow"
"log"
"net"

"arrow-flight-client-example/interfaces"
"github.com/apache/arrow-go/v18/arrow/flight"
flightgen "github.com/apache/arrow-go/v18/arrow/flight/gen/flight"
"github.com/apache/arrow-go/v18/arrow/memory"
Expand Down Expand Up @@ -50,14 +49,7 @@ Options:
--certs=<path> Path to trusted certificates for encrypted connection.
--project_id=<project_id> Dremio project ID`

type RecordReader interface {
Next() bool
Record() arrow.Record
Err() error
Release()
}

func WrapRecordReader(stream flight.FlightService_DoGetClient) (RecordReader, error) {
func WrapRecordReader(stream flight.FlightService_DoGetClient) (interfaces.RecordReader, error) {
return flight.NewRecordReader(stream)
}

Expand All @@ -67,7 +59,7 @@ func main() {
log.Fatalf("error parsing arguments: %v", err)
}

var config interfaces.FlightConfig
var config FlightConfig
if err := args.Bind(&config); err != nil {
log.Fatalf("error binding arguments: %v", err)
}
Expand Down Expand Up @@ -114,8 +106,8 @@ func main() {
}
}

func run(config interfaces.FlightConfig, client flight.Client,
readerCreator func(flight.FlightService_DoGetClient) (RecordReader, error),
func run(config FlightConfig, client flight.Client,
readerCreator func(flight.FlightService_DoGetClient) (interfaces.RecordReader, error),
) error {

// Two WLM settings can be provided upon initial authentication with the dremio
Expand Down
30 changes: 15 additions & 15 deletions go/example_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"arrow-flight-client-example/interfaces"
"bytes"
"context"
"github.com/apache/arrow-go/v18/arrow"
Expand All @@ -16,7 +17,6 @@ import (
"github.com/golang/mock/gomock"

"arrow-flight-client-example/implementations"
"arrow-flight-client-example/interfaces"
)

func TestUsernamePassAuth(t *testing.T) {
Expand All @@ -30,7 +30,7 @@ func TestUsernamePassAuth(t *testing.T) {
Return(context.Background(), nil).
Times(1)

config := interfaces.FlightConfig{
config := FlightConfig{
User: "testuser",
Pass: "testpass",
Query: "",
Expand Down Expand Up @@ -58,7 +58,7 @@ func TestPATAuth(t *testing.T) {
Return(nil, nil).
Times(1)

config := interfaces.FlightConfig{
config := FlightConfig{
Pat: "testpat",
ProjectID: "testproject",
}
Expand Down Expand Up @@ -138,13 +138,13 @@ func TestRun(t *testing.T) {
Return(mockStream, nil).
Times(1)

config := interfaces.FlightConfig{
config := FlightConfig{
User: "testuser",
Pass: "testpass",
Query: "SELECT * FROM test",
}

mockReaderCreator := func(stream flight.FlightService_DoGetClient) (RecordReader, error) {
mockReaderCreator := func(stream flight.FlightService_DoGetClient) (interfaces.RecordReader, error) {
return implementations.NewMockRecordReader([]arrow.Record{record}), nil
}

Expand Down Expand Up @@ -225,13 +225,13 @@ func TestRunWithPAT(t *testing.T) {
Return(nil, nil).
Times(1)

config := interfaces.FlightConfig{
config := FlightConfig{
Pat: "test_pat_token",
Query: "SELECT * FROM test",
ProjectID: "test_project_id",
}

mockReaderCreator := func(stream flight.FlightService_DoGetClient) (RecordReader, error) {
mockReaderCreator := func(stream flight.FlightService_DoGetClient) (interfaces.RecordReader, error) {
return implementations.NewMockRecordReader([]arrow.Record{record}), nil
}

Expand Down Expand Up @@ -302,12 +302,12 @@ func TestRunWithPATNoProjectID(t *testing.T) {
Return(mockStream, nil).
Times(1)

config := interfaces.FlightConfig{
config := FlightConfig{
Pat: "test_pat_token",
Query: "SELECT * FROM test",
}

mockReaderCreator := func(stream flight.FlightService_DoGetClient) (RecordReader, error) {
mockReaderCreator := func(stream flight.FlightService_DoGetClient) (interfaces.RecordReader, error) {
return implementations.NewMockRecordReader([]arrow.Record{record}), nil
}

Expand All @@ -329,13 +329,13 @@ func TestInvalidCredentials(t *testing.T) {
Return(context.Background(), expectedErr).
Times(1)

config := interfaces.FlightConfig{
config := FlightConfig{
User: "dremio",
Pass: "dremio12",
Query: "SELECT 1",
}

mockReaderCreator := func(stream flight.FlightService_DoGetClient) (RecordReader, error) {
mockReaderCreator := func(stream flight.FlightService_DoGetClient) (interfaces.RecordReader, error) {
t.Fatal("Reader creator should not be called due to authentication failure")
return nil, nil
}
Expand Down Expand Up @@ -367,13 +367,13 @@ func TestInvalidHost(t *testing.T) {
Return(context.Background(), expectedErr).
Times(1)

config := interfaces.FlightConfig{
config := FlightConfig{
User: "dremio",
Pass: "dremio12",
Query: "SELECT 1",
}

mockReaderCreator := func(stream flight.FlightService_DoGetClient) (RecordReader, error) {
mockReaderCreator := func(stream flight.FlightService_DoGetClient) (interfaces.RecordReader, error) {
t.Fatal("Reader creator should not be called due to authentication failure")
return nil, nil
}
Expand Down Expand Up @@ -405,13 +405,13 @@ func TestInvalidPort(t *testing.T) {
Return(context.Background(), expectedErr).
Times(1)

config := interfaces.FlightConfig{
config := FlightConfig{
User: "dremio",
Pass: "dremio12",
Query: "SELECT 1",
}

mockReaderCreator := func(stream flight.FlightService_DoGetClient) (RecordReader, error) {
mockReaderCreator := func(stream flight.FlightService_DoGetClient) (interfaces.RecordReader, error) {
t.Fatal("Reader creator should not be called due to authentication failure")
return nil, nil
}
Expand Down
2 changes: 1 addition & 1 deletion go/interfaces/flight_config.go → go/flight_config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package interfaces
package main

type FlightConfig struct {
Host string
Expand Down
3 changes: 3 additions & 0 deletions go/implementations/mock_record_reader.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package implementations

import (
"arrow-flight-client-example/interfaces"
"github.com/apache/arrow-go/v18/arrow"
)

var _ interfaces.RecordReader = (*MockRecordReader)(nil)

// MockRecordReader for testing purposes
type MockRecordReader struct {
records []arrow.Record
Expand Down
10 changes: 10 additions & 0 deletions go/interfaces/record_reader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package interfaces

import "github.com/apache/arrow-go/v18/arrow"

type RecordReader interface {
Next() bool
Record() arrow.Record
Err() error
Release()
}
Loading

0 comments on commit fb3fcaa

Please sign in to comment.