Add tests, fix Makefile

This commit is contained in:
Ryan McGuire 2024-12-06 15:27:45 -05:00
parent 820141f865
commit ee53efbcd6
2 changed files with 66 additions and 1 deletions

View File

@ -8,7 +8,7 @@ MAPPER_GEN_FILE := ./api/eiaapi_funcmap.gen.go
GO_FORMATTER := gofumpt
IS_GNU_SED := $(shell sed --version >/dev/null 2>&1 && echo true || echo false)
SED_INLINE := $(if $(IS_GNU_SED),-i,-i '')
SED_INLINE := $(if $(filter true,$(IS_GNU_SED)),-i,-i '')
.PHONY: all schema generate test build install clean

View File

@ -90,6 +90,19 @@ func Test_prepMethodArgs(t *testing.T) {
}
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{
@ -112,6 +125,7 @@ func Test_getRR(t *testing.T) {
args args
want eiaapi.RouteResponse
wantErr bool
wantNil bool
}{
{
name: "Extract routes response",
@ -121,6 +135,15 @@ func Test_getRR(t *testing.T) {
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) {
@ -128,6 +151,11 @@ func Test_getRR(t *testing.T) {
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)
@ -135,3 +163,40 @@ func Test_getRR(t *testing.T) {
})
}
}
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)
}
})
}
}