Skip to content

Commit f9a3bb7

Browse files
Jeremy DesanlisJguer
authored andcommitted
feat(models): make models Schema ordered
1 parent dcec5a2 commit f9a3bb7

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

pygitguardian/models.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,13 @@
1515
from .config import DOCUMENT_SIZE_THRESHOLD_BYTES
1616

1717

18+
class BaseSchema(Schema):
19+
class Meta:
20+
ordered = True
21+
22+
1823
class Base:
19-
SCHEMA: ClassVar[Schema]
24+
SCHEMA: ClassVar[BaseSchema]
2025

2126
def __init__(self):
2227
self.status_code = None
@@ -41,7 +46,7 @@ def __bool__(self) -> bool:
4146
return self.status_code == 200
4247

4348

44-
class DocumentSchema(Schema):
49+
class DocumentSchema(BaseSchema):
4550
class Meta:
4651
unknown = EXCLUDE
4752

@@ -89,7 +94,7 @@ def __repr__(self):
8994
return "filename:{0}, document:{1}".format(self.filename, self.document)
9095

9196

92-
class DetailSchema(Schema):
97+
class DetailSchema(BaseSchema):
9398
detail = fields.String(required=True)
9499

95100
@pre_load
@@ -124,7 +129,7 @@ def __repr__(self):
124129
return "{0}:{1}".format(self.status_code, self.detail)
125130

126131

127-
class MatchSchema(Schema):
132+
class MatchSchema(BaseSchema):
128133
match = fields.String(required=True)
129134
match_type = fields.String(data_key="type", required=True)
130135
line_start = fields.Int(allow_none=True)
@@ -188,7 +193,7 @@ def __repr__(self):
188193
)
189194

190195

191-
class PolicyBreakSchema(Schema):
196+
class PolicyBreakSchema(BaseSchema):
192197
break_type = fields.String(data_key="type", required=True)
193198
policy = fields.String(required=True)
194199
matches = fields.List(fields.Nested(MatchSchema), required=True)
@@ -226,7 +231,7 @@ def __repr__(self):
226231
)
227232

228233

229-
class ScanResultSchema(Schema):
234+
class ScanResultSchema(BaseSchema):
230235
policy_break_count = fields.Integer(required=True)
231236
policies = fields.List(fields.String(), required=True)
232237
policy_breaks = fields.List(fields.Nested(PolicyBreakSchema), required=True)
@@ -308,7 +313,7 @@ def __str__(self):
308313
)
309314

310315

311-
class MultiScanResultSchema(Schema):
316+
class MultiScanResultSchema(BaseSchema):
312317
scan_results = fields.List(
313318
fields.Nested(ScanResultSchema),
314319
required=True,
@@ -379,7 +384,7 @@ def __str__(self):
379384
)
380385

381386

382-
class QuotaSchema(Schema):
387+
class QuotaSchema(BaseSchema):
383388
count = fields.Int()
384389
limit = fields.Int()
385390
remaining = fields.Int()
@@ -419,7 +424,7 @@ def __repr__(self):
419424
)
420425

421426

422-
class QuotaResponseSchema(Schema):
427+
class QuotaResponseSchema(BaseSchema):
423428
content = fields.Nested(QuotaSchema)
424429

425430
@post_load

tests/test_client.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
from collections import OrderedDict
23
from datetime import date
34
from typing import Any, Dict, List, Optional, Type
45
from unittest.mock import Mock, patch
@@ -272,7 +273,7 @@ def test_health_check(client: GGClient):
272273
assert bool(health)
273274
assert health.success
274275

275-
assert type(health.to_dict()) == dict
276+
assert type(health.to_dict()) == OrderedDict
276277
assert type(health.to_json()) == str
277278

278279

@@ -285,7 +286,7 @@ def test_health_check_error(client: GGClient):
285286
assert bool(health) is False
286287
assert health.success is False
287288

288-
assert type(health.to_dict()) == dict
289+
assert type(health.to_dict()) == OrderedDict
289290
assert type(health.to_json()) == str
290291

291292

@@ -331,7 +332,7 @@ def test_multi_content_scan(
331332
pytest.fail("multiscan is not a MultiScanResult")
332333
return
333334

334-
assert type(multiscan.to_dict()) == dict
335+
assert type(multiscan.to_dict()) == OrderedDict
335336
assert type(multiscan.to_json()) == str
336337
assert type(repr(multiscan)) == str
337338
assert type(str(multiscan)) == str
@@ -463,7 +464,7 @@ def test_content_scan(
463464
else:
464465
pytest.fail("returned should be a ScanResult")
465466

466-
assert type(scan_result.to_dict()) == dict
467+
assert type(scan_result.to_dict()) == OrderedDict
467468
scan_result_json = scan_result.to_json()
468469
assert type(scan_result_json) == str
469470
assert type(json.loads(scan_result_json)) == dict
@@ -564,7 +565,7 @@ def test_quota_overview(client: GGClient):
564565
else:
565566
pytest.fail("returned should be a QuotaResponse")
566567

567-
assert type(quota_response.to_dict()) == dict
568+
assert type(quota_response.to_dict()) == OrderedDict
568569
quota_response_json = quota_response.to_json()
569570
assert type(quota_response_json) == str
570571
assert type(json.loads(quota_response_json)) == dict

0 commit comments

Comments
 (0)