Skip to content

Commit

Permalink
Add integration tests for essential logic
Browse files Browse the repository at this point in the history
  • Loading branch information
nifadyev committed Sep 17, 2024
1 parent e5d778f commit e3801b0
Show file tree
Hide file tree
Showing 5 changed files with 217 additions and 0 deletions.
9 changes: 9 additions & 0 deletions tests/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import pytest
from toggl_python.auth import TokenAuth
from toggl_python.entities.report_time_entry import ReportTimeEntry
from toggl_python.entities.user import CurrentUser
from toggl_python.entities.workspace import Workspace

Expand All @@ -30,6 +31,14 @@ def i_authed_workspace() -> Workspace:
return Workspace(auth=auth)


@pytest.fixture(scope="session")
def i_authed_report_time_entry() -> ReportTimeEntry:
token = os.environ["TOGGL_TOKEN"]
auth = TokenAuth(token=token)

return ReportTimeEntry(auth=auth)


@pytest.fixture()
def me_response(i_authed_user: CurrentUser) -> MeResponse:
return i_authed_user.me()
Expand Down
36 changes: 36 additions & 0 deletions tests/integration/test_project.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from __future__ import annotations

import os
from typing import TYPE_CHECKING

from toggl_python.schemas.project import ProjectResponse

# Necessary to mark all tests in module as integration
from tests.integration import pytestmark # noqa: F401 - imported but unused


if TYPE_CHECKING:
from toggl_python.entities.workspace import Workspace


def test_get_projects__without_query_params(i_authed_workspace: Workspace) -> None:
# Later Create project and init and delete it at the end
# Now this actions are not implemented
workspace_id = int(os.environ["WORKSPACE_ID"])
expected_result = set(ProjectResponse.model_fields.keys())

result = i_authed_workspace.get_projects(workspace_id)

assert result[0].model_fields_set == expected_result


def test_get_project_by_id(i_authed_workspace: Workspace) -> None:
# Later Create project and init and delete it at the end
# Now this actions are not implemented
workspace_id = int(os.environ["WORKSPACE_ID"])
project_id = int(os.environ["PROJECT_ID"])
expected_result = set(ProjectResponse.model_fields.keys())

result = i_authed_workspace.get_project(workspace_id, project_id)

assert result.model_fields_set == expected_result
73 changes: 73 additions & 0 deletions tests/integration/test_report_time_entry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import os

import pytest
from toggl_python.entities.report_time_entry import ReportTimeEntry
from toggl_python.entities.workspace import Workspace
from toggl_python.schemas.report_time_entry import (
SearchReportTimeEntriesResponse,
)

from tests.conftest import fake

# Necessary to mark all tests in module as integration
from tests.integration import pytestmark # noqa: F401 - imported but unused


try:
import zoneinfo
except ImportError:
from backports import zoneinfo


@pytest.mark.parametrize(
argnames="use_dates_repr",
argvalues=(True, False),
ids=("str date arguments", "date date arguments"),
)
def test_search_report_time_entries__with_start_and_end_dates(
use_dates_repr: bool,
i_authed_report_time_entry: ReportTimeEntry,
i_authed_workspace: Workspace,
) -> None:
workspace_id = int(os.environ["WORKSPACE_ID"])
timezone_name = fake.timezone()
tz = zoneinfo.ZoneInfo(timezone_name)
start_date = fake.date_this_decade()
delta = fake.time_delta()
end_date = start_date + delta
time_entry = i_authed_workspace.create_time_entry(
workspace_id,
start_datetime=fake.date_time_between_dates(start_date, end_date, tzinfo=tz),
created_with=fake.word(),
)

expected_result = set(SearchReportTimeEntriesResponse.model_fields.keys())

result = i_authed_report_time_entry.search(
workspace_id,
start_date=start_date.isoformat() if use_dates_repr else start_date,
end_date=end_date.isoformat() if use_dates_repr else end_date,
)

assert result[0].model_fields_set == expected_result

_ = i_authed_workspace.delete_time_entry(workspace_id, time_entry.id)


def test_search_report_time_entries__not_found(
i_authed_report_time_entry: ReportTimeEntry,
) -> None:
workspace_id = int(os.environ["WORKSPACE_ID"])
# Set explicit date range to avoid finding unexpected existing test TimeEntries
time_entry_start_date = fake.date_between(start_date="-15y", end_date="-2y")
delta = fake.time_delta()
end_date = time_entry_start_date + 2 * delta
start_date = fake.date_between_dates(time_entry_start_date, end_date)

result = i_authed_report_time_entry.search(
workspace_id,
start_date=start_date,
end_date=end_date,
)

assert result == []
69 changes: 69 additions & 0 deletions tests/integration/test_time_entry.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from __future__ import annotations

import os
from datetime import timedelta
from typing import TYPE_CHECKING

from toggl_python.schemas.time_entry import MeTimeEntryResponse

from tests.conftest import fake
from tests.factories.time_entry import (
time_entry_extended_request_factory,
time_entry_request_factory,
Expand All @@ -14,7 +16,14 @@
from tests.integration import pytestmark # noqa: F401 - imported but unused


try:
import zoneinfo
except ImportError:
from backports import zoneinfo


if TYPE_CHECKING:
from toggl_python.entities.user import CurrentUser
from toggl_python.entities.workspace import Workspace


Expand Down Expand Up @@ -60,3 +69,63 @@ def test_create_time_entry__all_fields(i_authed_workspace: Workspace) -> None:
assert result.model_fields_set == expected_result

_ = i_authed_workspace.delete_time_entry(workspace_id=workspace_id, time_entry_id=result.id)


def test_list_time_entries__with_start_and_end_date__datetime(
i_authed_user: CurrentUser, i_authed_workspace: Workspace
) -> None:
workspace_id = int(os.environ["WORKSPACE_ID"])
timezone_name = fake.timezone()
tz = zoneinfo.ZoneInfo(timezone_name)
start_date = fake.date_time_this_month(tzinfo=tz, before_now=True)
delta = fake.random_int(min=1, max=999999)
end_date = start_date + timedelta(seconds=delta)
time_entry = i_authed_workspace.create_time_entry(
workspace_id,
start_datetime=fake.date_time_between_dates(start_date, end_date, tzinfo=tz),
created_with=fake.word(),
)

expected_result = set(MeTimeEntryResponse.model_fields.keys())

result = i_authed_user.get_time_entries(start_date=start_date, end_date=end_date)

assert result[0].model_fields_set == expected_result

_ = i_authed_workspace.delete_time_entry(workspace_id, time_entry.id)


def test_list_time_entries__with_start_and_end_date__str(
i_authed_user: CurrentUser, i_authed_workspace: Workspace
) -> None:
workspace_id = int(os.environ["WORKSPACE_ID"])
start_date = fake.date_this_month(before_today=True)
delta = fake.random_int(min=1, max=999)
end_date = start_date + timedelta(days=delta)
timezone_name = fake.timezone()
tz = zoneinfo.ZoneInfo(timezone_name)
time_entry = i_authed_workspace.create_time_entry(
workspace_id,
start_datetime=fake.date_time_between_dates(start_date, end_date, tzinfo=tz),
created_with=fake.word(),
)

expected_result = set(MeTimeEntryResponse.model_fields.keys())

result = i_authed_user.get_time_entries(
start_date=start_date.isoformat(), end_date=end_date.isoformat()
)

assert result[0].model_fields_set == expected_result

_ = i_authed_workspace.delete_time_entry(workspace_id, time_entry.id)


def test_list_time_entries__no_results(i_authed_user: CurrentUser) -> None:
start_date = fake.date_time_between(start_date="-6m", end_date="-1m")
delta = fake.random_int(min=0, max=999)
end_date = start_date + timedelta(days=delta)

result = i_authed_user.get_time_entries(start_date=start_date, end_date=end_date)

assert result == []
30 changes: 30 additions & 0 deletions tests/integration/test_workspace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from __future__ import annotations

import os
from typing import TYPE_CHECKING

from toggl_python.schemas.workspace import WorkspaceResponse

# Necessary to mark all tests in module as integration
from tests.integration import pytestmark # noqa: F401 - imported but unused


if TYPE_CHECKING:
from toggl_python.entities.workspace import Workspace


def test_get_workspace_by_id(i_authed_workspace: Workspace) -> None:
workspace_id = int(os.environ["WORKSPACE_ID"])
expected_result = set(WorkspaceResponse.model_fields.keys())

result = i_authed_workspace.get(workspace_id)

assert result.model_fields_set == expected_result


def test_get_workspaces__without_query_params(i_authed_workspace: Workspace)-> None:
expected_result = set(WorkspaceResponse.model_fields.keys())

result = i_authed_workspace.list()

assert result[0].model_fields_set == expected_result

0 comments on commit e3801b0

Please sign in to comment.