File size: 1,715 Bytes
83a59ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package entities

import (
	"encoding/json"
	"reflect"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestMessageThreadUnreadCountHasDefault(t *testing.T) {
	threadType := reflect.TypeOf(MessageThread{})

	unreadCount, ok := threadType.FieldByName("UnreadCount")
	require.True(t, ok)
	assert.Contains(t, unreadCount.Tag.Get("gorm"), "not null")
	assert.Contains(t, unreadCount.Tag.Get("gorm"), "default:0")
	assert.Equal(t, "unread_count", unreadCount.Tag.Get("json"))
}

func TestMessageThreadHasUniqueOwnerContactPerUser(t *testing.T) {
	threadType := reflect.TypeOf(MessageThread{})

	expectedTags := map[string]string{
		"UserID":  "uniqueIndex:idx_message_threads_user_owner_contact,priority:1",
		"Owner":   "uniqueIndex:idx_message_threads_user_owner_contact,priority:2",
		"Contact": "uniqueIndex:idx_message_threads_user_owner_contact,priority:3",
	}
	for fieldName, expectedTag := range expectedTags {
		field, ok := threadType.FieldByName(fieldName)
		require.True(t, ok)
		assert.Contains(t, field.Tag.Get("gorm"), expectedTag)
	}
}

func TestMessageThreadContactDetailsAreTransientAndOmittedWhenNil(t *testing.T) {
	threadType := reflect.TypeOf(MessageThread{})

	contactDetails, ok := threadType.FieldByName("ContactDetails")
	require.True(t, ok)
	assert.Equal(t, "*entities.Contact", contactDetails.Type.String())
	assert.Equal(t, "contact_details,omitempty", contactDetails.Tag.Get("json"))
	assert.Equal(t, "-", contactDetails.Tag.Get("gorm"))

	data, err := json.Marshal(MessageThread{})
	require.NoError(t, err)

	var payload map[string]any
	require.NoError(t, json.Unmarshal(data, &payload))
	assert.NotContains(t, payload, "contact_details")
}