From 549124950afd8be6c69f9ae56863e3086e10493e Mon Sep 17 00:00:00 2001 From: Ryan D McGuire Date: Wed, 11 Dec 2024 15:41:09 -0500 Subject: [PATCH] Add logging tests --- pkg/eia/eia_logging.go | 9 ++++ pkg/eia/eia_logging_test.go | 48 +++++++++++++++++++++ pkg/eia/eia_reflection_test.go | 76 ++++++++++++++++++++++++++++++++++ 3 files changed, 133 insertions(+) create mode 100644 pkg/eia/eia_logging_test.go diff --git a/pkg/eia/eia_logging.go b/pkg/eia/eia_logging.go index 7834f34..a245181 100644 --- a/pkg/eia/eia_logging.go +++ b/pkg/eia/eia_logging.go @@ -2,6 +2,7 @@ package eia import ( "context" + "errors" "net/http" "time" @@ -12,6 +13,14 @@ import ( func newLoggingMiddleware(logger *zerolog.Logger, level zerolog.Level) eiaapi.RequestEditorFn { return func(_ context.Context, req *http.Request) error { + if req == nil { + return errors.New("invalid nil http request") + } else if req.URL == nil { + return errors.New("missing request url") + } else if req.Method == "" { + return errors.New("missing request method") + } + // Don't log api_key params := req.URL.Query() delete(params, "api_key") diff --git a/pkg/eia/eia_logging_test.go b/pkg/eia/eia_logging_test.go new file mode 100644 index 0000000..40ba721 --- /dev/null +++ b/pkg/eia/eia_logging_test.go @@ -0,0 +1,48 @@ +package eia + +import ( + "context" + "net/http" + "net/url" + "testing" + + "github.com/rs/zerolog" +) + +func Test_newLoggingMiddleware(t *testing.T) { + logger := zerolog.New(zerolog.NewTestWriter(t)) + + tests := []struct { + name string + request *http.Request + wantErr bool + }{ + { + name: "Test empty request", + request: &http.Request{}, + wantErr: true, + }, + { + name: "Test valid request", + request: &http.Request{ + Method: "GET", + URL: &url.URL{ + Scheme: "https", + Host: "test.test", + Path: "/test", + }, + }, + wantErr: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + middlewareFunc := newLoggingMiddleware(&logger, zerolog.TraceLevel) + err := middlewareFunc(context.Background(), tt.request) + if (err != nil) != tt.wantErr { + t.Errorf("Test %s had unexpected error: %s", tt.name, err.Error()) + } + }) + } +} diff --git a/pkg/eia/eia_reflection_test.go b/pkg/eia/eia_reflection_test.go index 13ee565..1111a0d 100644 --- a/pkg/eia/eia_reflection_test.go +++ b/pkg/eia/eia_reflection_test.go @@ -89,6 +89,82 @@ func Test_prepMethodArgs(t *testing.T) { } } +func Test_getFRR(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{ + Description: ptr.To("Annual Energy Outlook"), + Id: ptr.To("aeo"), + Name: ptr.To("aeo"), + Routes: ptr.To([]eiaapi.Routes{ + { + Id: ptr.To("2023"), + Name: ptr.To("2023"), + }, + }), + }, + } + type args struct { + json200 reflect.Value + } + tests := []struct { + name string + args args + want eiaapi.FinalRouteResponse + wantErr bool + wantNil bool + }{ + { + name: "Extract final route response", + args: args{ + json200: reflect.ValueOf(&sampleFinalRouteResponse), + }, + want: sampleFinalRouteResponse, + wantNil: false, + wantErr: false, + }, + { + name: "Unexpected routes response", + args: args{ + json200: reflect.ValueOf(&sampleRouteResponse), + }, + want: eiaapi.FinalRouteResponse{}, + wantNil: true, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := getFRR(tt.args.json200) + if (err != nil) != tt.wantErr { + t.Errorf("getFRR() 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("getFRR() = %v, want %v", got, tt.want) + } + }) + } +} + func Test_getRR(t *testing.T) { sampleFinalRouteResponse := eiaapi.FinalRouteResponse{ Request: &eiaapi.RouteRequest{},