Skip to content

Commit

Permalink
refactors environment.set_date test
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielBarberini committed Mar 6, 2024
1 parent 31fb869 commit 1ff9d89
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 24 deletions.
16 changes: 12 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1109,18 +1109,26 @@ def dimensionless_calisto(kg, m, dimensionless_cesaroni_m1670):

@pytest.fixture
def example_env():
"""Create a simple object of the Environment class to be used in the tests.
This allows to avoid repeating the same code in all tests. The environment
set here is the simplest possible, with no parameters set.
"""Simple object of the Environment class to be used in the tests.
Returns
-------
rocketpy.Environment
The simplest object of the Environment class
"""
return Environment()


@pytest.fixture
def example_date_naive():
"""Naive tomorrow date
Returns
-------
datetime.datetime
"""
return datetime.datetime.now()


@pytest.fixture
def example_env_robust():
"""Create an object of the Environment class to be used in the tests. This
Expand Down
29 changes: 9 additions & 20 deletions tests/unit/test_environment.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import datetime
import os

import numpy as np
Expand All @@ -8,41 +7,31 @@
from rocketpy import Environment


def test_env_set_date(example_env):
"""Test that the date is set correctly in the environment object. This
basically takes a date and time and converts it to a datetime object, then
set the date to the example environment object. The test checks if the
datetime object is the same as the one in the example environment object.
def test_date_naive_set_date(example_env, example_date_naive):
"""Test that the time zone is set correctly to UTC by default in the environment object for a date_naive input
Parameters
----------
example_env : rocketpy.Environment
Example environment object to be tested.
"""
tomorrow = datetime.date.today() + datetime.timedelta(days=1)
example_env.set_date((tomorrow.year, tomorrow.month, tomorrow.day, 12))
assert example_env.datetime_date == datetime.datetime(
tomorrow.year, tomorrow.month, tomorrow.day, 12, tzinfo=pytz.utc
)
example_env.set_date(example_date_naive)
assert example_env.datetime_date == pytz.utc.localize(example_date_naive)


def test_env_set_date_time_zone(example_env):
"""Test that the time zone is set correctly in the environment object
def test_date_aware_set_date(example_env, example_date_naive):
"""Test that the time zone is set accordingly in the environment object for a date_aware input
Parameters
----------
example_env : rocketpy.Environment
Example environment object to be tested.
"""
tomorrow = datetime.date.today() + datetime.timedelta(days=1)
example_env.set_date(
(tomorrow.year, tomorrow.month, tomorrow.day, 12), timezone="America/New_York"
example_date_naive, timezone="America/New_York"
)
date_naive = datetime.datetime(tomorrow.year, tomorrow.month, tomorrow.day, 12)
timezone = pytz.timezone("America/New_York")
date_aware_local_date = timezone.localize(date_naive)
date_aware_utc = date_aware_local_date.astimezone(pytz.UTC)
assert example_env.datetime_date == date_aware_utc
example_date_aware = pytz.timezone('America/New_York').localize(example_date_naive)
assert example_env.datetime_date == example_date_aware


def test_env_set_location(example_env):
Expand Down

0 comments on commit 1ff9d89

Please sign in to comment.