Skip to content

Commit e377491

Browse files
author
Pijush Chakraborty
committed
Adding script for Python Bug Bash
1 parent 6f9591b commit e377491

File tree

2 files changed

+70
-20
lines changed

2 files changed

+70
-20
lines changed

bug-bash-test.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import firebase_admin
2+
from firebase_admin import credentials
3+
from firebase_admin import remote_config
4+
import asyncio
5+
6+
# [Bug Bash 101] Credentials
7+
# Load creds for authentication - Update the json key from the one downloaded from the console.
8+
cred = credentials.Certificate('private-api-key.json.json')
9+
default_app = firebase_admin.initialize_app(cred)
10+
11+
# [Bug Bash 101] Default Config
12+
# Create default template for initializing ServerTemplate
13+
# For bug bash, update the default config to any config that you want to initialize
14+
# the app with. The configs will be cached and might get updated during evaluation of the template.
15+
default_config = {
16+
'default_key_str': 'default_val_str',
17+
'default_key_number': 123
18+
}
19+
20+
# Create initial template
21+
template = remote_config.init_server_template(app=default_app, default_config=default_config)
22+
23+
# Load the template from the backend
24+
asyncio.run(template.load())
25+
26+
# [Bug Bash 101] Custom Signals
27+
# Evaluate template - pass custom signals
28+
# Update the custom signals being passed in evaluate to test how variations of the
29+
# signals cause changes to the config evaluation.
30+
config = template.evaluate(
31+
# Update custom vars
32+
{
33+
'custom_key_str': 'custom_val_str',
34+
'custom_key_int': 123
35+
}
36+
)
37+
38+
# [Bug Bash 101] Verify Evaluation
39+
# Update the following print statements to verify if config is being created properly.
40+
# Print default config values
41+
print('[Default Config] default_key_str: ', config.get_string('default_key_str'))
42+
print('[Default Config] default_key_number: ', config.get_int('default_key_number'))
43+
44+
# Verify evaluated config
45+
print('[Evluated Config] Config values:', config.get_float('ssrc_test_key_float'))
46+
print('[Evluated Config] Config values:', config.get_boolean('ssrc_test_key_bool'))
47+
48+
# Verify value and source for configs
49+
print('Config Value:', config.get_value('ssrc_test_key').as_string(),
50+
'Value Source:', config.get_value('ssrc_test_key').get_source())

firebase_admin/remote_config.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ def get_float(self, key):
197197
return self.get_value(key).as_float()
198198

199199
def get_value(self, key):
200-
if self._config_values[key]:
200+
if key in self._config_values:
201201
return self._config_values[key]
202202
return _Value('static')
203203

@@ -454,10 +454,10 @@ def evaluate_custom_signal_condition(self, custom_signal_condition,
454454
Returns:
455455
True if the condition is met, False otherwise.
456456
"""
457-
custom_signal_operator = custom_signal_condition.get('custom_signal_operator') or {}
458-
custom_signal_key = custom_signal_condition.get('custom_signal_key') or {}
457+
custom_signal_operator = custom_signal_condition.get('customSignalOperator') or {}
458+
custom_signal_key = custom_signal_condition.get('customSignalKey') or {}
459459
target_custom_signal_values = (
460-
custom_signal_condition.get('target_custom_signal_values') or {})
460+
custom_signal_condition.get('targetCustomSignalValues') or {})
461461

462462
if not all([custom_signal_operator, custom_signal_key, target_custom_signal_values]):
463463
logger.warning("Missing operator, key, or target values for custom signal condition.")
@@ -471,71 +471,71 @@ def evaluate_custom_signal_condition(self, custom_signal_condition,
471471
logger.warning("Custom signal value not found in context: %s", custom_signal_key)
472472
return False
473473

474-
if custom_signal_operator == CustomSignalOperator.STRING_CONTAINS:
474+
if custom_signal_operator == CustomSignalOperator.STRING_CONTAINS.value:
475475
return self._compare_strings(target_custom_signal_values,
476476
actual_custom_signal_value,
477477
lambda target, actual: target in actual)
478-
if custom_signal_operator == CustomSignalOperator.STRING_DOES_NOT_CONTAIN:
478+
if custom_signal_operator == CustomSignalOperator.STRING_DOES_NOT_CONTAIN.value:
479479
return not self._compare_strings(target_custom_signal_values,
480480
actual_custom_signal_value,
481481
lambda target, actual: target in actual)
482-
if custom_signal_operator == CustomSignalOperator.STRING_EXACTLY_MATCHES:
482+
if (custom_signal_operator == CustomSignalOperator.STRING_EXACTLY_MATCHES.value):
483483
return self._compare_strings(target_custom_signal_values,
484484
actual_custom_signal_value,
485485
lambda target, actual: target.strip() == actual.strip())
486-
if custom_signal_operator == CustomSignalOperator.STRING_CONTAINS_REGEX:
486+
if custom_signal_operator == CustomSignalOperator.STRING_CONTAINS_REGEX.value:
487487
return self._compare_strings(target_custom_signal_values,
488488
actual_custom_signal_value,
489489
re.search)
490490

491491
# For numeric operators only one target value is allowed.
492-
if custom_signal_operator == CustomSignalOperator.NUMERIC_LESS_THAN:
492+
if custom_signal_operator == CustomSignalOperator.NUMERIC_LESS_THAN.value:
493493
return self._compare_numbers(target_custom_signal_values[0],
494494
actual_custom_signal_value,
495495
lambda r: r < 0)
496-
if custom_signal_operator == CustomSignalOperator.NUMERIC_LESS_EQUAL:
496+
if custom_signal_operator == CustomSignalOperator.NUMERIC_LESS_EQUAL.value:
497497
return self._compare_numbers(target_custom_signal_values[0],
498498
actual_custom_signal_value,
499499
lambda r: r <= 0)
500-
if custom_signal_operator == CustomSignalOperator.NUMERIC_EQUAL:
500+
if custom_signal_operator == CustomSignalOperator.NUMERIC_EQUAL.value:
501501
return self._compare_numbers(target_custom_signal_values[0],
502502
actual_custom_signal_value,
503503
lambda r: r == 0)
504-
if custom_signal_operator == CustomSignalOperator.NUMERIC_NOT_EQUAL:
504+
if custom_signal_operator == CustomSignalOperator.NUMERIC_NOT_EQUAL.value:
505505
return self._compare_numbers(target_custom_signal_values[0],
506506
actual_custom_signal_value,
507507
lambda r: r != 0)
508-
if custom_signal_operator == CustomSignalOperator.NUMERIC_GREATER_THAN:
508+
if custom_signal_operator == CustomSignalOperator.NUMERIC_GREATER_THAN.value:
509509
return self._compare_numbers(target_custom_signal_values[0],
510510
actual_custom_signal_value,
511511
lambda r: r > 0)
512-
if custom_signal_operator == CustomSignalOperator.NUMERIC_GREATER_EQUAL:
512+
if custom_signal_operator == CustomSignalOperator.NUMERIC_GREATER_EQUAL.value:
513513
return self._compare_numbers(target_custom_signal_values[0],
514514
actual_custom_signal_value,
515515
lambda r: r >= 0)
516516

517517
# For semantic operators only one target value is allowed.
518-
if custom_signal_operator == CustomSignalOperator.SEMANTIC_VERSION_LESS_THAN:
518+
if custom_signal_operator == CustomSignalOperator.SEMANTIC_VERSION_LESS_THAN.value:
519519
return self._compare_semantic_versions(target_custom_signal_values[0],
520520
actual_custom_signal_value,
521521
lambda r: r < 0)
522-
if custom_signal_operator == CustomSignalOperator.SEMANTIC_VERSION_LESS_EQUAL:
522+
if custom_signal_operator == CustomSignalOperator.SEMANTIC_VERSION_LESS_EQUAL.value:
523523
return self._compare_semantic_versions(target_custom_signal_values[0],
524524
actual_custom_signal_value,
525525
lambda r: r <= 0)
526-
if custom_signal_operator == CustomSignalOperator.SEMANTIC_VERSION_EQUAL:
526+
if custom_signal_operator == CustomSignalOperator.SEMANTIC_VERSION_EQUAL.value:
527527
return self._compare_semantic_versions(target_custom_signal_values[0],
528528
actual_custom_signal_value,
529529
lambda r: r == 0)
530-
if custom_signal_operator == CustomSignalOperator.SEMANTIC_VERSION_NOT_EQUAL:
530+
if custom_signal_operator == CustomSignalOperator.SEMANTIC_VERSION_NOT_EQUAL.value:
531531
return self._compare_semantic_versions(target_custom_signal_values[0],
532532
actual_custom_signal_value,
533533
lambda r: r != 0)
534-
if custom_signal_operator == CustomSignalOperator.SEMANTIC_VERSION_GREATER_THAN:
534+
if custom_signal_operator == CustomSignalOperator.SEMANTIC_VERSION_GREATER_THAN.value:
535535
return self._compare_semantic_versions(target_custom_signal_values[0],
536536
actual_custom_signal_value,
537537
lambda r: r > 0)
538-
if custom_signal_operator == CustomSignalOperator.SEMANTIC_VERSION_GREATER_EQUAL:
538+
if custom_signal_operator == CustomSignalOperator.SEMANTIC_VERSION_GREATER_EQUAL.value:
539539
return self._compare_semantic_versions(target_custom_signal_values[0],
540540
actual_custom_signal_value,
541541
lambda r: r >= 0)

0 commit comments

Comments
 (0)