Skip to content

Commit

Permalink
Add context with criteria function (#224)
Browse files Browse the repository at this point in the history
  • Loading branch information
dpanayotov authored Feb 1, 2019
1 parent 7dc3e3c commit 88cc4b0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/query/selection.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@ func CriteriaForContext(ctx context.Context) []Criterion {
return currentCriteria.([]Criterion)
}

// ContextWithCriteria returns a new context with given criteria
func ContextWithCriteria(ctx context.Context, criteria []Criterion) context.Context {
return context.WithValue(ctx, criteriaCtxKey{}, criteria)
}

// BuildCriteriaFromRequest builds criteria for the given request's query params and returns an error if the query is not valid
func BuildCriteriaFromRequest(request *web.Request) ([]Criterion, error) {
var criteria []Criterion
Expand Down
23 changes: 23 additions & 0 deletions pkg/query/selection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,29 @@ var _ = Describe("Selection", func() {
})
})

Describe("Context with criteria", func() {
Context("When there are no criteria in the context", func() {
It("Adds the new ones", func() {
newCriteria := []Criterion{ByField(EqualsOperator, "leftOp", "rightOp")}
newContext := ContextWithCriteria(ctx, newCriteria)
Expect(CriteriaForContext(newContext)).To(ConsistOf(newCriteria))
})
})

Context("When there are criteria already in the context", func() {
It("Overrides them", func() {
oldCriteria := []Criterion{ByField(EqualsOperator, "leftOp", "rightOp")}
oldContext := ContextWithCriteria(ctx, oldCriteria)

newCriteria := []Criterion{ByLabel(NotEqualsOperator, "leftOp1", "rightOp1")}
newContext := ContextWithCriteria(oldContext, newCriteria)
criteriaForNewContext := CriteriaForContext(newContext)
Expect(criteriaForNewContext).To(ConsistOf(newCriteria))
Expect(criteriaForNewContext).ToNot(ContainElement(oldCriteria[0]))
})
})
})

Describe("Build criteria from request", func() {
var request *web.Request
BeforeEach(func() {
Expand Down

0 comments on commit 88cc4b0

Please sign in to comment.