Skip to content

Commit

Permalink
Improve VCR and move to separate package
Browse files Browse the repository at this point in the history
  • Loading branch information
calvinmclean committed Aug 18, 2024
1 parent 21a7503 commit 06540c6
Show file tree
Hide file tree
Showing 14 changed files with 973 additions and 72 deletions.
6 changes: 4 additions & 2 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,12 @@ tasks:
desc: Run backend Go app server with VCR enabled
aliases: [vcr]
dir: ./garden-app
vars:
vcr_base_dir: server/vcr/testdata/vcr_server
env:
VCR_CASSETTE: server/testdata/vcr_server/fixtures/{{ .CLI_ARGS }}
VCR_CASSETTE: "{{ .vcr_base_dir }}/fixtures/{{ .CLI_ARGS }}"
cmds:
- go run main.go serve --config ./server/testdata/vcr_server/config.yaml
- go run main.go serve --config ./{{ .vcr_base_dir }}/config.yaml

run-controller:
desc: Run mock controller
Expand Down
3 changes: 2 additions & 1 deletion garden-app/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/calvinmclean/automated-garden/garden-app/server"
"github.com/calvinmclean/automated-garden/garden-app/server/vcr"

"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -17,7 +18,7 @@ var (
)

func Execute() {
defer server.StopRecorder()
defer vcr.StopRecorder()

api := server.NewAPI()
command := api.Command()
Expand Down
2 changes: 1 addition & 1 deletion garden-app/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,6 @@ require (
gopkg.in/ini.v1 v1.67.0 // indirect
)

replace gopkg.in/dnaeon/go-vcr.v3 => github.com/calvinmclean/go-vcr v0.2.0
replace gopkg.in/dnaeon/go-vcr.v3 => github.com/calvinmclean/go-vcr v0.3.0

//replace gopkg.in/dnaeon/go-vcr.v3 => ../../go-vcr
4 changes: 2 additions & 2 deletions garden-app/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ github.com/bytedance/sonic v1.10.2 h1:GQebETVBxYB7JGWJtLBi07OVzWwt+8dWA00gEVW2ZF
github.com/bytedance/sonic v1.10.2/go.mod h1:iZcSUejdk5aukTND/Eu/ivjQuEL0Cu9/rf50Hi0u/g4=
github.com/calvinmclean/babyapi v0.23.0 h1:rmYnAz80cX6TTDtbjQGt8DzjR7CDZNxkJm0/KvWRQHY=
github.com/calvinmclean/babyapi v0.23.0/go.mod h1:zSNiVRsL3DBPOMkXxMJOTFNtzU1ZrPFKD0LFx2JVp4I=
github.com/calvinmclean/go-vcr v0.2.0 h1:foa7hUNJrebZg++/Cg7sNrRWMTa8mqx9PBZXafuI8bA=
github.com/calvinmclean/go-vcr v0.2.0/go.mod h1:Oy/wiaJrF0S8WmwjGRpDSL09yGnb9HXV4qP87dHkfFk=
github.com/calvinmclean/go-vcr v0.3.0 h1:IDcpV3LktoWJjK+p11XdITqU6NOHZ+mCLICW0l7Hf+s=
github.com/calvinmclean/go-vcr v0.3.0/go.mod h1:Oy/wiaJrF0S8WmwjGRpDSL09yGnb9HXV4qP87dHkfFk=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
Expand Down
16 changes: 15 additions & 1 deletion garden-app/server/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import (
"fmt"
"log/slog"
"net/http"
"os"

"github.com/calvinmclean/automated-garden/garden-app/clock"
"github.com/calvinmclean/automated-garden/garden-app/pkg/influxdb"
"github.com/calvinmclean/automated-garden/garden-app/pkg/mqtt"
"github.com/calvinmclean/automated-garden/garden-app/pkg/storage"
"github.com/calvinmclean/automated-garden/garden-app/server/vcr"
"github.com/calvinmclean/automated-garden/garden-app/worker"

"github.com/calvinmclean/babyapi"
Expand Down Expand Up @@ -50,11 +53,22 @@ func NewAPI() *API {
AddNestedAPI(api.notificationClients).
AddNestedAPI(api.waterSchedules)

api.configureVCR()
cassetteName := os.Getenv("VCR_CASSETTE")
if cassetteName != "" {
EnableMock()
vcr.MustSetupVCR(cassetteName)

Check warning on line 59 in garden-app/server/api.go

View check run for this annotation

Codecov / codecov/patch

garden-app/server/api.go#L58-L59

Added lines #L58 - L59 were not covered by tests
}

return api
}

// EnableMock prepares mock IDs and clock
func EnableMock() {
enableMockIDs = true
mockIDIndex = 0
_ = clock.MockTime()

Check warning on line 69 in garden-app/server/api.go

View check run for this annotation

Codecov / codecov/patch

garden-app/server/api.go#L66-L69

Added lines #L66 - L69 were not covered by tests
}

// Setup will prepare to run by setting up clients and doing any final configurations for the API
func (api *API) Setup(cfg Config, validateData bool) error {
html.SetFS(templates, "templates/*")
Expand Down
52 changes: 0 additions & 52 deletions garden-app/server/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,68 +2,16 @@ package server

import (
"context"
"os"
"path/filepath"
"strings"
"testing"

"github.com/calvinmclean/automated-garden/garden-app/clock"
"github.com/calvinmclean/automated-garden/garden-app/pkg"
"github.com/calvinmclean/automated-garden/garden-app/pkg/storage"
"github.com/calvinmclean/automated-garden/garden-app/pkg/weather"

"github.com/calvinmclean/babyapi"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/dnaeon/go-vcr.v3/cassette"
)

func TestReplay(t *testing.T) {
// This is currently not in `init` because other tests in this package don't expect mock
_ = clock.MockTime()
enableMockIDs = true
t.Cleanup(func() {
enableMockIDs = false
clock.Reset()
})

dir := "testdata/vcr_server/fixtures"
entries, err := os.ReadDir(dir)
require.NoError(t, err)

for _, entry := range entries {
if entry.IsDir() {
continue
}

cassetteName := strings.TrimSuffix(entry.Name(), ".yaml")
t.Run(cassetteName, func(t *testing.T) {
api := NewAPI()
mockIDIndex = 0

var config Config
t.Run("SetupConfig", func(t *testing.T) {
viper.SetConfigFile("./testdata/vcr_server/config.yaml")

err := viper.ReadInConfig()
require.NoError(t, err)

err = viper.Unmarshal(&config)
require.NoError(t, err)
})

err := api.Setup(config, true)
require.NoError(t, err)

r, err := api.Router()
require.NoError(t, err)

cassette.TestServerReplay(t, filepath.Join(dir, cassetteName), r)
})
}
}

func TestValidateAllStoredResources(t *testing.T) {
tests := []struct {
name string
Expand Down
1 change: 1 addition & 0 deletions garden-app/server/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package server
import (
"github.com/calvinmclean/automated-garden/garden-app/clock"
"github.com/calvinmclean/babyapi"

"github.com/rs/xid"
)

Expand Down
Loading

0 comments on commit 06540c6

Please sign in to comment.