55
66from sentry .testutils .cases import APITestCase
77from sentry .testutils .helpers .features import with_feature
8+ from sentry .utils .cursors import Cursor
89
910
1011@with_feature ("organizations:seer-explorer" )
@@ -35,11 +36,11 @@ def tearDown(self) -> None:
3536 def test_get_simple (self ) -> None :
3637 self .make_seer_request .return_value .status = 200
3738 self .make_seer_request .return_value .json .return_value = {
38- "hello " : "world" ,
39+ "data " : [{ "run_id" : "1" }, { "run_id" : "2" }] ,
3940 }
4041 response = self .client .get (self .url )
4142 assert response .status_code == 200
42- assert response .json () == { "hello " : "world" }
43+ assert response .json ()[ "data" ] == [{ "run_id " : "1" }, { "run_id" : "2" }]
4344
4445 self .make_seer_request .assert_called_once ()
4546 call_args = self .make_seer_request .call_args
@@ -48,18 +49,21 @@ def test_get_simple(self) -> None:
4849 assert body_json == {
4950 "organization_id" : self .organization .id ,
5051 "user_id" : self .user .id ,
51- "limit" : None ,
52- "offset" : None ,
52+ "limit" : 101 , # Default per_page of 100 + 1 for has_more
53+ "offset" : 0 ,
5354 }
5455
55- def test_get_with_offset_and_limit (self ) -> None :
56+ def test_get_cursor_pagination (self ) -> None :
5657 self .make_seer_request .return_value .status = 200
58+ # Mock seer response for offset 0, limit 3.
5759 self .make_seer_request .return_value .json .return_value = {
58- "hello " : "world" ,
60+ "data " : [{ "run_id" : "1" }, { "run_id" : "2" }, { "run_id" : "3" }] ,
5961 }
60- response = self .client .get (self .url + "?offset=1&limit=11" )
62+ cursor = str (Cursor (0 , 0 ))
63+ response = self .client .get (self .url + f"?per_page=2&cursor={ cursor } " )
6164 assert response .status_code == 200
62- assert response .json () == {"hello" : "world" }
65+ assert response .json ()["data" ] == [{"run_id" : "1" }, {"run_id" : "2" }]
66+ assert 'rel="next"; results="true"' in response .headers ["Link" ]
6367
6468 self .make_seer_request .assert_called_once ()
6569 call_args = self .make_seer_request .call_args
@@ -68,26 +72,34 @@ def test_get_with_offset_and_limit(self) -> None:
6872 assert body_json == {
6973 "organization_id" : self .organization .id ,
7074 "user_id" : self .user .id ,
71- "limit" : 11 ,
72- "offset" : 1 ,
75+ "limit" : 3 , # +1 for has_more
76+ "offset" : 0 ,
7377 }
7478
75- def test_get_with_invalid_limit (self ) -> None :
76- for value in ["invalid" , "-1" , "0" ]:
77- response = self .client .get (self .url + f"?limit={ value } " )
78- assert response .status_code == 400
79- assert "limit" in response .json ()
79+ # Second page - mock seer response for offset 2, limit 3.
80+ self .make_seer_request .return_value .json .return_value = {
81+ "data" : [{"run_id" : "3" }, {"run_id" : "4" }],
82+ }
83+ cursor = str (Cursor (0 , 2 ))
84+ response = self .client .get (self .url + f"?per_page=2&cursor={ cursor } " )
85+ assert response .status_code == 200
86+ assert response .json ()["data" ] == [{"run_id" : "3" }, {"run_id" : "4" }]
87+ assert 'rel="next"; results="false"' in response .headers ["Link" ]
8088
81- def test_get_with_invalid_offset (self ) -> None :
82- for value in ["invalid" , "-1" ]:
83- response = self .client .get (self .url + f"?offset={ value } " )
84- assert response .status_code == 400
85- assert "offset" in response .json ()
89+ call_args = self .make_seer_request .call_args
90+ assert call_args [0 ][1 ] == "/v1/automation/explorer/runs"
91+ body_json = orjson .loads (call_args [0 ][2 ])
92+ assert body_json == {
93+ "organization_id" : self .organization .id ,
94+ "user_id" : self .user .id ,
95+ "limit" : 3 , # +1 for has_more
96+ "offset" : 2 ,
97+ }
8698
8799 def test_get_with_seer_error (self ) -> None :
88- self .make_seer_request .return_value .status = 500
100+ self .make_seer_request .return_value .status = 404
89101 response = self .client .get (self .url )
90- assert response .status_code == 502
102+ assert response .status_code == 500
91103
92104
93105class TestOrganizationSeerExplorerRunsEndpointFeatureFlags (APITestCase ):
0 commit comments