Skip to content

Commit 27402b0

Browse files
jguerreiroJguer
authored andcommitted
fix(fields): unknown fields should be excluded
1 parent 2badb7b commit 27402b0

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

pygitguardian/models.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
class BaseSchema(Schema):
1919
class Meta:
2020
ordered = True
21+
unknown = EXCLUDE
2122

2223

2324
class Base:
@@ -47,9 +48,6 @@ def __bool__(self) -> bool:
4748

4849

4950
class DocumentSchema(BaseSchema):
50-
class Meta:
51-
unknown = EXCLUDE
52-
5351
filename = fields.String(validate=validate.Length(max=256), allow_none=True)
5452
document = fields.String(required=True)
5553

tests/test_models.py

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,39 @@
1-
from unittest import TestCase
1+
from typing import OrderedDict
22

3-
from pygitguardian.models import Document
3+
from pygitguardian.models import Document, DocumentSchema, Match, MatchSchema
44

55

6-
class TestModel(TestCase):
6+
class TestModel:
77
def test_document_model(self):
8+
"""
9+
GIVEN a simple document
10+
THEN base model methods should produce the appropriate types.
11+
"""
812
document = Document("hello", "hello")
9-
self.assertIsInstance(document.to_json(), str)
10-
self.assertIsInstance(document.to_dict(), dict)
11-
self.assertIsInstance(str(document), str)
13+
assert isinstance(document.to_json(), str)
14+
assert isinstance(document.to_dict(), dict)
15+
assert isinstance(str(document), str)
16+
17+
def test_schema_excludes(self):
18+
"""
19+
GIVEN a simple document and an extra field in dict format
20+
WHEN loading using the schema
21+
THEN the extra field should be excluded
22+
"""
23+
document = {"filename": "hello", "document": "hello", "extra": "field"}
24+
schema = DocumentSchema()
25+
26+
document_obj = schema.load(document)
27+
assert isinstance(document_obj, OrderedDict)
28+
29+
def test_schema_loads(self):
30+
"""
31+
GIVEN a simple match and an extra field in dict format
32+
WHEN loading using the schema
33+
THEN the extra field should be excluded and the result should be a Match
34+
"""
35+
match = {"match": "hello", "type": "hello", "extra": "field"}
36+
schema = MatchSchema()
37+
38+
match_obj = schema.load(match)
39+
assert isinstance(match_obj, Match)

0 commit comments

Comments
 (0)