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

Create the time_travel API #4

Merged
merged 11 commits into from
Jul 21, 2017
Merged

Create the time_travel API #4

merged 11 commits into from
Jul 21, 2017

Conversation

snudler6
Copy link
Owner

@snudler6 snudler6 commented Jul 8, 2017

No description provided.

@snudler6 snudler6 self-assigned this Jul 8, 2017
setup.py Outdated
@@ -1,13 +1,15 @@
from setuptools import setup

version = '0.0.1'
version = '0.1.0'
Copy link
Contributor

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.

Copy link
Owner Author

Choose a reason for hiding this comment

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

Done.

"""Initialize a unifing clock."""
self._timestamp = start_timestamp

def get_timestamp(self):
Copy link
Contributor

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?

Copy link
Owner Author

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.

self._timestamp = float(timestatmp)

def advance_timestamp(self, secs):
"""Advance the clock timestamp since the epoch."""
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy pasta?

Copy link
Owner Author

Choose a reason for hiding this comment

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

Sorta. Done.


self.patches = [DatetimePatcher(self.clock),
TimePatcher(self.clock),
]
Copy link
Contributor

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.

Copy link
Owner Author

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
Copy link
Contributor

Choose a reason for hiding this comment

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

Cool.

Copy link
Owner Author

Choose a reason for hiding this comment

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

😃

"""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())
Copy link
Contributor

Choose a reason for hiding this comment

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

Use clock.timestamp.

Copy link
Owner Author

Choose a reason for hiding this comment

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

Done.

@@ -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.
Copy link
Contributor

Choose a reason for hiding this comment

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

This is outdated.

Copy link
Owner Author

Choose a reason for hiding this comment

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

Done?

self.sleep_patch.start()
# def advance_time(self, secs):
# """Increase the returned time in a given amount of seconds."""
# self.clock.advance_time(secs)
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove?

Copy link
Owner Author

Choose a reason for hiding this comment

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

Done.

"""Stop mocking datetime module."""
for p in self.patches:
p.stop()

def __enter__(self):
Copy link
Contributor

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).

Copy link
Owner Author

Choose a reason for hiding this comment

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

Done.


def start(self):
Copy link
Contributor

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.

Copy link
Owner Author

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():
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing an assert.

Copy link
Owner Author

Choose a reason for hiding this comment

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

Done.


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"])
Copy link
Contributor

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.

@snudler6 snudler6 merged commit c16ac2d into master Jul 21, 2017
@snudler6 snudler6 deleted the feature/Add_main_API branch July 21, 2017 07:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants