-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ split toggl repositories based on entities
- Loading branch information
1 parent
74e3f36
commit bea6235
Showing
22 changed files
with
348 additions
and
238 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
Oops, something went wrong.