|
| 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