Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scenario Values & Reading from Response #178

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Prev Previous commit
Next Next commit
cleanup scenarioFiller and add tests
  • Loading branch information
jcdietrich committed Nov 6, 2024
commit 7cc9d1e92f2f8c18b74bc1cbb4d862d6f053837c
3 changes: 3 additions & 0 deletions pkg/match/scenario_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ func (sm *InMemoryScenarioStore) SetStateValue(name, valueName, value string) {
if sm.paused {
return
}
if sm.values[strings.ToLower(name)] == nil {
sm.values[strings.ToLower(name)] = make(map[string]string)
}
sm.values[strings.ToLower(name)][strings.ToLower(valueName)] = value
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/vars/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (fp ResponseMessageEvaluator) Eval(req *mock.Request, m *mock.Definition, s
fakeFiller := fp.FillerFactory.CreateFakeFiller()
streamFiller := fp.FillerFactory.CreateStreamFiller()
responseFiller := fp.FillerFactory.CreateResponseFiller(&m.Response)
scenarioFiller := fp.FillerFactory.CreateScenarioFiller(req, m, store, m.Control.Scenario.Name)
scenarioFiller := fp.FillerFactory.CreateScenarioFiller(store, m.Control.Scenario.Name)

//first replace the external streams
holders := fp.walkAndGet(m.Response.HTTPEntity)
Expand Down
10 changes: 2 additions & 8 deletions pkg/vars/filler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ type FillerFactory interface {
CreateStreamFiller() Filler
CreateResponseFiller(res *mock.Response) Filler
CreateScenarioFiller(
req *mock.Request,
mock *mock.Definition,
scenarioStore match.ScenearioStorer,
scenarioName string,
) Filler
Expand Down Expand Up @@ -48,15 +46,11 @@ func (mff MockFillerFactory) CreateResponseFiller(res *mock.Response) Filler {
}

func (mff MockFillerFactory) CreateScenarioFiller(
req *mock.Request,
mock *mock.Definition,
scenarioStore match.ScenearioStorer,
scenarioName string) Filler {

return ScenarioFiller{
Mock: mock,
Request: req,
Name: scenarioName,
Store: scenarioStore,
Name: scenarioName,
Store: scenarioStore,
}
}
7 changes: 2 additions & 5 deletions pkg/vars/scenario.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@ package vars

import (
"github.com/jmartin82/mmock/v3/pkg/match"
"github.com/jmartin82/mmock/v3/pkg/mock"
"strings"
)

type ScenarioFiller struct {
Mock *mock.Definition
Request *mock.Request
Name string
Store match.ScenearioStorer
Name string
Store match.ScenearioStorer
}

func (sf ScenarioFiller) Fill(holders []string) map[string][]string {
Expand Down
67 changes: 67 additions & 0 deletions pkg/vars/scenario_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package vars

import (
"strings"
"testing"

"github.com/jmartin82/mmock/v3/pkg/match"
)

var scenarioTestVars = []struct {
key string
value string
expectToFind bool
}{
{"scenario.test1", "one", true},
{"scenario.test2", "two", true},
{"scenario.not_found", "nothing", false},
}

func getLoadedScenarioFiller() ScenarioFiller {
var scenarioName = "test_scenario"

var store = match.NewInMemoryScenarioStore()
store.SetStateValue(scenarioName, "test1", "one")
store.SetStateValue(scenarioName, "test2", "two")

filler := ScenarioFiller{
Name: scenarioName,
Store: store,
}

log.Errorf("filler: %v", filler)
log.Errorf("store: %v", store)
return filler
}

func TestScenarioFiller(t *testing.T) {
var filler = getLoadedScenarioFiller()

for _, tt := range scenarioTestVars {
log.Errorf("testing: %v", tt)
holders := []string{
tt.key,
}

vars := filler.Fill(holders)
log.Errorf("vars: %v", vars)

if len(vars) == 0 {
if tt.expectToFind {
t.Errorf("Unable to retrieve vars")
}
continue
}

v, ok := vars[tt.key]

if !ok {
t.Errorf("Unable to retrieve value inside vars")
continue
}

if strings.EqualFold(v[0], tt.value) != tt.expectToFind {
t.Errorf("Couldn't get the expected value. Expected: %s, Value found: %s", tt.value, v[0])
}
}
}