Skip to content

Commit c8986c0

Browse files
yasirfolio3msohailhussainmjc1283
authored andcommitted
refact(snapshot): Made decisions optional for snapshot entity. (#226)
* fixed decisions in snapshot to be optional. * Nit fixed. * Unit tests updated Co-authored-by: msohailhussain <[email protected]> Co-authored-by: Matt Carroll <[email protected]>
1 parent d19671f commit c8986c0

File tree

2 files changed

+88
-2
lines changed

2 files changed

+88
-2
lines changed

pkg/event/events.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/****************************************************************************
2-
* Copyright 2019, Optimizely, Inc. and contributors *
2+
* Copyright 2020, Optimizely, Inc. and contributors *
33
* *
44
* Licensed under the Apache License, Version 2.0 (the "License"); *
55
* you may not use this file except in compliance with the License. *
@@ -95,7 +95,7 @@ type VisitorAttribute struct {
9595

9696
// Snapshot represents a snapshot of a visitor
9797
type Snapshot struct {
98-
Decisions []Decision `json:"decisions"`
98+
Decisions []Decision `json:"decisions,omitempty"`
9999
Events []SnapshotEvent `json:"events"`
100100
}
101101

pkg/event/events_test.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/****************************************************************************
2+
* Copyright 2020, Optimizely, Inc. and contributors *
3+
* *
4+
* Licensed under the Apache License, Version 2.0 (the "License"); *
5+
* you may not use this file except in compliance with the License. *
6+
* You may obtain a copy of the License at *
7+
* *
8+
* http://www.apache.org/licenses/LICENSE-2.0 *
9+
* *
10+
* Unless required by applicable law or agreed to in writing, software *
11+
* distributed under the License is distributed on an "AS IS" BASIS, *
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13+
* See the License for the specific language governing permissions and *
14+
* limitations under the License. *
15+
***************************************************************************/
16+
17+
// Package event //
18+
package event
19+
20+
import (
21+
"encoding/json"
22+
"github.com/stretchr/testify/assert"
23+
"testing"
24+
)
25+
26+
func TestSnapshotHasOptionalDecisions(t *testing.T) {
27+
snapshot := Snapshot{
28+
Decisions: []Decision{
29+
Decision{
30+
VariationID: "1",
31+
},
32+
},
33+
}
34+
35+
// Check with decisions
36+
jsonValue, err := json.Marshal(snapshot)
37+
assert.Nil(t, err)
38+
39+
dict := map[string]interface{}{}
40+
err = json.Unmarshal(jsonValue, &dict)
41+
assert.Nil(t, err)
42+
_, ok := dict["decisions"]
43+
assert.True(t, ok)
44+
45+
// Check without decisions
46+
snapshot.Decisions = nil
47+
jsonValue, err = json.Marshal(snapshot)
48+
assert.Nil(t, err)
49+
50+
dict2 := map[string]interface{}{}
51+
err = json.Unmarshal(jsonValue, &dict2)
52+
assert.Nil(t, err)
53+
_, ok = dict2["decisions"]
54+
assert.False(t, ok)
55+
}
56+
57+
func TestSnapshotHasNonOptionalEvents(t *testing.T) {
58+
snapshot := Snapshot{
59+
Events: []SnapshotEvent{
60+
SnapshotEvent{
61+
EntityID: "1",
62+
},
63+
},
64+
}
65+
66+
// Check with events
67+
jsonValue, err := json.Marshal(snapshot)
68+
assert.Nil(t, err)
69+
70+
dict := map[string]interface{}{}
71+
err = json.Unmarshal(jsonValue, &dict)
72+
assert.Nil(t, err)
73+
_, ok := dict["events"]
74+
assert.True(t, ok)
75+
76+
// Check without events
77+
snapshot.Events = nil
78+
jsonValue, err = json.Marshal(snapshot)
79+
assert.Nil(t, err)
80+
81+
dict2 := map[string]interface{}{}
82+
err = json.Unmarshal(jsonValue, &dict2)
83+
assert.Nil(t, err)
84+
_, ok = dict2["events"]
85+
assert.True(t, ok)
86+
}

0 commit comments

Comments
 (0)