eia-api-go/pkg/eia/eia_reflection_test.go
Ryan McGuire 7431eb94f1
All checks were successful
Build and Publish / build (push) Successful in 46s
Build and Publish / publish (push) Successful in 5s
Move to public repo
2024-12-17 16:02:58 -05:00

279 lines
6.1 KiB
Go

package eia
import (
"context"
"reflect"
"testing"
"k8s.io/utils/ptr"
eiaapi "gitea.libretechconsulting.com/rmcguire/eia-api-go/api"
)
func TestGetRoutes(t *testing.T) {
type args struct {
suffixes []string
}
tests := []struct {
name string
args args
want []string
}{
{
name: "List known routes",
args: args{
suffixes: []string{
"Aeo",
"Electricity",
"Gas",
},
},
want: []string{
"GetV2Aeo",
"GetV2Electricity",
"GetV2NaturalGas",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := GetRoutes(tt.args.suffixes...); !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetRoutes() = %v, want %v", got, tt.want)
}
})
}
}
func Test_prepMethodArgs(t *testing.T) {
client, _ := eiaapi.NewClient("test.test")
type args struct {
method reflect.Value
name string
subs *MethodSubs
}
tests := []struct {
name string
args args
want []reflect.Value
}{
{
name: "Test substitute GetV2AeoRoute1FacetFacetId params",
args: args{
name: "GetV2AeoRoute1FacetFacetId",
method: reflect.ValueOf(
client.GetV2AeoRoute1FacetFacetId,
),
subs: &MethodSubs{
TypedParams: map[reflect.Type]reflect.Value{
reflect.TypeOf((*context.Context)(nil)).Elem(): reflect.ValueOf(context.TODO()),
},
StrTypedParams: map[string]reflect.Value{
"FacetId": reflect.ValueOf(eiaapi.FacetId("seriesId")),
"Route1": reflect.ValueOf(eiaapi.Route1("2023")),
},
},
},
want: []reflect.Value{
reflect.ValueOf(context.TODO()),
reflect.ValueOf(eiaapi.Route1("2023")),
reflect.ValueOf(eiaapi.FacetId("seriesId")),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := prepMethodArgs(tt.args.method, tt.args.name, tt.args.subs); !reflect.DeepEqual(got, tt.want) {
t.Errorf("prepMethodArgs() = %v, want %v", got, tt.want)
}
})
}
}
func Test_getFRR(t *testing.T) {
sampleFinalRouteResponse := eiaapi.FinalRouteResponse{
Request: &eiaapi.RouteRequest{},
Response: &eiaapi.FinalRoute{
Description: ptr.To("Annual Energy Outlook"),
Id: ptr.To("aeo"),
Facets: &[]eiaapi.FacetMetaData{
{
Description: ptr.To("Future Scenario"),
Id: ptr.To("scenario"),
},
},
},
}
sampleRouteResponse := eiaapi.RouteResponse{
Request: &eiaapi.RouteRequest{},
Response: &eiaapi.Routes{
Description: ptr.To("Annual Energy Outlook"),
Id: ptr.To("aeo"),
Name: ptr.To("aeo"),
Routes: ptr.To([]eiaapi.Routes{
{
Id: ptr.To("2023"),
Name: ptr.To("2023"),
},
}),
},
}
type args struct {
json200 reflect.Value
}
tests := []struct {
name string
args args
want eiaapi.FinalRouteResponse
wantErr bool
wantNil bool
}{
{
name: "Extract final route response",
args: args{
json200: reflect.ValueOf(&sampleFinalRouteResponse),
},
want: sampleFinalRouteResponse,
wantNil: false,
wantErr: false,
},
{
name: "Unexpected routes response",
args: args{
json200: reflect.ValueOf(&sampleRouteResponse),
},
want: eiaapi.FinalRouteResponse{},
wantNil: true,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := getFRR(tt.args.json200)
if (err != nil) != tt.wantErr {
t.Errorf("getFRR() error = %v, wantErr %v", err, tt.wantErr)
return
} else if (got == nil) != tt.wantNil {
t.Error("unexpected nil response")
return
} else if (got == nil) == tt.wantNil {
return
}
if !reflect.DeepEqual(*got, tt.want) {
t.Errorf("getFRR() = %v, want %v", got, tt.want)
}
})
}
}
func Test_getRR(t *testing.T) {
sampleFinalRouteResponse := eiaapi.FinalRouteResponse{
Request: &eiaapi.RouteRequest{},
Response: &eiaapi.FinalRoute{
Description: ptr.To("Annual Energy Outlook"),
Id: ptr.To("aeo"),
Facets: &[]eiaapi.FacetMetaData{
{
Description: ptr.To("Future Scenario"),
Id: ptr.To("scenario"),
},
},
},
}
sampleRouteResponse := eiaapi.RouteResponse{
Request: &eiaapi.RouteRequest{},
Response: &eiaapi.Routes{
Description: ptr.To("Annual Energy Outlook"),
Id: ptr.To("aeo"),
Name: ptr.To("aeo"),
Routes: ptr.To([]eiaapi.Routes{
{
Id: ptr.To("2023"),
Name: ptr.To("2023"),
},
}),
},
}
type args struct {
json200 reflect.Value
}
tests := []struct {
name string
args args
want eiaapi.RouteResponse
wantErr bool
wantNil bool
}{
{
name: "Extract routes response",
args: args{
json200: reflect.ValueOf(&sampleRouteResponse),
},
want: sampleRouteResponse,
wantErr: false,
},
{
name: "Unexpected final route",
args: args{
json200: reflect.ValueOf(&sampleFinalRouteResponse),
},
want: eiaapi.RouteResponse{},
wantNil: true,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := getRR(tt.args.json200)
if (err != nil) != tt.wantErr {
t.Errorf("getRR() error = %v, wantErr %v", err, tt.wantErr)
return
} else if (got == nil) != tt.wantNil {
t.Error("unexpected nil response")
return
} else if (got == nil) == tt.wantNil {
return
}
if !reflect.DeepEqual(*got, tt.want) {
t.Errorf("getRR() = %v, want %v", got, tt.want)
}
})
}
}
func Test_getParser(t *testing.T) {
type args struct {
forMethod string
}
tests := []struct {
name string
args args
want reflect.Value
wantErr bool
}{
{
name: "Get known parser",
args: args{forMethod: "GetV2Aeo"},
want: reflect.ValueOf(eiaapi.ParseFunctionsMap["ParseGetV2AeoResponse"]),
wantErr: false,
},
{
name: "Get unknown parser",
args: args{forMethod: "GetNonsense"},
want: reflect.ValueOf(nil),
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := getParser(tt.args.forMethod)
if (err != nil) != tt.wantErr {
t.Errorf("getParser() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("getParser() = %v, want %v", got, tt.want)
}
})
}
}