Skip to content

Commit ce3129e

Browse files
committed
organize test imports
1 parent 3b07463 commit ce3129e

File tree

5 files changed

+27
-25
lines changed

5 files changed

+27
-25
lines changed

servicestack/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
'WebServiceExceptionType',
6161
'to_json',
6262
'from_json',
63+
'to_dict',
6364
'convert',
6465
'qsvalue',
6566
'resolve_httpmethod',
@@ -107,7 +108,7 @@
107108
StringsResponse, AuditBase
108109

109110
from .clients import TypeConverters, JsonServiceClient, WebServiceException, \
110-
WebServiceExceptionType, to_json, from_json, convert, qsvalue, \
111+
WebServiceExceptionType, to_json, from_json, to_dict, convert, qsvalue, \
111112
resolve_httpmethod
112113

113114
from .utils import \

servicestack/clients.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,11 @@ def nameof(instance):
120120

121121

122122
def qsvalue(arg):
123-
if not arg:
123+
if arg is None:
124124
return ""
125125
arg_type = type(arg)
126+
if arg_type == bool:
127+
return f"{arg}".lower()
126128
if is_list(arg_type):
127129
return "[" + ','.join([qsvalue(x) for x in arg]) + "]"
128130
if is_dict(arg_type):
@@ -138,6 +140,7 @@ def append_querystring(url: str, args: dict[str, Any]):
138140
if args:
139141
for key in args:
140142
val = args[key]
143+
# print("append_querystring", key, val)
141144
if val is None:
142145
continue
143146
url += '&' if '?' in url else '?'
@@ -155,14 +158,17 @@ def _empty(x):
155158
return x is None or x == {} or x == []
156159

157160

158-
def _asdict(obj: Any):
161+
def to_dict(obj: Any):
159162
if obj is None:
160163
return {}
161164
if is_dataclass(obj):
162165
d = asdict(obj)
163166
to = {}
164167
for k, v in d.items():
165-
to[camelcase(k)] = v
168+
use_key = camelcase(k)
169+
if use_key[-1] == '_':
170+
use_key = use_key[0:-1]
171+
to[use_key] = v
166172
else:
167173
to = obj.__dict__
168174
return clean_any(to)
@@ -188,7 +194,7 @@ def clean_any(d):
188194

189195
def _json_encoder(obj: Any):
190196
if is_dataclass(obj):
191-
return _asdict(obj)
197+
return to_dict(obj)
192198
if hasattr(obj, '__dict__'):
193199
return vars(obj)
194200
if isinstance(obj, datetime):
@@ -548,7 +554,7 @@ def refresh_token_cookie(self):
548554
def create_url_from_dto(self, method: str, request: Any):
549555
url = urljoin(self.reply_base_url, nameof(request))
550556
if not has_request_body(method):
551-
url = append_querystring(url, _asdict(request))
557+
url = append_querystring(url, to_dict(request))
552558
return url
553559

554560
def get(self, request: IReturn[T], args: Dict[str, Any] = None) -> T:
@@ -781,7 +787,7 @@ def create_request(self, info: SendContext):
781787
body_not_request_dto = info.request and info.body
782788
if body_not_request_dto:
783789
url = urljoin(self.reply_base_url, nameof(info.request))
784-
url = append_querystring(url, _asdict(info.request))
790+
url = append_querystring(url, to_dict(info.request))
785791
else:
786792
url = self.create_url_from_dto(info.method, body)
787793

tests/test_client_auth.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22
"""
33

44
import unittest
5-
import dataclasses
6-
import json
7-
from .dtos import *
8-
from datetime import datetime, timedelta, timezone
9-
from servicestack import JsonServiceClient, WebServiceException, to_json
5+
from datetime import datetime
6+
7+
from servicestack import WebServiceException
108
from .config import *
9+
from .dtos import *
1110

1211

1312
def create_jwt(**args) -> CreateJwt:

tests/test_enum_serialization.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,10 @@
33

44
import unittest
55

6-
import requests
7-
from dataclasses_json import config, dataclass_json, Undefined
8-
9-
from .dtos import *
10-
from datetime import datetime, timedelta, timezone
11-
from servicestack import JsonServiceClient, WebServiceException, to_json, convert
12-
from .config import create_test_client
6+
from servicestack import to_json, convert
137
from servicestack.utils import *
148
from tests.dtos import HelloWithEnum
9+
from .dtos import *
1510

1611

1712
class TestEnumSerialization(unittest.TestCase):

tests/test_jsonserviceclient.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
"""JsonServiceClient Tests
22
"""
3-
import operator
43
import unittest
5-
from dataclasses_json import dataclass_json, LetterCase, Undefined, config
6-
import dataclasses
7-
8-
import requests
9-
from dataclasses_json import config, dataclass_json, Undefined
104

5+
from servicestack.clients import append_querystring
116
from servicestack.utils import *
127
from tests.config import *
138
from .dtos import *
@@ -33,3 +28,9 @@ def test_does_serialize_dates_correctly_via_get_request(self):
3328
request = EchoTypes(date_time=datetime.datetime(2015, 1, 1, tzinfo=timezone.utc))
3429
response: EchoTypes = client.get(request)
3530
self.assertEqual(response.date_time, request.date_time)
31+
32+
def test_should_generate_default_value(self):
33+
client = create_test_client()
34+
request = HelloTypes(bool_=False, int_=0)
35+
request_url = append_querystring(TEST_URL, to_dict(request))
36+
self.assertEqual(request_url, TEST_URL + "?bool=false&int=0")

0 commit comments

Comments
 (0)