pulumi/sdk/go/internal/gen-pux-applyn/tmpl/applyn_test.go.tmpl

150 lines
4.0 KiB
Cheetah

// Copyright 2016-2023, Pulumi Corporation.
//
// 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.
// Code generated by gen-pux-applyn; DO NOT EDIT.
//nolint:lll
package pulumix_test
import (
"context"
"errors"
"reflect"
"strconv"
"testing"
"time"
"github.com/pulumi/pulumi/sdk/v3/go/internal"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
"github.com/pulumi/pulumi/sdk/v3/go/pulumix"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
{{ range seq .Start .Stop -}}
{{ template "applyn_test" . }}
{{- end }}
{{/* applyn_test(int):
Generates tests for ApplyN functions with N parameters.
*/}}
{{ define "applyn_test" }}
{{ $name := printf "Apply%d" . -}}
{{ if eq . 1 -}}
{{ $name = "Apply" -}}
{{ end -}}
func Test{{ $name }}_simpleSuccess(t *testing.T) {
t.Parallel()
out := pulumix.{{ $name }}[{{ range seq 1 . }}int,{{ end }}](
{{ range seq 1 . -}}
pulumix.Val[int]({{ . }}),
{{ end -}}
func({{ range seq 1 . }}i{{ . }} int,{{ end }}) []string {
return []string{
{{ range seq 1 . -}}
strconv.Itoa(i{{ . }}),
{{ end }}
}
},
)
val, known, secret, deps, err := internal.AwaitOutput(context.Background(), out)
require.NoError(t, err)
assert.True(t, known)
assert.False(t, secret)
assert.Empty(t, deps)
assert.Equal(t, []string{ {{- range seq 1 . -}}"{{ . }}",{{ end -}} }, val)
}
{{/* A single secret value turns the whole output into a secret. */}}
func Test{{ $name }}_secretValue(t *testing.T) {
t.Parallel()
out := pulumix.{{ $name }}[{{ range seq 1 . }}int,{{ end }}](
{{ range seq 1 . -}}
{{ if eq . $ -}}
pulumix.Output[int]{
OutputState: internal.GetOutputState(pulumi.ToSecret({{ . }})),
},
{{ else -}}
pulumix.Val[int]({{ . }}),
{{ end -}}
{{ end -}}
func({{ range seq 1 . }}i{{ . }} int,{{ end }}) []string {
return []string{
{{ range seq 1 . -}}
strconv.Itoa(i{{ . }}),
{{ end }}
}
},
)
_, _, secret, _, err := internal.AwaitOutput(context.Background(), out)
require.NoError(t, err)
assert.True(t, secret)
}
func Test{{ $name }}Err_applyError(t *testing.T) {
t.Parallel()
giveErr := errors.New("great sadness")
out := pulumix.{{ $name }}Err[{{ range seq 1 . }}int,{{ end }}](
{{ range seq 1 . -}}
pulumix.Val[int]({{ . }}),
{{ end -}}
func({{ range seq 1 . }}int,{{ end }}) (string, error) {
return "", giveErr
},
)
_, _, _, _, err := internal.AwaitOutput(context.Background(), out)
assert.ErrorIs(t, err, giveErr)
}
{{/* If an output fails, none of the consecutive outputs are waited on.
and the function is not called. */}}
func Test{{ $name }}_failedOutput(t *testing.T) {
t.Parallel()
intType := reflect.TypeOf(0)
{{ range seq 1 . -}}
o{{ . }} := pulumix.Output[int]{OutputState: internal.NewOutputState(nil, intType)}
{{ end }}
{{ if gt . 1 }}// Reject the first output. Don't fill the others.{{ end }}
giveErr := errors.New("great sadness")
internal.RejectOutput(o1, giveErr)
out := pulumix.{{ $name }}[{{ range seq 1 . }}int,{{ end }}](
{{ range seq 1 . -}}o{{ . }},{{ end }}
func({{ range seq 1 . }}int,{{ end }}) string {
t.Errorf("applied function must not be called")
return ""
},
)
{{/* Because the outputs past the first are not filled,
awaiting on them will hang forever if the test fails.
Prevent that with a timeout. */}}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
_, _, _, _, err := internal.AwaitOutput(ctx, out)
assert.ErrorIs(t, err, giveErr)
}
{{ end }}