Commit ab075d1f authored by Tatuya Kamada's avatar Tatuya Kamada

Inventory: Change to use inventory cache even if we do not specify the date

This is not a new feature, this behavior is compatible with the old caching
implementation that was using inventory_stock table.
In other words, this brings backs the backward compatibility in inventory
caching.

(This commit fixes the incomplete tests that was reverted just before.)
parent ac62192a
......@@ -60,8 +60,10 @@ FROM \n
inventory_cache \n
WHERE \n
inventory_cache.query=<dtml-sqlvar query type="string"> \n
<dtml-if date>\n
AND \n
inventory_cache.date <= <dtml-sqlvar date type="datetime"> \n
inventory_cache.date <= <dtml-sqlvar date type="datetime">\n
</dtml-if>\n
ORDER BY \n
date DESC
......
13
\ No newline at end of file
14
\ No newline at end of file
......@@ -1303,7 +1303,6 @@ class SimulationTool(BaseTool):
# Get cached data
if getattr(self, "Resource_zGetInventoryCacheResult", None) is not None and \
optimisation__ and (not kw.get('from_date')) and \
(bool(kw.get("at_date")) ^ bool(kw.get("to_date"))) and \
'transformed_resource' not in kw:
# Here is the different kind of date
# from_date : >=
......@@ -1314,7 +1313,7 @@ class SimulationTool(BaseTool):
# of the same line
at_date = kw.pop("at_date", None)
if at_date is None:
to_date = kw.pop("to_date")
to_date = kw.pop("to_date", None)
else:
# add one second so that we can use to_date
to_date = at_date + MYSQL_MIN_DATETIME_RESOLUTION
......@@ -1402,10 +1401,9 @@ class SimulationTool(BaseTool):
**kw)).digest()
# Try to get result from cache
Resource_zGetInventoryCacheResult = self.Resource_zGetInventoryCacheResult
inventory_cache_kw = {
'query': sql_text_hash,
'date': to_date,
}
inventory_cache_kw = {'query': sql_text_hash}
if to_date is not None:
inventory_cache_kw['date'] = to_date
try:
cached_sql_result = Resource_zGetInventoryCacheResult(**inventory_cache_kw)
except ProgrammingError:
......@@ -1434,10 +1432,10 @@ class SimulationTool(BaseTool):
else:
cached_result = []
cache_lag = self.getInventoryCacheLag()
if cached_sql_result and to_date - DateTime(cached_sql_result[0].date) < cache_lag:
if cached_sql_result and (to_date is None or (to_date - DateTime(cached_sql_result[0].date) < cache_lag)):
cached_date = DateTime(cached_sql_result[0].date)
result = cached_result
else:
elif to_date is not None:
# Cache miss, or hit with old data: store a new entry in cache.
# Don't store it at to_date, as it risks being flushed soon (ie, when
# any document older than to_date gets reindexed in stock table).
......@@ -1475,6 +1473,12 @@ class SimulationTool(BaseTool):
'data': result._data,
}),
)
else:
# Cache miss and this getInventory() not specifying to_date,
# and other getInventory() have not created usable caches.
# In such case, do not create cache, do not use cache.
result = []
cached_date = None
if src__:
result = sql_source_list
return result, cached_date
......
......@@ -3022,6 +3022,86 @@ class TestInventoryCacheTable(InventoryAPITestCase):
}
)
def test_17_GetInventoryWithoutDateNoCache(self):
"""
Check getInventory without date when there is no cache.
"""
self.assertEquals(
self.INVENTORY_QUANTITY_1 + \
self.INVENTORY_QUANTITY_2 + self.INVENTORY_QUANTITY_3,
self.getInventory(node_uid=self.node_uid)
)
def test_17b_GetInventoryWithoutDateCacheEnable(self):
"""
Check getInventory without date. Without date means specifying
infinite future date. So it try to retrieve the latest cache.
Note: This behavior is compatible with the old caching implementation
that was using inventory_stock table. In other words, this brings backs
the backword compatibility in invetory caching.
"""
# store the right cache
self._fillCache()
# Check we can get right cache
# Note: The real stock value is doubled in assertInventoryEquals().
# Thus this equals must be fail when we can not get cache.
self.assertInventoryEquals(
self.INVENTORY_QUANTITY_1 + \
self.INVENTORY_QUANTITY_2 + self.INVENTORY_QUANTITY_3,
inventory_kw={
'node_uid': self.node_uid,
},
)
def test_18_GetInventoryListWithoutDateNoCache(self):
"""
Check that getInventoryList without date.
"""
self._checkInventoryList(
self.getInventoryList(node_uid=self.node_uid),
[{
'date': self.INVENTORY_DATE_3,
'inventory': self.INVENTORY_QUANTITY_3,
'node_uid': self.node_uid,
}, {
'date': self.INVENTORY_DATE_2,
'inventory': self.INVENTORY_QUANTITY_2,
'node_uid': self.node_uid,
}, {
'date': self.INVENTORY_DATE_1,
'inventory': self.INVENTORY_QUANTITY_1,
'node_uid': self.node_uid,
}],
)
def test_18b_GetInventoryListWithoutDateCacheEnable(self):
"""
Check that getInventoryList without date retrives the latest caches.
"""
# store the right cache
self._fillCache(True)
# break the stock
self.doubleStockValue()
# Check we get right cache not wrong stock
self._checkInventoryList(
self.getInventoryList(node_uid=self.node_uid),
[{
'date': self.INVENTORY_DATE_3,
'inventory': self.INVENTORY_QUANTITY_3,
'node_uid': self.node_uid,
}, {
'date': self.INVENTORY_DATE_2,
'inventory': self.INVENTORY_QUANTITY_2,
'node_uid': self.node_uid,
}, {
'date': self.INVENTORY_DATE_1,
'inventory': self.INVENTORY_QUANTITY_1,
'node_uid': self.node_uid,
}],
)
class BaseTestUnitConversion(InventoryAPITestCase):
QUANTITY_UNIT_DICT = {}
......
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