Spaces:
Paused
Paused
File size: 1,632 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 | package entities
import (
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
func TestContact_BSONFieldNames(t *testing.T) {
contactType := reflect.TypeOf(Contact{})
expected := map[string]string{
"ID": "_id",
"UserID": "user_id",
"Name": "name",
"Emails": "emails",
"PhoneNumbers": "phone_numbers",
"Properties": "properties",
"CreatedAt": "created_at",
"UpdatedAt": "updated_at",
}
for fieldName, bsonName := range expected {
field, found := contactType.FieldByName(fieldName)
assert.True(t, found)
assert.Equal(t, bsonName, field.Tag.Get("bson"))
}
}
func TestContactProperties_ValueScanRoundTrip(t *testing.T) {
cases := []ContactProperties{
nil,
{},
{"company": "Acme", "role": "CTO"},
}
for _, original := range cases {
value, err := original.Value()
assert.Nil(t, err)
var scanned ContactProperties
assert.Nil(t, scanned.Scan(value))
if len(original) == 0 {
assert.Equal(t, 0, len(scanned))
continue
}
assert.Equal(t, original, scanned)
}
}
func TestContactProperties_ScanFromString(t *testing.T) {
var scanned ContactProperties
assert.Nil(t, scanned.Scan(`{"k":"v"}`))
assert.Equal(t, ContactProperties{"k": "v"}, scanned)
}
func TestContactProperties_ScanNil(t *testing.T) {
var scanned ContactProperties
assert.Nil(t, scanned.Scan(nil))
assert.Equal(t, 0, len(scanned))
}
func TestContactProperties_ScanUnsupportedType(t *testing.T) {
var scanned ContactProperties
err := scanned.Scan(123)
assert.Error(t, err)
assert.Contains(t, err.Error(), "unsupported type [int] for ContactProperties")
}
|