Skip to content

Commit

Permalink
test(test): add missing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephKav committed Apr 5, 2024
1 parent 735d0dd commit 97f907e
Show file tree
Hide file tree
Showing 6 changed files with 452 additions and 19 deletions.
13 changes: 5 additions & 8 deletions notifiers/shoutrrr/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type TestPayload struct {
ServiceName string `json:"service_name"`
Name string `json:"name"`
NamePrevious string `json:"name_previous"`
Type *string `json:"type,omitempty"`
Type string `json:"type,omitempty"`
Options map[string]string `json:"options"`
URLFields map[string]string `json:"url_fields"`
Params map[string]string `json:"params"`
Expand All @@ -36,7 +36,7 @@ type TestPayload struct {
}

// FromPayload will create a Shoutrrr from a payload.
// Copying any undefined values from the previous Service Notify.
// Replacing any undefined values with that of the previous Notify.
func FromPayload(
payload *TestPayload,
serviceNotifies *Slice,
Expand All @@ -51,21 +51,18 @@ func FromPayload(
}

name := util.FirstNonDefault(payload.Name, payload.NamePrevious)
nType := util.DefaultIfNil(payload.Type)

// Original Notifier?
original := &Shoutrrr{}
if serviceNotifies != nil && (*serviceNotifies)[payload.NamePrevious] != nil {
original = (*serviceNotifies)[payload.NamePrevious]
// Copy that previous Notify Type
if payload.Type == nil {
nType = (*serviceNotifies)[payload.NamePrevious].Type
}
// Copy that previous Notify Type if not set
payload.Type = util.FirstNonDefault(payload.Type, (*serviceNotifies)[payload.NamePrevious].Type)
}

// Get the Type, Main, Defaults, and HardDefaults for this Notify
nType, main, dfault, hardDefault, err := sortDefaults(
name, nType,
name, payload.Type,
mains[name], defaults, hardDefaults)
if err != nil {
return
Expand Down
12 changes: 6 additions & 6 deletions notifiers/shoutrrr/new_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func TestShoutrrr_FromPayload(t *testing.T) {
payload: TestPayload{
ServiceName: "test",
Name: "no_main_no_type",
Type: &typeWithNoDefaults,
Type: typeWithNoDefaults,
URLFields: typeWithNoDefaultsURLFields},
want: &Shoutrrr{
ShoutrrrBase: ShoutrrrBase{
Expand Down Expand Up @@ -144,7 +144,7 @@ func TestShoutrrr_FromPayload(t *testing.T) {
payload: TestPayload{
ServiceName: "test",
Name: "no_main_with_type_and_defaults",
Type: &typeWithDefaults,
Type: typeWithDefaults,
URLFields: typeWithDefaultsURLFields},
want: &Shoutrrr{
ShoutrrrBase: ShoutrrrBase{
Expand All @@ -155,7 +155,7 @@ func TestShoutrrr_FromPayload(t *testing.T) {
payload: TestPayload{
ServiceName: "test",
Name: "main_no_type",
Type: &typeWithNoDefaults},
Type: typeWithNoDefaults},
want: &Shoutrrr{
ShoutrrrBase: ShoutrrrBase{
Type: typeWithNoDefaults}},
Expand All @@ -170,7 +170,7 @@ func TestShoutrrr_FromPayload(t *testing.T) {
payload: TestPayload{
ServiceName: "test",
Name: "main_with_type_and_defaults",
Type: &typeWithDefaults},
Type: typeWithDefaults},
want: &Shoutrrr{
ShoutrrrBase: ShoutrrrBase{
Type: typeWithDefaults}},
Expand All @@ -179,7 +179,7 @@ func TestShoutrrr_FromPayload(t *testing.T) {
payload: TestPayload{
ServiceName: "test",
Name: "main_with_type_and_defaults",
Type: &typeWithNoDefaults},
Type: typeWithNoDefaults},
err: `type: "[^"]+" != "[^"]+"`,
},
"edit, have Main, have Defaults - Fail, Invalid field": {
Expand Down Expand Up @@ -222,7 +222,7 @@ func TestShoutrrr_FromPayload(t *testing.T) {
payload: TestPayload{
ServiceName: "test",
Name: "main_not_on_service_with_defaults",
Type: &typeWithNoDefaults},
Type: typeWithNoDefaults},
err: `type: "[^"]+" != "[^"]+"`,
},
}
Expand Down
270 changes: 270 additions & 0 deletions test/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,270 @@
// Copyright [2024] [Argus]
//
// 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.

//go:build unit

package test

import "testing"

func TestBoolPtr(t *testing.T) {
// GIVEN a boolean value
tests := map[string]struct {
val bool
}{
"true": {val: true},
"false": {val: false},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
t.Parallel()

// WHEN BoolPtr is called
result := BoolPtr(tc.val)

// THEN the result should be a pointer to the boolean value
if *result != tc.val {
t.Errorf("expected %t but got %t",
tc.val, *result)
}
})
}
}

func TestIntPtr(t *testing.T) {
// GIVEN an integer value
tests := map[string]struct {
val int
}{
"positive": {val: 1},
"zero": {val: 0},
"negative": {val: -1},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
t.Parallel()

// WHEN IntPtr is called
result := IntPtr(tc.val)

// THEN the result should be a pointer to the integer value
if *result != tc.val {
t.Errorf("expected %d but got %d",
tc.val, *result)
}
})
}
}

func TestStringPtr(t *testing.T) {
// GIVEN a string value
tests := map[string]struct {
val string
}{
"empty": {val: ""},
"non-empty": {val: "hello"},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
t.Parallel()

// WHEN StringPtr is called
result := StringPtr(tc.val)

// THEN the result should be a pointer to the string value
if *result != tc.val {
t.Errorf("expected %q but got %q",
tc.val, *result)
}
})
}
}

func TestUIntPtr(t *testing.T) {
// GIVEN an integer value
tests := map[string]struct {
val uint
}{
"positive": {val: 1},
"zero": {val: 0},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
t.Parallel()

// WHEN UIntPtr is called
result := UIntPtr(int(tc.val))

// THEN the result should be a pointer to the unsigned integer value
if *result != uint(tc.val) {
t.Errorf("expected %d but got %d",
tc.val, *result)
}
})
}
}

