Skip to content

Commit

Permalink
made boolean omitempty values references
Browse files Browse the repository at this point in the history
  • Loading branch information
amaor-newrelic committed Nov 8, 2022
1 parent 653acd3 commit ade6c5f
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 6 deletions.
3 changes: 2 additions & 1 deletion pkg/notifications/channels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ func TestUpdateChannel(t *testing.T) {
t.Parallel()
respJSON := fmt.Sprintf(`{ "data":%s }`, testUpdateChannelResponseJSON)
notifications := newMockResponse(t, respJSON, http.StatusCreated)
var falseValue = false

updateChannelInput := AiNotificationsChannelUpdate{
Name: "test-notification-channel-1-update",
Expand All @@ -239,7 +240,7 @@ func TestUpdateChannel(t *testing.T) {
Value: "{\\n\\t\\\"id\\\": \\\"test-update\\\"\\n}",
},
},
Active: false,
Active: &falseValue,
}

expected := &AiNotificationsChannelResponse{
Expand Down
10 changes: 5 additions & 5 deletions pkg/notifications/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ type AiNotificationsChannel struct {
// AiNotificationsChannelFilter - Filter channel object
type AiNotificationsChannelFilter struct {
// active
Active bool `json:"active,omitempty"`
Active *bool `json:"active,omitempty"`
// destinationId
DestinationId string `json:"destinationId,omitempty"`
// id
Expand Down Expand Up @@ -639,7 +639,7 @@ type AiNotificationsChannelSorter struct {
// AiNotificationsChannelUpdate - Channel update object
type AiNotificationsChannelUpdate struct {
// active
Active bool `json:"active,omitempty"`
Active *bool `json:"active,omitempty"`
// name
Name string `json:"name,omitempty"`
// properties
Expand Down Expand Up @@ -715,7 +715,7 @@ type AiNotificationsDestination struct {
// AiNotificationsDestinationFilter - Filter destination object
type AiNotificationsDestinationFilter struct {
// active
Active bool `json:"active,omitempty"`
Active *bool `json:"active,omitempty"`
// authType
AuthType AiNotificationsAuthType `json:"authType,omitempty"`
// id
Expand Down Expand Up @@ -763,11 +763,11 @@ type AiNotificationsDestinationSorter struct {
// AiNotificationsDestinationUpdate - Destination update object
type AiNotificationsDestinationUpdate struct {
// active
Active bool `json:"active,omitempty"`
Active *bool `json:"active,omitempty"`
// auth
Auth *AiNotificationsCredentialsInput `json:"auth,omitempty"`
// disableAuth
DisableAuth bool `json:"disableAuth,omitempty"`
DisableAuth *bool `json:"disableAuth,omitempty"`
// name
Name string `json:"name,omitempty"`
// properties
Expand Down
136 changes: 136 additions & 0 deletions pkg/notifications/types_unit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
//go:build unit
// +build unit

package notifications

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
)


// Verify that boolean values with "empty" values (== false) are not ignored by json marshaller
// If `false` is not included into input, it would be impossible to change any boolean flag to `false` in an update
func TestAiNotificationsChannelFilter_OptionalBooleans_JsonFormat(t *testing.T) {
t.Parallel()
var falseValue = false
var input = AiNotificationsChannelFilter{
Name: "test-notification-channel-1-update",
Active: &falseValue,
}
var serialized, err = json.Marshal(input)

assert.NoError(t, err)
assert.Equal(
t,
"{\"active\":false,\"name\":\"test-notification-channel-1-update\",\"property\":{\"key\":\"\",\"value\":\"\"}}",
string(serialized),
)
}

// Verify that an empty update input is serialized into an empty json
func TestAiNotificationsChannelFilter_EmptyInput_JsonFormat(t *testing.T) {
t.Parallel()
var input = AiNotificationsChannelFilter{
}

var serialized, err = json.Marshal(input)

assert.NoError(t, err)
assert.Equal(t, "{\"property\":{\"key\":\"\",\"value\":\"\"}}", string(serialized))
}

// Verify that boolean values with "empty" values (== false) are not ignored by json marshaller
// If `false` is not included into input, it would be impossible to change any boolean flag to `false` in an update
func TestAiNotificationsChannelUpdate_OptionalBooleans_JsonFormat(t *testing.T) {
t.Parallel()
var falseValue = false
var input = AiNotificationsChannelUpdate{
Name: "test-notification-channel-1-update",
Properties: []AiNotificationsPropertyInput{},
Active: &falseValue,
}
var serialized, err = json.Marshal(input)

assert.NoError(t, err)
assert.Equal(
t,
"{\"active\":false,\"name\":\"test-notification-channel-1-update\"}",
string(serialized),
)
}

// Verify that an empty update input is serialized into an empty json
func TestAiNotificationsChannelUpdate_EmptyInput_JsonFormat(t *testing.T) {
t.Parallel()
var input = AiNotificationsChannelUpdate{}

var serialized, err = json.Marshal(input)

assert.NoError(t, err)
assert.Equal(t, "{}", string(serialized))
}

// Verify that boolean values with "empty" values (== false) are not ignored by json marshaller
// If `false` is not included into input, it would be impossible to change any boolean flag to `false` in an update
func TestAiNotificationsDestinationFilter_OptionalBooleans_JsonFormat(t *testing.T) {
t.Parallel()
var falseValue = false
var input = AiNotificationsDestinationFilter{
Name: "test-notification-channel-1-update",
Active: &falseValue,
}
var serialized, err = json.Marshal(input)

assert.NoError(t, err)
assert.Equal(
t,
"{\"active\":false,\"name\":\"test-notification-channel-1-update\",\"property\":{\"key\":\"\",\"value\":\"\"}}",
string(serialized),
)
}

// Verify that an empty update input is serialized into an empty json
func TestAiNotificationsDestinationFilter_EmptyInput_JsonFormat(t *testing.T) {
t.Parallel()
var input = AiNotificationsDestinationFilter{}

var serialized, err = json.Marshal(input)

assert.NoError(t, err)
assert.Equal(t, "{\"property\":{\"key\":\"\",\"value\":\"\"}}", string(serialized))
}

// Verify that boolean values with "empty" values (== false) are not ignored by json marshaller
// If `false` is not included into input, it would be impossible to change any boolean flag to `false` in an update
func TestAiNotificationsDestinationUpdate_OptionalBooleans_JsonFormat(t *testing.T) {
t.Parallel()
var falseValue = false
var input = AiNotificationsDestinationUpdate{
Name: "test-notification-channel-1-update",
Properties: []AiNotificationsPropertyInput{},
Active: &falseValue,
DisableAuth: &falseValue,
}
var serialized, err = json.Marshal(input)

assert.NoError(t, err)
assert.Equal(
t,
"{\"active\":false,\"disableAuth\":false,\"name\":\"test-notification-channel-1-update\"}",
string(serialized),
)
}

// Verify that an empty update input is serialized into an empty json
func TestAiNotificationsDestinationUpdate_EmptyInput_JsonFormat(t *testing.T) {
t.Parallel()
var input = AiNotificationsDestinationUpdate{}

var serialized, err = json.Marshal(input)

assert.NoError(t, err)
assert.Equal(t, "{}", string(serialized))
}

0 comments on commit ade6c5f

Please sign in to comment.