63 lines
1.8 KiB
Go
63 lines
1.8 KiB
Go
package model
|
|
|
|
import (
|
|
"reflect"
|
|
"sync"
|
|
"testing"
|
|
|
|
"gorm.io/gorm/schema"
|
|
|
|
"spark-server/internal/module/message/entity"
|
|
)
|
|
|
|
func TestTableOf(t *testing.T) {
|
|
if tableOf(entity.KindGroup) != tableGroupMessage {
|
|
t.Fatal("group")
|
|
}
|
|
if tableOf(entity.KindPrivate) != tablePrivateMessage {
|
|
t.Fatal("private")
|
|
}
|
|
if tableOf(0) != tablePrivateMessage {
|
|
t.Fatal("default private")
|
|
}
|
|
}
|
|
|
|
func TestQueryKeyRows(t *testing.T) {
|
|
priv := PrivateMessage{MessageRow: MessageRow{ID: 1, SenderID: 2}, ReceiverID: 3}
|
|
if got := priv.entity(); got.ReceiverID != 3 || got.Kind != entity.KindPrivate {
|
|
t.Fatalf("%+v", got)
|
|
}
|
|
grp := GroupMessage{MessageRow: MessageRow{ID: 4, SenderID: 5}, GroupID: 9}
|
|
if got := grp.entity(); got.GroupID != 9 || got.Kind != entity.KindGroup {
|
|
t.Fatalf("%+v", got)
|
|
}
|
|
}
|
|
|
|
func TestPrivateMessageEmbedsMessageRowForGORM(t *testing.T) {
|
|
f, ok := reflect.TypeOf(PrivateMessage{}).FieldByName("MessageRow")
|
|
if !ok || f.Tag.Get("gorm") != "embedded" {
|
|
t.Fatalf("want gorm embedded on MessageRow, got %+v", f)
|
|
}
|
|
rec := PrivateMessage{MessageRow: MessageRow{ID: 99, ConversationID: 1, SenderID: 2}, ReceiverID: 3}
|
|
if rec.ID != 99 || rec.ConversationID != 1 {
|
|
t.Fatalf("embedded fields not promoted %+v", rec)
|
|
}
|
|
}
|
|
|
|
func TestPrivateMessageGORMSchemaColumns(t *testing.T) {
|
|
var cache sync.Map
|
|
s, err := schema.Parse(&PrivateMessage{}, &cache, schema.NamingStrategy{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := []string{"id", "conversation_id", "sender_id", "client_msg_id", "message_type", "content", "status", "created_at", "receiver_id"}
|
|
if len(s.DBNames) != len(want) {
|
|
t.Fatalf("columns %v want %v", s.DBNames, want)
|
|
}
|
|
for i, name := range want {
|
|
if s.DBNames[i] != name {
|
|
t.Fatalf("col[%d] got %s want %s (all %v)", i, s.DBNames[i], name, s.DBNames)
|
|
}
|
|
}
|
|
}
|