11from typing import Union
22
33import pytest
4- from workos .directory_sync import AsyncDirectorySync , DirectorySync
54
65from tests .types .test_auto_pagination_function import TestAutoPaginationFunction
76from tests .utils .fixtures .mock_directory import (
1312from tests .utils .fixtures .mock_directory_user import MockDirectoryUser
1413from tests .utils .list_resource import list_data_to_dicts , list_response_of
1514from tests .utils .syncify import syncify
15+ from workos .directory_sync import (
16+ _prepare_request_params ,
17+ AsyncDirectorySync ,
18+ DirectorySync ,
19+ )
1620
1721
1822def api_directory_to_sdk (directory ):
@@ -145,7 +149,7 @@ def test_list_users_with_directory(
145149 assert request_kwargs ["url" ].endswith ("/directory_users" )
146150 assert request_kwargs ["method" ] == "get"
147151 assert request_kwargs ["params" ] == {
148- "directory_id " : "directory_id" ,
152+ "directory " : "directory_id" ,
149153 "limit" : 10 ,
150154 "order" : "desc" ,
151155 }
@@ -163,7 +167,7 @@ def test_list_users_with_group(
163167 assert request_kwargs ["url" ].endswith ("/directory_users" )
164168 assert request_kwargs ["method" ] == "get"
165169 assert request_kwargs ["params" ] == {
166- "group_id " : "directory_grp_id" ,
170+ "group " : "directory_grp_id" ,
167171 "limit" : 10 ,
168172 "order" : "desc" ,
169173 }
@@ -181,7 +185,7 @@ def test_list_groups_with_directory(
181185 assert request_kwargs ["url" ].endswith ("/directory_groups" )
182186 assert request_kwargs ["method" ] == "get"
183187 assert request_kwargs ["params" ] == {
184- "directory_id " : "directory_id" ,
188+ "directory " : "directory_id" ,
185189 "limit" : 10 ,
186190 "order" : "desc" ,
187191 }
@@ -199,7 +203,7 @@ def test_list_groups_with_user(
199203 assert request_kwargs ["url" ].endswith ("/directory_groups" )
200204 assert request_kwargs ["method" ] == "get"
201205 assert request_kwargs ["params" ] == {
202- "user_id " : "directory_user_id" ,
206+ "user " : "directory_user_id" ,
203207 "limit" : 10 ,
204208 "order" : "desc" ,
205209 }
@@ -371,3 +375,53 @@ def test_directory_user_groups_auto_pagination(
371375 list_function = self .directory_sync .list_groups ,
372376 expected_all_page_data = mock_directory_groups_multiple_data_pages ,
373377 )
378+
379+
380+ class TestPrepareRequestParams :
381+ """Tests for SDK-to-API parameter name translation.
382+
383+ The SDK uses Pythonic parameter names (directory_id, group_id, user_id)
384+ but the WorkOS API expects shorter names (directory, group, user).
385+ The _prepare_request_params function handles this translation.
386+
387+ See: https://github.com/workos/workos-python/issues/511
388+ See: https://github.com/workos/workos-python/issues/519
389+ """
390+
391+ def test_translates_directory_id_to_directory (self ):
392+ params = {"directory_id" : "dir_123" , "limit" : 10 }
393+ result = _prepare_request_params (params )
394+ assert "directory" in result
395+ assert "directory_id" not in result
396+ assert result ["directory" ] == "dir_123"
397+
398+ def test_translates_group_id_to_group (self ):
399+ params = {"group_id" : "grp_123" , "limit" : 10 }
400+ result = _prepare_request_params (params )
401+ assert "group" in result
402+ assert "group_id" not in result
403+ assert result ["group" ] == "grp_123"
404+
405+ def test_translates_user_id_to_user (self ):
406+ params = {"user_id" : "usr_123" , "limit" : 10 }
407+ result = _prepare_request_params (params )
408+ assert "user" in result
409+ assert "user_id" not in result
410+ assert result ["user" ] == "usr_123"
411+
412+ def test_preserves_non_id_params (self ):
413+ params = {
414+ "directory_id" : "dir_123" ,
415+ "limit" : 10 ,
416+ "order" : "desc" ,
417+ "after" : "cursor" ,
418+ }
419+ result = _prepare_request_params (params )
420+ assert result ["limit" ] == 10
421+ assert result ["order" ] == "desc"
422+ assert result ["after" ] == "cursor"
423+
424+ def test_handles_empty_params (self ):
425+ params = {"limit" : 10 , "order" : "desc" }
426+ result = _prepare_request_params (params )
427+ assert result == {"limit" : 10 , "order" : "desc" }
0 commit comments