Skip to content

Commit

Permalink
Refactor for unit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
notfelineit committed Aug 12, 2024
1 parent 91036a5 commit b18bb2f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
6 changes: 5 additions & 1 deletion cmd/internal/planetscale_edge_mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (p planetScaleEdgeMySQLAccess) GetTableNames(ctx context.Context, psc Plane
}

// skip any that are Vitess's GC tables.
if !gcTableNameRegexp.MatchString(name) && !vreplRegex.MatchString(name) {
if !filterTable(name) {
tables = append(tables, name)
}
}
Expand All @@ -148,6 +148,10 @@ func (p planetScaleEdgeMySQLAccess) GetTableNames(ctx context.Context, psc Plane
return tables, err
}

func filterTable(name string) bool {
return gcTableNameRegexp.MatchString(name) || vreplRegex.MatchString(name)
}

func (p planetScaleEdgeMySQLAccess) GetTableSchema(ctx context.Context, psc PlanetScaleSource, tableName string) (map[string]PropertyType, error) {
properties := map[string]PropertyType{}

Expand Down
53 changes: 53 additions & 0 deletions cmd/internal/planetscale_edge_mysql_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package internal

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestFilterNames_CanFilterInternalVitessTables(t *testing.T) {
var tests = []struct {
name string
tableName string
filtered bool
}{
{
name: "filters_vrepl_tables",
tableName: "_750a3e1f_e6f3_5249_82af_82f5d325ecab_20240528153135_vrepl",
filtered: true,
},
{
name: "filters_vt_DROP_tables",
tableName: "_vt_DROP_6ace8bcef73211ea87e9f875a4d24e90_20200915120410",
filtered: true,
},
{
name: "filters_vt_HOLD_tables",
tableName: "_vt_HOLD_6ace8bcef73211ea87e9f875a4d24e90_20200915120410",
filtered: true,
},
{
name: "filters_vt_EVAC_tables",
tableName: "_vt_EVAC_6ace8bcef73211ea87e9f875a4d24e90_20200915120410",
filtered: true,
},
{
name: "filters_vt_PURGE_tables",
tableName: "_vt_PURGE_6ace8bcef73211ea87e9f875a4d24e90_20200915120410",
filtered: true,
},
{
name: "does_not_filter_regular_table",
tableName: "customers",
filtered: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
filteredResult := filterTable(tt.tableName)
assert.Equal(t, tt.filtered, filteredResult)
})
}
}

0 comments on commit b18bb2f

Please sign in to comment.