-
Notifications
You must be signed in to change notification settings - Fork 338
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
FutureDatetime and PastDatetime providers #779
Closed
Closed
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3470bcf
Add week_date() in the FutureDatetime provider
sinecode 3ce313c
Add test for the exception in FutureDatetime.week_date()
sinecode 112d348
Implemented FutureDatetime with composition instead of inheritance
sinecode 6353271
Add FutureDatetime provider
sinecode 9b17413
Fix Codefactor issue
sinecode 2b66578
Remove unnecessary methods from FutureDatetime
sinecode 0a8018b
Add PastDatetime provider
sinecode 7716c1e
Update CHANGELOG
sinecode File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
# -*- coding: utf-8 -*- | ||
import datetime | ||
import re | ||
|
||
import pytest | ||
|
||
from mimesis import Datetime, FutureDatetime | ||
from mimesis.data import GMT_OFFSETS, TIMEZONES | ||
|
||
from . import patterns | ||
|
||
|
||
class TestFutureDatetime(object): | ||
|
||
@pytest.fixture | ||
def future_dt(self): | ||
return FutureDatetime() | ||
|
||
def test_str(self, dt): | ||
assert re.match(patterns.DATA_PROVIDER_STR_REGEX, str(dt)) | ||
|
||
def test_week_date(self, future_dt): | ||
result = future_dt.week_date() | ||
result = result.replace('-', ' ').replace('W', '') | ||
year, week = result.split(' ') | ||
assert int(year) >= future_dt.future.year | ||
assert int(year) <= future_dt.future.year + 1 | ||
assert int(week) <= 52 | ||
|
||
with pytest.raises(ValueError): | ||
future_dt.week_date(end=datetime.MINYEAR) | ||
|
||
def test_year(self, future_dt): | ||
result = future_dt.year() | ||
assert result >= future_dt.future.year | ||
assert result <= future_dt.future.year + 65 | ||
|
||
with pytest.raises(ValueError): | ||
future_dt.year(maximum=datetime.MINYEAR) | ||
|
||
def test_date(self, future_dt): | ||
date_object = future_dt.date() | ||
assert isinstance(date_object, datetime.date) | ||
assert date_object.year >= future_dt.future.year | ||
assert date_object.year <= future_dt.future.year + 19 | ||
|
||
with pytest.raises(ValueError): | ||
future_dt.date(end=datetime.MINYEAR) | ||
|
||
def test_formatted_date(self, future_dt): | ||
fmt_date = future_dt.formatted_date('%Y', end=datetime.MAXYEAR) | ||
assert int(fmt_date) >= future_dt.future.year | ||
assert isinstance(fmt_date, str) | ||
|
||
@pytest.mark.parametrize( | ||
'end, timezone', [ | ||
(datetime.MAXYEAR, 'Europe/Paris'), | ||
(datetime.MAXYEAR, None), | ||
], | ||
) | ||
def test_datetime(self, future_dt, end, timezone): | ||
dt_obj = future_dt.datetime(end=end, timezone=timezone) | ||
|
||
assert future_dt.future.year <= dt_obj.year <= datetime.MAXYEAR | ||
assert isinstance(dt_obj, datetime.datetime) | ||
|
||
with pytest.raises(ValueError): | ||
future_dt.datetime(end=datetime.MINYEAR) | ||
|
||
def test_formatted_datetime(self, future_dt): | ||
dt_obj = future_dt.formatted_datetime('%Y', end=datetime.MAXYEAR) | ||
assert int(dt_obj) >= future_dt.future.year | ||
assert isinstance(dt_obj, str) | ||
|
||
def test_is_subclass(self, future_dt): | ||
datetime_methods = [method for method in dir(Datetime) | ||
if callable(getattr(Datetime, method))] | ||
assert len(datetime_methods) > 0 | ||
for method in datetime_methods: | ||
assert method in dir(future_dt) | ||
|
||
|
||
class TestSeededFutureDatetime(object): | ||
|
||
@pytest.fixture | ||
def d1(self, seed): | ||
return FutureDatetime(seed=seed) | ||
|
||
@pytest.fixture | ||
def d2(self, seed): | ||
return FutureDatetime(seed=seed) | ||
|
||
def test_week_date(self, d1, d2): | ||
assert d1.week_date() == d2.week_date() | ||
assert d1.week_date(end=datetime.MAXYEAR) == \ | ||
d2.week_date(end=datetime.MAXYEAR) | ||
|
||
def test_year(self, d1, d2): | ||
assert d1.year() == d2.year() | ||
assert d1.year(maximum=datetime.MAXYEAR) == \ | ||
d2.year(maximum=datetime.MAXYEAR) | ||
|
||
def test_date(self, d1, d2): | ||
assert d1.date() == d2.date() | ||
assert d1.date(end=datetime.MAXYEAR) == d2.date(end=datetime.MAXYEAR) | ||
|
||
def test_formatted_date(self, d1, d2): | ||
assert d1.formatted_date() == d2.formatted_date() | ||
|
||
def test_datetime(self, d1, d2): | ||
assert d1.datetime() == d2.datetime() | ||
assert d1.datetime(end=datetime.MAXYEAR) == \ | ||
d2.datetime(end=datetime.MAXYEAR) | ||
|
||
def test_formatted_datetime(self, d1, d2): | ||
assert d1.formatted_datetime() == d2.formatted_datetime() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@lk-geimfari @sobolevn I had to switch to composition instead of inheritance because of the incompatibility with the overloaded methods.
I know this section is a bit tricky. It can be replaced with:
which it automatically adds the methods not overridden, but it is more black magic and less explicit. What do you prefer?
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.
@ceccoemi I think that we need to implement only some methods in this provider, not all of them. We must decide which methods can be really useful in
FutureDatetime
.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.
@lk-geimfari ok, if you don't want all the methods then it's really easy.
I propose to keep
date
,formatted_date
,datetime
,formatted_datetime
andtimestamp
only.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.
@lk-geimfari see my last commit!
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.
@ceccoemi I agree with your propose.