terraform-provider-stackitp.../stackit/internal/utils/attributes_test.go
Marcel S. Henselin 99f2853ae5
Feat: alpha sqlserver user (#12)
* fix: remove unused attribute types and functions from backup models

* fix: update API client references to use sqlserverflexalpha package

* fix: update package references to use sqlserverflexalpha and modify user data source model

* fix: add sqlserverflexalpha user data source to provider

* fix: add sqlserverflexalpha user resource and update related functionality

* chore: add stackit_sqlserverflexalpha_user resource and instance_id variable

* fix: refactor sqlserverflexalpha user resource and enhance schema with status and default_database

* chore: remove not needed service files

* chore: cleanup import in utils

* fix: provider name change

* fix: clone missing doc files

* fix: docs adjustments

* fix: docs CI scripts

* fix: docs naming

* fix: remove guides from docs

* fix: adjust examples

* fix: remove obsolete doc templates

* fix: docs adjustments

* fix: add missing doc file

* fix: temp rename file

---------

Co-authored-by: Andre Harms <andre.harms@stackit.cloud>
Co-authored-by: Marcel S. Henselin <marcel.henselin@stackit.cloud>
2025-12-19 15:15:42 +01:00

90 lines
2.2 KiB
Go

// Copyright (c) STACKIT
package utils
import (
"context"
"log"
"testing"
"time"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/types"
)
type attributeGetterFunc func(ctx context.Context, attributePath path.Path, target interface{}) diag.Diagnostics
func (a attributeGetterFunc) GetAttribute(ctx context.Context, attributePath path.Path, target interface{}) diag.Diagnostics {
return a(ctx, attributePath, target)
}
func mustLocation(name string) *time.Location {
loc, err := time.LoadLocation(name)
if err != nil {
log.Panicf("cannot load location %s: %v", name, err)
}
return loc
}
func TestGetTimeFromString(t *testing.T) {
type args struct {
path path.Path
source attributeGetterFunc
dateFormat string
}
tests := []struct {
name string
args args
wantErr bool
want time.Time
}{
{
name: "simple string",
args: args{
path: path.Root("foo"),
source: func(_ context.Context, _ path.Path, target interface{}) diag.Diagnostics {
t, ok := target.(*types.String)
if !ok {
log.Panicf("wrong type %T", target)
}
*t = types.StringValue("2025-02-06T09:41:00+01:00")
return nil
},
dateFormat: time.RFC3339,
},
want: time.Date(2025, 2, 6, 9, 41, 0, 0, mustLocation("Europe/Berlin")),
},
{
name: "invalid type",
args: args{
path: path.Root("foo"),
source: func(_ context.Context, p path.Path, _ interface{}) (diags diag.Diagnostics) {
diags.AddAttributeError(p, "kapow", "kapow")
return diags
},
dateFormat: time.RFC3339,
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var target time.Time
gotDiags := GetTimeFromStringAttribute(context.Background(), tt.args.path, tt.args.source, tt.args.dateFormat, &target)
if tt.wantErr {
if !gotDiags.HasError() {
t.Errorf("expected error")
}
} else {
if gotDiags.HasError() {
t.Errorf("expected no errors, but got %v", gotDiags)
} else {
if want, got := tt.want, target; !want.Equal(got) {
t.Errorf("got wrong date, want %s but got %s", want, got)
}
}
}
})
}
}