diff --git a/pkg/event/events.go b/pkg/event/events.go index d68fb3e2a..c79fa95c3 100644 --- a/pkg/event/events.go +++ b/pkg/event/events.go @@ -1,5 +1,5 @@ /**************************************************************************** - * Copyright 2019, Optimizely, Inc. and contributors * + * Copyright 2020, Optimizely, Inc. and contributors * * * * Licensed under the Apache License, Version 2.0 (the "License"); * * you may not use this file except in compliance with the License. * @@ -95,7 +95,7 @@ type VisitorAttribute struct { // Snapshot represents a snapshot of a visitor type Snapshot struct { - Decisions []Decision `json:"decisions"` + Decisions []Decision `json:"decisions,omitempty"` Events []SnapshotEvent `json:"events"` } diff --git a/pkg/event/events_test.go b/pkg/event/events_test.go new file mode 100644 index 000000000..1a4eabaa4 --- /dev/null +++ b/pkg/event/events_test.go @@ -0,0 +1,86 @@ +/**************************************************************************** + * Copyright 2020, Optimizely, Inc. and contributors * + * * + * Licensed under the Apache License, Version 2.0 (the "License"); * + * you may not use this file except in compliance with the License. * + * You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, software * + * distributed under the License is distributed on an "AS IS" BASIS, * + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * + * See the License for the specific language governing permissions and * + * limitations under the License. * + ***************************************************************************/ + +// Package event // +package event + +import ( + "encoding/json" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestSnapshotHasOptionalDecisions(t *testing.T) { + snapshot := Snapshot{ + Decisions: []Decision{ + Decision{ + VariationID: "1", + }, + }, + } + + // Check with decisions + jsonValue, err := json.Marshal(snapshot) + assert.Nil(t, err) + + dict := map[string]interface{}{} + err = json.Unmarshal(jsonValue, &dict) + assert.Nil(t, err) + _, ok := dict["decisions"] + assert.True(t, ok) + + // Check without decisions + snapshot.Decisions = nil + jsonValue, err = json.Marshal(snapshot) + assert.Nil(t, err) + + dict2 := map[string]interface{}{} + err = json.Unmarshal(jsonValue, &dict2) + assert.Nil(t, err) + _, ok = dict2["decisions"] + assert.False(t, ok) +} + +func TestSnapshotHasNonOptionalEvents(t *testing.T) { + snapshot := Snapshot{ + Events: []SnapshotEvent{ + SnapshotEvent{ + EntityID: "1", + }, + }, + } + + // Check with events + jsonValue, err := json.Marshal(snapshot) + assert.Nil(t, err) + + dict := map[string]interface{}{} + err = json.Unmarshal(jsonValue, &dict) + assert.Nil(t, err) + _, ok := dict["events"] + assert.True(t, ok) + + // Check without events + snapshot.Events = nil + jsonValue, err = json.Marshal(snapshot) + assert.Nil(t, err) + + dict2 := map[string]interface{}{} + err = json.Unmarshal(jsonValue, &dict2) + assert.Nil(t, err) + _, ok = dict2["events"] + assert.True(t, ok) +}