func TestStringifyPtr(t *testing.T) {
// GIVEN a pointer to a value
tests := map[string]struct {
ptr interface{}
want string
}{
"nil": {ptr: nil, want: "nil"},
"int, positive": {ptr: IntPtr(1), want: "1"},
"int, negative": {ptr: IntPtr(-1), want: "-1"},
"string": {ptr: StringPtr("hello"), want: "hello"},
"uint": {ptr: UIntPtr(1), want: "1"},
"bool": {ptr: BoolPtr(true), want: "true"},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
t.Parallel()

// WHEN StringifyPtr is called
var result string
switch v := tc.ptr.(type) {
case *bool:
result = StringifyPtr(v)
case *int:
result = StringifyPtr(v)
case *string:
result = StringifyPtr(v)
case *uint:
result = StringifyPtr(v)
case nil:
var nilPtr *int
result = StringifyPtr(nilPtr)
default:
t.Fatalf("unexpected type %T",
tc.ptr)
}

// THEN the result should be a string representation of the value
if result != tc.want {
t.Errorf("expected %q but got %q",
tc.want, result)
}
})
}
}

func TestCopyMapPtr(t *testing.T) {
// GIVEN a map
tests := map[string]struct {
tgt map[string]string
want map[string]string
}{
"nil": {
tgt: nil,
want: nil,
},
"empty": {
tgt: map[string]string{},
want: map[string]string{},
},
"non-empty": {
tgt: map[string]string{
"key": "value",
},
want: map[string]string{
"key": "value",
},
},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
t.Parallel()

// WHEN CopyMapPtr is called
result := CopyMapPtr(tc.tgt)

// THEN the result should be a pointer to a copy of the map
if len(*result) != len(tc.want) {
t.Errorf("length differs, expected %d but got %d",
len(tc.want), len(*result))
}
for k, v := range tc.want {
if (*result)[k] != v {
t.Errorf("%q: expected %q but got %q",
k, v, (*result)[k])
}
}
})
}
}

func TestTrimJSON(t *testing.T) {
// GIVEN a JSON string
tests := map[string]struct {
str string
want string
}{
"empty": {
str: "",
want: "",
},
"single line": {
str: `{"key": "value"}`,
want: `{"key":"value"}`,
},
"multi line": {
str: `
{
"key": "value"
}`,
want: `{"key":"value"}`,
},
"with tabs": {
str: `{
"key": "value"
}`,
want: `{"key":"value"}`,
},
"with spaces": {
str: `{
"key": "value"
}`,
want: `{"key":"value"}`,
},
"mixed": {
str: `{
"key": "value",
"key2": "value2"
}`,
want: `{"key":"value","key2":"value2"}`,
},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
t.Parallel()

// WHEN TrimJSON is called
result := TrimJSON(tc.str)

// THEN the result should be the JSON string without newlines and tabs
if result != tc.want {
t.Errorf("expected %q but got %q",
tc.want, result)
}
})
}
}
4 changes: 2 additions & 2 deletions test/shoutrrr.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func testShoutrrrrGotifyToken() (token string) {
return
}

func TestShoutrrrDefaults(failing bool, selfSignedCert bool) *shoutrrr.ShoutrrrDefaults {
func ShoutrrrDefaults(failing bool, selfSignedCert bool) *shoutrrr.ShoutrrrDefaults {
url := "valid.release-argus.io"
if selfSignedCert {
url = strings.Replace(url, "valid", "invalid", 1)
Expand All @@ -53,7 +53,7 @@ func TestShoutrrrDefaults(failing bool, selfSignedCert bool) *shoutrrr.ShoutrrrD
return shoutrrr
}

func TestShoutrrr(failing bool, selfSignedCert bool) *shoutrrr.Shoutrrr {
func Shoutrrr(failing bool, selfSignedCert bool) *shoutrrr.Shoutrrr {
url := "valid.release-argus.io"
if selfSignedCert {
url = strings.Replace(url, "valid", "invalid", 1)
Expand Down
Loading

0 comments on commit 97f907e

Please sign in to comment.