Add route tests

This commit is contained in:
Ryan McGuire 2024-12-05 12:59:02 -05:00
parent f0b1c33a36
commit 5fd94dcd63
3 changed files with 96 additions and 0 deletions

View File

@ -2,9 +2,11 @@ package eia
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"net/url" "net/url"
"slices" "slices"
"strings"
"time" "time"
"github.com/deepmap/oapi-codegen/pkg/securityprovider" "github.com/deepmap/oapi-codegen/pkg/securityprovider"
@ -66,7 +68,14 @@ func NewFacets(facets ...*Facet) *eiaapi.Facets {
func NewClient(opts *ClientOpts) (*Client, error) { func NewClient(opts *ClientOpts) (*Client, error) {
baseURL := defaultBaseURL baseURL := defaultBaseURL
if opts.BaseURL != nil { if opts.BaseURL != nil {
if !strings.HasPrefix(opts.BaseURL.Scheme, "http") {
return nil, errors.New("invalid scheme, only http or https supported")
}
baseURL = opts.BaseURL.String() baseURL = opts.BaseURL.String()
if _, err := url.Parse(baseURL); err != nil {
return nil, err
}
} }
hcTimeout := defaultPingTimeout hcTimeout := defaultPingTimeout

View File

@ -0,0 +1,40 @@
package eia
import (
"reflect"
"testing"
)
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)
}
})
}
}

View File

@ -1,6 +1,8 @@
package eia package eia
import ( import (
"context"
"net/url"
"reflect" "reflect"
"testing" "testing"
) )
@ -65,3 +67,48 @@ func TestNewFacets(t *testing.T) {
}) })
} }
} }
func TestNewClient(t *testing.T) {
type args struct {
opts *ClientOpts
}
tests := []struct {
name string
args args
want *Client
wantErr bool
}{
{
name: "Bad URL Scheme",
args: args{opts: &ClientOpts{
Context: context.TODO(),
APIKey: "testkey",
BaseURL: &url.URL{Scheme: "grpc"},
}},
want: nil,
wantErr: true,
},
{
name: "Bad Host",
args: args{opts: &ClientOpts{
Context: context.TODO(),
APIKey: "testkey",
BaseURL: &url.URL{Host: "bad host:realbad"},
}},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := NewClient(tt.args.opts)
if (err != nil) != tt.wantErr {
t.Errorf("NewClient() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewClient() = %v, want %v", got, tt.want)
}
})
}
}