|
| 1 | +package beholder_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/smartcontractkit/chainlink-common/pkg/beholder" |
| 6 | + |
| 7 | + "github.com/smartcontractkit/chainlink-common/pkg/chipingress/mocks" |
| 8 | + "github.com/smartcontractkit/chainlink-common/pkg/chipingress/pb" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/mock" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | + "testing" |
| 13 | +) |
| 14 | + |
| 15 | +func TestNewChipClient(t *testing.T) { |
| 16 | + t.Run("returns error when client is nil", func(t *testing.T) { |
| 17 | + registry, err := beholder.NewChipIngressClient(nil) |
| 18 | + assert.Nil(t, registry) |
| 19 | + assert.EqualError(t, err, "chip ingress client is nil") |
| 20 | + }) |
| 21 | + |
| 22 | + t.Run("returns schema registry when client is valid", func(t *testing.T) { |
| 23 | + mockClient := mocks.NewClient(t) |
| 24 | + registry, err := beholder.NewChipIngressClient(mockClient) |
| 25 | + require.NoError(t, err) |
| 26 | + assert.NotNil(t, registry) |
| 27 | + }) |
| 28 | +} |
| 29 | + |
| 30 | +func TestRegisterSchema(t *testing.T) { |
| 31 | + t.Run("successfully registers schemas", func(t *testing.T) { |
| 32 | + mockClient := mocks.NewClient(t) |
| 33 | + mockClient. |
| 34 | + On("RegisterSchema", mock.Anything, mock.Anything). |
| 35 | + Return(&pb.RegisterSchemaResponse{ |
| 36 | + Registered: []*pb.RegisteredSchema{ |
| 37 | + {Subject: "schema1", Version: 1}, |
| 38 | + {Subject: "schema2", Version: 2}, |
| 39 | + }, |
| 40 | + }, nil) |
| 41 | + |
| 42 | + registry, err := beholder.NewChipIngressClient(mockClient) |
| 43 | + require.NoError(t, err) |
| 44 | + |
| 45 | + schemas := []*pb.Schema{ |
| 46 | + {Subject: "schema1", Schema: `{"type":"record","name":"Test","fields":[{"name":"field1"}]}`, Format: 1}, |
| 47 | + {Subject: "schema2", Schema: `{"type":"record","name":"Test2","fields":[{"name":"field2"}]}`, Format: 2}, |
| 48 | + } |
| 49 | + |
| 50 | + result, err := registry.RegisterSchema(t.Context(), schemas...) |
| 51 | + require.NoError(t, err) |
| 52 | + assert.Equal(t, map[string]int{"schema1": 1, "schema2": 2}, result) |
| 53 | + }) |
| 54 | + |
| 55 | + t.Run("returns error when registration fails", func(t *testing.T) { |
| 56 | + mockClient := mocks.NewClient(t) |
| 57 | + mockClient. |
| 58 | + On("RegisterSchema", mock.Anything, mock.Anything). |
| 59 | + Return(nil, fmt.Errorf("registration failed")) |
| 60 | + |
| 61 | + registry, err := beholder.NewChipIngressClient(mockClient) |
| 62 | + require.NoError(t, err) |
| 63 | + |
| 64 | + schemas := []*pb.Schema{ |
| 65 | + {Subject: "schema1", Schema: `{"type":"record","name":"Test","fields":[{"name":"field1"}]}`, Format: 1}, |
| 66 | + } |
| 67 | + |
| 68 | + result, err := registry.RegisterSchema(t.Context(), schemas...) |
| 69 | + assert.Nil(t, result) |
| 70 | + assert.EqualError(t, err, "failed to register schema: registration failed") |
| 71 | + }) |
| 72 | +} |
0 commit comments