115 lines
2.2 KiB
Go
115 lines
2.2 KiB
Go
package eia
|
|
|
|
import (
|
|
"context"
|
|
"net/url"
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func TestNewFacets(t *testing.T) {
|
|
type args struct {
|
|
facets []*Facet
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
want map[string]interface{}
|
|
}{
|
|
{
|
|
name: "Empty facets",
|
|
args: args{facets: []*Facet{}},
|
|
want: map[string]interface{}{},
|
|
},
|
|
{
|
|
name: "Single facet",
|
|
args: args{
|
|
facets: []*Facet{
|
|
{Name: "seriesId", Data: "Region1"},
|
|
},
|
|
},
|
|
want: map[string]interface{}{
|
|
"facets[seriesId][0]": "Region1",
|
|
},
|
|
},
|
|
{
|
|
name: "Multiple facets with unique names",
|
|
args: args{
|
|
facets: []*Facet{
|
|
{Name: "seriesId", Data: "Region1"},
|
|
{Name: "scenario", Data: "low-gdp"},
|
|
},
|
|
},
|
|
want: map[string]interface{}{
|
|
"facets[seriesId][0]": "Region1",
|
|
"facets[scenario][0]": "low-gdp",
|
|
},
|
|
},
|
|
{
|
|
name: "Multiple facets with duplicate names",
|
|
args: args{
|
|
facets: []*Facet{
|
|
{Name: "scenario", Data: "low-gdp"},
|
|
{Name: "scenario", Data: "high-gdp"},
|
|
},
|
|
},
|
|
want: map[string]interface{}{
|
|
"facets[scenario][0]": "low-gdp",
|
|
"facets[scenario][1]": "high-gdp",
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if got := NewFacets(tt.args.facets...); !reflect.DeepEqual(*got, tt.want) {
|
|
t.Errorf("NewFacets() = %v, want %v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
})
|
|
}
|
|
}
|