Olemis Lang avatar Olemis Lang committed 61393f2

GViz QL : Tests for arithmetic operators ... [ok]

Comments (0)

Files changed (2)

trac-dev/gviz/tracgviz/gvizql.py

         'contains' : 'contains',
       }.get(op)
     if f is not None:
-      f = getattr(operator, f)
+      _f = getattr(operator, f)
+      if op in '+-*/':
+        f = lambda a,b: None if a is None or b is None else _f(a,b)
+      else:
+        f = _f
     else:
       from re import match
       from fnmatch import fnmatch
 
       def div(a, b):
+        if a is None or b is None:
+          return None
         try:
-          return operator.div(a, b)
+          return operator.div(float(a), b)
         except ZeroDivisionError:
           return None
 
       def match_like(a, b):
+        if a is None or b is None:
+          return None
         pattern = b.replace('*', '[*]').replace('?', '[?]') \
             .replace('%', '*').replace('_', '?')
         return fnmatch(a, pattern)
         is_schema_callable=False
       )
 
-  handle_sum = handle_sub = handle_mul = handle_div = handle_strcmp = \
+  handle_sum = handle_sub = handle_mult = handle_div = handle_strcmp = \
       handle_cmp = handle_orexp = handle_boolexp = _handle_binary_op
 
   def handle_not(self, _, expr):
       else:
         return True, lambda *args: f(*values)
     else:
-      return True, lambda *args: f(*(v(*args) if i in expand else v \
-              for i,v in enumerate(values))),
+      return True, lambda *args: f(*[v(*args) if i in expand else v \
+              for i,v in enumerate(values)]),
 
 #------------------------------------------------------
 #   Global Testing

trac-dev/gviz/tracgviz/testing/test_gvizql.py

          upper(dept) = MARKETING
 
       """,
+    'Parsing SELECT (artihmetic operators)' : r"""
+      >>> parse("select empSalary - empTax", 'cols')
+      *****
+      * Tokens
+      *****
+      Token.Keyword.Reserved select
+      Token.Name.Variable empSalary
+      Token.Operator.Arithmetic -
+      Token.Name.Variable empTax
+      *****
+      * Parsing
+      *****
+      [u'empSalary - empTax']
+      *****
+      * Result
+      *****
+      = Columns =
+      empSalary - empTax number
+      <BLANKLINE>
+      GVizRuntimeError  :  Unknown column empSalary.
+
+
+      >>> parse("select 2 * (max(empSalary) / max(empTax))", 'cols')
+      *****
+      * Tokens
+      *****
+      Token.Keyword.Reserved select
+      Token.Literal.Number.Integer 2
+      Token.Operator.Arithmetic *
+      Token.Punctuation (
+      Token.Name.Builtin max
+      Token.Punctuation (
+      Token.Name.Variable empSalary
+      Token.Punctuation )
+      Token.Operator.Arithmetic /
+      Token.Name.Builtin max
+      Token.Punctuation (
+      Token.Name.Variable empTax
+      Token.Punctuation )
+      Token.Punctuation )
+      *****
+      * Parsing
+      *****
+      [u'2 * (max(empSalary) / max(empTax))']
+      *****
+      * Result
+      *****
+      = Columns =
+      2 * (max(empSalary) / max(empTax)) number
+      <BLANKLINE>
+      NotImplementedError  :  Feature not supported ... yet
+
+      >>> parse("select 12 * (year(seniorityStartTime) - year(hireDate)) + "
+      ...       "month(seniorityStartTime) - month(hireDate)", 'cols')
+      ...
+      *****
+      * Tokens
+      *****
+      Token.Keyword.Reserved select
+      Token.Literal.Number.Integer 12
+      Token.Operator.Arithmetic *
+      Token.Punctuation (
+      Token.Name.Function year
+      Token.Punctuation (
+      Token.Name.Variable seniorityStartTime
+      Token.Punctuation )
+      Token.Operator.Arithmetic -
+      Token.Name.Function year
+      Token.Punctuation (
+      Token.Name.Variable hireDate
+      Token.Punctuation )
+      Token.Punctuation )
+      Token.Operator.Arithmetic +
+      Token.Name.Function month
+      Token.Punctuation (
+      Token.Name.Variable seniorityStartTime
+      Token.Punctuation )
+      Token.Operator.Arithmetic -
+      Token.Name.Function month
+      Token.Punctuation (
+      Token.Name.Variable hireDate
+      Token.Punctuation )
+      *****
+      * Parsing
+      *****
+      [u'12 * (year(seniorityStartTime) - year(hireDate)) + month(seniorityStartTime) - month(hireDate)']
+      *****
+      * Result
+      *****
+      = Columns =
+      12 * (year(seniorityStartTime) - year(hireDate)) + month(seniorityStartTime) - month(hireDate) number
+      = Row =
+         12 * (year(seniorityStartTime) - year(hireDate)) + month(seniorityStartTime) - month(hireDate) = 33
+      = Row =
+         12 * (year(seniorityStartTime) - year(hireDate)) + month(seniorityStartTime) - month(hireDate) = None
+      = Row =
+         12 * (year(seniorityStartTime) - year(hireDate)) + month(seniorityStartTime) - month(hireDate) = None
+      = Row =
+         12 * (year(seniorityStartTime) - year(hireDate)) + month(seniorityStartTime) - month(hireDate) = 29
+      = Row =
+         12 * (year(seniorityStartTime) - year(hireDate)) + month(seniorityStartTime) - month(hireDate) = None
+      = Row =
+         12 * (year(seniorityStartTime) - year(hireDate)) + month(seniorityStartTime) - month(hireDate) = 35
+
+
+      >>> parse("select salary / ( age -24 )", 'cols')
+      *****
+      * Tokens
+      *****
+      Token.Keyword.Reserved select
+      Token.Name.Variable salary
+      Token.Operator.Arithmetic /
+      Token.Punctuation (
+      Token.Name.Variable age
+      Token.Operator.Arithmetic -
+      Token.Literal.Number.Integer 24
+      Token.Punctuation )
+      *****
+      * Parsing
+      *****
+      [u'salary / (age - 24)']
+      *****
+      * Result
+      *****
+      = Columns =
+      salary / (age - 24) number
+      = Row =
+         salary / (age - 24) = 90.9090909091
+      = Row =
+         salary / (age - 24) = 166.666666667
+      = Row =
+         salary / (age - 24) = 100.0
+      = Row =
+         salary / (age - 24) = 50.0
+      = Row =
+         salary / (age - 24) = 350.0
+      = Row =
+         salary / (age - 24) = None
+
+      """,
   }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.