-
Notifications
You must be signed in to change notification settings - Fork 6
/
multitenancy_test.go
117 lines (97 loc) · 2.81 KB
/
multitenancy_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package multitenancy
import (
"context"
"testing"
"github.com/bartventer/gorm-multitenancy/v8/pkg/driver"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gorm.io/gorm"
"gorm.io/gorm/utils/tests"
)
type mockDriver struct{}
func (m *mockDriver) CurrentTenant(ctx context.Context, db *gorm.DB) string {
return "test-tenant"
}
func (m *mockDriver) RegisterModels(ctx context.Context, db *gorm.DB, models ...driver.TenantTabler) error {
return nil
}
func (m *mockDriver) MigrateSharedModels(ctx context.Context, db *gorm.DB) error {
return nil
}
func (m *mockDriver) MigrateTenantModels(ctx context.Context, db *gorm.DB, tenantID string) error {
return nil
}
func (m *mockDriver) OffboardTenant(ctx context.Context, db *gorm.DB, tenantID string) error {
return nil
}
func (m *mockDriver) UseTenant(ctx context.Context, db *gorm.DB, tenantID string) (reset func() error, err error) {
return func() error { return nil }, nil
}
func TestDB_CurrentTenant(t *testing.T) {
db := NewDB(&mockDriver{}, &gorm.DB{})
tenantID := db.CurrentTenant(context.Background())
if tenantID != "test-tenant" {
t.Errorf("Expected tenant ID 'test-tenant', got '%s'", tenantID)
}
}
func TestDB_RegisterModels(t *testing.T) {
db := NewDB(&mockDriver{}, &gorm.DB{})
err := db.RegisterModels(context.Background())
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
}
func TestDB_MigrateSharedModels(t *testing.T) {
db := NewDB(&mockDriver{}, &gorm.DB{})
err := db.MigrateSharedModels(context.Background())
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
}
func TestDB_MigrateTenantModels(t *testing.T) {
db := NewDB(&mockDriver{}, &gorm.DB{})
err := db.MigrateTenantModels(context.Background(), "test-tenant")
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
}
func TestDB_OffboardTenant(t *testing.T) {
db := NewDB(&mockDriver{}, &gorm.DB{})
err := db.OffboardTenant(context.Background(), "test-tenant")
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
}
func TestDB_UseTenant(t *testing.T) {
db := NewDB(&mockDriver{}, &gorm.DB{})
_, err := db.UseTenant(context.Background(), "test-tenant")
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
}
func newDB(t *testing.T) *DB {
t.Helper()
db, err := gorm.Open(tests.DummyDialector{}, &gorm.Config{})
require.NoError(t, err)
return NewDB(&mockDriver{}, db)
}
func TestDB_Session(t *testing.T) {
db := newDB(t)
newDB := db.Session(&gorm.Session{})
assert.NotNil(t, newDB)
}
func TestDB_Debug(t *testing.T) {
db := newDB(t)
newDB := db.Debug()
assert.NotNil(t, newDB)
}
func TestDB_WithContext(t *testing.T) {
db := newDB(t)
newDB := db.WithContext(context.Background())
assert.NotNil(t, newDB)
}
func TestDB_Begin(t *testing.T) {
db := newDB(t)
newDB := db.Begin()
assert.NotNil(t, newDB)
}