Skip to content

Commit

Permalink
add support for expireat
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Tsai committed Jul 5, 2013
1 parent 0acbe3a commit 77864a4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
8 changes: 8 additions & 0 deletions mockredis/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ def expire(self, key, seconds, currenttime=datetime.now()):
return 1
return 0

def expireat(self, key, when):
"""Emulate expireat"""
expire_time = datetime.fromtimestamp(when)
if key in self.redis:
self.timeouts[key] = expire_time
return 1
return 0

def ttl(self, key, currenttime=datetime.now()):
"""
Emulate ttl
Expand Down
16 changes: 16 additions & 0 deletions mockredis/tests/test_redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,22 @@ def test_ttl_no_timeout(self):
self.redis.set('key', 'key')
self.assertEqual(self.redis.ttl('key'), None)

def test_expireat_calculates_time(self):
"""
test whether expireat sets the correct ttl, setting a timestamp 30s in the future
"""
import time
from datetime import datetime

self.redis.set('key', 'key')
self.redis.expireat('key', time.mktime(datetime.utcnow().timetuple()) + 30)

result = self.redis.ttl('key')
# should be an int
self.assertTrue(isinstance(result, int))
# should be less than the timeout originally set
self.assertTrue(result <= 30)

def test_push_pop_returns_str(self):
key = 'l'
values = ['5', 5, [], {}]
Expand Down

0 comments on commit 77864a4

Please sign in to comment.