Start adding tests

This commit is contained in:
Ryan McGuire 2024-12-05 12:36:49 -05:00
parent 02d54324f6
commit f0b1c33a36
3 changed files with 79 additions and 4 deletions

View File

@ -8,17 +8,18 @@ MAPPER_GEN_FILE := ./api/eiaapi_funcmap.gen.go
GO_FORMATTER := gofumpt GO_FORMATTER := gofumpt
IS_GNU_SED := $(shell sed --version >/dev/null 2>&1 && echo true || echo false) 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 # Default target
all: schema generate build install all: schema generate test build install
# Retrieve and prepare schema # Retrieve and prepare schema
schema: schema:
curl https://www.eia.gov/opendata/$(SCHEMA_ZIP) -o $(SCHEMA_DIR)/$(SCHEMA_ZIP) curl https://www.eia.gov/opendata/$(SCHEMA_ZIP) -o $(SCHEMA_DIR)/$(SCHEMA_ZIP)
unzip -o $(SCHEMA_DIR)/$(SCHEMA_ZIP) -d $(SCHEMA_DIR) 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\/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\/route/schemas\/RouteResponse/g' $(SCHEMA_DIR)/$(SCHEMA_YML)
sed -E $(SED_INLINE) 's/responses\/facets/schemas\/FacetOptionListContainer/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 code
generate: generate:
go 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/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/Command[[:space:]]+\*\[\]string/Command *string/g' $(CLIENT_GEN_FILE)
sed -E $(SED_INLINE) 's/Routes[[:space:]]+\*\[\]string/Routes *[]Routes/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 $(CLIENT_GEN_FILE)
$(GO_FORMATTER) -w $(MAPPER_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 the client command binary
build: generate build: generate test
go build -o bin/eia-client $(CLIENT_PKG) go build -o bin/eia-client $(CLIENT_PKG)
# Install the client command binary # Install the client command binary

View File

@ -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 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 //go:generate go run api/funcmapper/funcmapper.go
func main() {}

67
pkg/eia/eia_test.go Normal file
View File

@ -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)
}
})
}
}