Exemplo n.º 1
0
 def test_rm_operation_removes_custom_operation(self):
     add_operation('custom', lambda: "Ha-ha!")
     try:
         self.assertEqual(jsonLogic({'custom': []}), "Ha-ha!")
     finally:
         rm_operation('custom')
     self.assertRaisesRegex(ValueError, "Unrecognized operation", jsonLogic,
                            {'custom': []})
Exemplo n.º 2
0
 def test_rm_operation_updates_exposed_operations_list(self):
     haha = lambda *args: "Ha-ha!"
     add_operation('custom', haha)
     try:
         self.assertIn('custom', operations)
         self.assertIs(operations['custom'], haha)
     finally:
         rm_operation('custom')
     self.assertNotIn('custom', operations)
Exemplo n.º 3
0
 def test_rm_operation_restores_overridden_operation(self):
     self.assertEqual(jsonLogic({'+': [2, 3]}), 5)
     add_operation('+', lambda *args: "Ha-ha!")
     try:
         self.assertEqual(jsonLogic({'+': [2, 3]}), "Ha-ha!")
     finally:
         rm_operation('+')
         self.assertEqual(jsonLogic({'+': [2, 3]}), 5)
         self.assertNotEqual(jsonLogic({'+': [2, 3]}), "Ha-ha!")
Exemplo n.º 4
0
 def test_rm_operation_restores_overridden_operation_in_exposed_list(self):
     haha = lambda *args: "Ha-ha!"
     add_operation('+', haha)
     try:
         self.assertIn('+', operations)
         self.assertIs(operations['+'], haha)
     finally:
         rm_operation('+')
     self.assertIn('+', operations)
     self.assertIsNot(operations['+'], haha)
Exemplo n.º 5
0
 def test_add_operation_overrides_existing_exposed_operations(self):
     haha = lambda *args: "Ha-ha!"
     self.assertIn('+', operations)
     self.assertIsNot(operations['+'], haha)
     add_operation('+', haha)
     try:
         self.assertIn('+', operations)
         self.assertIs(operations['+'], haha)
     finally:
         rm_operation('+')
Exemplo n.º 6
0
 def test_depth_first_rule_still_applies_to_custom_operators(self):
     add_operation('sum_up', lambda *args: sum(args))
     try:
         self.assertEqual(
             jsonLogic({'sum_up': [{
                 '-': [5, 3]
             }, {
                 '*': [2, 3]
             }]}), 8)
     finally:
         rm_operation('sum_up')
Exemplo n.º 7
0
 def test_add_operation_with_packages_fails_midway(self):
     add_operation('datetime', datetime)
     try:
         self.assertRaisesRegex(ValueError,
                                "datetime\.wrong_property(?!\.now)",
                                jsonLogic,
                                {'datetime.wrong_property.now': []})
         self.assertRaisesRegex(ValueError,
                                "datetime\.datetime.wrong_method",
                                jsonLogic,
                                {'datetime.datetime.wrong_method': []})
     finally:
         rm_operation('datetime')
Exemplo n.º 8
0
    def test_add_operation_with_simple_method(self):
        def add_to_five(*args):
            return sum((5, ) + args)

        self.assertRaisesRegex(ValueError, "Unrecognized operation", jsonLogic,
                               {'add_to_five': [3]})
        add_operation('add_to_five', add_to_five)
        try:
            self.assertEqual(jsonLogic({'add_to_five': 1}), 6)
            self.assertEqual(jsonLogic({'add_to_five': [3]}), 8)
            self.assertEqual(jsonLogic({'add_to_five': [3, 2]}), 10)
        finally:
            rm_operation('add_to_five')
Exemplo n.º 9
0
 def test_add_operation_with_packages(self):
     self.assertRaisesRegex(ValueError, "Unrecognized operation.*datetime",
                            jsonLogic, {'datetime.datetime.now': []})
     add_operation('datetime', datetime)
     try:
         # .now()
         start = datetime.datetime.now()
         returned_value = jsonLogic({'datetime.datetime.now': []})
         self.assertIsInstance(returned_value, datetime.datetime)
         self.assertTrue(start <= returned_value <= datetime.datetime.now())
         # .date()
         returned_value = jsonLogic({'datetime.date': [2018, 1, 1]})
         self.assertIsInstance(returned_value, datetime.date)
         self.assertEqual(returned_value, datetime.date(2018, 1, 1))
     finally:
         rm_operation('datetime')
Exemplo n.º 10
0
 def test_add_operation_does_not_override_other_operation_types(self):
     test_data = (('if', [True, "yes",
                          "no"], {}, "yes"), ('map', [[1, 2, 3], {
                              '*': [{
                                  'var': ''
                              }, 2]
                          }], {}, [2, 4, 6]), ('var', 'a', {
                              'a': "Ta-da!"
                          }, "Ta-da!"))
     for operation, arguments, data, expected_result in test_data:
         add_operation(operation, lambda *args: "Ha-ha!")
         try:
             result = jsonLogic({operation: arguments}, data)
             self.assertEqual(result, expected_result, operation)
             self.assertNotEqual(result, "Ha-ha!", operation)
         finally:
             rm_operation(operation)
Exemplo n.º 11
0
 def test_add_operation_may_override_common_operations(self):
     add_operation('+', lambda *args: "Ha-ha!")
     try:
         self.assertEqual(jsonLogic({'+': [1, 2]}), "Ha-ha!")
     finally:
         rm_operation('+')