Skip to content

Commit

Permalink
DateTime and more: Improve test case, also using sa.TIMESTAMP
Browse files Browse the repository at this point in the history
  • Loading branch information
amotl committed Jun 24, 2024
1 parent f52cd8c commit d3ca5df
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions tests/datetime_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ def dst(self, date_time):
return timedelta(seconds=-7200)


INPUT_DATE = dt.date(2009, 5, 13)
INPUT_DATETIME_NOTZ = dt.datetime(2009, 5, 13, 19, 19, 30, 123456)
INPUT_DATETIME_TZ = dt.datetime(2009, 5, 13, 19, 19, 30, 123456, tzinfo=CST())
OUTPUT_DATE = INPUT_DATE
OUTPUT_TIME = dt.time(19, 19, 30, 123000)
OUTPUT_DATETIME_NOTZ = dt.datetime(2009, 5, 13, 19, 19, 30, 123000)
OUTPUT_DATETIME_TZ = dt.datetime(2009, 5, 13, 19, 19, 30, 123000)


@skipIf(SA_VERSION < SA_1_4, "SQLAlchemy 1.3 suddenly has problems with these test cases")
@patch('crate.client.connection.Cursor', FakeCursor)
class SqlAlchemyDateAndDateTimeTest(TestCase):
Expand Down Expand Up @@ -91,7 +100,7 @@ def test_date_can_handle_datetime(self):
def test_date_can_handle_tz_aware_datetime(self):
character = self.Character()
character.name = "Athur"
character.timestamp = datetime(2009, 5, 13, 19, 19, 30, tzinfo=CST())
character.timestamp = INPUT_DATETIME_NOTZ
self.session.add(character)


Expand All @@ -103,6 +112,7 @@ class FooBar(Base):
name = sa.Column(sa.String, primary_key=True)
date = sa.Column(sa.Date)
datetime = sa.Column(sa.DateTime)
timestamp = sa.Column(sa.TIMESTAMP)


@pytest.fixture
Expand All @@ -124,19 +134,21 @@ def test_datetime_notz(session):
# Insert record.
foo_item = FooBar(
name="foo",
date=dt.date(2009, 5, 13),
datetime=dt.datetime(2009, 5, 13, 19, 19, 30, 123456),
date=INPUT_DATE,
datetime=INPUT_DATETIME_NOTZ,
timestamp=INPUT_DATETIME_NOTZ,
)
session.add(foo_item)
session.commit()
session.execute(sa.text("REFRESH TABLE foobar"))

# Query record.
result = session.execute(sa.select(FooBar.name, FooBar.date, FooBar.datetime)).mappings().first()
result = session.execute(sa.select(FooBar.name, FooBar.date, FooBar.datetime, FooBar.timestamp)).mappings().first()

# Compare outcome.
assert result["date"].year == 2009
assert result["datetime"].year == 2009
assert result["date"] == OUTPUT_DATE
assert result["datetime"] == OUTPUT_DATETIME_NOTZ
assert result["timestamp"] == OUTPUT_DATETIME_NOTZ
assert result["datetime"].tzname() is None
assert result["datetime"].timetz() == dt.time(19, 19, 30, 123000)
assert result["datetime"].tzinfo is None
Expand All @@ -152,18 +164,20 @@ def test_datetime_tz(session):
foo_item = FooBar(
name="foo",
date=dt.date(2009, 5, 13),
datetime=dt.datetime(2009, 5, 13, 19, 19, 30, 123456, tzinfo=CST()),
datetime=INPUT_DATETIME_TZ,
timestamp=INPUT_DATETIME_TZ,
)
session.add(foo_item)
session.commit()
session.execute(sa.text("REFRESH TABLE foobar"))

# Query record.
result = session.execute(sa.select(FooBar.name, FooBar.date, FooBar.datetime)).mappings().first()
result = session.execute(sa.select(FooBar.name, FooBar.date, FooBar.datetime, FooBar.timestamp)).mappings().first()

# Compare outcome.
assert result["date"].year == 2009
assert result["datetime"].year == 2009
assert result["date"] == OUTPUT_DATE
assert result["datetime"] == OUTPUT_DATETIME_TZ
assert result["timestamp"] == OUTPUT_DATETIME_TZ
assert result["datetime"].tzname() is None
assert result["datetime"].timetz() == dt.time(19, 19, 30, 123000)
assert result["datetime"].timetz() == OUTPUT_TIME
assert result["datetime"].tzinfo is None

0 comments on commit d3ca5df

Please sign in to comment.