package leaf import ( "encoding/json" "testing" "time" "github.com/stretchr/testify/assert" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" ) func TestBaseInitialization(t *testing.T) { id := primitive.NewObjectID() now := time.Now() base := Base{} base.SetID(id) base.SetCreatedAt(now) base.SetUpdatedAt(now) assert.Equal(t, id, base.ID()) assert.Equal(t, id, base.ObjectID) assert.True(t, base.CreatedAt.Equal(now)) assert.True(t, base.UpdatedAt.Equal(now)) } func TestBaseJSONMarshalling(t *testing.T) { base := Base{} base.SetID(primitive.NewObjectID()) base.SetCreatedAt(time.Now()) base.SetUpdatedAt(time.Now()) data, err := json.Marshal(base) assert.NoError(t, err) assert.True(t, json.Valid(data)) } func TestBaseBSONMarshalling(t *testing.T) { base := Base{} base.SetID(primitive.NewObjectID()) base.SetCreatedAt(time.Now()) base.SetUpdatedAt(time.Now()) data, err := bson.Marshal(base) assert.NoError(t, err) var m bson.M err = bson.Unmarshal(data, &m) assert.NoError(t, err) }