|
| 1 | +require "spec_helper" |
| 2 | + |
| 3 | +RSpec.describe OpenFeature::SDK::EvaluationContextBuilder do |
| 4 | + let(:builder) { described_class.new } |
| 5 | + let(:api_context) { OpenFeature::SDK::EvaluationContext.new("targeting_key" => "api", "api" => "key") } |
| 6 | + let(:client_context) { OpenFeature::SDK::EvaluationContext.new("targeting_key" => "client", "client" => "key") } |
| 7 | + let(:invocation_context) { OpenFeature::SDK::EvaluationContext.new("targeting_key" => "invocation", "invocation" => "key") } |
| 8 | + |
| 9 | + describe "#call" do |
| 10 | + context "when no available contexts" do |
| 11 | + it "returns nil" do |
| 12 | + result = builder.call(api_context: nil, client_context: nil, invocation_context: nil) |
| 13 | + |
| 14 | + expect(result).to be_nil |
| 15 | + end |
| 16 | + end |
| 17 | + |
| 18 | + context "when only api context" do |
| 19 | + it "returns api context" do |
| 20 | + result = builder.call(api_context:, client_context: nil, invocation_context: nil) |
| 21 | + |
| 22 | + expect(result).to eq(OpenFeature::SDK::EvaluationContext.new("targeting_key" => "api", "api" => "key")) |
| 23 | + end |
| 24 | + end |
| 25 | + |
| 26 | + context "when only client context" do |
| 27 | + it "returns client context" do |
| 28 | + result = builder.call(api_context: nil, client_context:, invocation_context: nil) |
| 29 | + |
| 30 | + expect(result).to eq(OpenFeature::SDK::EvaluationContext.new("targeting_key" => "client", "client" => "key")) |
| 31 | + end |
| 32 | + end |
| 33 | + |
| 34 | + context "when only invocation context" do |
| 35 | + it "returns invocation context" do |
| 36 | + result = builder.call(api_context: nil, client_context: nil, invocation_context:) |
| 37 | + |
| 38 | + expect(result).to eq(OpenFeature::SDK::EvaluationContext.new("targeting_key" => "invocation", "invocation" => "key")) |
| 39 | + end |
| 40 | + end |
| 41 | + |
| 42 | + context "when api and client contexts" do |
| 43 | + it "returns merged context" do |
| 44 | + result = builder.call(api_context:, client_context:, invocation_context: nil) |
| 45 | + |
| 46 | + expect(result).to eq(OpenFeature::SDK::EvaluationContext.new("targeting_key" => "client", "api" => "key", "client" => "key")) |
| 47 | + end |
| 48 | + end |
| 49 | + |
| 50 | + context "when client and invocation contexts" do |
| 51 | + it "returns merged context" do |
| 52 | + result = builder.call(api_context: nil, client_context:, invocation_context:) |
| 53 | + |
| 54 | + expect(result).to eq(OpenFeature::SDK::EvaluationContext.new("targeting_key" => "invocation", "client" => "key", "invocation" => "key")) |
| 55 | + end |
| 56 | + end |
| 57 | + |
| 58 | + context "when global and invocation contexts" do |
| 59 | + it "returns merged context" do |
| 60 | + result = builder.call(api_context:, client_context: nil, invocation_context:) |
| 61 | + |
| 62 | + expect(result).to eq(OpenFeature::SDK::EvaluationContext.new("targeting_key" => "invocation", "api" => "key", "invocation" => "key")) |
| 63 | + end |
| 64 | + end |
| 65 | + |
| 66 | + context "when all contexts" do |
| 67 | + it "returns merged context" do |
| 68 | + result = builder.call(api_context:, client_context:, invocation_context:) |
| 69 | + |
| 70 | + expect(result).to eq(OpenFeature::SDK::EvaluationContext.new("targeting_key" => "invocation", "api" => "key", "client" => "key", "invocation" => "key")) |
| 71 | + end |
| 72 | + end |
| 73 | + end |
| 74 | +end |
0 commit comments