From 67f5d13abe2069cbd1ad77d43509cbc4c167ddd5 Mon Sep 17 00:00:00 2001 From: Yasir Ali Date: Wed, 1 Jan 2020 16:02:33 +0500 Subject: [PATCH 1/3] fixed decisions in snapshot to be optional. --- pkg/event/events.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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"` } From 0163150ce1362f433cfc3963cdeb0db2479442aa Mon Sep 17 00:00:00 2001 From: Yasir Ali Date: Wed, 8 Jan 2020 14:07:36 +0500 Subject: [PATCH 2/3] Nit fixed. --- pkg/event/events_test.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/pkg/event/events_test.go b/pkg/event/events_test.go index 781443b99..9b80a440c 100644 --- a/pkg/event/events_test.go +++ b/pkg/event/events_test.go @@ -23,7 +23,7 @@ import ( "testing" ) -func TestSnapshotHasOptionalDecisions(t *testing.T) { +func TestSnapshotHasOptionalDecisionsAndNonOptionalEvents(t *testing.T) { snapshot := Snapshot{ Decisions: []Decision{ Decision{ @@ -37,7 +37,7 @@ func TestSnapshotHasOptionalDecisions(t *testing.T) { }, } - // Check with decisions + // Check with decisions and events jsonValue, err := json.Marshal(snapshot) assert.Nil(t, err) @@ -46,9 +46,12 @@ func TestSnapshotHasOptionalDecisions(t *testing.T) { assert.Nil(t, err) _, ok := dict["decisions"] assert.True(t, ok) + _, ok = dict["events"] + assert.True(t, ok) - // Check without decisions + // Check without decisions and events snapshot.Decisions = nil + snapshot.Events = nil jsonValue, err = json.Marshal(snapshot) assert.Nil(t, err) @@ -57,4 +60,6 @@ func TestSnapshotHasOptionalDecisions(t *testing.T) { assert.Nil(t, err) _, ok = dict2["decisions"] assert.False(t, ok) + _, ok = dict2["events"] + assert.True(t, ok) } From 8f15b99a25f829092365d45a670dd733876b0072 Mon Sep 17 00:00:00 2001 From: Yasir Ali Date: Thu, 9 Jan 2020 11:09:17 +0500 Subject: [PATCH 3/3] Unit tests updated --- pkg/event/events_test.go | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/pkg/event/events_test.go b/pkg/event/events_test.go index 9b80a440c..1a4eabaa4 100644 --- a/pkg/event/events_test.go +++ b/pkg/event/events_test.go @@ -23,13 +23,39 @@ import ( "testing" ) -func TestSnapshotHasOptionalDecisionsAndNonOptionalEvents(t *testing.T) { +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", @@ -37,20 +63,17 @@ func TestSnapshotHasOptionalDecisionsAndNonOptionalEvents(t *testing.T) { }, } - // Check with decisions and events + // 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["decisions"] - assert.True(t, ok) - _, ok = dict["events"] + _, ok := dict["events"] assert.True(t, ok) - // Check without decisions and events - snapshot.Decisions = nil + // Check without events snapshot.Events = nil jsonValue, err = json.Marshal(snapshot) assert.Nil(t, err) @@ -58,8 +81,6 @@ func TestSnapshotHasOptionalDecisionsAndNonOptionalEvents(t *testing.T) { dict2 := map[string]interface{}{} err = json.Unmarshal(jsonValue, &dict2) assert.Nil(t, err) - _, ok = dict2["decisions"] - assert.False(t, ok) _, ok = dict2["events"] assert.True(t, ok) }