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_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_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_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 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_feature(): strategies = [RemoteAddress(parameters={"IPs": IP_LIST}), UserWithId(parameters={"userIds": EMAIL_LIST})] yield Feature("My Feature", True, strategies)
def strategy(): yield RemoteAddress(parameters={"IPs": IP_LIST})
def test_init_with_bad_range(): BAD_IP_LIST = IP_LIST + ",ga/rbage" strategy = RemoteAddress(parameters={"IPs": BAD_IP_LIST}) assert len(strategy.parsed_provisioning) == 4