implement SetTableName patttern that allows table name to be set back to the default for the storage type. (#196)

pull/207/head
Jim 6 years ago committed by GitHub
parent fa5f45260b
commit c83e90ed38
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,16 +1,22 @@
package authtoken
const defaultAuthTokenTableName = "auth_token"
// TableName returns the table name for the auth token.
func (s *AuthToken) TableName() string {
if s.tableName != "" {
return s.tableName
}
return "auth_token"
return defaultAuthTokenTableName
}
// SetTableName sets the table name.
// SetTableName sets the table name. If the caller attempts to
// set the name to "" the name will be reset to the default name.
func (s *AuthToken) SetTableName(n string) {
if n != "" {
switch n {
case "":
s.tableName = defaultAuthTokenTableName
default:
s.tableName = n
}
}

@ -0,0 +1,45 @@
package authtoken
import (
"testing"
"github.com/hashicorp/watchtower/internal/authtoken/store"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestAuthToken_SetTableName(t *testing.T) {
defaultTableName := defaultAuthTokenTableName
tests := []struct {
name string
initialName string
setNameTo string
want string
}{
{
name: "new-name",
initialName: "",
setNameTo: "new-name",
want: "new-name",
},
{
name: "reset to default",
initialName: "initial",
setNameTo: "",
want: defaultTableName,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
def := allocAuthToken()
require.Equal(defaultTableName, def.TableName())
s := &AuthToken{
AuthToken: &store.AuthToken{},
tableName: tt.initialName,
}
s.SetTableName(tt.setNameTo)
assert.Equal(tt.want, s.TableName())
})
}
}

@ -6,6 +6,13 @@ import (
"google.golang.org/protobuf/proto"
)
const (
defaultUserTablename = "db_test_user"
defaultCarTableName = "db_test_car"
defaultRentalTableName = "db_test_rental"
defaultScooterTableName = "db_test_scooter"
)
type TestUser struct {
*StoreTestUser
table string `gorm:"-"`
@ -40,11 +47,14 @@ func (u *TestUser) TableName() string {
if u.table != "" {
return u.table
}
return "db_test_user"
return defaultUserTablename
}
func (u *TestUser) SetTableName(name string) {
if name != "" {
switch name {
case "":
u.table = defaultUserTablename
default:
u.table = name
}
}
@ -71,10 +81,13 @@ func (c *TestCar) TableName() string {
return c.table
}
return "db_test_car"
return defaultCarTableName
}
func (c *TestCar) SetTableName(name string) {
if name != "" {
switch name {
case "":
c.table = defaultCarTableName
default:
c.table = name
}
}
@ -100,10 +113,13 @@ func (r *TestRental) TableName() string {
return r.table
}
return "db_test_rental"
return defaultRentalTableName
}
func (r *TestRental) SetTableName(name string) {
if name != "" {
switch name {
case "":
r.table = defaultRentalTableName
default:
r.table = name
}
}
@ -135,11 +151,14 @@ func (t *TestScooter) TableName() string {
if t.table != "" {
return t.table
}
return "db_test_scooter"
return defaultScooterTableName
}
func (t *TestScooter) SetTableName(name string) {
if name != "" {
switch name {
case "":
t.table = defaultScooterTableName
default:
t.table = name
}
}

@ -1,16 +1,27 @@
package static
const (
defaultHostCatalogTableName = "static_host_catalog"
defaultHostTableName = "static_host"
defaultHostSetTableName = "static_host_set"
defaultHostSetMemberTableName = "static_host_set_member"
)
// TableName returns the table name for the host catalog.
func (c *HostCatalog) TableName() string {
if c.tableName != "" {
return c.tableName
}
return "static_host_catalog"
return defaultHostCatalogTableName
}
// SetTableName sets the table name.
// SetTableName sets the table name. If the caller attempts to
// set the name to "" the name will be reset to the default name.
func (c *HostCatalog) SetTableName(n string) {
if n != "" {
switch n {
case "":
c.tableName = defaultHostCatalogTableName
default:
c.tableName = n
}
}
@ -20,12 +31,16 @@ func (h *Host) TableName() string {
if h.tableName != "" {
return h.tableName
}
return "static_host"
return defaultHostTableName
}
// SetTableName sets the table name.
// SetTableName sets the table name. If the caller attempts to
// set the name to "" the name will be reset to the default name.
func (h *Host) SetTableName(n string) {
if n != "" {
switch n {
case "":
h.tableName = defaultHostTableName
default:
h.tableName = n
}
}
@ -35,12 +50,16 @@ func (s *HostSet) TableName() string {
if s.tableName != "" {
return s.tableName
}
return "static_host_set"
return defaultHostSetTableName
}
// SetTableName sets the table name.
// SetTableName sets the table name. If the caller attempts to
// set the name to "" the name will be reset to the default name.
func (s *HostSet) SetTableName(n string) {
if n != "" {
switch n {
case "":
s.tableName = defaultHostSetTableName
default:
s.tableName = n
}
}
@ -50,12 +69,16 @@ func (m *HostSetMember) TableName() string {
if m.tableName != "" {
return m.tableName
}
return "static_host_set_member"
return defaultHostSetMemberTableName
}
// SetTableName sets the table name.
// SetTableName sets the table name. If the caller attempts to
// set the name to "" the name will be reset to the default name.
func (m *HostSetMember) SetTableName(n string) {
if n != "" {
switch n {
case "":
m.tableName = defaultHostSetMemberTableName
default:
m.tableName = n
}
}

@ -0,0 +1,159 @@
package static
import (
"testing"
"github.com/hashicorp/watchtower/internal/host/static/store"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestHostCatalog_SetTableName(t *testing.T) {
defaultTableName := defaultHostCatalogTableName
tests := []struct {
name string
initialName string
setNameTo string
want string
}{
{
name: "new-name",
initialName: "",
setNameTo: "new-name",
want: "new-name",
},
{
name: "reset to default",
initialName: "initial",
setNameTo: "",
want: defaultTableName,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
def := allocCatalog()
require.Equal(defaultTableName, def.TableName())
s := &HostCatalog{
HostCatalog: &store.HostCatalog{},
tableName: tt.initialName,
}
s.SetTableName(tt.setNameTo)
assert.Equal(tt.want, s.TableName())
})
}
}
func TestHost_SetTableName(t *testing.T) {
defaultTableName := defaultHostTableName
tests := []struct {
name string
initialName string
setNameTo string
want string
}{
{
name: "new-name",
initialName: "",
setNameTo: "new-name",
want: "new-name",
},
{
name: "reset to default",
initialName: "initial",
setNameTo: "",
want: defaultTableName,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
def := &Host{
Host: &store.Host{},
}
require.Equal(defaultTableName, def.TableName())
s := &Host{
Host: &store.Host{},
tableName: tt.initialName,
}
s.SetTableName(tt.setNameTo)
assert.Equal(tt.want, s.TableName())
})
}
}
func TestHostSet_SetTableName(t *testing.T) {
defaultTableName := defaultHostSetTableName
tests := []struct {
name string
initialName string
setNameTo string
want string
}{
{
name: "new-name",
initialName: "",
setNameTo: "new-name",
want: "new-name",
},
{
name: "reset to default",
initialName: "initial",
setNameTo: "",
want: defaultTableName,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
def := &HostSet{
HostSet: &store.HostSet{},
}
require.Equal(defaultTableName, def.TableName())
s := &HostSet{
HostSet: &store.HostSet{},
tableName: tt.initialName,
}
s.SetTableName(tt.setNameTo)
assert.Equal(tt.want, s.TableName())
})
}
}
func TestHostSetMember_SetTableName(t *testing.T) {
defaultTableName := defaultHostSetMemberTableName
tests := []struct {
name string
initialName string
setNameTo string
want string
}{
{
name: "new-name",
initialName: "",
setNameTo: "new-name",
want: "new-name",
},
{
name: "reset to default",
initialName: "initial",
setNameTo: "",
want: defaultTableName,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
def := &HostSetMember{
HostSetMember: &store.HostSetMember{},
}
require.Equal(defaultTableName, def.TableName())
s := &HostSetMember{
HostSetMember: &store.HostSetMember{},
tableName: tt.initialName,
}
s.SetTableName(tt.setNameTo)
assert.Equal(tt.want, s.TableName())
})
}
}

@ -11,6 +11,10 @@ import (
"google.golang.org/protobuf/proto"
)
const (
defaultAuthAccountTableName = "auth_account"
)
// AuthAccount is from the auth subsystem and iam is only allowed to: lookup and
// update auth accounts. That's why there is no "new" factory for AuthAccounts.
type AuthAccount struct {
@ -61,12 +65,17 @@ func (a *AuthAccount) TableName() string {
if a.tableName != "" {
return a.tableName
}
return "auth_account"
return defaultAuthAccountTableName
}
// SetTableName sets the tablename and satisfies the ReplayableMessage interface.
// SetTableName sets the tablename and satisfies the ReplayableMessage
// interface. If the caller attempts to set the name to "" the name will be
// reset to the default name.
func (a *AuthAccount) SetTableName(n string) {
if n != "" {
switch n {
case "":
a.tableName = defaultAuthAccountTableName
default:
a.tableName = n
}
}

@ -6,6 +6,7 @@ import (
"github.com/hashicorp/watchtower/internal/db"
dbassert "github.com/hashicorp/watchtower/internal/db/assert"
"github.com/hashicorp/watchtower/internal/iam/store"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
@ -83,3 +84,41 @@ func TestAuthAccount_Clone(t *testing.T) {
assert.True(!proto.Equal(cp.(*AuthAccount).AuthAccount, acct2.AuthAccount))
})
}
func TestAuthAccount_SetTableName(t *testing.T) {
defaultTableName := defaultAuthAccountTableName
tests := []struct {
name string
initialName string
setNameTo string
want string
}{
{
name: "new-name",
initialName: "",
setNameTo: "new-name",
want: "new-name",
},
{
name: "reset to default",
initialName: "initial",
setNameTo: "",
want: defaultTableName,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
def := &AuthAccount{
AuthAccount: &store.AuthAccount{},
}
require.Equal(defaultTableName, def.TableName())
s := &AuthAccount{
AuthAccount: &store.AuthAccount{},
tableName: tt.initialName,
}
s.SetTableName(tt.setNameTo)
assert.Equal(tt.want, s.TableName())
})
}
}

@ -12,6 +12,10 @@ import (
"google.golang.org/protobuf/proto"
)
const (
defaultGroupTableName = "iam_group"
)
// Group is made up of principals which are scoped to an org.
type Group struct {
*store.Group
@ -88,12 +92,17 @@ func (g *Group) TableName() string {
if g.tableName != "" {
return g.tableName
}
return "iam_group"
return defaultGroupTableName
}
// SetTableName sets the tablename and satisfies the ReplayableMessage interface.
// SetTableName sets the tablename and satisfies the ReplayableMessage
// interface. If the caller attempts to set the name to "" the name will be
// reset to the default name.
func (g *Group) SetTableName(n string) {
if n != "" {
switch n {
case "":
g.tableName = defaultGroupTableName
default:
g.tableName = n
}
}

@ -352,3 +352,77 @@ func TestGroupMember_Clone(t *testing.T) {
assert.True(!proto.Equal(cp.(*GroupMemberUser).GroupMemberUser, gm2.GroupMemberUser))
})
}
func TestGroupMember_SetTableName(t *testing.T) {
defaultTableName := groupMemberViewDefaultTableName
tests := []struct {
name string
initialName string
setNameTo string
want string
}{
{
name: "new-name",
initialName: "",
setNameTo: "new-name",
want: "new-name",
},
{
name: "reset to default",
initialName: "initial",
setNameTo: "",
want: defaultTableName,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
def := &GroupMember{
GroupMemberView: &store.GroupMemberView{},
}
require.Equal(defaultTableName, def.TableName())
s := &GroupMember{
GroupMemberView: &store.GroupMemberView{},
tableName: tt.initialName,
}
s.SetTableName(tt.setNameTo)
assert.Equal(tt.want, s.TableName())
})
}
}
func TestGroupMemberUser_SetTableName(t *testing.T) {
defaultTableName := groupMemberUserDefaultTable
tests := []struct {
name string
initialName string
setNameTo string
want string
}{
{
name: "new-name",
initialName: "",
setNameTo: "new-name",
want: "new-name",
},
{
name: "reset to default",
initialName: "initial",
setNameTo: "",
want: defaultTableName,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
def := allocGroupMember()
require.Equal(defaultTableName, def.TableName())
s := &GroupMemberUser{
GroupMemberUser: &store.GroupMemberUser{},
tableName: tt.initialName,
}
s.SetTableName(tt.setNameTo)
assert.Equal(tt.want, s.TableName())
})
}
}

@ -8,6 +8,7 @@ import (
"github.com/hashicorp/watchtower/internal/db"
dbassert "github.com/hashicorp/watchtower/internal/db/assert"
"github.com/hashicorp/watchtower/internal/iam/store"
"github.com/hashicorp/watchtower/internal/oplog"
"github.com/hashicorp/watchtower/internal/types/action"
"github.com/hashicorp/watchtower/internal/types/resource"
@ -483,3 +484,39 @@ func TestGroup_Clone(t *testing.T) {
assert.True(!proto.Equal(cp.(*Group).Group, grp2.Group))
})
}
func TestGroup_SetTableName(t *testing.T) {
defaultTableName := defaultGroupTableName
tests := []struct {
name string
initialName string
setNameTo string
want string
}{
{
name: "new-name",
initialName: "",
setNameTo: "new-name",
want: "new-name",
},
{
name: "reset to default",
initialName: "initial",
setNameTo: "",
want: defaultTableName,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
def := allocGroup()
require.Equal(defaultTableName, def.TableName())
s := &Group{
Group: &store.Group{},
tableName: tt.initialName,
}
s.SetTableName(tt.setNameTo)
assert.Equal(tt.want, s.TableName())
})
}
}

@ -739,3 +739,113 @@ func TestGroupRole_Clone(t *testing.T) {
assert.True(!proto.Equal(cp.(*GroupRole).GroupRole, grpRole2.GroupRole))
})
}
func TestPrincipalRole_SetTableName(t *testing.T) {
defaultTableName := principalRoleViewDefaultTable
tests := []struct {
name string
initialName string
setNameTo string
want string
}{
{
name: "new-name",
initialName: "",
setNameTo: "new-name",
want: "new-name",
},
{
name: "reset to default",
initialName: "initial",
setNameTo: "",
want: defaultTableName,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
def := &PrincipalRole{
PrincipalRoleView: &store.PrincipalRoleView{},
}
require.Equal(defaultTableName, def.TableName())
s := &PrincipalRole{
PrincipalRoleView: &store.PrincipalRoleView{},
tableName: tt.initialName,
}
s.SetTableName(tt.setNameTo)
assert.Equal(tt.want, s.TableName())
})
}
}
func TestUserRole_SetTableName(t *testing.T) {
defaultTableName := userRoleDefaultTable
tests := []struct {
name string
initialName string
setNameTo string
want string
}{
{
name: "new-name",
initialName: "",
setNameTo: "new-name",
want: "new-name",
},
{
name: "reset to default",
initialName: "initial",
setNameTo: "",
want: defaultTableName,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
def := allocUserRole()
require.Equal(defaultTableName, def.TableName())
s := &UserRole{
UserRole: &store.UserRole{},
tableName: tt.initialName,
}
s.SetTableName(tt.setNameTo)
assert.Equal(tt.want, s.TableName())
})
}
}
func TestGroupRole_SetTableName(t *testing.T) {
defaultTableName := groupRoleDefaultTable
tests := []struct {
name string
initialName string
setNameTo string
want string
}{
{
name: "new-name",
initialName: "",
setNameTo: "new-name",
want: "new-name",
},
{
name: "reset to default",
initialName: "initial",
setNameTo: "",
want: defaultTableName,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
def := allocGroupRole()
require.Equal(defaultTableName, def.TableName())
s := &GroupRole{
GroupRole: &store.GroupRole{},
tableName: tt.initialName,
}
s.SetTableName(tt.setNameTo)
assert.Equal(tt.want, s.TableName())
})
}
}

