Skip to content

Commit

Permalink
corrected _from and from_ keywords / params - Fixes #108
Browse files Browse the repository at this point in the history
  • Loading branch information
bensteUEM committed Oct 4, 2024
1 parent 8cd66cb commit 8b97ae9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 38 deletions.
20 changes: 10 additions & 10 deletions churchtools_api/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ def get_bookings(self, **kwargs) -> list[dict]:
booking_id: int: only one booking by id (use standalone only)
resources_ids:list[int]: required if not booking_id
status_ids: list[int]: filter by list of stats ids to consider (requires resource_ids)
_from: datetime: date range to consider (use only with _to! - might have a bug in API - Support Ticket 130123)
_to: datetime: date range to consider (use only with _from! - might have a bug in API - Support Ticket 130123)
appointment_id: int: get resources for one specific calendar_appointment only (use together with _to and _from for performance reasons)
from_: datetime: date range to consider (use only with to_! - might have a bug in API - Support Ticket 130123)
to_: datetime: date range to consider (use only with from_! - might have a bug in API - Support Ticket 130123)
appointment_id: int: get resources for one specific calendar_appointment only (use together with to_ and from_ for performance reasons)
"""
url = self.domain + "/api/bookings"
headers = {"accept": "application/json"}
Expand All @@ -81,15 +81,15 @@ def get_bookings(self, **kwargs) -> list[dict]:

if status_ids := kwargs.get("status_ids"):
params["status_ids[]"] = status_ids
if "_from" in kwargs or "_to" in kwargs:
if "_from" not in kwargs or "_to" not in kwargs:
if "from_" in kwargs or "to_" in kwargs:
if "from_" not in kwargs or "to_" not in kwargs:
logger.info(
"missing _from or _to defaults to first or last day of current month"
"missing from_ or to_ defaults to first or last day of current month"
)
if _from := kwargs.get("_from"):
params["from"] = _from.strftime("%Y-%m-%d")
if _to := kwargs.get("_to"):
params["to"] = _to.strftime("%Y-%m-%d")
if from_ := kwargs.get("from_"):
params["from"] = from_.strftime("%Y-%m-%d")
if to_ := kwargs.get("to_"):
params["to"] = to_.strftime("%Y-%m-%d")
if appointment_id := kwargs.get("appointment_id"):
if "from" not in params:
logger.warning(
Expand Down
56 changes: 28 additions & 28 deletions tests/test_churchtools_api_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ def test_get_booking_from_to_date_without_resource_id(self, caplog):
the hard coded sample exists on ELKW1610.KRZ.TOOLS"""
caplog.set_level(logging.WARNING)
SAMPLE_DATES = {
"_from": datetime(year=2024, month=12, day=24),
"_to": datetime(year=2024, month=12, day=24),
"from_": datetime(year=2024, month=12, day=24),
"to_": datetime(year=2024, month=12, day=24),
}
self.api.get_bookings(_from=SAMPLE_DATES["_from"])
self.api.get_bookings(from_=SAMPLE_DATES["from_"])

expected_response = "invalid argument combination in get_bookings - please check docstring for requirements"
assert expected_response in caplog.messages
Expand All @@ -117,12 +117,12 @@ def test_get_booking_from_date(self, caplog):
caplog.set_level(logging.INFO)
RESOURCE_ID_SAMPLES = [8, 20]
SAMPLE_DATES = {
"_from": datetime(year=2024, month=9, day=21),
"_to": datetime(year=2024, month=9, day=30),
"from_": datetime(year=2024, month=9, day=21),
"to_": datetime(year=2024, month=9, day=30),
}

result = self.api.get_bookings(
_from=SAMPLE_DATES["_from"], resource_ids=RESOURCE_ID_SAMPLES
from_=SAMPLE_DATES["from_"], resource_ids=RESOURCE_ID_SAMPLES
)
assert set(RESOURCE_ID_SAMPLES) == {i["base"]["resource"]["id"] for i in result}

Expand All @@ -131,11 +131,11 @@ def test_get_booking_from_date(self, caplog):
for i in result
}
assert all(
[SAMPLE_DATES["_from"] <= compare_date for compare_date in result_dates]
[SAMPLE_DATES["from_"] <= compare_date for compare_date in result_dates]
)

expected_response = (
"missing _from or _to defaults to first or last day of current month"
"missing from_ or to_ defaults to first or last day of current month"
)
assert expected_response in caplog.messages

Expand All @@ -145,11 +145,11 @@ def test_get_booking_to_date(self, caplog):
caplog.set_level(logging.INFO)
RESOURCE_ID_SAMPLES = [8, 20]
SAMPLE_DATES = {
"_to": datetime.now() + timedelta(days=30),
"to_": datetime.now() + timedelta(days=30),
}

result = self.api.get_bookings(
_to=SAMPLE_DATES["_to"], resource_ids=RESOURCE_ID_SAMPLES
to_=SAMPLE_DATES["to_"], resource_ids=RESOURCE_ID_SAMPLES
)
assert set(RESOURCE_ID_SAMPLES) == {i["base"]["resource"]["id"] for i in result}

Expand All @@ -158,11 +158,11 @@ def test_get_booking_to_date(self, caplog):
for i in result
}
assert all(
[SAMPLE_DATES["_to"] >= compare_date for compare_date in result_dates]
[SAMPLE_DATES["to_"] >= compare_date for compare_date in result_dates]
)

expected_response = (
"missing _from or _to defaults to first or last day of current month"
"missing from_ or to_ defaults to first or last day of current month"
)
assert expected_response in caplog.messages

Expand All @@ -171,15 +171,15 @@ def test_get_booking_from_to_date(self, caplog):
the hard coded sample exists on ELKW1610.KRZ.TOOLS"""
RESOURCE_ID_SAMPLES = [8, 20]
SAMPLE_DATES = {
"_from": datetime(year=2024, month=9, day=21),
"_to": datetime(year=2024, month=9, day=30),
"from_": datetime(year=2024, month=9, day=21),
"to_": datetime(year=2024, month=9, day=30),
}

caplog.set_level(logging.WARNING)

result = self.api.get_bookings(
_from=SAMPLE_DATES["_from"],
_to=SAMPLE_DATES["_to"],
from_=SAMPLE_DATES["from_"],
to_=SAMPLE_DATES["to_"],
resource_ids=RESOURCE_ID_SAMPLES,
)
assert set(RESOURCE_ID_SAMPLES) == {i["base"]["resource"]["id"] for i in result}
Expand All @@ -189,10 +189,10 @@ def test_get_booking_from_to_date(self, caplog):
for i in result
}
assert all(
[SAMPLE_DATES["_from"] <= compare_date for compare_date in result_dates]
[SAMPLE_DATES["from_"] <= compare_date for compare_date in result_dates]
)
assert all(
[SAMPLE_DATES["_to"] >= compare_date for compare_date in result_dates]
[SAMPLE_DATES["to_"] >= compare_date for compare_date in result_dates]
)

assert [] == caplog.messages
Expand All @@ -204,15 +204,15 @@ def test_get_booking_appointment_id(self, caplog):

RESOURCE_ID_SAMPLES = [16]
SAMPLE_DATES = {
"_from": datetime(year=2024, month=9, day=21),
"_to": datetime(year=2024, month=9, day=30),
"from_": datetime(year=2024, month=9, day=21),
"to_": datetime(year=2024, month=9, day=30),
}
SAMPLE_APPOINTMENT_ID = 327883 # 22.9.2024 GH
result = self.api.get_bookings(
appointment_id=SAMPLE_APPOINTMENT_ID,
resource_ids=RESOURCE_ID_SAMPLES,
_from=SAMPLE_DATES["_from"],
_to=SAMPLE_DATES["_to"],
from_=SAMPLE_DATES["from_"],
to_=SAMPLE_DATES["to_"],
)

assert result[0]["base"]["caption"] == "Zentral-Gottesdienst im Gemeindehaus"
Expand All @@ -232,13 +232,13 @@ def test_get_booking_appointment_id_daterange(self):

SAMPLE_APPOINTMENT_ID = 327883
SAMPLE_DATES = {
"_from": datetime(year=2024, month=9, day=22),
"_to": datetime(year=2024, month=9, day=22),
"from_": datetime(year=2024, month=9, day=22),
"to_": datetime(year=2024, month=9, day=22),
}

result = self.api.get_bookings(
_from=SAMPLE_DATES["_from"],
_to=SAMPLE_DATES["_to"],
from_=SAMPLE_DATES["from_"],
to_=SAMPLE_DATES["to_"],
appointment_id=SAMPLE_APPOINTMENT_ID,
resource_ids=RESOURCE_ID_SAMPLES,
)
Expand All @@ -249,7 +249,7 @@ def test_get_booking_appointment_id_daterange(self):

assert len(result) == 1
# check dates incl. max 1 day diff because of reservations before event start
assert SAMPLE_DATES["_from"] - timedelta(days=1) <= result_date
assert SAMPLE_DATES["_to"] + timedelta(days=1) >= result_date
assert SAMPLE_DATES["from_"] - timedelta(days=1) <= result_date
assert SAMPLE_DATES["to_"] + timedelta(days=1) >= result_date
assert result[0]["base"]["caption"] == "Zentral-Gottesdienst im Gemeindehaus"
assert result[0]["base"]["resource"]["id"] in set(RESOURCE_ID_SAMPLES)

0 comments on commit 8b97ae9

Please sign in to comment.