def test_create_feature_true(): strategies = [ RemoteAddress(parameters={"IPs": IP_LIST}), UserWithId(parameters={"userIds": EMAIL_LIST}) ] my_feature = Feature("My Feature", True, strategies) CONTEXT["remoteAddress"] = "69.208.0.1" assert my_feature.is_enabled(CONTEXT) assert my_feature.yes_count == 1 my_feature.reset_stats() assert my_feature.yes_count == 0
def test_aggregate_and_send_metrics(): responses.add(responses.POST, FULL_METRICS_URL, json={}, status=200) last_run_datetime = datetime.datetime.now() - datetime.timedelta( seconds=30) strategies = [RemoteAddress(parameters={"IPs": IP_LIST}), Default()] my_feature1 = Feature("My Feature1", True, strategies) my_feature1.yes_count = 1 my_feature1.no_count = 1 my_feature2 = Feature("My Feature2", True, strategies) my_feature2.yes_count = 2 my_feature2.no_count = 2 features = {"My Feature1": my_feature1, "My Feature 2": my_feature2} aggregate_and_send_metrics(URL, APP_NAME, INSTANCE_ID, CUSTOM_HEADERS, features, last_run_datetime) assert len(responses.calls) == 1 request = json.loads(responses.calls[0].request.body) assert len(request['bucket']["toggles"].keys()) == 2 assert request['bucket']["toggles"]["My Feature1"]["yes"] == 1 assert request['bucket']["toggles"]["My Feature1"]["no"] == 1
def test_aggregate_and_send_metrics(): responses.add(responses.POST, FULL_METRICS_URL, json={}, status=200) start_time = datetime.now(pytz.utc) - timedelta(seconds=60) cache = FileCache("TestCache") cache[METRIC_LAST_SENT_TIME] = start_time strategies = [RemoteAddress(parameters={"IPs": IP_LIST}), Default()] my_feature1 = Feature("My Feature1", True, strategies) my_feature1.yes_count = 1 my_feature1.no_count = 1 my_feature2 = Feature("My Feature2", True, strategies) my_feature2.yes_count = 2 my_feature2.no_count = 2 features = {"My Feature1": my_feature1, "My Feature 2": my_feature2} aggregate_and_send_metrics(URL, APP_NAME, INSTANCE_ID, CUSTOM_HEADERS, CUSTOM_OPTIONS, features, cache) assert len(responses.calls) == 1 request = json.loads(responses.calls[0].request.body) assert len(request['bucket']["toggles"].keys()) == 2 assert request['bucket']["toggles"]["My Feature1"]["yes"] == 1 assert request['bucket']["toggles"]["My Feature1"]["no"] == 1 assert cache[METRIC_LAST_SENT_TIME] > start_time
def test_create_feature_false(): strategies = [ RemoteAddress(parameters={"IPs": IP_LIST}), UserWithId(parameters={"userIds": EMAIL_LIST}) ] my_feature = Feature("My Feature", True, strategies) CONTEXT["remoteAddress"] = "1.208.0.1" CONTEXT["userId"] = "*****@*****.**" assert not my_feature.is_enabled(CONTEXT) assert my_feature.no_count == 1 my_feature.reset_stats() assert my_feature.no_count == 0
def _create_feature(provisioning: dict, strategy_mapping: dict) -> Feature: if "strategies" in provisioning.keys(): parsed_strategies = _create_strategies(provisioning, strategy_mapping) else: parsed_strategies = [] return Feature(name=provisioning["name"], enabled=provisioning["enabled"], strategies=parsed_strategies)
def test_no_metrics(): responses.add(responses.POST, FULL_METRICS_URL, json={}, status=200) start_time = datetime.now(timezone.utc) - timedelta(seconds=60) cache = FileCache("TestCache") cache.set(METRIC_LAST_SENT_TIME, start_time) strategies = [RemoteAddress(parameters={"IPs": IP_LIST}), Default()] my_feature1 = Feature("My Feature1", True, strategies) my_feature1.yes_count = 0 my_feature1.no_count = 0 features = {"My Feature1": my_feature1} aggregate_and_send_metrics(URL, APP_NAME, INSTANCE_ID, CUSTOM_HEADERS, CUSTOM_OPTIONS, features, cache) assert len(responses.calls) == 0
def test_create_feature_exception(test_feature): strategies = [{}, UserWithId(parameters={"userIds": EMAIL_LIST})] my_feature = Feature("My Feature", True, strategies) CONTEXT["remoteAddress"] = "69.208.0.1" assert not my_feature.is_enabled(CONTEXT)
def test_feature_variants(): strategies = [Default()] variants = Variants(VARIANTS, "My Feature") yield Feature("My Feature", True, strategies, variants)
def test_feature(): strategies = [RemoteAddress(parameters={"IPs": IP_LIST}), UserWithId(parameters={"userIds": EMAIL_LIST})] yield Feature("My Feature", True, strategies)