Skip to content

Commit

Permalink
Add baseline software tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amotl committed Aug 23, 2023
1 parent 3cf5440 commit c89d11b
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TESTDRIVE_DATETIME = "2023-08-17T23:03:17+0200"
86 changes: 86 additions & 0 deletions tests/test_arbitrary_dateparser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Copyright (c) 2023, The Panodata developers and contributors.
# Distributed under the terms of the MIT license, see LICENSE.

import datetime as dt

from freezegun import freeze_time

from aika import DaterangeExpression
from tests import TESTDRIVE_DATETIME


@freeze_time(TESTDRIVE_DATETIME)
def test_single_empty():
"""
An empty date range constraint should yield the same result as when using "now".
"""
dr = DaterangeExpression()
assert (
dr.parse("")
== dr.parse("now")
== (
dt.datetime(2023, 8, 17, 23, 3, 17),
dt.datetime(2023, 8, 17, 23, 59, 59, 999999),
)
)


@freeze_time(TESTDRIVE_DATETIME)
def test_range_basics():
# Make the parser exclusively use `arbitrary-dateparser`.
from aika.core import adp_parse_english

dr = DaterangeExpression()
dr.clear_parsers()
dr.add_parser(name="arbitrary-dateparser [en]", fun=adp_parse_english)

# Verify parsing.
assert dr.parse("jul 1 to jul 7") == (
dt.datetime(2023, 7, 1, 0, 0),
dt.datetime(2023, 7, 7, 23, 59, 59, 999999),
)


@freeze_time(TESTDRIVE_DATETIME)
def test_range_relative():
dr = DaterangeExpression()
assert dr.parse("tomorrow to next thursday") == (
dt.datetime(2023, 8, 18, 0, 0, 0),
dt.datetime(2023, 8, 24, 23, 59, 59, 999999),
)
assert dr.parse("tomorrow - next week") == (
dt.datetime(2023, 8, 18, 0, 0, 0),
dt.datetime(2023, 8, 27, 23, 59, 59, 999999),
)
assert dr.parse("next month") == (
dt.datetime(2023, 9, 1, 0, 0, 0),
dt.datetime(2023, 9, 30, 23, 59, 59, 999999),
)
assert dr.parse("december") == (
dt.datetime(2023, 12, 1, 0, 0, 0),
dt.datetime(2023, 12, 31, 23, 59, 59, 999999),
)


@freeze_time(TESTDRIVE_DATETIME)
def test_default_start_time():
dr = DaterangeExpression(
default_start_time=dt.time(hour=9),
default_end_time=dt.time(hour=17),
)
assert dr.parse("tomorrow to next thursday") == (
dt.datetime(2023, 8, 18, 9, 0, 0),
dt.datetime(2023, 8, 24, 17, 0, 0),
)


@freeze_time(TESTDRIVE_DATETIME)
def test_range_relative_weekdays():
dr = DaterangeExpression(
default_start_time=dt.time(hour=9),
default_end_time=dt.time(hour=17),
)
assert dr.parse("Sat - Tue") == (
dt.datetime(2023, 8, 19, 9, 0, 0),
dt.datetime(2023, 8, 22, 17, 0, 0),
)
83 changes: 83 additions & 0 deletions tests/test_daterangeparser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Copyright (c) 2023, The Panodata developers and contributors.
# Distributed under the terms of the MIT license, see LICENSE.

import datetime as dt

from freezegun import freeze_time

from aika import DaterangeExpression
from tests import TESTDRIVE_DATETIME


@freeze_time(TESTDRIVE_DATETIME)
def test_range_basics():
dr = DaterangeExpression()
assert dr.parse("jul 1 to jul 7") == (
dt.datetime(2023, 7, 1, 0, 0),
dt.datetime(2023, 7, 7, 23, 59, 59, 999999),
)

assert dr.parse("1-7 july") == (
dt.datetime(2023, 7, 1, 0, 0),
dt.datetime(2023, 7, 7, 23, 59, 59, 999999),
)


@freeze_time(TESTDRIVE_DATETIME)
def test_single_basics():
dr = DaterangeExpression()
assert dr.parse("1st july") == (
dt.datetime(2023, 7, 1, 0, 0),
dt.datetime(2023, 7, 1, 23, 59, 59, 999999),
)
assert dr.parse("March 2024") == (
dt.datetime(2024, 3, 1, 0, 0, 0),
dt.datetime(2024, 3, 31, 23, 59, 59, 999999),
)


@freeze_time(TESTDRIVE_DATETIME)
def test_single_relative():
dr = DaterangeExpression()
assert dr.parse("today") == (
dt.datetime(2023, 8, 17, 0, 0, 0),
dt.datetime(2023, 8, 17, 23, 59, 59, 999999),
)
assert dr.parse("now") == (
dt.datetime(2023, 8, 17, 23, 3, 17),
dt.datetime(2023, 8, 17, 23, 59, 59, 999999),
)


@freeze_time(TESTDRIVE_DATETIME)
def test_default_start_time():
dr = DaterangeExpression(default_start_time=dt.time(hour=9))
assert dr.parse("jul 1 to jul 7") == (
dt.datetime(2023, 7, 1, 9, 0),
dt.datetime(2023, 7, 7, 23, 59, 59, 999999),
)
assert dr.parse("today") == (
dt.datetime(2023, 8, 17, 9, 0, 0),
dt.datetime(2023, 8, 17, 23, 59, 59, 999999),
)
# A specific datetime must not be changed through `default_start_time`.
assert dr.parse("now") == (
dt.datetime(2023, 8, 17, 23, 3, 17),
dt.datetime(2023, 8, 17, 23, 59, 59, 999999),
)


@freeze_time(TESTDRIVE_DATETIME)
def test_months():
dr = DaterangeExpression(
default_start_time=dt.time(hour=9),
default_end_time=dt.time(hour=17),
)
assert dr.parse("March") == (
dt.datetime(2023, 3, 1, 9, 0, 0),
dt.datetime(2023, 3, 31, 17, 0, 0),
)
assert dr.parse("July to December") == (
dt.datetime(2023, 7, 1, 9, 0, 0),
dt.datetime(2023, 12, 31, 17, 0, 0),
)
24 changes: 24 additions & 0 deletions tests/test_misc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright (c) 2023, The Panodata developers and contributors.
# Distributed under the terms of the MIT license, see LICENSE.

import datetime as dt

import pytest

from aika.misc import next_date_by_weekday


def test_weekdaymap_de():
date_value = next_date_by_weekday("do")
assert isinstance(date_value, dt.date)


def test_weekdaymap_en():
date_value = next_date_by_weekday("su")
assert isinstance(date_value, dt.date)


def test_weekdaymap_fail():
with pytest.raises(KeyError) as ex:
next_date_by_weekday("foo")
assert ex.match("Weekday not found: foo")

0 comments on commit c89d11b

Please sign in to comment.