11from httpx import AsyncClient
22
3+ from conftest import api_path
4+
35
46async def _create_user (async_client : AsyncClient ) -> int :
57 response = await async_client .post (
6- "/users/" ,
8+ api_path ( "/users/" ) ,
79 json = {"username" : "kirill" , "email" : "kirill@example.com" },
810 )
911 return response .json ()["id" ]
1012
1113
1214async def _create_device (async_client : AsyncClient , name : str , user_id : int ) -> int :
1315 response = await async_client .post (
14- "/devices/" ,
16+ api_path ( "/devices/" ) ,
1517 json = {"name" : name , "user_id" : user_id },
1618 )
1719 return response .json ()["id" ]
@@ -27,10 +29,8 @@ async def test_add_measurement(async_client: AsyncClient):
2729 user_id = await _create_user (async_client )
2830 device_id = await _create_device (async_client , "Analytics Device" , user_id )
2931
30- response = await async_client .post (
31- f"/analytics/{ device_id } /data" ,
32- json = {"x" : 1.5 , "y" : 2.5 , "z" : 3.5 },
33- )
32+ analytics_url = api_path ("/analytics/" ).rstrip ("/" )
33+ response = await async_client .post (f"{ analytics_url } /{ device_id } /data" , json = {"x" : 1.5 , "y" : 2.5 , "z" : 3.5 })
3434
3535 assert response .status_code == 201
3636 data = response .json ()
@@ -49,10 +49,8 @@ async def test_add_measurement_device_not_found(async_client: AsyncClient):
4949 Ожидает код 404 при обращении к отсутствующему объекту.
5050 """
5151
52- response = await async_client .post (
53- "/analytics/999999/data" ,
54- json = {"x" : 1.0 , "y" : 2.0 , "z" : 3.0 },
55- )
52+ analytics_url = api_path ("/analytics/" ).rstrip ("/" )
53+ response = await async_client .post (f"{ analytics_url } /999999/data" , json = {"x" : 1.0 , "y" : 2.0 , "z" : 3.0 })
5654
5755 assert response .status_code == 404
5856
@@ -67,17 +65,12 @@ async def test_get_analytics_by_device(async_client: AsyncClient):
6765 user_id = await _create_user (async_client )
6866 device_id = await _create_device (async_client , "Analytics Reader Device" , user_id )
6967
70- await async_client .post (
71- f"/analytics/{ device_id } /data" ,
72- json = {"x" : 1.0 , "y" : 2.0 , "z" : 3.0 }
73- )
74- await async_client .post (
75- f"/analytics/{ device_id } /data" ,
76- json = {"x" : 3.0 , "y" : 4.0 , "z" : 5.0 }
77- )
68+ analytics_url = api_path ("/analytics/" ).rstrip ("/" )
69+ await async_client .post (f"{ analytics_url } /{ device_id } /data" , json = {"x" : 1.0 , "y" : 2.0 , "z" : 3.0 })
70+ await async_client .post (f"{ analytics_url } /{ device_id } /data" , json = {"x" : 3.0 , "y" : 4.0 , "z" : 5.0 })
7871
7972 response = await async_client .get (
80- "/analytics/" ,
73+ api_path ( "/analytics/" ) ,
8174 params = {"device_id" : device_id , "limit" : 25 , "offset" : 0 },
8275 )
8376
@@ -107,15 +100,16 @@ async def test_get_analytics_by_user_uses_stable_pagination(async_client: AsyncC
107100 first_device_id = await _create_device (async_client , "Device A" , user_id )
108101 second_device_id = await _create_device (async_client , "Device B" , user_id )
109102
110- await async_client .post (f"/analytics/{ first_device_id } /data" , json = {"x" : 1.0 , "y" : 1.0 , "z" : 1.0 })
111- await async_client .post (f"/analytics/{ second_device_id } /data" , json = {"x" : 2.0 , "y" : 2.0 , "z" : 2.0 })
103+ analytics_url = api_path ("/analytics/" ).rstrip ("/" )
104+ await async_client .post (f"{ analytics_url } /{ first_device_id } /data" , json = {"x" : 1.0 , "y" : 1.0 , "z" : 1.0 })
105+ await async_client .post (f"{ analytics_url } /{ second_device_id } /data" , json = {"x" : 2.0 , "y" : 2.0 , "z" : 2.0 })
112106
113107 first_page_response = await async_client .get (
114- "/analytics/" ,
108+ api_path ( "/analytics/" ) ,
115109 params = {"user_id" : user_id , "limit" : 1 , "offset" : 0 },
116110 )
117111 second_page_response = await async_client .get (
118- "/analytics/" ,
112+ api_path ( "/analytics/" ) ,
119113 params = {"user_id" : user_id , "limit" : 1 , "offset" : 1 },
120114 )
121115
@@ -138,7 +132,7 @@ async def test_get_analytics_requires_filter(async_client: AsyncClient):
138132 Ожидает ошибку 400 при отсутствии обязательного фильтра запроса.
139133 """
140134
141- response = await async_client .get ("/analytics/" )
135+ response = await async_client .get (api_path ( "/analytics/" ) )
142136 assert response .status_code == 400
143137
144138
@@ -150,7 +144,7 @@ async def test_get_analytics_validates_limit_and_offset(async_client: AsyncClien
150144 """
151145
152146 response = await async_client .get (
153- "/analytics/" ,
147+ api_path ( "/analytics/" ) ,
154148 params = {"device_id" : 1 , "limit" : 0 , "offset" : - 1 },
155149 )
156150
@@ -164,6 +158,6 @@ async def test_generate_analytics_route_not_conflicting(async_client: AsyncClien
164158 Ожидает бизнес-ошибку фильтров, а не парсинг `generate` как `device_id`.
165159 """
166160
167- response = await async_client .post ("/analytics/generate" )
161+ response = await async_client .post (api_path ( "/analytics/generate" ) )
168162
169163 assert response .status_code == 400
0 commit comments