Commit 5113ed6d authored by Vincent Pelletier's avatar Vincent Pelletier

testDomainTool: Reduce code duplication.

Also, use valid base categories for
not-in-preferred_predicate_category_list test.
Also, improve coding style.
Also, after changing preferred predicate category list, only reindex
the module containing predicates instead of the whole site, to reduce
test duration.
parent 0cefde41
...@@ -147,134 +147,76 @@ class TestDomainTool(TestPredicateMixIn): ...@@ -147,134 +147,76 @@ class TestDomainTool(TestPredicateMixIn):
self.tic() self.tic()
def checkPredicate(self, test=None): def checkPredicate(self, test=None):
predicate = self.getPredicate() predicate = self.getPredicate()
#predicate.setMembershipCriterionBaseCategoryList([])
#predicate.setMembershipCriterionCategoryList([])
#predicate.setCriterion('quantity',identity=45,min=None,max=None)
#predicate.immediateReindexObject()
order_line = self.getOrderLine() order_line = self.getOrderLine()
domain_tool = self.getDomainTool() searchPredicateList = self.getDomainTool().searchPredicateList
def assertPredicateItemsMatchingOrderLineEqual(expected, **kw):
self.tic()
self.assertItemsEqual(
expected,
searchPredicateList(order_line, test=test, **kw),
)
# Test with order line and predicate to none # Test with order line and predicate to none
predicate_list = domain_tool.searchPredicateList(order_line,test=test, # Actually, a predicate where nothing is defined is ok
portal_type=self.portal_type_query) assertPredicateItemsMatchingOrderLineEqual([predicate], portal_type=self.portal_type_query)
self.assertEqual(len(predicate_list),1) # Actually, a predicate where
# nothing is defined is ok
# Test with order line not none and predicate to none # Test with order line not none and predicate to none
order_line.setQuantity(45) order_line.setQuantity(45)
self.tic() assertPredicateItemsMatchingOrderLineEqual([predicate], portal_type=self.portal_type_query)
predicate_list = domain_tool.searchPredicateList(order_line,test=test,
portal_type=self.portal_type_query)
self.assertEqual(len(predicate_list),1)
# Test with order line not none and predicate to identity # Test with order line not none and predicate to identity
order_line.setQuantity(45) order_line.setQuantity(45)
kw = {'portal_type':'Mapped Value'} predicate.setCriterion('quantity', identity=45, min=None, max=None)
predicate.setCriterion('quantity',identity=45,min=None,max=None) assertPredicateItemsMatchingOrderLineEqual([predicate], portal_type='Mapped Value')
self.tic()
predicate_list = domain_tool.searchPredicateList(order_line,test=test,**kw)
self.assertEqual(len(predicate_list),1)
order_line.setQuantity(40) order_line.setQuantity(40)
self.tic() assertPredicateItemsMatchingOrderLineEqual([], portal_type='Mapped Value')
predicate_list = domain_tool.searchPredicateList(order_line,test=test,**kw)
self.assertEqual(len(predicate_list),0)
# Test with order line not none and predicate to min # Test with order line not none and predicate to min
order_line.setQuantity(45) order_line.setQuantity(45)
predicate = self.getPredicate() predicate.setCriterion('quantity', identity=None, min=30, max=None)
predicate.setCriterion('quantity',identity=None,min=30,max=None) assertPredicateItemsMatchingOrderLineEqual([predicate], portal_type='Mapped Value')
self.tic()
predicate_list = domain_tool.searchPredicateList(order_line,test=test,**kw)
self.assertEqual(len(predicate_list),1)
order_line.setQuantity(10) order_line.setQuantity(10)
self.tic() assertPredicateItemsMatchingOrderLineEqual([], portal_type=self.portal_type_query) # XXX: mistake in the test ?
predicate_list = domain_tool.searchPredicateList(order_line,test=test,
portal_type=self.portal_type_query)
self.assertEqual(len(predicate_list),0)
# Test with order line not none and predicate to max # Test with order line not none and predicate to max
order_line.setQuantity(45) order_line.setQuantity(45)
predicate = self.getPredicate() predicate.setCriterion('quantity', identity=None, min=None, max=50)
predicate.setCriterion('quantity',identity=None,min=None,max=50) assertPredicateItemsMatchingOrderLineEqual([predicate], portal_type='Mapped Value')
self.tic()
predicate_list = domain_tool.searchPredicateList(order_line,test=test,**kw)
self.assertEqual(len(predicate_list),1)
order_line.setQuantity(60) order_line.setQuantity(60)
self.tic() assertPredicateItemsMatchingOrderLineEqual([], portal_type='Mapped Value')
predicate_list = domain_tool.searchPredicateList(order_line,test=test,**kw)
self.assertEqual(len(predicate_list),0)
# Test with order line not none and predicate to min max # Test with order line not none and predicate to min max
order_line.setQuantity(20) order_line.setQuantity(20)
predicate = self.getPredicate() predicate.setCriterion('quantity', identity=None, min=30, max=50)
predicate.setCriterion('quantity',identity=None,min=30,max=50) assertPredicateItemsMatchingOrderLineEqual([], portal_type='Mapped Value')
self.tic()
predicate_list = domain_tool.searchPredicateList(order_line,test=test,**kw)
self.assertEqual(len(predicate_list),0)
order_line.setQuantity(60) order_line.setQuantity(60)
self.tic() assertPredicateItemsMatchingOrderLineEqual([], portal_type='Mapped Value')
predicate_list = domain_tool.searchPredicateList(order_line,test=test,**kw)
self.assertEqual(len(predicate_list),0)
order_line.setQuantity(45) order_line.setQuantity(45)
predicate_list = domain_tool.searchPredicateList(order_line,test=test,**kw) assertPredicateItemsMatchingOrderLineEqual([predicate], portal_type='Mapped Value')
self.tic()
self.assertEqual(len(predicate_list),1)
# Test with order line not none and predicate to min max # Test with order line not none and predicate to min max
# and also predicate to a category # and also predicate to a category
predicate.setMembershipCriterionBaseCategoryList(['region']) predicate.setMembershipCriterionBaseCategoryList(['region'])
predicate.setMembershipCriterionCategoryList(['region/europe']) predicate.setMembershipCriterionCategoryList(['region/europe'])
self.tic() assertPredicateItemsMatchingOrderLineEqual([], portal_type='Mapped Value')
predicate_list = domain_tool.searchPredicateList(order_line,test=test,**kw)
self.assertEqual(len(predicate_list),0)
order_line.setCategoryList(['region/africa']) order_line.setCategoryList(['region/africa'])
self.tic() assertPredicateItemsMatchingOrderLineEqual([], portal_type='Mapped Value')
predicate_list = domain_tool.searchPredicateList(order_line,test=test,**kw)
self.assertEqual(len(predicate_list),0)
order_line.setCategoryList(['region/europe']) order_line.setCategoryList(['region/europe'])
self.tic() assertPredicateItemsMatchingOrderLineEqual([predicate], portal_type='Mapped Value')
predicate_list = domain_tool.searchPredicateList(order_line,test=test,**kw)
self.assertEqual(len(predicate_list),1)
order_line.setQuantity(60) order_line.setQuantity(60)
self.tic() assertPredicateItemsMatchingOrderLineEqual([], portal_type='Mapped Value')
predicate_list = domain_tool.searchPredicateList(order_line,test=test,**kw)
self.assertEqual(len(predicate_list),0)
# Test with order line not none and predicate to date min and date max # Test with order line not none and predicate to date min and date max
kw = {'portal_type':'Supply Line'}
self.supply_line.setBasePrice(23) self.supply_line.setBasePrice(23)
self.supply_line.setPricedQuantity(1) self.supply_line.setPricedQuantity(1)
self.supply_line.setDefaultResourceValue(self.resource) self.supply_line.setDefaultResourceValue(self.resource)
order_line.setDefaultResourceValue(self.resource) order_line.setDefaultResourceValue(self.resource)
self.assertEqual(self.supply_line.getDefaultResourceValue(),self.resource) self.assertEqual(self.supply_line.getDefaultResourceValue(), self.resource)
self.assertEqual(order_line.getDefaultResourceValue(),self.resource) self.assertEqual(order_line.getDefaultResourceValue(), self.resource)
date1 = DateTime('2005/04/08 10:47:26.388 GMT-4') date1 = DateTime('2005/04/08 10:47:26.388 GMT-4')
date2 = DateTime('2005/04/10 10:47:26.388 GMT-4') date2 = DateTime('2005/04/10 10:47:26.388 GMT-4')
self.supply_line.setStartDateRangeMin(date1) self.supply_line.setStartDateRangeMin(date1)
self.supply_line.setStartDateRangeMax(date2) self.supply_line.setStartDateRangeMax(date2)
current_date = DateTime('2005/04/1 10:47:26.388 GMT-4') current_date = DateTime('2005/04/1 10:47:26.388 GMT-4')
order_line.setStartDate(current_date) order_line.setStartDate(current_date)
self.tic() assertPredicateItemsMatchingOrderLineEqual([], portal_type='Supply Line')
predicate_list = domain_tool.searchPredicateList(order_line,test=test,**kw)
self.assertEqual(len(predicate_list),0)
current_date = DateTime('2005/04/09 10:47:26.388 GMT-4') current_date = DateTime('2005/04/09 10:47:26.388 GMT-4')
order_line.setStartDate(current_date) order_line.setStartDate(current_date)
self.tic() assertPredicateItemsMatchingOrderLineEqual([self.supply_line], portal_type='Supply Line')
predicate_list = domain_tool.searchPredicateList(order_line,test=test,**kw)
self.assertEqual(len(predicate_list),1)
def test_01_SearchPredidateListWithNoTest(self): def test_01_SearchPredidateListWithNoTest(self):
self.createData() self.createData()
...@@ -418,239 +360,82 @@ class TestDomainTool(TestPredicateMixIn): ...@@ -418,239 +360,82 @@ class TestDomainTool(TestPredicateMixIn):
self.assertTrue(predicate_one_match not in portal_domains.searchPredicateList(document, portal_type=self.portal_type_query, test=0)) self.assertTrue(predicate_one_match not in portal_domains.searchPredicateList(document, portal_type=self.portal_type_query, test=0))
def test_07_NonLeftJoinModeOfSearchPredicateList(self): def test_07_NonLeftJoinModeOfSearchPredicateList(self):
searchPredicateList = self.portal.portal_domains.searchPredicateList
# Add system preference # Add system preference
system_preference = self.portal.portal_preferences.newContent(portal_type='System Preference') system_preference = self.portal.portal_preferences.newContent(portal_type='System Preference')
system_preference.setPreferredPredicateCategoryList( system_preference.setPreferredPredicateCategoryList(['source_section', 'destination_section', 'price_currency'])
['source_section', 'destination_section', 'price_currency'])
self.tic()
# Add sample data # Add sample data
jpy = self.portal.currency_module.newContent(portal_type='Currency', jpy = self.portal.currency_module.newContent(portal_type='Currency', title='JPY', reference='JPY')
title='JPY',
reference='JPY')
jpy.validate() jpy.validate()
euro = self.portal.currency_module.newContent(portal_type='Currency', title='EURO', reference='EUR')
euro = self.portal.currency_module.newContent(portal_type='Currency',
title='EURO',
reference='EUR')
euro.validate() euro.validate()
newOrganisation = self.portal.organisation_module.newContent
organisation_module = self.portal.organisation_module company= newOrganisation(portal_type='Organisation', title='Company')
company= organisation_module.newContent(portal_type='Organisation', shop = newOrganisation(portal_type='Organisation', title='Shop')
title='Company') supplier = newOrganisation(portal_type='Organisation', title='Supplier')
shop = organisation_module.newContent(portal_type='Organisation',
title='Shop')
supplier = organisation_module.newContent(portal_type='Organisation',
title='Supplier')
product_module = self.portal.product_module product_module = self.portal.product_module
product1 = product_module.newContent(portal_type='Product', product1 = product_module.newContent(portal_type='Product', title='Product1')
title='Product1') product2 = product_module.newContent(portal_type='Product', title='Product2')
product2 = product_module.newContent(portal_type='Product',
title='Product2')
supply_module = self.portal.sale_supply_module supply_module = self.portal.sale_supply_module
supply1 = supply_module.newContent(portal_type='Sale Supply', supply1 = supply_module.newContent(portal_type='Sale Supply', title='Supply1', source_section_value=supplier, destination_section_value=shop, price_currency_value=jpy)
title='Supply1',
source_section_value=supplier,
destination_section_value=shop,
price_currency_value=jpy)
supply1.validate() supply1.validate()
supply1.newContent(portal_type='Sale Supply Line', supply1_line1 = supply1.newContent(portal_type='Sale Supply Line', resource_value=product1)
resource_value=product1) supply1_line2 = supply1.newContent(portal_type='Sale Supply Line', resource_value=product2)
supply1.newContent(portal_type='Sale Supply Line', supply2 = supply_module.newContent(portal_type='Sale Supply', title='Supply2', source_section_value=supplier, destination_section_value=company, price_currency_value=jpy)
resource_value=product2)
supply2 = supply_module.newContent(portal_type='Sale Supply',
title='Supply2',
source_section_value=supplier,
destination_section_value=company,
price_currency_value=jpy)
supply2.validate() supply2.validate()
supply2.newContent(portal_type='Sale Supply Line', supply2_line1 = supply2.newContent(portal_type='Sale Supply Line', resource_value=product2)
resource_value=product2) supply3 = supply_module.newContent(portal_type='Sale Supply', title='Supply3', source_section_value=supplier, price_currency_value=euro)
supply3 = supply_module.newContent(portal_type='Sale Supply',
title='Supply3',
source_section_value=supplier,
price_currency_value=euro)
supply3.validate() supply3.validate()
supply3.newContent(portal_type='Sale Supply Line', supply3_line1 = supply3.newContent(portal_type='Sale Supply Line', resource_value=product1)
resource_value=product1)
order = self.portal.sale_order_module.newContent( order = self.portal.sale_order_module.newContent(
portal_type='Sale Order', portal_type='Sale Order',
source_section_value=supplier, source_section_value=supplier,
destination_section_value=shop, destination_section_value=shop,
price_currency_value=jpy) price_currency_value=jpy)
order_line = order.newContent(portal_type='Sale Order Line', order_line = order.newContent(
resource_value=product1) portal_type='Sale Order Line',
resource_value=product1,
)
self.tic() self.tic()
# Test def assertUsesLeftJoinAndPredicateItemsMatchingOrderLineEqual(uses_left_join, expected, **kw):
# Check traditional left join mode kw['context'] = order_line
domain_tool = self.portal.portal_domains kw['portal_type'] = 'Sale Supply Line'
searchPredicateList = domain_tool.searchPredicateList src = searchPredicateList(src__=1, **kw)
self.assertEqual(len( if uses_left_join:
searchPredicateList(order_line, self.assertIn('LEFT JOIN', src)
portal_type='Sale Supply Line')), else:
1) self.assertNotIn('LEFT JOIN', src)
self.assert_('LEFT JOIN' in searchPredicateList( self.assertItemsEqual(expected, searchPredicateList(**kw))
order_line,
portal_type='Sale Supply Line', # Check left join mode
src__=1)) assertUsesLeftJoinAndPredicateItemsMatchingOrderLineEqual(True, [supply1_line1])
self.assertEqual(len(searchPredicateList( assertUsesLeftJoinAndPredicateItemsMatchingOrderLineEqual(True, [supply1_line1, supply1_line2, supply2_line1, supply3_line1], tested_base_category_list=['source_section'])
order_line, assertUsesLeftJoinAndPredicateItemsMatchingOrderLineEqual(True, [supply1_line1, supply1_line2, supply3_line1], tested_base_category_list=['source_section', 'destination_section'])
portal_type='Sale Supply Line', assertUsesLeftJoinAndPredicateItemsMatchingOrderLineEqual(True, [supply1_line1, supply1_line2], tested_base_category_list=['source_section', 'destination_section', 'price_currency'])
tested_base_category_list=['source_section'])), assertUsesLeftJoinAndPredicateItemsMatchingOrderLineEqual(True, [supply1_line1], tested_base_category_list=['source_section', 'destination_section', 'price_currency', 'resource'])
4)
self.assert_('LEFT JOIN' in searchPredicateList( # Check inner-join mode
order_line, # Enable system preference and reindex relevant predicates
portal_type='Sale Supply Line',
tested_base_category_list=['source_section', 'destination_section',
'price_currency'],
src__=1))
self.assertEqual(len(searchPredicateList(
order_line,
portal_type='Sale Supply Line',
tested_base_category_list=['source_section', 'destination_section'])),
3)
self.assert_('LEFT JOIN' in searchPredicateList(
order_line,
portal_type='Sale Supply Line',
tested_base_category_list=['source_section', 'destination_section'],
src__=1))
self.assertEqual(len(searchPredicateList(
order_line,
portal_type='Sale Supply Line',
tested_base_category_list=['source_section', 'destination_section',
'price_currency'])),
2)
self.assert_('LEFT JOIN' in searchPredicateList(
order_line,
portal_type='Sale Supply Line',
tested_base_category_list=['source_section', 'destination_section',
'price_currency'],
src__=1))
self.assertEqual(len(searchPredicateList(
order_line,
portal_type='Sale Supply Line',
tested_base_category_list=['source_section', 'destination_section',
'price_currency', 'resource'])),
1)
self.assert_('LEFT JOIN' in searchPredicateList(
order_line,
portal_type='Sale Supply Line',
tested_base_category_list=['source_section', 'destination_section',
'price_currency', 'resource'],
src__=1))
# if wrong base categories are passed, then nothing is matched
self.assertEqual(len(searchPredicateList(
order_line,
portal_type='Sale Supply Line',
tested_base_category_list=['WAAA', 'BOOO'])),
0)
self.assert_('LEFT JOIN' in searchPredicateList(
order_line,
portal_type='Sale Supply Line',
tested_base_category_list=['WAAA', 'BOOO'],
src__=1))
# Check non-left join mode
# Enable system preference and reindex all
system_preference.enable() system_preference.enable()
self.tic() self.tic()
self.portal.ERP5Site_reindexAll() supply_module.Folder_reindexAll()
self.tic() self.tic()
# if document has relations using base categories which are not present in the preference, then left join mode is still used.
# if tested_base_category_list is not passed, then left join mode is assertUsesLeftJoinAndPredicateItemsMatchingOrderLineEqual(True, [supply1_line1])
# still used. assertUsesLeftJoinAndPredicateItemsMatchingOrderLineEqual(False, [supply1_line1, supply1_line2, supply2_line1, supply3_line1], tested_base_category_list=['source_section'])
self.assertEqual(len( assertUsesLeftJoinAndPredicateItemsMatchingOrderLineEqual(False, [supply1_line1, supply1_line2, supply3_line1], tested_base_category_list=['source_section', 'destination_section'])
searchPredicateList(order_line, assertUsesLeftJoinAndPredicateItemsMatchingOrderLineEqual(False, [supply1_line1, supply1_line2], tested_base_category_list=['source_section', 'destination_section', 'price_currency'])
portal_type='Sale Supply Line')), # resource is not in preferred predicate category list, so left join is used
1) assertUsesLeftJoinAndPredicateItemsMatchingOrderLineEqual(True, [supply1_line1], tested_base_category_list=['source_section', 'destination_section', 'price_currency', 'resource'])
self.assert_('LEFT JOIN' in searchPredicateList( # add resource to preference, this enables non-left join mode
order_line, system_preference.setPreferredPredicateCategoryList(['source_section', 'destination_section', 'price_currency', 'resource'])
portal_type='Sale Supply Line',
src__=1))
self.assertEqual(len(searchPredicateList(
order_line,
portal_type='Sale Supply Line',
tested_base_category_list=['source_section'])),
4)
self.assert_(not 'LEFT JOIN' in searchPredicateList(
order_line,
portal_type='Sale Supply Line',
tested_base_category_list=['source_section', 'destination_section',
'price_currency'],
src__=1))
self.assertEqual(len(searchPredicateList(
order_line,
portal_type='Sale Supply Line',
tested_base_category_list=['source_section', 'destination_section'])),
3)
self.assert_(not 'LEFT JOIN' in searchPredicateList(
order_line,
portal_type='Sale Supply Line',
tested_base_category_list=['source_section', 'destination_section'],
src__=1))
self.assertEqual(len(searchPredicateList(
order_line,
portal_type='Sale Supply Line',
tested_base_category_list=['source_section', 'destination_section',
'price_currency'])),
2)
self.assert_(not 'LEFT JOIN' in searchPredicateList(
order_line,
portal_type='Sale Supply Line',
tested_base_category_list=['source_section', 'destination_section',
'price_currency'],
src__=1))
self.assertEqual(len(searchPredicateList(
order_line,
portal_type='Sale Supply Line',
tested_base_category_list=['source_section', 'destination_section',
'price_currency', 'resource'])),
1)
# resource is not in preferred predicate category list, so left join
# is used
self.assert_('LEFT JOIN' in searchPredicateList(
order_line,
portal_type='Sale Supply Line',
tested_base_category_list=['source_section', 'destination_section',
'price_currency', 'resource'],
src__=1))
# if wrong base categories are passed, then nothing is matched
self.assertEqual(len(searchPredicateList(
order_line,
portal_type='Sale Supply Line',
tested_base_category_list=['WAAA', 'BOOO'])),
0)
self.assert_('LEFT JOIN' in searchPredicateList(
order_line,
portal_type='Sale Supply Line',
tested_base_category_list=['WAAA', 'BOOO'],
src__=1))
# add WAAA and BOOO to preference, this enables non-left join mode
system_preference.setPreferredPredicateCategoryList(
['source_section', 'destination_section', 'price_currency',
'WAAA', 'BOOO'])
self.portal.portal_caches.clearAllCache() self.portal.portal_caches.clearAllCache()
self.tic() self.tic()
self.portal.ERP5Site_reindexAll() supply_module.Folder_reindexAll()
self.tic() self.tic()
self.assertEqual(len(searchPredicateList( # resource is not in preferred predicate category list, so only inner join is used
order_line, assertUsesLeftJoinAndPredicateItemsMatchingOrderLineEqual(False, [supply1_line1], tested_base_category_list=['source_section', 'destination_section', 'price_currency', 'resource'])
portal_type='Sale Supply Line',
tested_base_category_list=['WAAA', 'BOOO'])),
0)
self.assert_(not 'LEFT JOIN' in searchPredicateList(
order_line,
portal_type='Sale Supply Line',
tested_base_category_list=['WAAA', 'BOOO'],
src__=1))
def test_searchPredicateInvalidCategories(self): def test_searchPredicateInvalidCategories(self):
predicate = self.portal.sale_supply_module.newContent( predicate = self.portal.sale_supply_module.newContent(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment