From a895bc52b90b88b9367b3877e96d95d9f05789ff Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A9rome=20Perrin?= <jerome@nexedi.com>
Date: Tue, 3 Jan 2006 11:13:07 +0000
Subject: [PATCH] Add a test to make sure predicate matrix are checked in the
 right order.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@4996 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ERP5/tests/testAccountingRules.py | 59 ++++++++++++++++++++++-
 1 file changed, 57 insertions(+), 2 deletions(-)

diff --git a/product/ERP5/tests/testAccountingRules.py b/product/ERP5/tests/testAccountingRules.py
index 508a0faf24..1f079196c0 100755
--- a/product/ERP5/tests/testAccountingRules.py
+++ b/product/ERP5/tests/testAccountingRules.py
@@ -85,6 +85,8 @@ class TestAccountingRules(ERP5TypeTestCase):
                      = "Sale Invoice Transaction Line"
   sale_invoice_line_portal_type        = "Invoice Line"
   sale_invoice_cell_portal_type        = "Invoice Cell"
+  invoice_transaction_rule_portal_type \
+                    = "Invoice Transaction Rule"
   
   purchase_invoice_transaction_portal_type \
                       = "Purchase Invoice Transaction"
@@ -1462,7 +1464,7 @@ class TestAccountingRules(ERP5TypeTestCase):
       stepCreateNotMatchableSaleInvoiceMovements
       stepCheckNotMatchableSaleInvoiceMovements
     """)
-    
+  
   def test_04_SimpleInvoice(self, quiet=0, run=RUN_ALL_TESTS):
     """ Simple Invoice.
     Try to expand an invoice containing only one simple Invoice Line.
@@ -1793,7 +1795,60 @@ class TestAccountingRules(ERP5TypeTestCase):
     #   date from trade condition
     #   quantity from sum of receivable movement
     #   link to sale invoice
-     
+ 
+  def test_08_GetMatchingCellIntIndex(self, quiet=0, run=RUN_ALL_TESTS):
+    """ Int index order in getMatchingCell.
+    When defining a matrix rule, predicates must be tested in the order 
+    defined by 'int_index' property on those predicates.
+    """
+    if not run:
+      return
+    if not quiet:
+      message = 'Int index order in getMatchingCell'
+      ZopeTestCase._print('\n%s ' % message)
+      LOG('Testing... ', INFO, message)
+       
+    # create a matrix
+    #     1a    2b
+    # 1x a*x   b*x 
+    # 2y a*y   b*y
+    
+    rule = self.getPortal().portal_rules.newContent(
+          portal_type = self.invoice_transaction_rule_portal_type)
+    predicate_1a = rule.newContent(
+        portal_type = self.predicate_portal_type,
+        string_index = "product",
+        id = "1a",
+        int_index = 1)
+    predicate_2b = rule.newContent(
+        portal_type = self.predicate_portal_type,
+        string_index = "product",
+        id = "2b",
+        int_index = 2)
+    predicate_1x = rule.newContent(
+        portal_type = self.predicate_portal_type,
+        string_index = "region",
+        id = "1x",
+        int_index = 1)
+    predicate_2y = rule.newContent(
+        portal_type = self.predicate_portal_type,
+        string_index = "region",
+        id = "2y",
+        int_index = 2)
+    get_transaction().commit()
+    self.tic()
+    rule.updateMatrix()
+    
+    # make sure predicates are checked in the right order
+    previous_priority = -100
+    for key_list in rule._getSortedCellKeyList():
+      priority = 0
+      for index, path in enumerate(key_list) :
+        priority += self.getPortal().restrictedTraverse(path).getIntIndex()
+      self.assert_( previous_priority <= priority,
+                    "%s <= %s" % (previous_priority, priority))
+      previous_priority = priority
+    
 if __name__ == '__main__':
   framework()
 else:
-- 
2.30.9