def test_get_all(self): """ get_all should return a set of the content of all operators """ logic = And(And("hey", Or("ho", "lets"), "go"), Or("lets", "go")) self.assertEqual(logic.get_all(), set(("hey", "ho", "lets", "go")))
def test_get_relevant_selectors_ignored_empty_selectors(self): """ empty selectors shouldn't be updated with new requests """ rules = Or('user:10/15s', 'apikey:1/m') limit = Limit(None, rules, user="******", apikey=None, key="upload") selectors = limit.get_relevant_selectors() self.assertEqual(selectors['upload:user:vova']["allowed_requests"], 10) self.assertEqual(selectors['upload:user:vova']["requests_span"], 15) self.assertEqual(len(selectors), 1)
def test_get_relevant_selectors_merges_duplicates(self): """ different rates on the same selector (or empty selector when non specified) should be merged by choosing the longest request span and highest allowed requests from all rules for the same selector """ limit = Limit(None, And('5/m', Or('10/5s', '1/m')), key="vova") selectors = limit.get_relevant_selectors() self.assertEqual(selectors['vova']["allowed_requests"], 10) self.assertEqual(selectors['vova']["requests_span"], 60) self.assertEqual(len(selectors), 1)
def test_get_relevant_selectors_multiple_selectors(self): """ a more typical complete example with multiple selectors """ rules = Or('user:10/15s', 'apikey:1/m') limit = Limit(None, rules, user="******", apikey="my_api", key="k") selectors = limit.get_relevant_selectors() self.assertEqual(selectors['k:user:vova']["allowed_requests"], 10) self.assertEqual(selectors['k:user:vova']["requests_span"], 15) self.assertEqual(selectors['k:apikey:my_api']["allowed_requests"], 1) self.assertEqual(selectors['k:apikey:my_api']["requests_span"], 60) self.assertEqual(len(selectors), 2)
def test_things_aint_called_when_not_needed(self): yield self.assertLogic(And(True, False, Exception), False) yield self.assertLogic(Or(True, Exception), True)
def test_we_need_to_go_deeper(self): yield self.assertLogic( And(And(True, Or(True, False), True), Or(True, False)), True)
def test_combinations(self): yield self.assertLogic(Or(And(True, True), Or(True, False)), True) yield self.assertLogic(Or(And(False, True), Or(False, False)), False) yield self.assertLogic(And(And(False, True), Or(False, False)), False) yield self.assertLogic(And(And(True, True), Or(True, False)), True) yield self.assertLogic( Or(Or(Or(Or(Or(False, False, False), False), False), False), True), True) yield self.assertLogic( Or(Or(Or(Or(Or(False, False, True), False), False), False), False), True)
def test_or_some_true_returns_true(self): yield self.assertLogic(Or(True), True) yield self.assertLogic(Or(False, False, True), True) yield self.assertLogic(Or(True, False, True), True) yield self.assertLogic(Or(True, False, False), True)
def test_or_all_false_returns_false(self): yield self.assertLogic(Or(False), False) yield self.assertLogic(Or(False, False), False)