Skip to content

Commit

Permalink
Merge pull request #239 from checkr/zz/fix-eval-result-required-fields
Browse files Browse the repository at this point in the history
Remove required fields from evalResult swagger model
  • Loading branch information
zhouzhuojie authored Mar 19, 2019
2 parents 82dcc45 + 8caac82 commit 7c9ffed
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 281 deletions.
14 changes: 0 additions & 14 deletions docs/api_docs/bundle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1105,20 +1105,10 @@ definitions:
type: string
evalResult:
type: object
required:
- flagID
- flagKey
- segmentID
- variantID
- variantKey
- variantAttachment
- evalContext
- timestamp
properties:
flagID:
type: integer
format: int64
minimum: 1
flagKey:
type: string
flagSnapshotID:
Expand All @@ -1127,21 +1117,17 @@ definitions:
segmentID:
type: integer
format: int64
minimum: 1
variantID:
type: integer
format: int64
minimum: 1
variantKey:
type: string
minLength: 1
variantAttachment:
type: object
evalContext:
$ref: '#/definitions/evalContext'
timestamp:
type: string
minLength: 1
evalDebugLog:
$ref: '#/definitions/evalDebugLog'
evalDebugLog:
Expand Down
41 changes: 0 additions & 41 deletions pkg/config/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ import (
"fmt"
"net/http"
"net/http/httptest"
"reflect"
"testing"

"github.com/DataDog/datadog-go/statsd"
"github.com/bouk/monkey"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -293,41 +290,3 @@ func TestAuthMiddlewareWithUnauthorized(t *testing.T) {
}
})
}

func TestStatsMiddleware(t *testing.T) {
h := &okHandler{}

t.Run("it will setup statsd if statsd is enabled", func(t *testing.T) {
Config.StatsdEnabled = true
defer func() { Config.StatsdEnabled = false }()
hh := SetupGlobalMiddleware(h)

res := httptest.NewRecorder()
res.Body = new(bytes.Buffer)
req, _ := http.NewRequest("GET", "http://localhost:18000/api/v1/flags", nil)

incrCalled := false
timingCalled := false
defer monkey.PatchInstanceMethod(
reflect.TypeOf(Global.StatsdClient),
"Incr",
func(_ *statsd.Client, _ string, _ []string, _ float64) error {
incrCalled = true
return nil
},
).Unpatch()
defer monkey.PatchInstanceMethod(
reflect.TypeOf(Global.StatsdClient),
"TimeInMilliseconds",
func(_ *statsd.Client, _ string, _ float64, _ []string, _ float64) error {
timingCalled = true
return nil
},
).Unpatch()

hh.ServeHTTP(res, req)
assert.Equal(t, http.StatusOK, res.Code)
assert.True(t, incrCalled)
assert.True(t, timingCalled)
})
}
17 changes: 8 additions & 9 deletions pkg/handler/data_record_frame_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package handler
import (
"testing"

"github.com/checkr/flagr/pkg/util"
"github.com/checkr/flagr/swagger_gen/models"
"github.com/stretchr/testify/assert"
)
Expand All @@ -13,11 +12,11 @@ func TestFrameOutput(t *testing.T) {
EvalContext: &models.EvalContext{
EntityID: "123",
},
FlagID: util.Int64Ptr(int64(1)),
FlagID: 1,
FlagSnapshotID: 1,
SegmentID: util.Int64Ptr(int64(1)),
VariantID: util.Int64Ptr(int64(1)),
VariantKey: util.StringPtr("control"),
SegmentID: 1,
VariantID: 1,
VariantKey: "control",
}

t.Run("empty options", func(t *testing.T) {
Expand Down Expand Up @@ -75,11 +74,11 @@ func TestGetPartitionKey(t *testing.T) {
EvalContext: &models.EvalContext{
EntityID: "123",
},
FlagID: util.Int64Ptr(int64(1)),
FlagID: 1,
FlagSnapshotID: 1,
SegmentID: util.Int64Ptr(int64(1)),
VariantID: util.Int64Ptr(int64(1)),
VariantKey: util.StringPtr("control"),
SegmentID: 1,
VariantID: 1,
VariantKey: "control",
}
frame := DataRecordFrame{evalResult: er}
assert.Equal(t, "123", frame.GetPartitionKey())
Expand Down
2 changes: 1 addition & 1 deletion pkg/handler/data_recorder_kinesis.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package handler

import (
"github.com/a8m/kinesis-producer"
producer "github.com/a8m/kinesis-producer"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/kinesis"
Expand Down
11 changes: 5 additions & 6 deletions pkg/handler/data_recorder_kinesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ package handler
import (
"testing"

"github.com/a8m/kinesis-producer"
"github.com/checkr/flagr/pkg/util"
producer "github.com/a8m/kinesis-producer"
"github.com/checkr/flagr/swagger_gen/models"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -37,11 +36,11 @@ func TestKinesisAsyncRecord(t *testing.T) {
EvalContext: &models.EvalContext{
EntityID: "d08042018",
},
FlagID: util.Int64Ptr(int64(1)),
FlagID: 1,
FlagSnapshotID: 1,
SegmentID: util.Int64Ptr(int64(1)),
VariantID: util.Int64Ptr(int64(1)),
VariantKey: util.StringPtr("control"),
SegmentID: 1,
VariantID: 1,
VariantKey: "control",
},
)
})
Expand Down
9 changes: 4 additions & 5 deletions pkg/handler/data_recorder_pubsub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"cloud.google.com/go/pubsub"
"cloud.google.com/go/pubsub/pstest"
"github.com/checkr/flagr/pkg/util"
"github.com/checkr/flagr/swagger_gen/models"
"github.com/prashantv/gostub"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -45,11 +44,11 @@ func TestPubsubAsyncRecord(t *testing.T) {
EvalContext: &models.EvalContext{
EntityID: "d08042018",
},
FlagID: util.Int64Ptr(int64(1)),
FlagID: 1,
FlagSnapshotID: 1,
SegmentID: util.Int64Ptr(int64(1)),
VariantID: util.Int64Ptr(int64(1)),
VariantKey: util.StringPtr("control"),
SegmentID: 1,
VariantID: 1,
VariantKey: "control",
},
)
})
Expand Down
18 changes: 8 additions & 10 deletions pkg/handler/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,10 @@ func BlankResult(f *entity.Flag, evalContext models.EvalContext, msg string) *mo
Msg: msg,
SegmentDebugLogs: nil,
},
FlagID: util.Int64Ptr(int64(flagID)),
FlagKey: util.StringPtr(flagKey),
FlagID: int64(flagID),
FlagKey: flagKey,
FlagSnapshotID: int64(flagSnapshotID),
SegmentID: nil,
VariantID: nil,
Timestamp: util.StringPtr(util.TimeNow()),
Timestamp: util.TimeNow(),
}
}

