Skip to content

Commit

Permalink
fix default variant filter logic, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tyiuhc committed May 16, 2024
1 parent 33f4205 commit 18feb53
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/experiment/remote/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,10 @@ def should_retry_fetch?(err)

def filter_default_variants(variants)
variants.each do |key, value|
default = value&.metadata&.default
deployed = value&.metadata&.deployed
default ||= false
deployed ||= true
default = value&.metadata&.fetch('default', nil)
deployed = value&.metadata&.fetch('deployed', nil)
default = false if default.nil?
deployed = true if deployed.nil?
variants.delete(key) if default || !deployed
end
variants
Expand Down
66 changes: 66 additions & 0 deletions spec/experiment/remote/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,5 +128,71 @@ def self.test_fetch_async(response, test_user, variant_name, debug, expected_sta
end
end
end

describe '#fetch_v2' do
def self.test_fetch_v2(response, test_user, variant_name, debug, expected_state, expected_payload)
it "fetch v2 sync success with response #{response}, user #{test_user.user_id}, debug #{debug}" do
stub_request(:post, SERVER_URL)
.to_return(status: 200, body: response)
client = RemoteEvaluationClient.new(API_KEY, RemoteEvaluationConfig.new(debug: debug))
expected_variant = Variant.new(expected_state, expected_payload)
variants = client.fetch_v2(test_user)
expect(variants.key?(variant_name)).to be_truthy
expect(variants.fetch(variant_name)).to eq(expected_variant)
end
end

test_fetch_v2 response_with_key, test_user, variant_name, false, 'on', 'payload'
test_fetch_v2 response_with_value, test_user_with_properties, variant_name, false, 'on', 'payload'
test_fetch_v2 response_with_int_payload, test_user, variant_name, true, 'off', 123
test_fetch_v2 response_with_boolean_payload, test_user_with_properties, variant_name, false, 'on', false
test_fetch_v2 response_with_list_payload, test_user, variant_name, false, 'on', %w[payload1 payload2]
test_fetch_v2 response_with_hash_payload, test_user_with_properties, variant_name, false, 'off', { 'nested' => 'nested payload' }
test_fetch_v2 response_without_payload, test_user, variant_name, false, 'on', nil
test_fetch_v2 response_with_value_without_payload, test_user, variant_name, false, 'on', nil
end

describe '#fetch_async_v2' do
before do
allow(Thread).to receive(:new).and_yield
end

def self.test_fetch_async_v2(response, test_user, variant_name, debug, expected_state, expected_payload)
it "fetch async v2 success with response #{response}, user #{test_user.user_id}, debug #{debug}" do
stub_request(:post, SERVER_URL)
.to_return(status: 200, body: response)
client = RemoteEvaluationClient.new(API_KEY, RemoteEvaluationConfig.new(debug: debug))
expected_variant = Variant.new(expected_state, expected_payload)
variants = client.fetch_async_v2(test_user) do |user, block_variants|
expect(user).to equal(test_user)
expect(block_variants.fetch(variant_name)).to eq(expected_variant)
end
expect(variants.key?(variant_name)).to be_truthy
expect(variants.fetch(variant_name)).to eq(expected_variant)
end
end

test_fetch_async_v2 response_with_key, test_user, variant_name, false, 'on', 'payload'
test_fetch_async_v2 response_with_value, test_user_with_properties, variant_name, false, 'on', 'payload'
test_fetch_async_v2 response_with_int_payload, test_user, variant_name, true, 'off', 123
test_fetch_async_v2 response_with_boolean_payload, test_user_with_properties, variant_name, false, 'on', false
test_fetch_async_v2 response_with_list_payload, test_user, variant_name, false, 'on', %w[payload1 payload2]
test_fetch_async_v2 response_with_hash_payload, test_user_with_properties, variant_name, false, 'off', { 'nested' => 'nested payload' }
test_fetch_async_v2 response_without_payload, test_user, variant_name, false, 'on', nil
test_fetch_async_v2 response_with_value_without_payload, test_user, variant_name, false, 'on', nil
end

describe '#filter_default_variants' do
it 'filters out default and non-deployed variants' do
variants = {
'variant_1' => Variant.new('value_1', 'payload_1', { 'default' => true, 'deployed' => true }),
'variant_2' => Variant.new('value_2', 'payload_2', { 'default' => false, 'deployed' => false }),
'variant_3' => Variant.new('value_3', 'payload_3', { 'default' => false, 'deployed' => true })
}
client = RemoteEvaluationClient.new(API_KEY)
filtered_variants = client.send(:filter_default_variants, variants)
expect(filtered_variants.keys).to contain_exactly('variant_3')
end
end
end
end

0 comments on commit 18feb53

Please sign in to comment.