@ -13,6 +13,10 @@ import (
"google.golang.org/protobuf/proto"
)
const (
defaultRoleTableName = "iam_role"
)
// Roles are granted permissions and assignable to Users and Groups.
type Role struct {
*store.Role
@ -96,12 +100,17 @@ func (r *Role) TableName() string {
if r.tableName != "" {
return r.tableName
}
return "iam_role"
return defaultRoleTableName
}
// SetTableName sets the tablename and satisfies the ReplayableMessage interface.
// SetTableName sets the tablename and satisfies the ReplayableMessage
// interface. If the caller attempts to set the name to "" the name will be
// reset to the default name.
func (r *Role) SetTableName(n string) {
if n != "" {
switch n {
case "":
r.tableName = defaultRoleTableName
default:
r.tableName = n
}
}

@ -98,7 +98,9 @@ func (g *RoleGrant) TableName() string {
return defaultRoleGrantTable
}
// SetTableName sets the tablename and satisfies the ReplayableMessage interface
// SetTableName sets the tablename and satisfies the ReplayableMessage
// interface. If the caller attempts to set the name to "" the name will be
// reset to the default name.
func (g *RoleGrant) SetTableName(n string) {
switch n {
case "":

@ -261,3 +261,39 @@ func TestRoleGrant_Clone(t *testing.T) {
assert.True(!proto.Equal(cp.(*RoleGrant).RoleGrant, g2.RoleGrant))
})
}
func TestRoleGrant_SetTableName(t *testing.T) {
defaultTableName := defaultRoleGrantTable
tests := []struct {
name string
initialName string
setNameTo string
want string
}{
{
name: "new-name",
initialName: "",
setNameTo: "new-name",
want: "new-name",
},
{
name: "reset to default",
initialName: "initial",
setNameTo: "",
want: defaultTableName,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
def := allocRoleGrant()
require.Equal(defaultTableName, def.TableName())
s := &RoleGrant{
RoleGrant: &store.RoleGrant{},
tableName: tt.initialName,
}
s.SetTableName(tt.setNameTo)
assert.Equal(tt.want, s.TableName())
})
}
}

@ -8,6 +8,7 @@ import (
"github.com/hashicorp/watchtower/internal/db"
dbassert "github.com/hashicorp/watchtower/internal/db/assert"
"github.com/hashicorp/watchtower/internal/iam/store"
"github.com/hashicorp/watchtower/internal/oplog"
"github.com/hashicorp/watchtower/internal/types/action"
"github.com/hashicorp/watchtower/internal/types/resource"
@ -586,3 +587,39 @@ func TestRole_Clone(t *testing.T) {
assert.True(!proto.Equal(cp.(*Role).Role, role2.Role))
})
}
func TestRole_SetTableName(t *testing.T) {
defaultTableName := defaultRoleTableName
tests := []struct {
name string
initialName string
setNameTo string
want string
}{
{
name: "new-name",
initialName: "",
setNameTo: "new-name",
want: "new-name",
},
{
name: "reset to default",
initialName: "initial",
setNameTo: "",
want: defaultTableName,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
def := allocRole()
require.Equal(defaultTableName, def.TableName())
s := &Role{
Role: &store.Role{},
tableName: tt.initialName,
}
s.SetTableName(tt.setNameTo)
assert.Equal(tt.want, s.TableName())
})
}
}

