From 5f6fadd187480bfe405e60f7c959886a140f7f3b Mon Sep 17 00:00:00 2001 From: Sam Fonseca Date: Sat, 16 Jul 2016 13:39:55 -0500 Subject: [PATCH] removed unused md5 import from setup.py bumped version to 1.1.4 added virtualenvs to gitignore removed unused md5 import from setup.py to make module pip-installable from python3 removed venv entries from gitignore --- .gitignore | 2 +- expiringdict/__init__.py | 1 - setup.py | 7 +---- tests/__init__.py | 6 +++- tests/expiringdict_test.py | 60 +++++++++++++++++++++++++++++--------- tox.ini | 14 +++++++-- 6 files changed, 64 insertions(+), 26 deletions(-) diff --git a/.gitignore b/.gitignore index 77b3b0c..fec45e9 100644 --- a/.gitignore +++ b/.gitignore @@ -45,4 +45,4 @@ tramp # Org-mode .org-id-locations -*_archive \ No newline at end of file +*_archive diff --git a/expiringdict/__init__.py b/expiringdict/__init__.py index 3e2f298..232a6f5 100644 --- a/expiringdict/__init__.py +++ b/expiringdict/__init__.py @@ -62,7 +62,6 @@ def __getitem__(self, key, with_age=False): else: return item[0] else: - del self[key] raise KeyError(key) def __setitem__(self, key, value): diff --git a/setup.py b/setup.py index 86781c1..bb6f68b 100644 --- a/setup.py +++ b/setup.py @@ -1,16 +1,11 @@ from setuptools import setup, find_packages -try: - import md5 # fix for "No module named _md5" error -except ImportError: - # python 3 moved md5 - from hashlib import md5 with open("README.rst") as f: long_description = f.read() setup(name='expiringdict', - version='1.1.3', + version='1.1.4', description="Dictionary with auto-expiring values for caching purposes", long_description=long_description, author='Anton Efimenko', diff --git a/tests/__init__.py b/tests/__init__.py index 0af0c03..4dd0538 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -1,2 +1,6 @@ -from mock import Mock, patch +try: + # Python 3.4 and greater + from unittest.mock import Mock, patch +except ImportError: + from mock import Mock, patch from nose.tools import * diff --git a/tests/expiringdict_test.py b/tests/expiringdict_test.py index 2e34a71..b655f2e 100644 --- a/tests/expiringdict_test.py +++ b/tests/expiringdict_test.py @@ -11,40 +11,71 @@ def test_create(): eq_(len(d), 0) -def test_basics(): +def test_setgetitem(): d = ExpiringDict(max_len=3, max_age_seconds=0.01) - eq_(d.get('a'), None) d['a'] = 'x' - eq_(d.get('a'), 'x') + eq_(d['a'], 'x') + + +def test_get_method_unset_item(): + d = ExpiringDict(max_len=3, max_age_seconds=0.01) + + eq_(d.get('a'), None) + +def test_max_age_expires(): + d = ExpiringDict(max_len=3, max_age_seconds=0.01) + + d['a'] = 'x' sleep(0.01) eq_(d.get('a'), None) + +def test_update_existing_item(): + d = ExpiringDict(max_len=3, max_age_seconds=0.01) + + d['a'] = 'x' + eq_(d.get('a'), 'x') + d['a'] = 'y' eq_(d.get('a'), 'y') - ok_('b' not in d) + +def test_key_in(): + d = ExpiringDict(max_len=3, max_age_seconds=0.01) + d['b'] = 'y' ok_('b' in d) - sleep(0.01) + +def test_key_not_in(): + d = ExpiringDict(max_len=3, max_age_seconds=0.01) + ok_('b' not in d) - # a is still in expiringdict, next values should expire it - d['c'] = 'x' - d['d'] = 'y' - d['e'] = 'z' + +def test_max_items_expires(): + d = ExpiringDict(max_len=3, max_age_seconds=0.01) + + d['a'] = 1 + d['b'] = 2 + d['c'] = 3 + # a is still in expiringdict, next value should expire it + d['d'] = 4 # dict if full + eq_(len(d), 3) + ok_('a' not in d) + ok_('b' in d) ok_('c' in d) ok_('d' in d) - d['f'] = '1' - # c should gone after that - ok_('c' not in d, 'Len of dict is more than max_len') - # test __delitem__ +def test_delitem(): + d = ExpiringDict(max_len=3, max_age_seconds=0.01) + + d['e'] = 1 del d['e'] ok_('e' not in d) @@ -75,6 +106,7 @@ def test_iter(): eq_([k for k in d.values()], ['x', 'y', 'z']) sleep(0.01) + # eq_(list(d.values()), []) eq_([k for k in d.values()], []) @@ -97,7 +129,7 @@ def test_ttl(): eq_(None, d.ttl('b')) # expired key - with patch.object(OrderedDict, '__getitem__', + with patch.object(ExpiringDict, '__getitem__', Mock(return_value=('x', 10**9))): eq_(None, d.ttl('a')) diff --git a/tox.ini b/tox.ini index 45715f2..1b729ec 100644 --- a/tox.ini +++ b/tox.ini @@ -4,15 +4,23 @@ # and then run "tox" from this directory. [tox] -envlist = py26, py27 +envlist = py26, py27, py34, py35 [testenv] -commands = nosetests +commands = nosetests {posargs:--with-coverage --cover-package=expiringdict} deps = nose + coverage + +[testenv:py35] +basepython = python3.5 + +[testenv:py27] +deps = + {[testenv]deps} mock [testenv:py26] deps = - {[testenv]deps} + {[testenv:py27]deps} ordereddict