Add route tests

This commit is contained in:
Ryan McGuire 2024-12-05 15:24:08 -05:00
parent 5fd94dcd63
commit 820141f865
3 changed files with 100 additions and 0 deletions

1
go.mod
View File

@ -12,6 +12,7 @@ require (
github.com/rs/zerolog v1.33.0
github.com/spf13/cobra v1.8.1
golang.org/x/tools v0.27.0
k8s.io/utils v0.0.0-20241104163129-6fe5fd82f078
)
require (

2
go.sum
View File

@ -211,3 +211,5 @@ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
k8s.io/utils v0.0.0-20241104163129-6fe5fd82f078 h1:jGnCPejIetjiy2gqaJ5V0NLwTpF4wbQ6cZIItJCSHno=
k8s.io/utils v0.0.0-20241104163129-6fe5fd82f078/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=

View File

@ -1,8 +1,13 @@
package eia
import (
"context"
"reflect"
"testing"
"k8s.io/utils/ptr"
eiaapi "gitea.libretechconsulting.com/50W/eia-api-go/api"
)
func TestGetRoutes(t *testing.T) {
@ -38,3 +43,95 @@ func TestGetRoutes(t *testing.T) {
})
}
}
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_getRR(t *testing.T) {
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
}{
{
name: "Extract routes response",
args: args{
json200: reflect.ValueOf(&sampleRouteResponse),
},
want: sampleRouteResponse,
wantErr: false,
},
}
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
}
if !reflect.DeepEqual(*got, tt.want) {
t.Errorf("getRR() = %v, want %v", got, tt.want)
}
})
}
}