-
Notifications
You must be signed in to change notification settings - Fork 64
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
feat: allow decimal places for seconds setting in NOW replacement #416
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
""" | ||
|
||
import datetime | ||
import re | ||
from uuid import UUID | ||
|
||
import pytest | ||
|
@@ -182,18 +183,18 @@ def test_replace_param_datetime_language_ignored(): | |
|
||
def test_replace_param_today_spanish(): | ||
param = replace_param('[TODAY]', language='es') | ||
assert param == datetime.datetime.today().strftime('%d/%m/%Y') | ||
assert param == datetime.datetime.utcnow().strftime('%d/%m/%Y') | ||
|
||
|
||
def test_replace_param_today_not_spanish(): | ||
param = replace_param('[TODAY]', language='en') | ||
assert param == datetime.datetime.today().strftime('%Y/%m/%d') | ||
assert param == datetime.datetime.utcnow().strftime('%Y/%m/%d') | ||
|
||
|
||
def test_replace_param_today_offset(): | ||
param = replace_param('[TODAY - 1 DAYS]', language='es') | ||
assert param == datetime.datetime.strftime( | ||
datetime.datetime.today() - datetime.timedelta(days=1), '%d/%m/%Y') | ||
datetime.datetime.utcnow() - datetime.timedelta(days=1), '%d/%m/%Y') | ||
|
||
|
||
def test_replace_param_now_spanish(): | ||
|
@@ -211,6 +212,20 @@ def test_replace_param_now_with_format(): | |
assert param == datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ') | ||
|
||
|
||
def test_replace_param_now_with_format_and_decimals_limit(): | ||
param = replace_param('[NOW(%Y-%m-%dT%H:%M:%S.%3fZ)]') | ||
param_till_dot = param[:param.find('.')] | ||
assert param_till_dot == datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. estos tests funcionan siempre (la pregunta aplica a todos, incluidos los anteriores)? el now del replace param y esta llamada pueden caer en un segundo distinto y fallará el test no? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sí, lo pensé cuando los metí, pero he visto que tardan del orden de microsegundos en ejecutarse y en la práctica no fallan nunca. Con todo, si quieres, se puede meter lo que dices, pero casi preferiría que fuera en otra PR y aquí seguir una aproximación más continuista para no meter más cambios. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Como curiosidad, los que sí fallaban siempre entre las 00:00 y la 01:00 o entre las 00:00 y las 02:00 (según la época del año) cuando se ejecutaban en una máquina con hora española, eran los que usaban today(), porque tiene en cuenta la zona horaria, mientras nuestros replacements usan UTC. No preguntéis cómo me di cuenta 😆 |
||
assert re.match(param_till_dot + r'\.\d{3}Z', param) | ||
|
||
|
||
def test_replace_param_now_with_format_and_decimals_limit_beyond_microseconds(): | ||
param = replace_param('[NOW(%Y-%m-%dT%H:%M:%S.%12fZ)]') | ||
param_till_dot = param[:param.find('.')] | ||
assert param_till_dot == datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S') | ||
assert re.match(param_till_dot + r'\.\d{12}Z', param) | ||
|
||
|
||
def test_not_replace_param_now_with_invalid_opening_parenthesis_in_format(): | ||
param = replace_param('[NOW(%Y-%m-%dT(%H:%M:%SZ)]') | ||
assert param == '[NOW(%Y-%m-%dT(%H:%M:%SZ)]' | ||
|
@@ -236,14 +251,14 @@ def test_replace_param_now_offset_with_format(): | |
def test_replace_param_today_offset_and_more(): | ||
param = replace_param('The day [TODAY - 1 DAYS] was yesterday', language='es') | ||
offset_date = datetime.datetime.strftime( | ||
datetime.datetime.today() - datetime.timedelta(days=1), '%d/%m/%Y') | ||
datetime.datetime.utcnow() - datetime.timedelta(days=1), '%d/%m/%Y') | ||
assert param == f'The day {offset_date} was yesterday' | ||
|
||
|
||
def test_replace_param_today_offset_and_more_not_spanish(): | ||
param = replace_param('The day [TODAY - 1 DAYS] was yesterday', language='it') | ||
offset_date = datetime.datetime.strftime( | ||
datetime.datetime.today() - datetime.timedelta(days=1), '%Y/%m/%d') | ||
datetime.datetime.utcnow() - datetime.timedelta(days=1), '%Y/%m/%d') | ||
assert param == f'The day {offset_date} was yesterday' | ||
|
||
|
||
|
@@ -285,29 +300,29 @@ def test_replace_param_today_offset_with_format_and_more_with_extra_spaces(): | |
def test_replace_param_today_offset_and_more_with_extra_spaces(): | ||
param = replace_param('The day [TODAY - 1 DAYS ] was yesterday', language='es') | ||
offset_date = datetime.datetime.strftime( | ||
datetime.datetime.today() - datetime.timedelta(days=1), '%d/%m/%Y') | ||
datetime.datetime.utcnow() - datetime.timedelta(days=1), '%d/%m/%Y') | ||
assert param == f'The day {offset_date} was yesterday' | ||
|
||
|
||
def test_replace_param_today_offset_and_more_at_the_end(): | ||
param = replace_param('Yesterday was [TODAY - 1 DAYS]', language='es') | ||
offset_date = datetime.datetime.strftime( | ||
datetime.datetime.today() - datetime.timedelta(days=1), '%d/%m/%Y') | ||
datetime.datetime.utcnow() - datetime.timedelta(days=1), '%d/%m/%Y') | ||
assert param == f'Yesterday was {offset_date}' | ||
|
||
|
||
def test_replace_param_today_offset_and_more_at_the_beginning(): | ||
param = replace_param('[TODAY - 1 DAYS] is yesterday', language='es') | ||
offset_date = datetime.datetime.strftime( | ||
datetime.datetime.today() - datetime.timedelta(days=1), '%d/%m/%Y') | ||
datetime.datetime.utcnow() - datetime.timedelta(days=1), '%d/%m/%Y') | ||
assert param == f'{offset_date} is yesterday' | ||
|
||
|
||
def test_replace_param_today_offsets_and_more(): | ||
param = replace_param( | ||
'The day [TODAY - 1 DAYS] was yesterday and I have an appointment at [NOW + 10 MINUTES]', language='es') | ||
offset_date = datetime.datetime.strftime( | ||
datetime.datetime.today() - datetime.timedelta(days=1), '%d/%m/%Y') | ||
datetime.datetime.utcnow() - datetime.timedelta(days=1), '%d/%m/%Y') | ||
offset_datetime = datetime.datetime.strftime( | ||
datetime.datetime.utcnow() + datetime.timedelta(minutes=10), '%d/%m/%Y %H:%M:%S') | ||
assert param == f'The day {offset_date} was yesterday and I have an appointment at {offset_datetime}' | ||
|
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.
este método está deprecado hace tiempo en python, la recomendación es usar
.now(datetime.timezone.utc)
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.
Ídem, también me fijé y también lo vi como un cambio que se podría hacer de manera generalizada en otra PR si queremos seguir la recomendación.