fix shared-db tenant runtime gaps

This commit is contained in:
richjohnrambo
2026-09-22 19:28:33 +08:00
parent 49e43aa3cf
commit 9ddf6ec5ee
12 changed files with 346 additions and 58 deletions
+70 -2
View File
@@ -112,8 +112,8 @@ func TestHTTPInfrastructureTablesCreated(t *testing.T) {
if err := db.Get(&count, query, args...); err != nil {
t.Fatal(err)
}
if count != 0 {
t.Fatalf("HTTP infrastructure tables contain %d tenant_id columns", count)
if count != len(tables) {
t.Fatalf("HTTP infrastructure tenant_id columns: got %d, want %d", count, len(tables))
}
}
@@ -644,6 +644,74 @@ func TestTopicCreateP2P(t *testing.T) {
}
}
func TestTenantTopicCreateP2PWritesTenantSubscriptions(t *testing.T) {
tenantAdapter, ok := adp.(adapter.TenantBusinessAdapter)
if !ok {
t.Fatal("MySQL adapter does not implement TenantBusinessAdapter")
}
var tenantID types.TenantID
if err := db.Get(&tenantID, "SELECT id FROM im_tenant WHERE code='default'"); err != nil {
t.Fatal(err)
}
var tenantColumns int
if err := db.Get(&tenantColumns, `SELECT COUNT(*) FROM information_schema.columns
WHERE table_schema=DATABASE() AND table_name='im_subscriptions' AND column_name='tenant_id'`); err != nil {
t.Fatal(err)
}
if tenantColumns == 0 {
t.Skip("im_subscriptions.tenant_id is missing in the current test database")
}
first := types.ParseUserId("usr" + testData.Users[0].Id)
second := types.ParseUserId("usr" + testData.Users[2].Id)
topic := first.P2PName(second)
if _, err := db.Exec("DELETE FROM im_subscriptions WHERE tenant_id=? AND topic=?", tenantID, topic); err != nil {
t.Fatal(err)
}
if _, err := db.Exec("DELETE FROM im_topics WHERE tenant_id=? AND name=?", tenantID, topic); err != nil {
t.Fatal(err)
}
now := types.TimeNow()
one := &types.Subscription{
TenantID: tenantID,
ObjHeader: types.ObjHeader{CreatedAt: now, UpdatedAt: now},
User: first.String(),
Topic: topic,
ModeWant: types.ModeCPublic,
ModeGiven: types.ModeCPublic,
}
one.SetTouchedAt(now)
two := &types.Subscription{
TenantID: tenantID,
ObjHeader: types.ObjHeader{CreatedAt: now, UpdatedAt: now},
User: second.String(),
Topic: topic,
ModeWant: types.ModeCPublic,
ModeGiven: types.ModeCPublic,
}
two.SetTouchedAt(now)
if err := tenantAdapter.TenantTopicCreateP2P(tenantID, one, two); err != nil {
t.Fatal(err)
}
var count int
if err := db.Get(&count, "SELECT COUNT(*) FROM im_subscriptions WHERE tenant_id=? AND topic=?", tenantID, topic); err != nil {
t.Fatal(err)
}
if count != 2 {
t.Fatalf("tenant P2P subscriptions: got %d, want 2", count)
}
subs, err := tenantAdapter.TenantUsersForTopic(tenantID, topic, false, nil)
if err != nil {
t.Fatal(err)
}
if len(subs) != 2 {
t.Fatalf("TenantUsersForTopic returned %d subscriptions, want 2", len(subs))
}
}
func TestTopicShare(t *testing.T) {
if err := adp.TopicShare(testData.Subs[0].Topic, testData.Subs); err != nil {
t.Fatal(err)