@ -13,6 +13,10 @@ import (
"google.golang.org/protobuf/proto"
)
const (
defaultScopeTableName = "iam_scope"
)
// Scope is used to create a hierarchy of "containers" that encompass the scope of
// an IAM resource. Scopes are Global, Orgs and Projects.
type Scope struct {
@ -205,12 +209,17 @@ func (s *Scope) TableName() string {
if s.tableName != "" {
return s.tableName
}
return "iam_scope"
return defaultScopeTableName
}
// SetTableName sets the tablename and satisfies the ReplayableMessage interface
// SetTableName sets the tablename and satisfies the ReplayableMessage
// interface. If the caller attempts to set the name to "" the name will be
// reset to the default name.
func (s *Scope) SetTableName(n string) {
if n != "" {
switch n {
case "":
s.tableName = defaultScopeTableName
default:
s.tableName = n
}
}

@ -240,3 +240,39 @@ func TestScope_GlobalErrors(t *testing.T) {
assert.Equal(t, 0, rows)
})
}
func TestScope_SetTableName(t *testing.T) {
defaultTableName := defaultScopeTableName
tests := []struct {
name string
initialName string
setNameTo string
want string
}{
{
name: "new-name",
initialName: "",
setNameTo: "new-name",
want: "new-name",
},
{
name: "reset to default",
initialName: "initial",
setNameTo: "",
want: defaultTableName,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
def := allocScope()
require.Equal(defaultTableName, def.TableName())
s := &Scope{
Scope: &store.Scope{},
tableName: tt.initialName,
}
s.SetTableName(tt.setNameTo)
assert.Equal(tt.want, s.TableName())
})
}
}

