Skip to content

Commit

Permalink
♻️ split toggl repositories based on entities
Browse files Browse the repository at this point in the history
  • Loading branch information
ulisesantana committed May 2, 2023
1 parent 74e3f36 commit bea6235
Show file tree
Hide file tree
Showing 22 changed files with 348 additions and 238 deletions.
20 changes: 14 additions & 6 deletions tests/cases/test_continue_with_last_time_entry_use_case.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
from unittest.mock import Mock

import pytest

from tests.mocks import MockTimeHelper
from track.application.cases import ContinueWithLastTimeEntryUseCase
from track.application.repositories import TimeEntryRepository
from track.core import TimeEntry


def test_continue_with_last_time_entry_use_case_exec():
time_entry_repository = Mock(spec=TimeEntryRepository)
@pytest.fixture
def time_entry_repository():
repository = Mock(spec=TimeEntryRepository)
repository.workspace_id = 123
return repository


def test_continue_with_last_time_entry_use_case_exec(time_entry_repository):
last_entry = TimeEntry(id=1, wid=123, description="Test Description", pid=456, _duration=60)
time_entry_repository.get_last_entry.return_value = last_entry
expected_call = dict(start=MockTimeHelper.get_current_utc_date(), id=last_entry.id, wid=last_entry.wid,
Expand All @@ -16,7 +24,7 @@ def test_continue_with_last_time_entry_use_case_exec():
description=last_entry.description, pid=last_entry.pid,
_duration=-60)
time_entry_repository.create_entry.return_value = expected
case = ContinueWithLastTimeEntryUseCase(time_entry_repository, MockTimeHelper)
case = ContinueWithLastTimeEntryUseCase(time_entry_repository=time_entry_repository, time_helper=MockTimeHelper)

result = case.exec()

Expand All @@ -25,10 +33,10 @@ def test_continue_with_last_time_entry_use_case_exec():
time_entry_repository.create_entry.assert_called_with(**expected_call)


def test_continue_with_last_time_entry_use_case_exec_no_last_entry():
time_entry_repository = Mock(spec=TimeEntryRepository)
def test_continue_with_last_time_entry_use_case_exec_no_last_entry(time_entry_repository):
time_entry_repository.get_last_entry.return_value = None
continue_with_last_time_entry_use_case = ContinueWithLastTimeEntryUseCase(time_entry_repository, MockTimeHelper)
continue_with_last_time_entry_use_case = ContinueWithLastTimeEntryUseCase(
time_entry_repository=time_entry_repository, time_helper=MockTimeHelper)

result = continue_with_last_time_entry_use_case.exec()

Expand Down
35 changes: 25 additions & 10 deletions tests/cases/test_get_current_time_entry_use_case.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,50 @@
import time
from unittest.mock import Mock

import pytest

from track.application.cases import GetCurrentTimeEntryUseCase
from track.application.repositories import TimeEntryRepository
from track.application.repositories import ProjectRepository, TimeEntryRepository
from track.core import Project, TimeEntry


def test_return_current_time_entry_successfully():
time_entry_repository = Mock(spec=TimeEntryRepository)
@pytest.fixture
def time_entry_repository():
repository = Mock(spec=TimeEntryRepository)
repository.workspace_id = 123
return repository


@pytest.fixture
def project_repository():
repository = Mock(spec=ProjectRepository)
repository.workspace_id = 123
return repository


def test_return_current_time_entry_successfully(time_entry_repository, project_repository):
current_entry = TimeEntry(id=1, wid=123, _duration=-100, pid=456, description="Test")
project = Project(id=456, name="Test Project")
time_entry_repository.get_current_entry.return_value = current_entry
time_entry_repository.get_project_by_id.return_value = project
get_current_time_entry_use_case = GetCurrentTimeEntryUseCase(time_entry_repository)
project_repository.get_project_by_id.return_value = project
get_current_time_entry_use_case = GetCurrentTimeEntryUseCase(time_entry_repository=time_entry_repository,
project_repository=project_repository)

result = get_current_time_entry_use_case.exec()

