-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from planetscale/add-tests
Add tests for Sync & Discover mode
- Loading branch information
Showing
8 changed files
with
629 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,137 @@ | ||
package internal | ||
|
||
import ( | ||
"context" | ||
"github.com/pkg/errors" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestDiscover_CanFailIfCredentialsInvalid(t *testing.T) { | ||
tma := getTestMysqlAccess() | ||
tma.PingContextFn = func(ctx context.Context, source PlanetScaleSource) error { | ||
return errors.New("Access Denied") | ||
} | ||
_, err := Discover(context.Background(), PlanetScaleSource{}, tma) | ||
assert.NotNil(t, err) | ||
assert.ErrorContains(t, err, "unable to access PlanetScale Database: Access Denied") | ||
assert.True(t, tma.PingContextFnInvoked) | ||
assert.False(t, tma.GetVitessTabletsFnInvoked) | ||
} | ||
|
||
func TestDiscover_CanFailIfCannotQuery(t *testing.T) { | ||
tma := getTestMysqlAccess() | ||
tma.GetTableNamesFn = func(ctx context.Context, source PlanetScaleSource) ([]string, error) { | ||
return []string{}, errors.New("read prohibited") | ||
} | ||
|
||
_, err := Discover(context.Background(), PlanetScaleSource{}, tma) | ||
assert.NotNil(t, err) | ||
assert.ErrorContains(t, err, "unable to retrieve table names: read prohibited") | ||
assert.True(t, tma.PingContextFnInvoked) | ||
assert.False(t, tma.GetVitessTabletsFnInvoked) | ||
} | ||
|
||
func TestDiscover_SchemaHasPrimaryKeys(t *testing.T) { | ||
tma := getTestMysqlAccess() | ||
tma.GetTableNamesFn = func(ctx context.Context, source PlanetScaleSource) ([]string, error) { | ||
return []string{ | ||
"employees", | ||
}, nil | ||
} | ||
|
||
tma.GetTableSchemaFn = func(ctx context.Context, source PlanetScaleSource, s string) (map[string]StreamProperty, error) { | ||
return map[string]StreamProperty{ | ||
"emp_no": {Types: []string{"null", "string"}}, | ||
"first_name": {Types: []string{"null", "string"}}, | ||
"last_name": {Types: []string{"null", "string"}}, | ||
}, nil | ||
} | ||
|
||
tma.GetTablePrimaryKeysFn = func(ctx context.Context, source PlanetScaleSource, s string) ([]string, error) { | ||
return []string{ | ||
"emp_no", | ||
}, nil | ||
} | ||
|
||
c, err := Discover(context.Background(), PlanetScaleSource{}, tma) | ||
assert.Nil(t, err) | ||
assert.True(t, tma.PingContextFnInvoked) | ||
assert.Len(t, c.Streams, 1) | ||
emp := c.Streams[0] | ||
assert.Equal(t, []string{"emp_no"}, emp.KeyProperties) | ||
} | ||
|
||
func TestDiscover_SchemaHasCursorProperties(t *testing.T) { | ||
tma := getTestMysqlAccess() | ||
tma.GetTableNamesFn = func(ctx context.Context, source PlanetScaleSource) ([]string, error) { | ||
return []string{ | ||
"employees", | ||
}, nil | ||
} | ||
|
||
tma.GetTableSchemaFn = func(ctx context.Context, source PlanetScaleSource, s string) (map[string]StreamProperty, error) { | ||
return map[string]StreamProperty{ | ||
"emp_no": {Types: []string{"null", "string"}}, | ||
"first_name": {Types: []string{"null", "string"}}, | ||
"last_name": {Types: []string{"null", "string"}}, | ||
}, nil | ||
} | ||
|
||
tma.GetTablePrimaryKeysFn = func(ctx context.Context, source PlanetScaleSource, s string) ([]string, error) { | ||
return []string{ | ||
"emp_no", | ||
}, nil | ||
} | ||
|
||
c, err := Discover(context.Background(), PlanetScaleSource{}, tma) | ||
assert.Nil(t, err) | ||
assert.True(t, tma.PingContextFnInvoked) | ||
assert.Len(t, c.Streams, 1) | ||
emp := c.Streams[0] | ||
assert.Equal(t, []string{"emp_no"}, emp.CursorProperties) | ||
} | ||
|
||
func TestDiscover_SchemaHasValidMetadata(t *testing.T) { | ||
tma := getTestMysqlAccess() | ||
tma.GetTableNamesFn = func(ctx context.Context, source PlanetScaleSource) ([]string, error) { | ||
return []string{ | ||
"employees", | ||
}, nil | ||
} | ||
|
||
tma.GetTableSchemaFn = func(ctx context.Context, source PlanetScaleSource, s string) (map[string]StreamProperty, error) { | ||
return map[string]StreamProperty{ | ||
"emp_no": {Types: []string{"null", "string"}}, | ||
"first_name": {Types: []string{"null", "string"}}, | ||
"last_name": {Types: []string{"null", "string"}}, | ||
}, nil | ||
} | ||
|
||
tma.GetTablePrimaryKeysFn = func(ctx context.Context, source PlanetScaleSource, s string) ([]string, error) { | ||
return []string{ | ||
"emp_no", | ||
}, nil | ||
} | ||
|
||
c, err := Discover(context.Background(), PlanetScaleSource{}, tma) | ||
assert.Nil(t, err) | ||
assert.True(t, tma.PingContextFnInvoked) | ||
assert.Len(t, c.Streams, 1) | ||
emp := c.Streams[0] | ||
mm := emp.Metadata.GetPropertyMap() | ||
assert.Equal(t, NodeMetadata{ | ||
Inclusion: "automatic", | ||
BreadCrumb: []string{"properties", "emp_no"}, | ||
}, mm["emp_no"].Metadata, "key properties should be auto-included") | ||
|
||
assert.Equal(t, NodeMetadata{ | ||
Inclusion: "available", | ||
BreadCrumb: []string{"properties", "first_name"}, | ||
}, mm["first_name"].Metadata, "non-key properties should be selectable") | ||
|
||
assert.Equal(t, NodeMetadata{ | ||
Inclusion: "available", | ||
BreadCrumb: []string{"properties", "last_name"}, | ||
}, mm["last_name"].Metadata, "non-key properties should be selectable") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.