@ -12,6 +12,10 @@ import (
"google.golang.org/protobuf/proto"
)
const (
defaultUserTableName = "iam_user"
)
// User defines watchtower users which are scoped to an Org
type User struct {
*store.User
@ -89,12 +93,17 @@ func (u *User) TableName() string {
if u.tableName != "" {
return u.tableName
}
return "iam_user"
return defaultUserTableName
}
// SetTableName sets the tablename and satisfies the ReplayableMessage interface
// SetTableName sets the tablename and satisfies the ReplayableMessage
// interface. If the caller attempts to set the name to "" the name will be
// reset to the default name.
func (u *User) SetTableName(n string) {
if n != "" {
switch n {
case "":
u.tableName = defaultUserTableName
default:
u.tableName = n
}
}

@ -6,6 +6,7 @@ import (
"testing"
"github.com/hashicorp/watchtower/internal/db"
"github.com/hashicorp/watchtower/internal/iam/store"
"github.com/hashicorp/watchtower/internal/types/action"
"github.com/hashicorp/watchtower/internal/types/resource"
"github.com/stretchr/testify/assert"
@ -380,3 +381,39 @@ func TestUser_ResourceType(t *testing.T) {
u := allocUser()
assert.Equal(t, resource.User, u.ResourceType())
}
func TestUser_SetTableName(t *testing.T) {
defaultTableName := defaultUserTableName
tests := []struct {
name string
initialName string
setNameTo string
want string
}{
{
name: "new-name",
initialName: "",
setNameTo: "new-name",
want: "new-name",
},
{
name: "reset to default",
initialName: "initial",
setNameTo: "",
want: defaultTableName,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
def := allocUser()
require.Equal(defaultTableName, def.TableName())
s := &User{
User: &store.User{},
tableName: tt.initialName,
}
s.SetTableName(tt.setNameTo)
assert.Equal(tt.want, s.TableName())
})
}
}

@ -3,6 +3,12 @@ package oplog_test
import "github.com/jinzhu/gorm"
const (
defaultTestUserTableName = "oplog_test_user"
defaultTestCarTableName = "oplog_test_car"
defaultTestRentalTableName = "oplog_test_rental"
)
// Init will use gorm migrations to init tables for test models
func Init(db *gorm.DB) {
db.AutoMigrate(&TestUser{})
@ -23,12 +29,17 @@ func (u *TestUser) TableName() string {
if u.Table != "" {
return u.Table
}
return "oplog_test_user"
return defaultTestUserTableName
}
// SetTableName allows the table name to be overridden and makes a TestUser a ReplayableMessage
// SetTableName allows the table name to be overridden and makes a TestUser a
// ReplayableMessage. If the caller attempts to set the name to "" the name will be
// reset to the default name.
func (u *TestUser) SetTableName(n string) {
if n != "" {
switch n {
case "":
u.Table = defaultTestUserTableName
default:
u.Table = n
}
}
@ -38,12 +49,15 @@ func (c *TestCar) TableName() string {
if c.Table != "" {
return c.Table
}
return "oplog_test_car"
return defaultTestCarTableName
}
// SetTableName allows the table name to be overridden and makes a TestCar a ReplayableMessage
func (c *TestCar) SetTableName(n string) {
if n != "" {
switch n {
case "":
c.Table = defaultTestCarTableName
default:
c.Table = n
}
}
@ -53,12 +67,15 @@ func (r *TestRental) TableName() string {
if r.Table != "" {
return r.Table
}
return "oplog_test_rental"
return defaultTestRentalTableName
}
// SetTableName allows the table name to be overridden and makes a TestRental a ReplayableMessage
func (r *TestRental) SetTableName(n string) {
if n != "" {
switch n {
case "":
r.Table = defaultTestRentalTableName
default:
r.Table = n
}
}

Loading…
Cancel
Save