92 lines
3.4 KiB
Go
92 lines
3.4 KiB
Go
//go:build mysql
|
|
|
|
package mysql
|
|
|
|
import (
|
|
"database/sql"
|
|
"strings"
|
|
"testing"
|
|
|
|
types "github.com/tinode/chat/server/store/types"
|
|
)
|
|
|
|
type recordingGroupSchemaExecutor struct {
|
|
statements []string
|
|
}
|
|
|
|
func (e *recordingGroupSchemaExecutor) Exec(query string, _ ...any) (sql.Result, error) {
|
|
e.statements = append(e.statements, query)
|
|
return nil, nil
|
|
}
|
|
|
|
func TestEnsureIMGroupBusinessTablesUsesSelectedTenantDatabase(t *testing.T) {
|
|
exec := &recordingGroupSchemaExecutor{}
|
|
if err := ensureIMGroupBusinessTables(exec); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(exec.statements) != 25 {
|
|
t.Fatalf("executed %d group schema statements, want 25", len(exec.statements))
|
|
}
|
|
for _, statement := range exec.statements {
|
|
if strings.Contains(strings.ToLower(statement), "tinode.") || strings.Contains(strings.ToLower(statement), "create database") {
|
|
t.Fatalf("group schema must use the selected tenant database: %s", statement)
|
|
}
|
|
}
|
|
joined := strings.ToLower(strings.Join(exec.statements, "\n"))
|
|
if strings.Contains(joined, "im_group_events_outbox") {
|
|
t.Fatal("group event outbox table must not be created")
|
|
}
|
|
for _, expected := range []string{
|
|
"primary key(id), unique index uk_im_group_settings_topic(topic)",
|
|
"primary key(id), unique index uk_im_group_announcement_reads_announcement_user(announcement_id, user_id)",
|
|
"primary key(id), unique index uk_im_group_mutes_topic_user(topic, user_id)",
|
|
} {
|
|
if !strings.Contains(joined, expected) {
|
|
t.Fatalf("group schema is missing snowflake primary-key definition %q", expected)
|
|
}
|
|
}
|
|
if !strings.Contains(joined, "json_set(public, '$.fn'") {
|
|
t.Fatal("group schema is missing historical public.fn backfill")
|
|
}
|
|
for _, expected := range []string{"im_group_message_shards", "im_group_message_topic_shards", "im_group_message_dedup", "primary key(message_id)"} {
|
|
if !strings.Contains(joined, expected) {
|
|
t.Fatalf("group shard schema is missing %q", expected)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestGroupMessageShardTableRejectsUnsafeMonth(t *testing.T) {
|
|
if table, ok := groupMessageShardTable(1, "202609"); !ok || table != "im_001_group_messages_202609" {
|
|
t.Fatalf("unexpected valid shard: table=%q ok=%v", table, ok)
|
|
}
|
|
for _, month := range []string{"", "20269", "202613", "2026_09", "202609;DROP TABLE im_users"} {
|
|
if table, ok := groupMessageShardTable(1, month); ok || table != "" {
|
|
t.Fatalf("unsafe shard month accepted: month=%q table=%q", month, table)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestTenantPartitionTableNames(t *testing.T) {
|
|
tests := []struct {
|
|
tenantID types.TenantID
|
|
friends string
|
|
messages string
|
|
group string
|
|
}{
|
|
{tenantID: 1, friends: "im_001_friendships", messages: "im_001_messages", group: "im_001_group_messages_202609"},
|
|
{tenantID: 10, friends: "im_010_friendships", messages: "im_010_messages", group: "im_010_group_messages_202609"},
|
|
{tenantID: 1000, friends: "im_1000_friendships", messages: "im_1000_messages", group: "im_1000_group_messages_202609"},
|
|
}
|
|
for _, tc := range tests {
|
|
if got := tenantFriendshipsTable(tc.tenantID); got != tc.friends {
|
|
t.Fatalf("tenant %d friendship table: got %q, want %q", tc.tenantID, got, tc.friends)
|
|
}
|
|
if got := tenantMessagesTable(tc.tenantID); got != tc.messages {
|
|
t.Fatalf("tenant %d message table: got %q, want %q", tc.tenantID, got, tc.messages)
|
|
}
|
|
if got, ok := tenantGroupMessagesTable(tc.tenantID, "202609"); !ok || got != tc.group {
|
|
t.Fatalf("tenant %d group message table: got %q, ok=%v, want %q", tc.tenantID, got, ok, tc.group)
|
|
}
|
|
}
|
|
}
|