diff --git a/Makefile b/Makefile index 1cd508e..758a093 100644 --- a/Makefile +++ b/Makefile @@ -8,17 +8,18 @@ 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 $(IS_GNU_SED),-i,-i '') -.PHONY: all schema generate build install clean +.PHONY: all schema generate test build install clean # Default target -all: schema generate build install +all: schema generate test build install # Retrieve and prepare schema schema: curl https://www.eia.gov/opendata/$(SCHEMA_ZIP) -o $(SCHEMA_DIR)/$(SCHEMA_ZIP) unzip -o $(SCHEMA_DIR)/$(SCHEMA_ZIP) -d $(SCHEMA_DIR) + @echo "Using GNU sed: $(IS_GNU_SED)" sed -E $(SED_INLINE) 's/responses\/data/schemas\/DataResponseContainer/g' $(SCHEMA_DIR)/$(SCHEMA_YML) sed -E $(SED_INLINE) 's/responses\/route/schemas\/RouteResponse/g' $(SCHEMA_DIR)/$(SCHEMA_YML) sed -E $(SED_INLINE) 's/responses\/facets/schemas\/FacetOptionListContainer/g' $(SCHEMA_DIR)/$(SCHEMA_YML) @@ -30,6 +31,7 @@ schema: # Generate code generate: go generate ./... + @echo "Using GNU sed: $(IS_GNU_SED)" sed -E $(SED_INLINE) 's/Total[[:space:]]+\*int/Total *string/g' $(CLIENT_GEN_FILE) sed -E $(SED_INLINE) 's/Command[[:space:]]+\*\[\]string/Command *string/g' $(CLIENT_GEN_FILE) sed -E $(SED_INLINE) 's/Routes[[:space:]]+\*\[\]string/Routes *[]Routes/g' $(CLIENT_GEN_FILE) @@ -37,8 +39,12 @@ generate: $(GO_FORMATTER) -w $(CLIENT_GEN_FILE) $(GO_FORMATTER) -w $(MAPPER_GEN_FILE) +test: + # Test EIA API client package + go test -v ./pkg/eia + # Build the client command binary -build: generate +build: generate test go build -o bin/eia-client $(CLIENT_PKG) # Install the client command binary diff --git a/main.go b/main.go index 16f245c..36090e7 100644 --- a/main.go +++ b/main.go @@ -2,3 +2,5 @@ package main //go:generate go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen --config=api/genclient.yaml schema/eia-api-swagger.yaml //go:generate go run api/funcmapper/funcmapper.go + +func main() {} diff --git a/pkg/eia/eia_test.go b/pkg/eia/eia_test.go new file mode 100644 index 0000000..403496f --- /dev/null +++ b/pkg/eia/eia_test.go @@ -0,0 +1,67 @@ +package eia + +import ( + "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) + } + }) + } +}