time_entry_repository.get_current_entry.assert_called_once()
time_entry_repository.get_project_by_id.assert_called_with(current_entry.pid)
project_repository.get_project_by_id.assert_called_with(current_entry.pid)

assert result == (current_entry, project)
assert current_entry.duration >= int(time.time()) - 100


def test_return_none_if_there_is_no_current_entry():
time_entry_repository = Mock(spec=TimeEntryRepository)
get_current_time_entry_use_case = GetCurrentTimeEntryUseCase(time_entry_repository)
def test_return_none_if_there_is_no_current_entry(time_entry_repository, project_repository):
get_current_time_entry_use_case = GetCurrentTimeEntryUseCase(time_entry_repository, project_repository)
time_entry_repository.get_current_entry.return_value = None

result = get_current_time_entry_use_case.exec()

time_entry_repository.get_current_entry.assert_called_once()
time_entry_repository.get_project_by_id.assert_not_called()
project_repository.get_project_by_id.assert_not_called()
assert result is None
26 changes: 21 additions & 5 deletions tests/cases/test_get_current_week_report_use_case.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,36 @@
from unittest.mock import Mock

import pytest

from track.application.cases import GetCurrentWeekReportUseCase
from track.application.repositories import TimeEntryRepository
from track.application.repositories import ProjectRepository, TimeEntryRepository
from track.core import Project


def test_get_current_week_report_use_case_exec():
@pytest.fixture
def time_entry_repository():
repository = Mock(spec=TimeEntryRepository)
repository.workspace_id = 123
return repository


@pytest.fixture
def project_repository():
repository = Mock(spec=ProjectRepository)
repository.workspace_id = 123
return repository


def test_get_current_week_report_use_case_exec(time_entry_repository, project_repository):
dummy_entries = [
{"duration": 3600},
{"duration": 1800},
]
projects_dict = {"project_1": Project(1, 'Test name')}
time_entry_repository = Mock(spec=TimeEntryRepository)
time_entry_repository.get_current_week_entries.return_value = dummy_entries
time_entry_repository.get_projects.return_value = projects_dict
use_case = GetCurrentWeekReportUseCase(time_entry_repository=time_entry_repository)
project_repository.get_projects.return_value = projects_dict
use_case = GetCurrentWeekReportUseCase(time_entry_repository=time_entry_repository,
project_repository=project_repository)

entries, projects = use_case.exec()

Expand Down
44 changes: 29 additions & 15 deletions tests/cases/test_start_time_entry_use_case.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,55 @@
from unittest.mock import Mock

import pytest

from tests.mocks import MockTimeHelper
from track.application.cases import StartTimeEntryUseCase
from track.application.repositories import TimeEntryRepository
from track.application.repositories import ProjectRepository, TimeEntryRepository
from track.core import Project


def test_start_time_entry_use_case_exec_with_valid_id():
toggl_repository = Mock(TimeEntryRepository)
toggl_repository.workspace_id = 42
start_time_entry_use_case = StartTimeEntryUseCase(toggl_repository, MockTimeHelper)
@pytest.fixture
def time_entry_repository():
repository = Mock(spec=TimeEntryRepository)
repository.workspace_id = 123
return repository


@pytest.fixture
def project_repository():
repository = Mock(spec=ProjectRepository)
repository.workspace_id = 123
return repository


def test_start_time_entry_use_case_exec_with_valid_id(time_entry_repository, project_repository):
start_time_entry_use_case = StartTimeEntryUseCase(time_entry_repository=time_entry_repository,
project_repository=project_repository, time_helper=MockTimeHelper)
description = "Test description"
project_id = 123

start_time_entry_use_case.exec(description, project_id)

