82 lines
2.4 KiB
Go
82 lines
2.4 KiB
Go
package invitationdb
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
|
|
"git.sendico.io/tech/sxchnge/bff/pkg/db/repository"
|
|
"git.sendico.io/tech/sxchnge/bff/pkg/db/repository/builder"
|
|
ri "git.sendico.io/tech/sxchnge/bff/pkg/db/repository/index"
|
|
"git.sendico.io/tech/sxchnge/bff/pkg/model"
|
|
"go.mongodb.org/mongo-driver/v2/bson"
|
|
)
|
|
|
|
func TestPendingInvitationEmailIndexDefinition(t *testing.T) {
|
|
def := pendingInvitationEmailIndexDefinition()
|
|
|
|
expectedKeys := []ri.Key{
|
|
{Field: "organizationRef", Sort: ri.Asc},
|
|
{Field: "description.email", Sort: ri.Asc},
|
|
}
|
|
if !reflect.DeepEqual(def.Keys, expectedKeys) {
|
|
t.Fatalf("keys mismatch: got %#v want %#v", def.Keys, expectedKeys)
|
|
}
|
|
if !def.Unique {
|
|
t.Fatal("index must be unique")
|
|
}
|
|
if def.Name != uniquePendingInvitationEmailIndexName {
|
|
t.Fatalf("index name mismatch: got %q want %q", def.Name, uniquePendingInvitationEmailIndexName)
|
|
}
|
|
|
|
expectedFilter := repository.Query().
|
|
In(repository.Field("status"), "created", "sent").
|
|
Comparison(repository.Field("description.email"), builder.Type, "string").
|
|
BuildQuery()
|
|
if !reflect.DeepEqual(def.PartialFilter.BuildQuery(), expectedFilter) {
|
|
t.Fatalf("partial filter mismatch: got %#v want %#v", def.PartialFilter.BuildQuery(), expectedFilter)
|
|
}
|
|
}
|
|
|
|
func TestPendingInvitationEmailIndexUsesModelPendingStatuses(t *testing.T) {
|
|
def := pendingInvitationEmailIndexDefinition()
|
|
filter := def.PartialFilter.BuildQuery()
|
|
statuses := statusInValues(t, filter)
|
|
|
|
expected := model.PendingInvitationStatusValues()
|
|
if !reflect.DeepEqual(statuses, expected) {
|
|
t.Fatalf("index statuses drifted from model: got %#v want %#v", statuses, expected)
|
|
}
|
|
}
|
|
|
|
func statusInValues(t *testing.T, filter bson.D) []string {
|
|
t.Helper()
|
|
for _, item := range filter {
|
|
if item.Key != "status" {
|
|
continue
|
|
}
|
|
comparison, ok := item.Value.(bson.M)
|
|
if !ok {
|
|
t.Fatalf("status filter has unexpected type %T", item.Value)
|
|
}
|
|
rawValues, ok := comparison["$in"]
|
|
if !ok {
|
|
t.Fatal("status filter must use $in")
|
|
}
|
|
values, ok := rawValues.([]any)
|
|
if !ok {
|
|
t.Fatalf("status $in has unexpected type %T", rawValues)
|
|
}
|
|
statuses := make([]string, 0, len(values))
|
|
for _, value := range values {
|
|
status, ok := value.(string)
|
|
if !ok {
|
|
t.Fatalf("status value has unexpected type %T", value)
|
|
}
|
|
statuses = append(statuses, status)
|
|
}
|
|
return statuses
|
|
}
|
|
t.Fatal("status filter not found")
|
|
return nil
|
|
}
|