Commits

Olemis Lang committed dce4213

GViz QL : Tests for scalar functions ... [ok]

  • Participants
  • Parent commits 9e38f31
  • Branches gviz_ql

Comments (0)

Files changed (3)

File trac-dev/gviz/tracgviz/gvizql.py

   def handle_funcargs(self, funcnm, _, seq, __):
     funcnm = funcnm[1]
     seq = seq[1]
+    if not isinstance(seq, self.Sequence):
+      seq = self.Sequence([seq])
     try:
       f = self.scalar_funcs[funcnm]
-      return_type = r.return_type
+      return_type = f.return_type
     except KeyError:
       raise LookupError('Unknown scalar function %s' % (funcnm,))
     except AttributeError:
   # Internal methods
 
   def _function_label(self, funcnm, seq):
-    return '%s(%s)' % (funcnm, ', '.join(x['label'] for x in seq))
+    return '%s(%s)' % (funcnm, 
+        ', '.join(x['schema'][0] if not x['is_schema_callable'] \
+            else x['schema'](lambda colnm: (colnm, None))[0] \
+            for x in seq))
 
   def _eval_expr(self, f, seq, deterministic=True):
     r"""Functor computing evaluation function for a compound statement.

File trac-dev/gviz/tracgviz/scalar.py

 __all__ = 'year month day hour minute second millisecond quarter dayOfWeek ' \
     'now dateDiff toDate upper lower'.split()
 
-from datetime import datetime, date
+from datetime import datetime, date, time
 
 def check_datatype(value, types, expected_msg=None):
-  if not isinstance(value, types):
+  if value is None:
+    pass
+  elif not isinstance(value, types):
     msg = '%s expected but received %s' % (expected_msg, value) \
         if expected_msg else 'Unexpected value %s' % (value,)
     raise ValueError(msg)
 
 def year(value):
   check_datatype(value, (datetime, date), 'Date value')
-  return value.date
+  return value.year if value is not None else None
 
 year.return_type = 'number'
 
 def month(value):
   check_datatype(value, (datetime, date), 'Date value')
-  return value.month - 1
+  return value.month - 1 if value is not None else None
 
 month.return_type = 'number'
 
 def day(value):
   check_datatype(value, (datetime, date), 'Date value')
-  return value.day
+  return value.day if value is not None else None
 
 day.return_type = 'number'
 
 def hour(value):
   check_datatype(value, (time, date), 'Time value')
-  return value.hour
+  return value.hour if value is not None else None
 
 hour.return_type = 'number'
 
 def minute(value):
   check_datatype(value, (time, date), 'Time value')
-  return value.minute
+  return value.minute if value is not None else None
 
 minute.return_type = 'number'
 
 def second(value):
   check_datatype(value, (time, date), 'Time value')
-  return value.second
+  return value.second if value is not None else None
 
 second.return_type = 'number'
 
 def millisecond(value):
   check_datatype(value, (time, date), 'Time value')
-  return value.microsecond / 1000
+  return value.microsecond / 1000 if value is not None else None
 
 millisecond.return_type = 'number'
 
 def quarter(value):
-  return 1 + month(value) / 3
+  return 1 + month(value) / 3 if value is not None else None
 
 quarter.return_type = 'number'
 
 def dayOfWeek(value):
   check_datatype(value, (datetime, date), 'Date value')
-  return value.isoweekday() % 7 + 1
+  return value.isoweekday() % 7 + 1 if value is not None else None
 
 dayOfWeek.return_type = 'number'
 
 def dateDiff(until, since):
   check_datatype(since, (datetime, date), 'Date value')
   check_datatype(until, (datetime, date), 'Date value')
-  return (until - since).days
+  return (until - since).days if until is not None and since is not None \
+      else None
 
 dateDiff.return_type = 'number'
 
-def now(value):
+def now():
   return datetime.now()
 
 now.return_type = 'datetime'
 
 def toDate(value):
   check_datatype(value, (date, datetime, int, long), 'Date value')
-  if isinstance(value, date):
+  if value is None:
+    return None
+  if isinstance(value, datetime):
+    return value.date()
+  elif isinstance(value, date):
     return value
-  elif isinstance(value, datetime):
-    return value.date()
   elif isinstance(value, (int, long)):
     return date.fromtimestamp(value / 1000)
   raise ValueError('Date value expected but received %s' % (value,))
 
 def upper(value):
   check_datatype(value, basestring, 'String value')
-  return value.upper()
+  return value.upper() if value is not None else None
 
 upper.return_type = 'string'
 
 def lower(value):
   check_datatype(value, basestring, 'String value')
-  return value.lower()
+  return value.lower() if value is not None else None
 
 lower.return_type = 'string'
 

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

       * Parsing
       *****
       Unexpected token group.
-      """
+      """,
+    'Parsing SELECT (scalar functions)' : r"""
+      >>> parse("select year(hireDate), month(hireDate), day(hireDate), "
+      ...       "year(seniorityStartTime), month(seniorityStartTime), "
+      ...       "day(seniorityStartTime), hour(seniorityStartTime), "
+      ...       "minute(seniorityStartTime), second(seniorityStartTime)", 
+      ...       'cols')
+      ...
+      *****
+      * Tokens
+      *****
+      Token.Keyword.Reserved select
+      Token.Name.Function year
+      Token.Punctuation (
+      Token.Name.Variable hireDate
+      Token.Punctuation )
+      Token.Punctuation ,
+      Token.Name.Function month
+      Token.Punctuation (
+      Token.Name.Variable hireDate
+      Token.Punctuation )
+      Token.Punctuation ,
+      Token.Name.Function day
+      Token.Punctuation (
+      Token.Name.Variable hireDate
+      Token.Punctuation )
+      Token.Punctuation ,
+      Token.Name.Function year
+      Token.Punctuation (
+      Token.Name.Variable seniorityStartTime
+      Token.Punctuation )
+      Token.Punctuation ,
+      Token.Name.Function month
+      Token.Punctuation (
+      Token.Name.Variable seniorityStartTime
+      Token.Punctuation )
+      Token.Punctuation ,
+      Token.Name.Function day
+      Token.Punctuation (
+      Token.Name.Variable seniorityStartTime
+      Token.Punctuation )
+      Token.Punctuation ,
+      Token.Name.Function hour
+      Token.Punctuation (
+      Token.Name.Variable seniorityStartTime
+      Token.Punctuation )
+      Token.Punctuation ,
+      Token.Name.Function minute
+      Token.Punctuation (
+      Token.Name.Variable seniorityStartTime
+      Token.Punctuation )
+      Token.Punctuation ,
+      Token.Name.Function second
+      Token.Punctuation (
+      Token.Name.Variable seniorityStartTime
+      Token.Punctuation )
+      *****
+      * Parsing
+      *****
+      [u'year(hireDate)', u'month(hireDate)', u'day(hireDate)', u'year(seniorityStartTime)', u'month(seniorityStartTime)', u'day(seniorityStartTime)', u'hour(seniorityStartTime)', u'minute(seniorityStartTime)', u'second(seniorityStartTime)']
+      *****
+      * Result
+      *****
+      = Columns =
+      year(hireDate) number
+      month(hireDate) number
+      day(hireDate) number
+      year(seniorityStartTime) number
+      month(seniorityStartTime) number
+      day(seniorityStartTime) number
+      hour(seniorityStartTime) number
+      minute(seniorityStartTime) number
+      second(seniorityStartTime) number
+      = Row =
+         year(hireDate) = 2005
+         month(hireDate) = 2
+         day(hireDate) = 19
+         year(seniorityStartTime) = 2007
+         month(seniorityStartTime) = 11
+         day(seniorityStartTime) = 2
+         hour(seniorityStartTime) = 15
+         minute(seniorityStartTime) = 56
+         second(seniorityStartTime) = 0
+      = Row =
+         year(hireDate) = 2006
+         month(hireDate) = 3
+         day(hireDate) = 19
+         year(seniorityStartTime) = None
+         month(seniorityStartTime) = None
+         day(seniorityStartTime) = None
+         hour(seniorityStartTime) = None
+         minute(seniorityStartTime) = None
+         second(seniorityStartTime) = None
+      = Row =
+         year(hireDate) = 2005
+         month(hireDate) = 9
+         day(hireDate) = 10
+         year(seniorityStartTime) = None
+         month(seniorityStartTime) = None
+         day(seniorityStartTime) = None
+         hour(seniorityStartTime) = None
+         minute(seniorityStartTime) = None
+         second(seniorityStartTime) = None
+      = Row =
+         year(hireDate) = 2002
+         month(hireDate) = 9
+         day(hireDate) = 10
+         year(seniorityStartTime) = 2005
+         month(seniorityStartTime) = 2
+         day(seniorityStartTime) = 9
+         hour(seniorityStartTime) = 12
+         minute(seniorityStartTime) = 30
+         second(seniorityStartTime) = 0
+      = Row =
+         year(hireDate) = 2004
+         month(hireDate) = 8
+         day(hireDate) = 8
+         year(seniorityStartTime) = None
+         month(seniorityStartTime) = None
+         day(seniorityStartTime) = None
+         hour(seniorityStartTime) = None
+         minute(seniorityStartTime) = None
+         second(seniorityStartTime) = None
+      = Row =
+         year(hireDate) = 2005
+         month(hireDate) = 0
+         day(hireDate) = 10
+         year(seniorityStartTime) = 2007
+         month(seniorityStartTime) = 11
+         day(seniorityStartTime) = 30
+         hour(seniorityStartTime) = 14
+         minute(seniorityStartTime) = 40
+         second(seniorityStartTime) = 0
+
+      >>> parse("select quarter(hireDate), dayOfWeek(hireDate), "
+      ...       "quarter(seniorityStartTime), dayOfWeek(seniorityStartTime)",
+      ...       'cols')
+      *****
+      * Tokens
+      *****
+      Token.Keyword.Reserved select
+      Token.Name.Function quarter
+      Token.Punctuation (
+      Token.Name.Variable hireDate
+      Token.Punctuation )
+      Token.Punctuation ,
+      Token.Name.Function dayOfWeek
+      Token.Punctuation (
+      Token.Name.Variable hireDate
+      Token.Punctuation )
+      Token.Punctuation ,
+      Token.Name.Function quarter
+      Token.Punctuation (
+      Token.Name.Variable seniorityStartTime
+      Token.Punctuation )
+      Token.Punctuation ,
+      Token.Name.Function dayOfWeek
+      Token.Punctuation (
+      Token.Name.Variable seniorityStartTime
+      Token.Punctuation )
+      *****
+      * Parsing
+      *****
+      [u'quarter(hireDate)', u'dayOfWeek(hireDate)', u'quarter(seniorityStartTime)', u'dayOfWeek(seniorityStartTime)']
+      *****
+      * Result
+      *****
+      = Columns =
+      quarter(hireDate) number
+      dayOfWeek(hireDate) number
+      quarter(seniorityStartTime) number
+      dayOfWeek(seniorityStartTime) number
+      = Row =
+         quarter(hireDate) = 1
+         dayOfWeek(hireDate) = 7
+         quarter(seniorityStartTime) = 4
+         dayOfWeek(seniorityStartTime) = 1
+      = Row =
+         quarter(hireDate) = 2
+         dayOfWeek(hireDate) = 4
+         quarter(seniorityStartTime) = None
+         dayOfWeek(seniorityStartTime) = None
+      = Row =
+         quarter(hireDate) = 4
+         dayOfWeek(hireDate) = 2
+         quarter(seniorityStartTime) = None
+         dayOfWeek(seniorityStartTime) = None
+      = Row =
+         quarter(hireDate) = 4
+         dayOfWeek(hireDate) = 5
+         quarter(seniorityStartTime) = 1
+         dayOfWeek(seniorityStartTime) = 4
+      = Row =
+         quarter(hireDate) = 3
+         dayOfWeek(hireDate) = 4
+         quarter(seniorityStartTime) = None
+         dayOfWeek(seniorityStartTime) = None
+      = Row =
+         quarter(hireDate) = 1
+         dayOfWeek(hireDate) = 2
+         quarter(seniorityStartTime) = 4
+         dayOfWeek(seniorityStartTime) = 1
+
+      >>> parse("select toDate(seniorityStartTime)",
+      ...       'cols')
+      ...
+      *****
+      * Tokens
+      *****
+      Token.Keyword.Reserved select
+      Token.Name.Function toDate
+      Token.Punctuation (
+      Token.Name.Variable seniorityStartTime
+      Token.Punctuation )
+      *****
+      * Parsing
+      *****
+      [u'toDate(seniorityStartTime)']
+      *****
+      * Result
+      *****
+      = Columns =
+      toDate(seniorityStartTime) date
+      = Row =
+         toDate(seniorityStartTime) = 2007-12-02
+      = Row =
+         toDate(seniorityStartTime) = None
+      = Row =
+         toDate(seniorityStartTime) = None
+      = Row =
+         toDate(seniorityStartTime) = 2005-03-09
+      = Row =
+         toDate(seniorityStartTime) = None
+      = Row =
+         toDate(seniorityStartTime) = 2007-12-30
+
+      >>> parse("select lower(name) + '@example.com', upper(dept)",
+      ...       'cols')
+      ...
+      *****
+      * Tokens
+      *****
+      Token.Keyword.Reserved select
+      Token.Name.Function lower
+      Token.Punctuation (
+      Token.Name.Variable name
+      Token.Punctuation )
+      Token.Operator.Arithmetic +
+      Token.Literal.String.Single '@example.com'
+      Token.Punctuation ,
+      Token.Name.Function upper
+      Token.Punctuation (
+      Token.Name.Variable dept
+      Token.Punctuation )
+      *****
+      * Parsing
+      *****
+      [u"lower(name) + '@example.com'", u'upper(dept)']
+      *****
+      * Result
+      *****
+      = Columns =
+      lower(name) + '@example.com' number
+      upper(dept) string
+      = Row =
+         lower(name) + '@example.com' = john@example.com
+         upper(dept) = ENG
+      = Row =
+         lower(name) + '@example.com' = dave@example.com
+         upper(dept) = ENG
+      = Row =
+         lower(name) + '@example.com' = sally@example.com
+         upper(dept) = ENG
+      = Row =
+         lower(name) + '@example.com' = ben@example.com
+         upper(dept) = SALES
+      = Row =
+         lower(name) + '@example.com' = dana@example.com
+         upper(dept) = SALES
+      = Row =
+         lower(name) + '@example.com' = mike@example.com
+         upper(dept) = MARKETING
+
+      """,
   }