Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make pip-installable for python3 and update tests #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ tramp

# Org-mode
.org-id-locations
*_archive
*_archive
1 change: 0 additions & 1 deletion expiringdict/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
7 changes: 1 addition & 6 deletions setup.py
Original file line number Diff line number Diff line change
@@ -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',
Expand Down
6 changes: 5 additions & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
@@ -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 *
60 changes: 46 additions & 14 deletions tests/expiringdict_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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()], [])


Expand All @@ -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'))

Expand Down
14 changes: 11 additions & 3 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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