test/test_client.rb in prefab-cloud-ruby-0.24.6 vs test/test_client.rb in prefab-cloud-ruby-1.0.0
- old
+ new
@@ -22,38 +22,37 @@
values: [
PrefabProto::ConditionalValue.new(value: DEFAULT_VALUE_CONFIG)
]
)
- def setup
- @client = new_client
- end
-
def test_get
_, err = capture_io do
- assert_equal 'default', @client.get('does.not.exist', 'default')
- assert_equal 'test sample value', @client.get('sample')
- assert_equal 123, @client.get('sample_int')
+ client = new_client
+ assert_equal 'default', client.get('does.not.exist', 'default')
+ assert_equal 'test sample value', client.get('sample')
+ assert_equal 123, client.get('sample_int')
end
assert_equal '', err
end
def test_get_with_default
+ client = new_client
# A `false` value is not replaced with the default
- assert_equal false, @client.get('false_value', 'red')
+ assert_equal false, client.get('false_value', 'red')
# A falsy value is not replaced with the default
- assert_equal 0, @client.get('zero_value', 'red')
+ assert_equal 0, client.get('zero_value', 'red')
# A missing value returns the default
- assert_equal 'buckets', @client.get('missing_value', 'buckets')
+ assert_equal 'buckets', client.get('missing_value', 'buckets')
end
def test_get_with_missing_default
+ client = new_client
# it raises by default
err = assert_raises(Prefab::Errors::MissingDefaultError) do
- assert_nil @client.get('missing_value')
+ assert_nil client.get('missing_value')
end
assert_match(/No value found for key/, err.message)
assert_match(/on_no_default/, err.message)
@@ -61,51 +60,85 @@
client = new_client(on_no_default: Prefab::Options::ON_NO_DEFAULT::RETURN_NIL)
assert_nil client.get('missing_value')
end
def test_enabled
- assert_equal false, @client.enabled?('does_not_exist')
- assert_equal true, @client.enabled?('enabled_flag')
- assert_equal false, @client.enabled?('disabled_flag')
- assert_equal false, @client.enabled?('flag_with_a_value')
+ client = new_client
+ assert_equal false, client.enabled?('does_not_exist')
+ assert_equal true, client.enabled?('enabled_flag')
+ assert_equal false, client.enabled?('disabled_flag')
+ assert_equal false, client.enabled?('flag_with_a_value')
end
def test_ff_enabled_with_user_key_match
- assert_equal_context_and_jit(false, :enabled?, 'user_key_match', { user: { key: 'jimmy' } })
- assert_equal_context_and_jit(true, :enabled?, 'user_key_match', { user: { key: 'abc123' } })
- assert_equal_context_and_jit(true, :enabled?, 'user_key_match', { user: { key: 'xyz987' } })
+ client = new_client
+
+ ctx = { user: { key: 'jimmy' } }
+ assert_equal false, client.enabled?('user_key_match', ctx)
+ assert_equal false, Prefab::Context.with_context(ctx) { client.enabled?('user_key_match') }
+
+ ctx = { user: { key: 'abc123' } }
+ assert_equal true, client.enabled?('user_key_match', ctx)
+ assert_equal true, Prefab::Context.with_context(ctx) { client.enabled?('user_key_match') }
+
+ ctx = { user: { key: 'xyz987' } }
+ assert_equal true, client.enabled?('user_key_match', ctx)
+ assert_equal true, Prefab::Context.with_context(ctx) { client.enabled?('user_key_match') }
end
+ # NOTE: these are all `false` because we're doing a enabled? on a FF with string variants
+ # see test_ff_get_with_context for the raw value tests
def test_ff_enabled_with_context
- assert_equal_context_and_jit(false, :enabled?, 'just_my_domain', user: { domain: 'gmail.com' })
- assert_equal_context_and_jit(false, :enabled?, 'just_my_domain', user: { domain: 'prefab.cloud' })
- assert_equal_context_and_jit(false, :enabled?, 'just_my_domain', user: { domain: 'example.com' })
+ client = new_client
+
+ ctx = { user: { domain: 'gmail.com' } }
+ assert_equal false, client.enabled?('just_my_domain', ctx)
+ assert_equal false, Prefab::Context.with_context(ctx) { client.enabled?('just_my_domain') }
+
+ ctx = { user: { domain: 'prefab.cloud' } }
+ assert_equal false, client.enabled?('just_my_domain', ctx)
+ assert_equal false, Prefab::Context.with_context(ctx) { client.enabled?('just_my_domain') }
+
+ ctx = { user: { domain: 'example.com' } }
+ assert_equal false, client.enabled?('just_my_domain', ctx)
+ assert_equal false, Prefab::Context.with_context(ctx) { client.enabled?('just_my_domain') }
end
def test_ff_get_with_context
- assert_nil @client.get('just_my_domain', 'abc123', user: { domain: 'gmail.com' })
- assert_equal 'DEFAULT', @client.get('just_my_domain', 'abc123', { user: { domain: 'gmail.com' } }, 'DEFAULT')
+ client = new_client
- assert_equal_context_and_jit('new-version', :get, 'just_my_domain', { user: { domain: 'prefab.cloud' } })
- assert_equal_context_and_jit('new-version', :get, 'just_my_domain', { user: { domain: 'example.com' } })
+ ctx = { user: { domain: 'gmail.com' } }
+ assert_equal 'DEFAULT', client.get('just_my_domain', 'DEFAULT', ctx)
+ assert_equal 'DEFAULT', Prefab::Context.with_context(ctx) { client.get('just_my_domain', 'DEFAULT') }
+
+ ctx = { user: { domain: 'prefab.cloud' } }
+ assert_equal 'new-version', client.get('just_my_domain', 'DEFAULT', ctx)
+ assert_equal 'new-version', Prefab::Context.with_context(ctx) { client.get('just_my_domain', 'DEFAULT') }
+
+ ctx = { user: { domain: 'example.com' } }
+ assert_equal 'new-version', client.get('just_my_domain', 'DEFAULT', ctx)
+ assert_equal 'new-version', Prefab::Context.with_context(ctx) { client.get('just_my_domain', 'DEFAULT') }
end
def test_deprecated_no_dot_notation_ff_enabled_with_jit_context
+ client = new_client
# with no lookup key
- assert_equal false, @client.enabled?('deprecated_no_dot_notation', { domain: 'gmail.com' })
- assert_equal true, @client.enabled?('deprecated_no_dot_notation', { domain: 'prefab.cloud' })
- assert_equal true, @client.enabled?('deprecated_no_dot_notation', { domain: 'example.com' })
+ assert_equal false, client.enabled?('deprecated_no_dot_notation', { domain: 'gmail.com' })
+ assert_equal true, client.enabled?('deprecated_no_dot_notation', { domain: 'prefab.cloud' })
+ assert_equal true, client.enabled?('deprecated_no_dot_notation', { domain: 'example.com' })
- # with a lookup key
- assert_equal false, @client.enabled?('deprecated_no_dot_notation', 'some-lookup-key', { domain: 'gmail.com' })
- assert_equal true, @client.enabled?('deprecated_no_dot_notation', 'some-lookup-key', { domain: 'prefab.cloud' })
- assert_equal true, @client.enabled?('deprecated_no_dot_notation', 'some-lookup-key', { domain: 'example.com' })
+ assert_stderr [
+ "[DEPRECATION] Prefab contexts should be a hash with a key of the context name and a value of a hash.",
+ "[DEPRECATION] Prefab contexts should be a hash with a key of the context name and a value of a hash.",
+ "[DEPRECATION] Prefab contexts should be a hash with a key of the context name and a value of a hash."
+ ]
end
def test_getting_feature_flag_value
- assert_equal false, @client.enabled?('flag_with_a_value')
- assert_equal 'all-features', @client.get('flag_with_a_value')
+ client = new_client
+ assert_equal false, client.enabled?('flag_with_a_value')
+ assert_equal 'all-features', client.get('flag_with_a_value')
end
def test_initialization_with_an_options_object
options_hash = {
namespace: 'test-namespace',
@@ -132,48 +165,37 @@
def test_evaluation_summary_aggregator
fake_api_key = '123-development-yourapikey-SDK'
# it is nil by default
- assert_nil Prefab::Client.new(api_key: fake_api_key).evaluation_summary_aggregator
+ assert_nil new_client(api_key: fake_api_key).evaluation_summary_aggregator
# it is nil when local_only even if collect_max_evaluation_summaries is true
- assert_nil Prefab::Client.new(prefab_datasources: LOCAL_ONLY,
- collect_evaluation_summaries: true).evaluation_summary_aggregator
+ assert_nil new_client(prefab_datasources: LOCAL_ONLY,
+ collect_evaluation_summaries: true, ).evaluation_summary_aggregator
# it is nil when collect_max_evaluation_summaries is false
- assert_nil Prefab::Client.new(api_key: fake_api_key,
+ assert_nil new_client(api_key: fake_api_key,
+ prefab_datasources: :all,
collect_evaluation_summaries: false).evaluation_summary_aggregator
# it is not nil when collect_max_evaluation_summaries is true and the datasource is not local_only
assert_equal Prefab::EvaluationSummaryAggregator,
- Prefab::Client.new(api_key: fake_api_key,
- collect_evaluation_summaries: true).evaluation_summary_aggregator.class
+ new_client(api_key: fake_api_key,
+ prefab_datasources: :all,
+ collect_evaluation_summaries: true).evaluation_summary_aggregator.class
+
+ assert_logged [
+ "WARN 2023-08-09 15:18:12 -0400: cloud.prefab.client No success loading checkpoints"
+ ]
end
def test_get_with_basic_value
- config = PrefabProto::Config.new(
- id: 123,
- key: KEY,
- config_type: PrefabProto::ConfigType::CONFIG,
- rows: [
- DEFAULT_ROW,
- PrefabProto::ConfigRow.new(
- project_env_id: PROJECT_ENV_ID,
- values: [
- PrefabProto::ConditionalValue.new(
- criteria: [PrefabProto::Criterion.new(operator: PrefabProto::Criterion::CriterionOperator::ALWAYS_TRUE)],
- value: DESIRED_VALUE_CONFIG
- )
- ]
- )
- ]
- )
+ config = basic_value_config
+ client = new_client(config: config, project_env_id: PROJECT_ENV_ID, collect_evaluation_summaries: true,
+ context_upload_mode: :periodic_example, allow_telemetry_in_local_mode: true)
- client = new_client(config: config, project_env_id: PROJECT_ENV_ID, collect_evaluation_summaries: :force,
- collect_example_contexts: :force)
-
assert_equal DESIRED_VALUE, client.get(config.key, IRRELEVANT, 'user' => { 'key' => 99 })
assert_summary client, {
[KEY, :CONFIG] => {
{
@@ -188,10 +210,35 @@
}
assert_example_contexts client, [Prefab::Context.new({ user: { 'key' => 99 } })]
end
+ def test_get_with_basic_value_with_context
+ config = basic_value_config
+ client = new_client(config: config, project_env_id: PROJECT_ENV_ID, collect_evaluation_summaries: true,
+ context_upload_mode: :periodic_example, allow_telemetry_in_local_mode: true)
+
+ client.with_context('user' => { 'key' => 99 }) do
+ assert_equal DESIRED_VALUE, client.get(config.key)
+ end
+
+ assert_summary client, {
+ [KEY, :CONFIG] => {
+ {
+ config_id: config.id,
+ config_row_index: 1,
+ selected_value: DESIRED_VALUE_CONFIG,
+ conditional_value_index: 0,
+ weighted_value_index: nil,
+ selected_index: nil
+ } => 1
+ }
+ }
+
+ assert_example_contexts client, [Prefab::Context.new({ user: { 'key' => 99 } })]
+ end
+
def test_get_with_weighted_values
config = PrefabProto::Config.new(
id: 123,
key: KEY,
config_type: PrefabProto::ConfigType::CONFIG,
@@ -208,12 +255,12 @@
]
)
]
)
- client = new_client(config: config, project_env_id: PROJECT_ENV_ID, collect_evaluation_summaries: :force,
- collect_example_contexts: :force)
+ client = new_client(config: config, project_env_id: PROJECT_ENV_ID, collect_evaluation_summaries: true,
+ context_upload_mode: :periodic_example, allow_telemetry_in_local_mode: true)
2.times do
assert_equal 'abc', client.get(config.key, IRRELEVANT, 'user' => { 'key' => '1' })
end
@@ -308,11 +355,11 @@
)
]
)
client = new_client(config: [config, segment_config], project_env_id: PROJECT_ENV_ID,
- collect_evaluation_summaries: :force, collect_example_contexts: :force)
+ collect_evaluation_summaries: true, context_upload_mode: :periodic_example, allow_telemetry_in_local_mode: true)
assert_equal DEFAULT_VALUE, client.get(config.key)
assert_equal DEFAULT_VALUE,
client.get(config.key, IRRELEVANT, user: { key: 'abc', email: 'example@prefab.cloud' })
assert_equal DESIRED_VALUE, client.get(config.key, IRRELEVANT, user: { key: 'def', email: 'example@hotmail.com' })
@@ -354,23 +401,35 @@
)
]
)
client = new_client(config: config, project_env_id: PROJECT_ENV_ID,
- collect_evaluation_summaries: :force)
+ collect_evaluation_summaries: true, allow_telemetry_in_local_mode: true)
assert_equal :DEBUG, client.get(config.key, IRRELEVANT)
# nothing is summarized for log levels
assert_summary client, {}
end
private
- def assert_equal_context_and_jit(expected, method, key, context)
- assert_equal expected, @client.send(method, key, context)
-
- Prefab::Context.with_context(context) do
- assert_equal expected, @client.send(method, key)
- end
+ def basic_value_config
+ PrefabProto::Config.new(
+ id: 123,
+ key: KEY,
+ config_type: PrefabProto::ConfigType::CONFIG,
+ rows: [
+ DEFAULT_ROW,
+ PrefabProto::ConfigRow.new(
+ project_env_id: PROJECT_ENV_ID,
+ values: [
+ PrefabProto::ConditionalValue.new(
+ criteria: [PrefabProto::Criterion.new(operator: PrefabProto::Criterion::CriterionOperator::ALWAYS_TRUE)],
+ value: DESIRED_VALUE_CONFIG
+ )
+ ]
+ )
+ ]
+ )
end
end