mirror of https://github.com/gmbrax/Pilgrim.git
Add the tests for the delete photo service and also fixed the missing import
This commit is contained in:
parent
65cf7b04f3
commit
32f363f15a
|
|
@ -4,6 +4,7 @@ from sqlalchemy.orm import sessionmaker
|
||||||
|
|
||||||
from pilgrim.database import Base
|
from pilgrim.database import Base
|
||||||
from pilgrim.models.travel_diary import TravelDiary
|
from pilgrim.models.travel_diary import TravelDiary
|
||||||
|
from pilgrim.models.photo import Photo
|
||||||
|
|
||||||
# Todos os imports necessários para as fixtures devem estar aqui
|
# Todos os imports necessários para as fixtures devem estar aqui
|
||||||
# ...
|
# ...
|
||||||
|
|
|
||||||
|
|
@ -107,4 +107,33 @@ def test_check_photo_by_hash_returns_none_when_not_found(session_with_photos):
|
||||||
assert result1 is None
|
assert result1 is None
|
||||||
invalid_diary_id = 999
|
invalid_diary_id = 999
|
||||||
result2 = service.check_photo_by_hash(existing_hash, invalid_diary_id)
|
result2 = service.check_photo_by_hash(existing_hash, invalid_diary_id)
|
||||||
assert result2 is None
|
assert result2 is None
|
||||||
|
|
||||||
|
@patch('pathlib.Path.unlink')
|
||||||
|
@patch('pathlib.Path.exists')
|
||||||
|
@patch.object(DirectoryManager, 'get_diaries_root', return_value="/fake/diaries_root")
|
||||||
|
def test_delete_photo_successfully(mock_get_root, mock_exists, mock_unlink, session_with_photos):
|
||||||
|
session, photos = session_with_photos
|
||||||
|
service = PhotoService(session)
|
||||||
|
photo_to_delete = photos[0]
|
||||||
|
photo_id = photo_to_delete.id
|
||||||
|
mock_exists.return_value = True
|
||||||
|
deleted_photo_data = service.delete(photo_to_delete)
|
||||||
|
mock_unlink.assert_called_once()
|
||||||
|
assert deleted_photo_data is not None
|
||||||
|
assert deleted_photo_data.id == photo_id
|
||||||
|
photo_in_db = service.read_by_id(photo_id)
|
||||||
|
assert photo_in_db is None
|
||||||
|
|
||||||
|
@patch('pathlib.Path.unlink')
|
||||||
|
def test_delete_returns_none_for_non_existent_photo(mock_unlink, db_session):
|
||||||
|
service = PhotoService(db_session)
|
||||||
|
non_existent_photo = Photo(
|
||||||
|
filepath="/fake/path.jpg", name="dummy",
|
||||||
|
photo_hash="dummy_hash", fk_travel_diary_id=1
|
||||||
|
)
|
||||||
|
non_existent_photo.id = 999
|
||||||
|
result = service.delete(non_existent_photo)
|
||||||
|
assert result is None
|
||||||
|
mock_unlink.assert_not_called()
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue