From c186e90b289b154e1bc6eb82888efb0c5910aaf5 Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Thu, 5 Sep 2024 11:18:11 +0200 Subject: [PATCH 1/2] Test icingadb/v1.Environment#NewContext() --- pkg/icingadb/v1/environment_test.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 pkg/icingadb/v1/environment_test.go diff --git a/pkg/icingadb/v1/environment_test.go b/pkg/icingadb/v1/environment_test.go new file mode 100644 index 000000000..107fae7c4 --- /dev/null +++ b/pkg/icingadb/v1/environment_test.go @@ -0,0 +1,19 @@ +package v1 + +import ( + "context" + "github.com/stretchr/testify/require" + "testing" + "time" +) + +func TestEnvironment_NewContext(t *testing.T) { + deadline := time.Now().Add(time.Minute) + parent, cancel := context.WithDeadline(context.Background(), deadline) + defer cancel() + + actual, ok := (*Environment)(nil).NewContext(parent).Deadline() + + require.True(t, ok) + require.Equal(t, deadline, actual) +} From 7393b3c679af4277d578dc3643bf2b263c7801e7 Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Thu, 5 Sep 2024 11:24:06 +0200 Subject: [PATCH 2/2] Test icingadb/v1.EnvironmentFromContext() --- pkg/icingadb/v1/environment_test.go | 41 +++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/pkg/icingadb/v1/environment_test.go b/pkg/icingadb/v1/environment_test.go index 107fae7c4..f7b8f9d5e 100644 --- a/pkg/icingadb/v1/environment_test.go +++ b/pkg/icingadb/v1/environment_test.go @@ -2,6 +2,7 @@ package v1 import ( "context" + "github.com/icinga/icinga-go-library/types" "github.com/stretchr/testify/require" "testing" "time" @@ -17,3 +18,43 @@ func TestEnvironment_NewContext(t *testing.T) { require.True(t, ok) require.Equal(t, deadline, actual) } + +func TestEnvironmentFromContext(t *testing.T) { + subtests := []struct { + name string + input context.Context + output *Environment + ok bool + }{ + { + name: "background", + input: context.Background(), + }, + { + name: "nil", + input: (*Environment)(nil).NewContext(context.Background()), + ok: true, + }, + { + name: "empty", + input: (&Environment{}).NewContext(context.Background()), + output: &Environment{}, + ok: true, + }, + { + name: "named", + input: (&Environment{Name: types.MakeString("foobar")}).NewContext(context.Background()), + output: &Environment{Name: types.MakeString("foobar")}, + ok: true, + }, + } + + for _, st := range subtests { + t.Run(st.name, func(t *testing.T) { + actual, ok := EnvironmentFromContext(st.input) + + require.Equal(t, st.output, actual) + require.Equal(t, st.ok, ok) + }) + } +}