Skip to content

Commit

Permalink
fix: Fractional evaluation (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
augustinasrce authored Aug 29, 2024
1 parent cb33998 commit 5bb34f2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def prepare_evaluation_context(evaluation_context)
return nil unless evaluation_context

fields = evaluation_context.fields
fields["targetingKey"] = fields.delete(:targeting_key)
fields["targetingKey"] = fields.delete("targeting_key")
Google::Protobuf::Struct.from_hash(fields)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,26 @@
)
end
end

context "EvaluationContext transformation" do
subject { client.send(:prepare_evaluation_context, evaluation_context) }
let(:evaluation_context) { OpenFeature::SDK::EvaluationContext.new(**fields) }

context "No context" do
let(:evaluation_context) { nil }

it do
expect(subject).to be nil
end
end

context "targeting_key transformation to targetingKey" do
let(:fields) { {targeting_key: "example"} }
let(:expected) { Google::Protobuf::Struct.from_hash({targetingKey: "example"}.transform_keys(&:to_s)) }

it do
expect(subject).to eq(expected)
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,17 @@

it do
fetch_value_with_targeting_key = ->(targeting_key) do
client.fetch_boolean_value(
client.fetch_string_value(
flag_key: "color-palette-experiment",
default_value: "#b91c1c",
evaluation_context: OpenFeature::SDK::EvaluationContext.new(targeting_key: targeting_key)
)
end

initial_value = fetch_value_with_targeting_key.call("123")
(0..2).to_a.each do # try with 1000
expect(fetch_value_with_targeting_key.call("123")).to eq(initial_value)
end
expect(fetch_value_with_targeting_key.call("1234")).to eq("#b91c1c")
expect(fetch_value_with_targeting_key.call("qwe")).to eq("#0284c7")
expect(fetch_value_with_targeting_key.call("abcd")).to eq("#16a34a")
expect(fetch_value_with_targeting_key.call("rfv")).to eq("#b91c1c")
end
end

Expand Down

0 comments on commit 5bb34f2

Please sign in to comment.