Use pendulum for datetime operations when possible
Currently, we use both Python's built-in datetime library and Pendulum to do datetime operations. For the sake of consistency, we should try to stick to one library for datetimes. We could have used either, but Pendulum has a more ergonomic API, so I decided to go with it when possible. The places where were we didn't / couldn't replace datetime are: - checking instances of datetimes. Pendulum's objects are subclasses of python native datetime objects, so it's still useful to import datetime in those cases of using is_instance() - WTForms date validators expect datetime style string formats -- Pendulum has its own format for formatting/ parsing strings. As such, our custom validator DateRange needs to use datetime.stptime() to account for this format.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import pytest
|
||||
import pendulum
|
||||
|
||||
from atst.domain.permission_sets import PermissionSets
|
||||
from atst.domain.environment_roles import EnvironmentRoles
|
||||
@@ -61,7 +62,7 @@ def test_environment_roles():
|
||||
|
||||
|
||||
def test_display_status():
|
||||
yesterday = datetime.date.today() - datetime.timedelta(days=1)
|
||||
yesterday = pendulum.today().subtract(days=1)
|
||||
expired_invite = ApplicationInvitationFactory.create(expiration_time=yesterday)
|
||||
assert expired_invite.role.display_status == "invite_expired"
|
||||
|
||||
|
@@ -6,7 +6,6 @@ from tests.factories import (
|
||||
random_future_date,
|
||||
random_past_date,
|
||||
)
|
||||
import datetime
|
||||
import pendulum
|
||||
from decimal import Decimal
|
||||
import pytest
|
||||
@@ -83,7 +82,7 @@ def test_funding_duration(session):
|
||||
portfolio=portfolio,
|
||||
signed_at=random_past_date(),
|
||||
create_clins=[
|
||||
{"start_date": datetime.datetime.now(), "end_date": funding_end_date,}
|
||||
{"start_date": pendulum.now(tz="utc"), "end_date": funding_end_date,}
|
||||
],
|
||||
)
|
||||
|
||||
@@ -106,7 +105,7 @@ def test_days_remaining(session):
|
||||
|
||||
assert (
|
||||
portfolio.days_to_funding_expiration
|
||||
== (funding_end_date - datetime.date.today()).days
|
||||
== (funding_end_date - pendulum.today()).days
|
||||
)
|
||||
|
||||
# empty portfolio
|
||||
@@ -121,8 +120,8 @@ def test_active_task_orders(session):
|
||||
signed_at=random_past_date(),
|
||||
create_clins=[
|
||||
{
|
||||
"start_date": datetime.date(2019, 1, 1),
|
||||
"end_date": datetime.date(2019, 10, 31),
|
||||
"start_date": pendulum.date(2019, 1, 1),
|
||||
"end_date": pendulum.date(2019, 10, 31),
|
||||
}
|
||||
],
|
||||
)
|
||||
|
@@ -1,4 +1,4 @@
|
||||
import datetime
|
||||
import pendulum
|
||||
|
||||
from atst.models import InvitationStatus, PortfolioRoleStatus
|
||||
|
||||
@@ -17,7 +17,7 @@ def test_expired_invite_is_not_revokable():
|
||||
portfolio=portfolio, user=user, status=PortfolioRoleStatus.PENDING
|
||||
)
|
||||
invite = PortfolioInvitationFactory.create(
|
||||
expiration_time=datetime.datetime.now() - datetime.timedelta(minutes=60),
|
||||
expiration_time=pendulum.now(tz="utc").subtract(minutes=60),
|
||||
role=portfolio_role,
|
||||
)
|
||||
assert not invite.is_revokable
|
||||
|
@@ -1,5 +1,5 @@
|
||||
import pytest
|
||||
import datetime
|
||||
import pendulum
|
||||
|
||||
from atst.domain.environments import Environments
|
||||
from atst.domain.portfolios import Portfolios
|
||||
@@ -204,7 +204,7 @@ def test_status_when_invitation_is_expired():
|
||||
PortfolioInvitationFactory.create(
|
||||
role=portfolio_role,
|
||||
status=InvitationStatus.PENDING,
|
||||
expiration_time=datetime.datetime.now() - datetime.timedelta(seconds=1),
|
||||
expiration_time=pendulum.now(tz="utc").subtract(seconds=1),
|
||||
)
|
||||
assert portfolio_role.display_status == "invite_expired"
|
||||
|
||||
|
@@ -1,6 +1,6 @@
|
||||
from werkzeug.datastructures import FileStorage
|
||||
import pytest
|
||||
from datetime import date
|
||||
import pendulum
|
||||
from unittest.mock import patch, PropertyMock
|
||||
import pendulum
|
||||
|
||||
@@ -13,11 +13,11 @@ from tests.mocks import PDF_FILENAME
|
||||
|
||||
|
||||
def test_period_of_performance_is_first_to_last_clin():
|
||||
start_date = date(2019, 6, 6)
|
||||
end_date = date(2020, 6, 6)
|
||||
start_date = pendulum.date(2019, 6, 6)
|
||||
end_date = pendulum.date(2020, 6, 6)
|
||||
|
||||
intermediate_start_date = date(2019, 7, 1)
|
||||
intermediate_end_date = date(2020, 3, 1)
|
||||
intermediate_start_date = pendulum.date(2019, 7, 1)
|
||||
intermediate_end_date = pendulum.date(2020, 3, 1)
|
||||
|
||||
task_order = TaskOrderFactory.create(
|
||||
clins=[
|
||||
|
@@ -1,6 +1,6 @@
|
||||
import pytest
|
||||
from sqlalchemy.exc import InternalError
|
||||
from datetime import datetime
|
||||
import pendulum
|
||||
|
||||
from atst.database import db
|
||||
from atst.domain.users import Users
|
||||
@@ -44,7 +44,7 @@ def test_deleted_application_roles_are_ignored(session):
|
||||
|
||||
def test_does_not_log_user_update_when_updating_last_login(mock_logger):
|
||||
user = UserFactory.create()
|
||||
user.last_login = datetime.now()
|
||||
user.last_login = pendulum.now(tz="utc")
|
||||
db.session.add(user)
|
||||
db.session.commit()
|
||||
assert "Audit Event update" not in mock_logger.messages
|
||||
|
Reference in New Issue
Block a user