예제 #1
0
 def test_ast_expression_left_operator_right_arithmeticcomparison_datetime(
         self):
     past_date = ast.DatetimeExpression(context,
                                        datetime.datetime(2016, 10, 15))
     now = ast.DatetimeExpression(context, datetime.datetime.now())
     self.assertExpressionTests('ge', past_date, now, False)
     self.assertExpressionTests('gt', past_date, now, False)
     self.assertExpressionTests('le', past_date, now, True)
     self.assertExpressionTests('lt', past_date, now, True)
예제 #2
0
    def test_ast_expression_datetime_attributes(self):
        timestamp = datetime.datetime(2019,
                                      9,
                                      11,
                                      20,
                                      46,
                                      57,
                                      506406,
                                      tzinfo=dateutil.tz.UTC)
        symbol = ast.DatetimeExpression(context, timestamp)

        attributes = {
            'day': 11,
            'hour': 20,
            'microsecond': 506406,
            'millisecond': decimal.Decimal('506.406'),
            'minute': 46,
            'month': 9,
            'second': 57,
            'weekday': timestamp.strftime('%A'),
            'year': 2019,
            'zone_name': 'UTC',
        }
        for attribute_name, value in attributes.items():
            expression = ast.GetAttributeExpression(context, symbol,
                                                    attribute_name)
            self.assertEqual(expression.evaluate(None), value,
                             "attribute {} failed".format(attribute_name))
예제 #3
0
    {'test': {
        'one': 1.0,
        'two': 2.0
    }})
# literal expressions which should evaluate to false
falseish = (ast.ArrayExpression(context, tuple()),
            ast.BooleanExpression(context,
                                  False), ast.FloatExpression(context, 0.0),
            ast.NullExpression(context), ast.StringExpression(context, ''))
# literal expressions which should evaluate to true
trueish = (ast.ArrayExpression(context, tuple(
    (ast.NullExpression(context), ))),
           ast.ArrayExpression(context,
                               tuple((ast.FloatExpression(context, 1.0), ))),
           ast.BooleanExpression(context, True),
           ast.DatetimeExpression(context, datetime.datetime.now()),
           ast.FloatExpression(context, float('-inf')),
           ast.FloatExpression(context,
                               -1.0), ast.FloatExpression(context, 1.0),
           ast.FloatExpression(context, float('inf')),
           ast.StringExpression(context, 'non-empty'))


class UnknownType(object):
    pass


class LiteralExpressionTests(unittest.TestCase):
    context = engine.Context()

    def assertLiteralTests(self, ExpressionClass, false_value, *true_values):