commit-gnue
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gnue] r7286 - trunk/gnue-common/tests


From: johannes
Subject: [gnue] r7286 - trunk/gnue-common/tests
Date: Fri, 1 Apr 2005 08:14:35 -0600 (CST)

Author: johannes
Date: 2005-04-01 08:14:34 -0600 (Fri, 01 Apr 2005)
New Revision: 7286

Added:
   trunk/gnue-common/tests/datasources_GConditions.py
Modified:
   trunk/gnue-common/tests/
   trunk/gnue-common/tests/apps_checktype.py
   trunk/gnue-common/tests/formatting_masks.py
   trunk/gnue-common/tests/utils_TextUtils.py
Log:
Added unittest for new GConditions



Property changes on: trunk/gnue-common/tests
___________________________________________________________________
Name: svn:ignore
   + *.pyc



Property changes on: trunk/gnue-common/tests/apps_checktype.py
___________________________________________________________________
Name: svn:keywords
   + Id

Added: trunk/gnue-common/tests/datasources_GConditions.py
===================================================================
--- trunk/gnue-common/tests/datasources_GConditions.py  2005-04-01 14:14:10 UTC 
(rev 7285)
+++ trunk/gnue-common/tests/datasources_GConditions.py  2005-04-01 14:14:34 UTC 
(rev 7286)
@@ -0,0 +1,294 @@
+# GNU Enterprise Common Library - Unit Testing - GConditions
+#
+# Copyright 2001-2005 Free Software Foundation
+#
+# This file is part of GNU Enterprise
+#
+# GNU Enterprise is free software; you can redistribute it
+# and/or modify it under the terms of the GNU General Public
+# License as published by the Free Software Foundation; either
+# version 2, or (at your option) any later version.
+#
+# GNU Enterprise is distributed in the hope that it will be
+# useful, but WITHOUT ANY WARRANTY; without even the implied
+# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+# PURPOSE. See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public
+# License along with program; see the file COPYING. If not,
+# write to the Free Software Foundation, Inc., 59 Temple Place
+# - Suite 330, Boston, MA 02111-1307, USA.
+#
+# $Id$
+
+import unittest
+import sets
+import mx.DateTime
+
+from gnue.common.datasources.GConditions import *
+
+# =============================================================================
+# TestCase for codition construction
+# =============================================================================
+
+class ConstructionTestCase (unittest.TestCase):
+  """
+  This class implements a test case for all tests related to condition
+  construction (including combination).
+  """
+
+  # ---------------------------------------------------------------------------
+  # test case setup 
+  # ---------------------------------------------------------------------------
+
+  def setUp (self):
+    """
+    Create predefined condition dictionary and prefix sequence
+    """
+
+    self.condDict = {'foo': 'bar', 'frob': 20}
+    self.prefix   = ['and', ['eq', ['Field', 'foo'], ['Const', 'bar']],
+                            ['eq', ['Field', 'frob'], ['Const', 20]]]
+    self.bigList  = ['and', ['or', ['eq', ['Field', 'foo'], ['Field', 'bar']],
+                                   ['eq', ['Field', 'baz'], ['Const', 20]]],
+                            ['ne', ['Field', 'barbaz'], ['Const', 21]],
+                            ['not', ['null', ['Field', 'empty']]]]
+
+
+  # ---------------------------------------------------------------------------
+  # construction from dictionaries
+  # ---------------------------------------------------------------------------
+
+  def testFromDict (self):
+    """
+    Test condition construction from dictionaries
+    """
+
+    # Empty dictionary
+    cond = buildConditionFromDict ({})
+
+    # AND/EQ (default)
+    cond = buildConditionFromDict (self.condDict)
+    self.assertEqual (cond.evaluate (self.condDict), True)
+
+    # other logic and comparison
+    cond = buildConditionFromDict (self.condDict, GCeq, GCor)
+    self.assertEqual (cond.evaluate (self.condDict), True)
+
+
+  # ---------------------------------------------------------------------------
+  # construction from prefix notation sequences
+  # ---------------------------------------------------------------------------
+
+  def testFromPrefix (self):
+    """
+    Test condition construction from prefix notation sequences 
+    """
+
+    # Empty dictionary
+    cond = buildConditionFromPrefix ([])
+
+    # with condition
+    cond = buildConditionFromPrefix (self.prefix)
+    self.assertEqual (self.prefix, cond.prefixNotation ())
+
+    # bigger condition
+    cond = GCondition (prefixList = self.bigList)
+    self.assertEqual (self.bigList, cond.prefixNotation ())
+
+    # arugment count error
+    mcond = ['eq', ['Field', 'foo']]
+    self.assertRaises (ArgumentCountError, buildConditionFromPrefix, mcond)
+
+
+  # ---------------------------------------------------------------------------
+  # mixed construction 
+  # ---------------------------------------------------------------------------
+
+  def testFromBoth (self):
+    """
+    Test construction from both dictionary- and prefix-based variants
+    """
+
+    # Construction from empty structures
+    cond = buildCondition (None)
+    self.assertEqual (cond, None)
+
+    c1 = buildCondition ([])
+    c2 = buildCondition ({})
+    self.assertEqual (c1.prefixNotation (), c2.prefixNotation ())
+
+    # Construction from dictionary
+    cond = buildCondition (self.condDict)
+    self.assertEqual (cond.evaluate (self.condDict), True)
+
+    # Construction from prefixList
+    cond = buildCondition (self.prefix)
+    self.assertEqual (cond.evaluate (self.condDict), True)
+
+
+  # ---------------------------------------------------------------------------
+  # test combination of conditions
+  # ---------------------------------------------------------------------------
+
+  def testCombination (self):
+    """
+    Test the combination of conditions
+    """
+
+    # Create a condition matching the predefined condition dictionary
+    c1 = buildCondition ({'foo': 'bar'})
+    c2 = buildCondition ({'frob': 20})
+
+    cond = combineConditions (c1, c2)
+    self.assertEqual (cond.evaluate (self.condDict), True)
+
+    # Test the combination with an empty element
+    c2 = buildCondition ([])
+    cond = combineConditions (c1, c2)
+    self.assertEqual (cond.evaluate (self.condDict), True)
+
+    # Use another condition for the first part
+    c1 = buildCondition (['like', ['Field', 'foo'], ['Const', 'b%']])
+    cond = combineConditions (c1, c2)
+    self.assertEqual (cond.evaluate (self.condDict), True)
+
+
+  # ---------------------------------------------------------------------------
+  # Test SQL code generation
+  # ---------------------------------------------------------------------------
+
+  def testAsSQL (self):
+    """
+    Test the SQL code generation capabilities.
+    """
+
+    # Match the predefined (simple) condition
+    (rCode, params) = self.__getAsSQL (buildCondition (self.prefix))
+    code = '(foo = %(p0)s) AND (frob = %(p1)s)'
+    pars = [('p0', 'bar'), ('p1', 20)]
+
+    self.assertEqual (rCode, code)
+    self.assertEqual (params, pars)
+
+    # Check wether EQ with a None-type argument translates correctly
+    cond = buildCondition (['or', ['eq', ['Field', 'foo'], ['Field', 'bar']],
+                                  ['null', ['Field', 'baz']],
+                                  ['eq', ['Field', 'daz'], ['Const', None]]])
+    (rCode, params) = self.__getAsSQL (cond)
+    self.assertEqual (rCode, '(foo = bar) OR (baz IS NULL) OR (daz IS %(p0)s)')
+    self.assertEqual (params, [('p0', None)])
+
+    # And finally create a quite complex one 
+    (rCode, params) = self.__getAsSQL (buildCondition (self.bigList))
+
+    self.assertEqual (rCode, "((foo = bar) OR (baz = %(p0)s)) AND "
+                             "(barbaz != %(p1)s) AND NOT (empty IS NULL)")
+    self.assertEqual (params, [('p0', 20), ('p1', 21)])
+
+
+  # ---------------------------------------------------------------------------
+  # Create a SQL query string and return a sorted parameter sequence
+  # ---------------------------------------------------------------------------
+
+  def __getAsSQL (self, condition):
+    """
+    Call a condition's asSQL () method and return a tuple (code, params) where
+    params is a sorted sequence of parameter items (key, value).
+
+    @param condition: GCondition tree
+    @return: ordered sequence of parameter-items [(p0, value), (p1, value), 
...]
+    """
+
+    pard = {}
+    code = condition.asSQL (pard)
+
+    params = pard.items ()
+    params.sort ()
+
+    return (code, params)
+
+
+
+# =============================================================================
+# Test case for condition evaluation
+# =============================================================================
+
+class EvaluationTestCase (unittest.TestCase):
+  """
+  This class implements a test case for condition evaluation
+  """
+
+  # ---------------------------------------------------------------------------
+  # Prepare the test case
+  # ---------------------------------------------------------------------------
+
+  def setUp (self):
+    """
+    Prepare the test case
+    """
+
+    self.unifications = [[u'17', 5], [5, '18'],
+        [mx.DateTime.now (), '2004-08-15 14:00'],
+        ['13.0', 12], ['15', ''], [7L, '12'], [7L, '12.2'], [1, 'True'], [],
+        [1], [None], ['5.43', 12, 18L], [None, 'foo'],
+        ['foo', None], [5, None, 2, None, '10']]
+        
+    self.nogos = [['trash', True], ['atext', 5.43]]
+
+    self.lookup = {'foo': 'bar', 'frob': 20}
+
+
+  # ---------------------------------------------------------------------------
+  # Test the unification of values
+  # ---------------------------------------------------------------------------
+
+  def testUnify (self):
+    """
+    Test the unification of values
+    """
+
+    # all sequences listed in self.unifications should work fine
+    for sequence in self.unifications:
+      tset   = sets.Set ()
+      result = unify (sequence)
+      allowedTypes = 1
+
+      for item in result:
+        if item is None: allowedTypes = 2
+        tset.add (type (item))
+
+      if len (result):
+        self.assert_ (len (tset) <= allowedTypes)
+
+    # all items listed in no-go's shouldn't work
+    for item in self.nogos:
+      self.assertRaises (ConversionError, unify, item)
+
+
+  # ---------------------------------------------------------------------------
+  # Test the condition evaluation
+  # ---------------------------------------------------------------------------
+
+  def testEvaluation (self):
+    """
+    Test condition evaluation
+    """
+
+    cond = buildCondition ({'foo': 'bar', 'frob': 20})
+    self.assertEqual (cond.evaluate (self.lookup), True)
+
+    self.assertEqual (cond.evaluate ({'foo': None, 'frob': 10}), False)
+
+    self.assertRaises (MissingFieldError, cond.evaluate, {})
+
+    cond = buildCondition (['null', ['Field', 'foo']])
+    self.assertEqual (cond.evaluate ({'foo': None}), True)
+
+
+# =============================================================================
+# If called as main, run all tests
+# =============================================================================
+
+if __name__ == '__main__':
+  unittest.main ()


Property changes on: trunk/gnue-common/tests/datasources_GConditions.py
___________________________________________________________________
Name: svn:keywords
   + Id


Property changes on: trunk/gnue-common/tests/formatting_masks.py
___________________________________________________________________
Name: svn:keywords
   + Id


Property changes on: trunk/gnue-common/tests/utils_TextUtils.py
___________________________________________________________________
Name: svn:keywords
   + Id





reply via email to

[Prev in Thread] Current Thread [Next in Thread]