Skip to content

Commit

Permalink
test: change test method for test_get_calendar_text
Browse files Browse the repository at this point in the history
  • Loading branch information
tbmc committed May 7, 2024
1 parent ed78b6e commit 854e1b8
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 26 deletions.
1 change: 0 additions & 1 deletion .github/workflows/build_image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ on:

jobs:
build_and_publish_image:

permissions:
contents: read
packages: write
Expand Down
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![Github Container Pulls](https://img.shields.io/badge/Github%20Container%20Pulls-780-blue
[![Github Container Pulls](https://img.shields.io/badge/Github%20Container%20Pulls-960-blue
)](https://github.com/tbmc/sporteasy-calendar-connector/pkgs/container/sporteasy-calendar-connector)
[![Docker Pulls](https://img.shields.io/docker/pulls/tbmc/sporteasy-calendar-connector)](https://hub.docker.com/r/tbmc/sporteasy-calendar-connector)

Expand Down
35 changes: 25 additions & 10 deletions test/test_get_calendar_text.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,48 @@
import datetime
import importlib
from unittest.mock import patch, MagicMock

import requests_mock

import utils.calendar_converter
from utils.consts import url_list_events, url_list_teams, url_authenticate
from .test_utils import (
read_text_by_name,
replace_unwanted_lines,
replace_last_sync_and_sequence,
)
from utils.calendar_converter import CalendarConverter
from utils.consts import url_list_events, url_list_teams, url_authenticate


def test_get_calendar_text() -> None:
@patch("utils.datetime_utils.get_current_timestamp", return_value=173512350)
@patch(
"utils.datetime_utils.get_current_datetime",
return_value=datetime.datetime(2024, 12, 25, 10, 45, 0),
)
def test_get_calendar_text(
timestamp_mock: MagicMock,
datetime_mock: MagicMock,
) -> None:
importlib.reload(utils.calendar_converter)

mocked_response_teams = read_text_by_name("list_teams.json")
mocked_response_events = read_text_by_name("list_events.json")

expected_calendar = replace_unwanted_lines(
read_text_by_name("expected_calendar.ics")
)

with requests_mock.Mocker() as m:
m.post(
with requests_mock.Mocker() as request_mocker:
request_mocker.post(
url_authenticate,
status_code=200,
cookies={"sporteasy": "token test calendar"},
)
m.get(url_list_teams, text=mocked_response_teams)
m.get(url_list_events.format(team_id=1), text=mocked_response_events)
request_mocker.get(url_list_teams, text=mocked_response_teams)
request_mocker.get(
url_list_events.format(team_id=1), text=mocked_response_events
)

converter = CalendarConverter()
converter = utils.calendar_converter.CalendarConverter()
calendar_text = converter.get_calendar_text("username", "password", "1")

result = replace_last_sync_and_sequence(replace_unwanted_lines(calendar_text))
result = replace_unwanted_lines(calendar_text)
assert result == expected_calendar
11 changes: 0 additions & 11 deletions test/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import re
import json
from pathlib import Path
from typing import Any
Expand Down Expand Up @@ -41,13 +40,3 @@ def read_json(path: Path) -> Any:

def replace_unwanted_lines(text: str) -> str:
return text.replace("\r\n", "\n").replace("\n\n", "\n").strip()


_SEQUENCE_REGEX = r"(?<=SEQUENCE:)\d+"
_LAST_SYNC_REGEX = r"(?<=\| Last sync: )\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}"


def replace_last_sync_and_sequence(text: str) -> str:
text_sequence = re.sub(_SEQUENCE_REGEX, "173512350", text)
text_date = re.sub(_LAST_SYNC_REGEX, "2024-12-25 10:45:00", text_sequence)
return text_date
7 changes: 4 additions & 3 deletions utils/calendar_converter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import time
from datetime import datetime, timedelta
from typing import Any, cast, Literal

Expand All @@ -14,6 +13,8 @@
url_list_teams,
url_list_events,
)

from utils.datetime_utils import get_current_timestamp, get_current_datetime
from utils.env import load_env_data
from utils.normalize import normalize

Expand Down Expand Up @@ -165,7 +166,7 @@ def event_to_calendar_event(team_name: str, event_data: EVENT_TYPE) -> Event:
_extract_event_description(event_data, event)

event.add("class", "PUBLIC")
current_timestamp = int(time.time() / 10)
current_timestamp = get_current_timestamp()
event.add("sequence", current_timestamp)
event.add("transp", "OPAQUE")

Expand Down Expand Up @@ -227,7 +228,7 @@ def get_calendar_text(
cal.add("x-wr-calname", "SportEasy Calendar")
cal.add("x-wr-timezone", "Europe/Paris")

formatted_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
formatted_time = get_current_datetime().strftime("%Y-%m-%d %H:%M:%S")
cal.add("x-wr-caldesc", f"SportEasy Calendar | Last sync: {formatted_time}")
cal.add("REFRESH-INTERVAL;VALUE=DURATION", "PT8H")
cal.add("X-PUBLISHED-TTL", "PT8H")
Expand Down
10 changes: 10 additions & 0 deletions utils/datetime_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import time
import datetime


def get_current_timestamp() -> int:
return int(time.time() / 10)


def get_current_datetime() -> datetime.datetime:
return datetime.datetime.now()

0 comments on commit 854e1b8

Please sign in to comment.