diff --git a/pkg/query/selection.go b/pkg/query/selection.go index f9535a366..e4de57a72 100644 --- a/pkg/query/selection.go +++ b/pkg/query/selection.go @@ -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 diff --git a/pkg/query/selection_test.go b/pkg/query/selection_test.go index 0823a154f..77671cc69 100644 --- a/pkg/query/selection_test.go +++ b/pkg/query/selection_test.go @@ -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() {