File size: 3,671 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package repositories

import (
	"testing"

	"github.com/NdoleStudio/httpsms/pkg/entities"
	"github.com/google/uuid"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"go.mongodb.org/mongo-driver/v2/bson"
)

func TestMongoContactFilter_ScopesByUserAndSearchesContactFields(t *testing.T) {
	filter := mongoContactFilter(entities.UserID("user-1"), "Alice")

	assert.Equal(t, bson.D{
		{Key: "user_id", Value: "user-1"},
		{Key: "$or", Value: bson.A{
			bson.D{{Key: "name", Value: bson.Regex{Pattern: "Alice", Options: "i"}}},
			bson.D{{Key: "emails", Value: bson.Regex{Pattern: "Alice", Options: "i"}}},
			bson.D{{Key: "phone_numbers", Value: bson.Regex{Pattern: "Alice", Options: "i"}}},
		}},
	}, filter)
}

func TestMongoContactFilter_WithoutSearchOnlyScopesByUser(t *testing.T) {
	filter := mongoContactFilter(entities.UserID("user-1"), "")

	assert.Equal(t, bson.D{{Key: "user_id", Value: "user-1"}}, filter)
}

func TestMongoContactFilter_TreatsSearchAsLiteralText(t *testing.T) {
	filter := mongoContactFilter(entities.UserID("user-1"), "Alice.*")

	expectedExpression := bson.Regex{Pattern: `Alice\.\*`, Options: "i"}
	assert.Equal(t, bson.D{
		{Key: "user_id", Value: "user-1"},
		{Key: "$or", Value: bson.A{
			bson.D{{Key: "name", Value: expectedExpression}},
			bson.D{{Key: "emails", Value: expectedExpression}},
			bson.D{{Key: "phone_numbers", Value: expectedExpression}},
		}},
	}, filter)
}

func TestMongoContactSort_UsesStableRequestedOrdering(t *testing.T) {
	tests := []struct {
		name     string
		params   IndexParams
		expected bson.D
	}{
		{
			name:     "default",
			params:   IndexParams{},
			expected: bson.D{{Key: "updated_at", Value: -1}, {Key: "_id", Value: -1}},
		},
		{
			name:     "name ascending",
			params:   IndexParams{SortBy: "name"},
			expected: bson.D{{Key: "name", Value: 1}, {Key: "_id", Value: 1}},
		},
		{
			name:     "name descending",
			params:   IndexParams{SortBy: "name", SortDescending: true},
			expected: bson.D{{Key: "name", Value: -1}, {Key: "_id", Value: -1}},
		},
		{
			name:     "updated ascending",
			params:   IndexParams{SortBy: "updated_at"},
			expected: bson.D{{Key: "updated_at", Value: 1}, {Key: "_id", Value: 1}},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			assert.Equal(t, tt.expected, mongoContactSort(tt.params))
		})
	}
}

func TestMongoContactPhoneNumbersFilter_ScopesByUserAndRequestedNumbers(t *testing.T) {
	filter := mongoContactPhoneNumbersFilter(
		entities.UserID("user-1"),
		[]string{"+18005550199", "+18005550100"},
	)

	assert.Equal(t, bson.D{
		{Key: "user_id", Value: "user-1"},
		{Key: "phone_numbers", Value: bson.D{{Key: "$in", Value: []string{"+18005550199", "+18005550100"}}}},
	}, filter)
}

func TestMongoContactIDFilter_ScopesByUserAndContactID(t *testing.T) {
	contactID := uuid.MustParse("11111111-1111-1111-1111-111111111111")

	filter := mongoContactIDFilter(entities.UserID("user-1"), contactID)

	assert.Equal(t, bson.D{
		{Key: "user_id", Value: "user-1"},
		{Key: "_id", Value: contactID.String()},
	}, filter)
}

func TestContactMongoIndexModels_CoverListingAndPhoneLookup(t *testing.T) {
	indexes := contactMongoIndexModels()

	require.Len(t, indexes, 3)
	assert.Equal(t, bson.D{
		{Key: "user_id", Value: 1},
		{Key: "updated_at", Value: -1},
		{Key: "_id", Value: -1},
	}, indexes[0].Keys)
	assert.Equal(t, bson.D{
		{Key: "user_id", Value: 1},
		{Key: "name", Value: 1},
		{Key: "_id", Value: 1},
	}, indexes[1].Keys)
	assert.Equal(t, bson.D{
		{Key: "user_id", Value: 1},
		{Key: "phone_numbers", Value: 1},
		{Key: "updated_at", Value: 1},
		{Key: "_id", Value: 1},
	}, indexes[2].Keys)
}