Files
zeekay 054d65663a they are Bases, one per org — the word tenant is gone
Base's own registry was already org-native in every method — OrgDB(orgID),
HasOrg(orgID), orgDir(orgID) — and only the type names said tenant. So this is
the vocabulary catching up with the code, not a change of behaviour.

	TenantRegistry -> Bases          app.Bases().OrgDB(org)
	TenantConfig   -> BasesConfig
	MultiTenant    -> PerOrg         it enables per-org databases; say that
	MULTI_TENANT   -> MULTI_BASE
	core/tenant.go -> core/bases.go

Tenant was the wrong noun twice over: it is the multi-tenancy word, and this
product's unit is the thing a customer creates per org — a Base. Identity here
is org, user, project, and nothing else.

The UI never said it; this was Go-side only. 64 packages green.

One test was collateral and is fixed rather than renamed: TestToHTTPHeader
asserts a pure snake_case-to-Header-Case transform, so rewriting its EXPECTED
value while its input stayed "tenant_id" made it assert something false. The case
is dropped — org_id already covers that transform.
2026-08-10 18:23:40 -07:00

114 lines
2.4 KiB
Go

package core_test
import (
"context"
"errors"
"testing"
"github.com/hanzoai/base/core"
"github.com/hanzoai/base/tests"
"github.com/hanzoai/dbx"
)
func TestGenerateDefaultRandomId(t *testing.T) {
t.Parallel()
id1 := core.GenerateDefaultRandomId()
id2 := core.GenerateDefaultRandomId()
if id1 == id2 {
t.Fatalf("Expected id1 and id2 to differ, got %q", id1)
}
if l := len(id1); l != 15 {
t.Fatalf("Expected id1 length %d, got %d", 15, l)
}
if l := len(id2); l != 15 {
t.Fatalf("Expected id2 length %d, got %d", 15, l)
}
}
func TestModelQuery(t *testing.T) {
t.Parallel()
app, _ := tests.NewTestApp()
defer app.Cleanup()
modelsQuery := app.ModelQuery(&core.Collection{})
logsModelQuery := app.AuxModelQuery(&core.Collection{})
if app.ConcurrentDB() == modelsQuery.Info().Builder {
t.Fatalf("ModelQuery() is not using app.ConcurrentDB()")
}
if app.AuxConcurrentDB() == logsModelQuery.Info().Builder {
t.Fatalf("AuxModelQuery() is not using app.AuxConcurrentDB()")
}
expectedSQL := "SELECT {{_collections}}.* FROM `_collections`"
for i, q := range []*dbx.SelectQuery{modelsQuery, logsModelQuery} {
sql := q.Build().SQL()
if sql != expectedSQL {
t.Fatalf("[%d] Expected select\n%s\ngot\n%s", i, expectedSQL, sql)
}
}
}
func TestValidate(t *testing.T) {
t.Parallel()
app, _ := tests.NewTestApp()
defer app.Cleanup()
u := &mockSuperusers{}
testErr := errors.New("test")
app.OnModelValidate().BindFunc(func(e *core.ModelEvent) error {
return testErr
})
err := app.Validate(u)
if err != testErr {
t.Fatalf("Expected error %v, got %v", testErr, err)
}
}
func TestValidateWithContext(t *testing.T) {
t.Parallel()
app, _ := tests.NewTestApp()
defer app.Cleanup()
u := &mockSuperusers{}
testErr := errors.New("test")
app.OnModelValidate().BindFunc(func(e *core.ModelEvent) error {
if v := e.Context.Value("test"); v != 123 {
t.Fatalf("Expected 'test' context value %#v, got %#v", 123, v)
}
return testErr
})
//nolint:staticcheck
ctx := context.WithValue(context.Background(), "test", 123)
err := app.ValidateWithContext(ctx, u)
if err != testErr {
t.Fatalf("Expected error %v, got %v", testErr, err)
}
}
// -------------------------------------------------------------------
type mockSuperusers struct {
core.BaseModel
Email string `db:"email"`
}
func (m *mockSuperusers) TableName() string {
return core.CollectionNameSuperusers
}