Skip to content

Commit

Permalink
- add test and do value coercion if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
dataflake committed May 17, 2019
1 parent 215d050 commit 050295b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/App/ApplicationManager.py
Expand Up @@ -258,12 +258,21 @@ def manage_minimize(self, value=1, REQUEST=None):
@requestmethod('POST')
def manage_pack(self, days=0, REQUEST=None):
"""Pack the database"""
t = time.time() - (days * 86400)

self._getDB().pack(t)
if not isinstance(days, (int, float)):
try:
days = float(days)
except ValueError:
days = None

if days is not None:
t = time.time() - (days * 86400)
self._getDB().pack(t)
msg = 'Database packed to %s days' % str(days)
else:
t = None
msg = 'Invalid days value %s' % str(days)

if REQUEST is not None:
msg = 'Database packed to %s days' % str(days)
url = '%s/manage_main?manage_tabs_message=%s' % (REQUEST['URL1'],
msg)
REQUEST['RESPONSE'].redirect(url)
Expand Down
34 changes: 34 additions & 0 deletions src/App/tests/test_ApplicationManager.py
Expand Up @@ -331,6 +331,40 @@ def test_db_size_gt_1_meg(self):
am._p_jar = self._makeJar('foo', (2048 * 1024) + 123240)
self.assertEqual(am.db_size(), '2.1M')

def test_manage_pack(self):
am = self._makeOne()
am._p_jar = self._makeJar('foo', '')

# The default value for days is 0, meaning pack to now
pack_to = time.time()
am.manage_pack()
self.assertAlmostEqual(am._getDB()._packed, pack_to, delta=1)

# Try a float value
pack_to = time.time() - 10800 # 3 hrs, 0.125 days
packed_to = am.manage_pack(days=.125)
self.assertAlmostEqual(am._getDB()._packed, pack_to, delta=1)
self.assertAlmostEqual(packed_to, pack_to, delta=1)

# Try an integer
pack_to = time.time() - 86400 # 1 day
packed_to = am.manage_pack(days=1)
self.assertAlmostEqual(am._getDB()._packed, pack_to, delta=1)
self.assertAlmostEqual(packed_to, pack_to, delta=1)

# Pass a string
pack_to = time.time() - 97200 # 27 hrs, 1.125 days
packed_to = am.manage_pack(days='1.125')
self.assertAlmostEqual(am._getDB()._packed, pack_to, delta=1)
self.assertAlmostEqual(packed_to, pack_to, delta=1)

# Set the dummy storage pack indicator manually
am._getDB()._packed = None
# Pass an invalid value
self.assertIsNone(am.manage_pack(days='foo'))
# The dummy storage value should not change because pack was not called
self.assertIsNone(am._getDB()._packed)


class MenuDtmlTests(ConfigTestBase, Testing.ZopeTestCase.FunctionalTestCase):
"""Browser testing ..dtml.menu.dtml."""
Expand Down

0 comments on commit 050295b

Please sign in to comment.