Expand Down Expand Up @@ -138,17 +136,17 @@ var evalFlag = func(evalContext models.EvalContext) *models.EvalResult {
}

logs := []*models.SegmentDebugLog{}
var vID *int64
var sID *int64
var vID int64
var sID int64

for _, segment := range f.Segments {
sID = util.Int64Ptr(int64(segment.ID))
sID = int64(segment.ID)
variantID, log, evalNextSegment := evalSegment(f.ID, evalContext, segment)
if config.Config.EvalDebugEnabled && evalContext.EnableDebug {
logs = append(logs, log)
}
if variantID != nil {
vID = util.Int64Ptr(int64(*variantID))
vID = int64(*variantID)
}
if !evalNextSegment {
break
Expand All @@ -161,7 +159,7 @@ var evalFlag = func(evalContext models.EvalContext) *models.EvalResult {
v := f.FlagEvaluation.VariantsMap[util.SafeUint(vID)]
if v != nil {
evalResult.VariantAttachment = v.Attachment
evalResult.VariantKey = util.StringPtr(v.Key)
evalResult.VariantKey = v.Key
}

logEvalResult(evalResult, f.DataRecordsEnabled)
Expand Down
11 changes: 5 additions & 6 deletions pkg/handler/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"testing"

"github.com/checkr/flagr/pkg/entity"
"github.com/checkr/flagr/pkg/util"
"github.com/checkr/flagr/swagger_gen/models"
"github.com/checkr/flagr/swagger_gen/restapi/operations/evaluation"
"github.com/jinzhu/gorm"
Expand Down Expand Up @@ -94,7 +93,7 @@ func TestEvalFlag(t *testing.T) {
t.Run("test empty evalContext", func(t *testing.T) {
defer gostub.StubFunc(&GetEvalCache, GenFixtureEvalCache()).Reset()
result := evalFlag(models.EvalContext{FlagID: int64(100)})
assert.Nil(t, result.VariantID)
assert.Zero(t, result.VariantID)
assert.NotZero(t, result.FlagID)
assert.NotEmpty(t, result.EvalContext.EntityID)
})
Expand Down Expand Up @@ -194,7 +193,7 @@ func TestEvalFlag(t *testing.T) {
FlagID: int64(100),
})
assert.NotNil(t, result)
assert.Nil(t, result.VariantID)
assert.Zero(t, result.VariantID)
})

t.Run("test no match path with multiple constraints", func(t *testing.T) {
Expand Down Expand Up @@ -226,7 +225,7 @@ func TestEvalFlag(t *testing.T) {
FlagID: int64(100),
})
assert.NotNil(t, result)
assert.Nil(t, result.VariantID)
assert.Zero(t, result.VariantID)
})

t.Run("test enabled=false", func(t *testing.T) {
Expand All @@ -242,7 +241,7 @@ func TestEvalFlag(t *testing.T) {
FlagID: int64(100),
})
assert.NotNil(t, result)
assert.Nil(t, result.VariantID)
assert.Zero(t, result.VariantID)
})

t.Run("test entityType override", func(t *testing.T) {
Expand Down Expand Up @@ -329,7 +328,7 @@ func TestPostEvaluationBatch(t *testing.T) {
}

func TestRateLimitPerFlagConsoleLogging(t *testing.T) {
r := &models.EvalResult{FlagID: util.Int64Ptr(int64(1))}
r := &models.EvalResult{FlagID: 1}
t.Run("running fast triggers rate limiting", func(t *testing.T) {
for i := 0; i < 100; i++ {
rateLimitPerFlagConsoleLogging(r)
Expand Down
14 changes: 0 additions & 14 deletions swagger/index.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -408,20 +408,10 @@ definitions:
type: string
evalResult:
type: object
required:
- flagID
- flagKey
- segmentID
- variantID
- variantKey
- variantAttachment
- evalContext
- timestamp
properties:
flagID:
type: integer
format: int64
minimum: 1
flagKey:
type: string
flagSnapshotID:
Expand All @@ -430,21 +420,17 @@ definitions:
segmentID:
type: integer
format: int64
minimum: 1
variantID:
type: integer
format: int64
minimum: 1
variantKey:
type: string
minLength: 1
variantAttachment:
type: object
evalContext:
$ref: "#/definitions/evalContext"
timestamp:
type: string
minLength: 1
evalDebugLog:
$ref: "#/definitions/evalDebugLog"
evalDebugLog:
Expand Down
Loading

0 comments on commit 7c9ffed

Please sign in to comment.