-
Notifications
You must be signed in to change notification settings - Fork 6
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
Create the time_travel API #4
Conversation
setup.py
Outdated
@@ -1,13 +1,15 @@ | |||
from setuptools import setup | |||
|
|||
version = '0.0.1' | |||
version = '0.1.0' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest you use bumpversion
for this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
src/time_travel/time_travel.py
Outdated
"""Initialize a unifing clock.""" | ||
self._timestamp = start_timestamp | ||
|
||
def get_timestamp(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you need this, given the property?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To solve early evaluation of the property when passing it to the mock's side_effect.
Moved the function to the relevant patcher.
src/time_travel/time_travel.py
Outdated
self._timestamp = float(timestatmp) | ||
|
||
def advance_timestamp(self, secs): | ||
"""Advance the clock timestamp since the epoch.""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copy pasta?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorta. Done.
src/time_travel/time_travel.py
Outdated
|
||
self.patches = [DatetimePatcher(self.clock), | ||
TimePatcher(self.clock), | ||
] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really weird formatting.
IMHO it should be either
lst = [a,
b]
or
lst = [
a.
b.
]
And I strongly prefer the latter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
@@ -0,0 +1,2 @@ | |||
[metadata] | |||
description-file = README.md |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😃
src/time_travel/datetime_patcher.py
Outdated
"""Return a datetime object of the currently set time.""" | ||
return self.real_datetime(self.timestamp) | ||
def _now(self): | ||
return self.real_fromtimestamp(self.clock.get_timestamp()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use clock.timestamp
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
src/time_travel/time_patcher.py
Outdated
@@ -10,38 +10,36 @@ class TimePatcher(object): | |||
- time | |||
""" | |||
|
|||
def __init__(self, start_time=0.0): | |||
def __init__(self, clock): | |||
"""Create the patch. | |||
|
|||
@start_time is time in seconds since the epoch. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is outdated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done?
src/time_travel/time_patcher.py
Outdated
self.sleep_patch.start() | ||
# def advance_time(self, secs): | ||
# """Increase the returned time in a given amount of seconds.""" | ||
# self.clock.advance_time(secs) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
src/time_travel/datetime_patcher.py
Outdated
"""Stop mocking datetime module.""" | ||
for p in self.patches: | ||
p.stop() | ||
|
||
def __enter__(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you need to allow this to be used as a context manager? I suggest you have a single API interface (=TimeTravel
).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
src/time_travel/time_patcher.py
Outdated
|
||
def start(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The patchers are pretty similar (this method for example) - you can have a common base class.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
assert time.time() == 7200 | ||
|
||
|
||
def test_datetime_patch_set_time(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing an assert.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
src/time_travel/datetime_patcher.py
Outdated
|
||
self.real_datetime = datetime_lib.datetime.fromtimestamp | ||
self.real_fromtimestamp = datetime_lib.datetime.fromtimestamp | ||
|
||
self.datetime = mock.Mock(wraps=datetime_lib.datetime) | ||
self.datetime.mock_add_spec( | ||
["today", "now", "utcnow", "fromtimestamp"]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, I mean, why do you need to mock this at all? It looks like you're only reimplementing today
.
No description provided.