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

Fixed time zone issue in tests #11

Merged
merged 1 commit into from Feb 26, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 12 additions & 15 deletions tests/test_cache_control.py
Expand Up @@ -3,14 +3,13 @@
"""
import pytest
from mock import Mock
import datetime
import time

from cachecontrol import CacheController
from cachecontrol.cache import DictCache


TIME_FMT = "%a, %d %b %Y %H:%M:%S"
TIME_FMT = "%a, %d %b %Y %H:%M:%S GMT"


class TestCacheControllerResponse(object):
Expand All @@ -35,7 +34,7 @@ def cc(self):

def test_no_cache_non_20x_response(self, cc):
# No caching without some extra headers, so we add them
now = datetime.datetime.utcnow().strftime(TIME_FMT)
now = time.strftime(TIME_FMT, time.gmtime())
resp = self.resp({'cache-control': 'max-age=3600',
'date': now})

Expand Down Expand Up @@ -64,8 +63,7 @@ def test_cache_response_no_cache_control(self, cc):
assert not cc.cache.set.called

def test_cache_response_cache_max_age(self, cc):

now = datetime.datetime.utcnow().strftime(TIME_FMT)
now = time.strftime(TIME_FMT, time.gmtime())
resp = self.resp({'cache-control': 'max-age=3600',
'date': now})
cc.cache_response(self.req(), resp)
Expand Down Expand Up @@ -115,7 +113,7 @@ def test_cache_request_not_in_cache(self):
assert not resp

def test_cache_request_fresh_max_age(self):
now = datetime.datetime.utcnow().strftime(TIME_FMT)
now = time.strftime(TIME_FMT, time.gmtime())
resp = Mock(headers={'cache-control': 'max-age=3600',
'date': now})

Expand All @@ -125,19 +123,18 @@ def test_cache_request_fresh_max_age(self):
assert r == resp

def test_cache_request_unfresh_max_age(self):
earlier = time.time() - 3700
now = datetime.datetime.fromtimestamp(earlier).strftime(TIME_FMT)

earlier = time.time() - 3700 # epoch - 1h01m40s
now = time.strftime(TIME_FMT, time.gmtime(earlier))
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The useful part is here, I decided then to remove all datetime references but this is not necessary, only a matter of taste because I believe its naive/aware approach of TZ is confusing.

resp = Mock(headers={'cache-control': 'max-age=3600',
'date': now})
self.c.cache = DictCache({self.url: resp})
r = self.req({})
assert not r

def test_cache_request_fresh_expires(self):
later = datetime.timedelta(days=1)
expires = (datetime.datetime.utcnow() + later).strftime(TIME_FMT)
now = datetime.datetime.utcnow().strftime(TIME_FMT)
later = time.time() + 86400 # GMT + 1 day
expires = time.strftime(TIME_FMT, time.gmtime(later))
now = time.strftime(TIME_FMT, time.gmtime())
resp = Mock(headers={'expires': expires,
'date': now})
cache = DictCache({self.url: resp})
Expand All @@ -146,9 +143,9 @@ def test_cache_request_fresh_expires(self):
assert r == resp

def test_cache_request_unfresh_expires(self):
later = datetime.timedelta(days=-1)
expires = (datetime.datetime.utcnow() + later).strftime(TIME_FMT)
now = datetime.datetime.utcnow().strftime(TIME_FMT)
sooner = time.time() - 86400 # GMT - 1 day
expires = time.strftime(TIME_FMT, time.gmtime(sooner))
now = time.strftime(TIME_FMT, time.gmtime())
resp = Mock(headers={'expires': expires,
'date': now})
cache = DictCache({self.url: resp})
Expand Down