1- import pytest
2- from unittest .mock import AsyncMock , patch , MagicMock
1+ from unittest .mock import AsyncMock , MagicMock , patch
32from uuid import uuid4
3+
4+ import pytest
45from fastapi import HTTPException
56
67from app .models .client import Client
8+ from app .models .guide import ComplexityLevel , Guide
79from app .models .topic import Topic
8- from app .models .guide import Guide , ComplexityLevel
9- from app .schemas .topic import TopicCreate
1010from app .schemas .guide import GuideCreate
1111from app .services .content import content_service
1212
@@ -46,15 +46,17 @@ async def test_create_guide_success() -> None:
4646 )
4747
4848 # We need to mock get_topic to succeed (it checks access)
49- with patch .object (content_service , "get_topic" , return_value = topic ) as mock_get_topic :
50- with patch ("app.services.content.guide_repo.get_by_slug" , return_value = None ):
51- mock_session .add = MagicMock ()
49+ with (
50+ patch .object (content_service , "get_topic" , return_value = topic ) as mock_get_topic ,
51+ patch ("app.services.content.guide_repo.get_by_slug" , return_value = None ),
52+ ):
53+ mock_session .add = MagicMock ()
5254
53- result = await content_service .create_guide (mock_session , client , guide_in )
55+ result = await content_service .create_guide (mock_session , client , guide_in )
5456
55- assert result .title == "Guide 1"
56- assert result .client_id == client .id
57- mock_get_topic .assert_awaited_once_with (mock_session , client , topic .id )
57+ assert result .title == "Guide 1"
58+ assert result .client_id == client .id
59+ mock_get_topic .assert_awaited_once_with (mock_session , client , topic .id )
5860
5961
6062@pytest .mark .asyncio ()
@@ -73,12 +75,13 @@ async def test_create_guide_slug_collision() -> None:
7375 complexity_level = ComplexityLevel .BEGINNER ,
7476 )
7577
76- with patch .object (content_service , "get_topic" , return_value = topic ):
77- # Return an existing object to trigger collision
78- with patch ("app.services.content.guide_repo.get_by_slug" , return_value = Guide ()):
79- with pytest .raises (HTTPException ) as exc :
80- await content_service .create_guide (mock_session , client , guide_in )
81- assert exc .value .status_code == 400
78+ with (
79+ patch .object (content_service , "get_topic" , return_value = topic ),
80+ patch ("app.services.content.guide_repo.get_by_slug" , return_value = Guide ()),
81+ ):
82+ with pytest .raises (HTTPException ) as exc :
83+ await content_service .create_guide (mock_session , client , guide_in )
84+ assert exc .value .status_code == 400
8285
8386
8487@pytest .mark .asyncio ()
0 commit comments