toggl_repository.create_entry.assert_called_with(
wid=toggl_repository.workspace_id,
time_entry_repository.create_entry.assert_called_with(
wid=time_entry_repository.workspace_id,
description=description,
pid=project_id,
start=MockTimeHelper.get_current_utc_date(),
)


def test_start_time_entry_use_case_exec_with_project_name():
toggl_repository = Mock(TimeEntryRepository)
toggl_repository.workspace_id = 42
start_time_entry_use_case = StartTimeEntryUseCase(toggl_repository, MockTimeHelper)
def test_start_time_entry_use_case_exec_with_project_name(time_entry_repository, project_repository):
start_time_entry_use_case = StartTimeEntryUseCase(time_entry_repository=time_entry_repository,
project_repository=project_repository, time_helper=MockTimeHelper)
description = "Test description"
project = Project(id=123, name="Test project")
toggl_repository.get_project_by_name.return_value = project
project_repository.get_project_by_name.return_value = project

start_time_entry_use_case.exec(description, project.name)

toggl_repository.get_project_by_name.assert_called_with(project.name)
toggl_repository.create_entry.assert_called_with(
wid=toggl_repository.workspace_id,
project_repository.get_project_by_name.assert_called_with(project.name)
time_entry_repository.create_entry.assert_called_with(
wid=time_entry_repository.workspace_id,
description=description,
pid=project.id,
start=MockTimeHelper.get_current_utc_date(),
Expand Down
6 changes: 3 additions & 3 deletions tests/cases/test_stop_time_entry_use_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
from tests.mocks import MockTimeHelper
from track.application.cases import StopTimeEntryUseCase
from track.core import TimeEntry
from track.infrastructure.repositories import TogglRepository
from track.infrastructure.repositories import TogglTimeEntryRepository


def test_stop_time_entry_use_case_exec():
toggl_repository = Mock(spec=TogglRepository)
toggl_repository = Mock(spec=TogglTimeEntryRepository)
current_entry = TimeEntry(id=1, wid=123, description="Reading emails", pid=123, _duration=60)
expected = dict(id=current_entry.id, wid=current_entry.wid, description=current_entry.description,
stop=MockTimeHelper.get_current_utc_date())
Expand All @@ -27,7 +27,7 @@ def test_stop_time_entry_use_case_exec():


def test_stop_time_entry_use_case_exec_no_current_entry():
toggl_repository = Mock(spec=TogglRepository)
toggl_repository = Mock(spec=TogglTimeEntryRepository)
toggl_repository.get_current_entry.return_value = None
stop_time_entry_use_case = StopTimeEntryUseCase(toggl_repository)

Expand Down
67 changes: 67 additions & 0 deletions tests/infrastructure/test_toggl_project_repository.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from unittest.mock import Mock, patch

from track.core import Project
from track.infrastructure.repositories import TogglProjectRepository


def test_get_projects():
with patch('requests.get') as mocked_get:
# Mock the response for the GET request
mocked_response = Mock()
mocked_response.json.return_value = [
{
"id": 456,
"name": "Test project",
},
{
"id": 789,
"name": "Test project 2",
}
]
mocked_get.return_value = mocked_response

repository = TogglProjectRepository(workspace_id=123, token='test-token')
projects = repository.get_projects()
assert len(projects) == 2
assert isinstance(projects[456], Project)
assert projects[456].name == 'Test project'


def test_get_project_by_id():
with patch('requests.get') as mocked_get:
# Mock the response for the GET request
mocked_response = Mock()
mocked_response.json.return_value = {
"id": 456,
"name": "Test project",
}
mocked_get.return_value = mocked_response

repository = TogglProjectRepository(workspace_id=123, token='test-token')
project = repository.get_project_by_id(456)
assert isinstance(project, Project)
assert project.id == 456
assert project.name == 'Test project'


def test_get_project_by_name():
with patch('requests.get') as mocked_get:
# Mock the response for the GET request
mocked_response = Mock()
mocked_response.json.return_value = [
{
"id": 456,
"name": "Test project",
},
{
"id": 789,
"name": "Test project 2",
}
]
mocked_get.return_value = mocked_response

repository = TogglProjectRepository(workspace_id=123, token='test-token')
project = repository.get_project_by_name("Test project 2")
assert isinstance(project, Project)
assert project.id == 789
assert project.name == 'Test project 2'
Loading

0 comments on commit bea6235

Please sign in to comment.