Skip to content

Commit

Permalink
✅ adding unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jbleduigou committed Mar 18, 2024
1 parent 602dd17 commit 7e77c7d
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
11 changes: 10 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,13 @@ module github.com/jbleduigou/slog-aws-lambda

go 1.22.1

require github.com/aws/aws-lambda-go v1.46.0
require (
github.com/aws/aws-lambda-go v1.46.0
github.com/stretchr/testify v1.9.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
github.com/aws/aws-lambda-go v1.46.0 h1:UWVnvh2h2gecOlFhHQfIPQcD8pL/f7pVCutmFl+oXU8=
github.com/aws/aws-lambda-go v1.46.0/go.mod h1:dpMpZgvWx5vuQJfBt0zqBha60q7Dd7RfgJv23DymV8A=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
51 changes: 51 additions & 0 deletions handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package slogawslambda

import (
"log/slog"
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetLogLevel(t *testing.T) {
tests := []struct {
name string
envVariableValue string
want slog.Leveler
}{
{
name: "Should return Info given not set",
envVariableValue: "",
want: slog.LevelInfo,
},
{
name: "Should return Info given invalid value",
envVariableValue: "not-a-valid-value",
want: slog.LevelInfo,
},
{
name: "Should return Debug given lowercase debug",
envVariableValue: "debug",
want: slog.LevelDebug,
},
{
name: "Should return Warn given uppercase warn",
envVariableValue: "WARN",
want: slog.LevelWarn,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.envVariableValue != "" {
os.Setenv("LOG_LEVEL", tt.envVariableValue)
}

got := getLogLevel()

os.Unsetenv("LOG_LEVEL")

assert.Equal(t, got, tt.want)
})
}
}

0 comments on commit 7e77c7d

Please sign in to comment.