Skip to content

Commit

Permalink
[BUG] Fix End-to-End test suite (#189)
Browse files Browse the repository at this point in the history
* - debug e2e test

* - debug e2e test

* - debug e2e test

* - debug e2e test

* - debug e2e test

* - extend unit test for the ExperimentRunner impl
  • Loading branch information
romanwozniak authored Mar 28, 2022
1 parent d12014c commit 6d9df86
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 31 deletions.
84 changes: 53 additions & 31 deletions api/e2e/test/01_create_router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,40 +81,62 @@ func TestCreateRouter(t *testing.T) {
assert.Equal(t, http.StatusOK, response.StatusCode,
"Unexpected response (code %d): %s",
response.StatusCode, string(responsePayload))
actualResponse := gjson.GetBytes(responsePayload, "#.data.response").String()
expectedResponse := `[{
"experiment": {
"configuration": {
"foo":"bar"
}
},
"route_responses": [
t.Logf("Response Payload:\n%s", string(responsePayload))
expectedResponse := `[
{
"data": {
"version": "control"
},
"is_default": true,
"route": "control"
}
]
},
{
"experiment": {
"configuration": {
"bar": "baz"
}
},
"route_responses": [
"code": 200,
"data": {
"request": {
"client": {
"id": 4
}
},
"response": {
"experiment": {
"configuration": {
"foo": "bar"
}
},
"route_responses": [
{
"data": {
"version": "control"
},
"is_default": true,
"route": "control"
}
]
}
}
},
{
"data": {
"version": "control"
},
"is_default": true,
"route": "control"
"code": 200,
"data": {
"request": {
"client": {
"id": 7
}
},
"response": {
"experiment": {
"configuration": {
"bar": "baz"
}
},
"route_responses": [
{
"data": {
"version": "control"
},
"is_default": true,
"route": "control"
}
]
}
}
}
]
}]`
assert.JSONEq(t, expectedResponse, actualResponse)
]`
assert.JSONEq(t, expectedResponse, string(responsePayload))
})

t.Log("Test endpoints for router logs")
Expand Down
1 change: 1 addition & 0 deletions engines/experiment/examples/plugins/hardcoded/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.14
require (
github.com/gojek/turing/engines/experiment v1.0.0
github.com/hashicorp/go-hclog v0.16.0
github.com/stretchr/testify v1.7.0
)

replace github.com/gojek/turing/engines/experiment => ../../../
94 changes: 94 additions & 0 deletions engines/experiment/examples/plugins/hardcoded/runner_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package hardcoded

import (
"encoding/json"
"net/http"
"testing"

"github.com/gojek/turing/engines/experiment/manager"
"github.com/gojek/turing/engines/experiment/pkg/request"
"github.com/gojek/turing/engines/experiment/runner"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestExperimentRunner_GetTreatmentForRequest(t *testing.T) {
experiments := []Experiment{
{
Experiment: manager.Experiment{
ID: "001",
Name: "exp_1",
Variants: []manager.Variant{
{
Name: "control",
},
{
Name: "treatment-1",
},
},
},
SegmentationConfig: SegmenterConfig{
Name: "customer_id",
SegmenterSource: request.PayloadFieldSource,
SegmenterValue: "client.id",
},
VariantsConfig: map[string]TreatmentConfig{
"control": {
Traffic: 0.85,
Data: json.RawMessage(`{"foo": "bar"}`),
},
"treatment-1": {
Traffic: 0.15,
Data: json.RawMessage(`{"bar": "baz"}`),
},
},
},
}

suite := map[string]struct {
experiments []Experiment
header http.Header
payload json.RawMessage
expected *runner.Treatment
err string
}{
"success | client_id:4": {
experiments: experiments,
payload: json.RawMessage(`{"client": {"id": 4}}`),
expected: &runner.Treatment{
ExperimentName: "exp_1",
Name: "control",
Config: json.RawMessage(`{"foo": "bar"}`),
},
},
"success | client_id:7": {
experiments: experiments,
payload: json.RawMessage(`{"client": {"id": 7}}`),
expected: &runner.Treatment{
ExperimentName: "exp_1",
Name: "treatment-1",
Config: json.RawMessage(`{"bar": "baz"}`),
},
},
"failure": {
experiments: experiments,
payload: json.RawMessage(`{}`),
err: "no experiment configured for the unit",
},
}

for name, tt := range suite {
t.Run(name, func(t *testing.T) {
expRunner := ExperimentRunner{experiments: tt.experiments}

actual, err := expRunner.GetTreatmentForRequest(tt.header, tt.payload, runner.GetTreatmentOptions{})
if tt.err != "" {
require.Error(t, err)
require.EqualError(t, err, tt.err)
} else {
require.NoError(t, err)
assert.Equal(t, tt.expected, actual)
}
})
}
}

0 comments on commit 6d9df86

Please sign in to comment.