Skip to content

Commit

Permalink
Merge pull request #806 from Icinga/TestEnvironment
Browse files Browse the repository at this point in the history
Test Environment
  • Loading branch information
julianbrost committed Sep 9, 2024
2 parents 590b754 + 7393b3c commit fb4c64f
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions pkg/icingadb/v1/environment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package v1

import (
"context"
"github.com/icinga/icinga-go-library/types"
"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)
}

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

0 comments on commit fb4c64f

Please sign in to comment.