-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #806 from Icinga/TestEnvironment
Test Environment
- Loading branch information
Showing
1 changed file
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |