def test_star(self, throttler): def is_crash(throttler, thing): return isinstance(thing, dict) # Asserts that the is_crash condition function got a crash as an # argument. rule = Rule('test', '*', is_crash, ACCEPT) assert rule.match(throttler, {'ProductName': 'test'}) is True # Asserts that the is_crash condition function did not get a crash as # an argument. rule = Rule('test', 'ProductName', is_crash, ACCEPT) assert rule.match(throttler, {'ProductName': 'Test'}) is False
def test_condition_value(self): rule = Rule('test', 'ProductName', 'test', 100) assert rule.match({'ProductName': 'test'}) is True assert rule.match({'ProductName': 'testabc'}) is False rule = Rule('test', 'Version', 1.0, 100) assert rule.match({'Version': 1.0}) is True assert rule.match({'Version': 2.0}) is False assert rule.match({'ProductName': 'testabc'}) is False
def test_percentage(self, throttler, randommock): # Overrwrite the rule set for something we need throttler.rule_set = [ Rule('test', 'ProductName', lambda throttler, x: x == 'test', (50, ACCEPT, REJECT)) ] with randommock(0.45): # Below the percentage line, so ACCEPT! assert throttler.throttle({'ProductName': 'test'}) == (ACCEPT, 'test', 50) with randommock(0.55): # Above the percentage line, so DEFER! assert throttler.throttle({'ProductName': 'test'}) == (REJECT, 'test', 50)
def test_percentage(self, randommock): throttler = Throttler(ConfigManager.from_dict({})) # Overrwrite the rule set for something we need throttler.rule_set = [ Rule('test', 'ProductName', 'test', 50) ] with randommock(0.45): # Below the percentage line, so ACCEPT! assert throttler.throttle({'ProductName': 'test'}) == (ACCEPT, 'test', 50) with randommock(0.55): # Above the percentage line, so DEFER! assert throttler.throttle({'ProductName': 'test'}) == (DEFER, 'test', 50)
def test_star(self): def is_crash(thing): return isinstance(thing, dict) # Asserts that the is_crash condition function got a crash as an # argument. rule = Rule('test', '*', is_crash, 100) assert rule.match({'ProductName': 'test'}) is True # Asserts that the is_crash condition function did not get a crash as # an argument. rule = Rule('test', 'ProductName', is_crash, 100) assert rule.match({'ProductName': 'Test'}) is False
def test_condition_regexp(self): rule = Rule('test', 'ProductName', re.compile('^test'), 100) assert rule.match({'ProductName': 'testabc'}) is True assert rule.match({'ProductName': 'abc'}) is False
def test_condition_function(self): rule = Rule('test', '*', lambda x: True, 100) assert rule.match({'ProductName': 'test'}) is True rule = Rule('test', '*', lambda x: False, 100) assert rule.match({'ProductName': 'test'}) is False
def test_invalid_rule_name(self): with pytest.raises(ValueError): Rule('o m g!', '*', lambda x: True, 100)
def test_condition_function(self, throttler): rule = Rule('test', '*', lambda throttler, x: True, ACCEPT) assert rule.match(throttler, {'ProductName': 'test'}) is True rule = Rule('test', '*', lambda throttler, x: False, ACCEPT) assert rule.match(throttler, {'ProductName': 'test'}) is False
def test_invalid_rule_name(self): with pytest.raises(ValueError): Rule('o m g!', '*', lambda throttler, x: True, ACCEPT)