From 804aa70281483832c9b0cbd685cb15434b33a7ab Mon Sep 17 00:00:00 2001 From: Mike Bennett Date: Sun, 11 Jun 2023 11:16:32 +0100 Subject: [PATCH 1/3] Add tests for excluding context --- tests/test_basic.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/test_basic.py b/tests/test_basic.py index 57cdbcc..f29c47f 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -87,6 +87,27 @@ def testLabel(self): self.assertTrue('en' in manifest.label, 'Manifest seems to be missing English label') self.assertEqual(manifest.label['en'][0], 'default label', 'Unexpected label for manifest') + def test_context_default(self): + """Test that @context is included by default for .json() calls.""" + manifest = Manifest(id='http://iiif.example.org/prezi/Manifest/0', type='Manifest', label={'en': ['default label']}) + manifest_json = manifest.json() + + self.assertEqual(manifest_json[:61], '{"@context": "http://iiif.io/api/presentation/3/context.json"') + + def test_context_jsonld(self): + """Test that @context is included by default for .jsonld() calls.""" + manifest = Manifest(id='http://iiif.example.org/prezi/Manifest/0', type='Manifest', label={'en': ['default label']}) + manifest_json = manifest.jsonld() + + self.assertEqual(manifest_json[:61], '{"@context": "http://iiif.io/api/presentation/3/context.json"') + + def test_context_excluded(self): + """Test that @context is excluded when requested.""" + manifest = Manifest(id='http://iiif.example.org/prezi/Manifest/0', type='Manifest', label={'en': ['default label']}) + manifest_json = manifest.json(exclude_context=True) + + self.assertEqual(manifest_json[:49], '{"id": "http://iiif.example.org/prezi/Manifest/0"') + if __name__ == '__main__': unittest.main() From a833527f4b5f5539e20170f897e4e0acb24fbc7e Mon Sep 17 00:00:00 2001 From: Mike Bennett Date: Sun, 11 Jun 2023 11:16:48 +0100 Subject: [PATCH 2/3] Implement optional context exclusion --- iiif_prezi3/base.py | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/iiif_prezi3/base.py b/iiif_prezi3/base.py index 1399b51..c032d70 100644 --- a/iiif_prezi3/base.py +++ b/iiif_prezi3/base.py @@ -5,7 +5,6 @@ class Base(BaseModel): - class Config: validate_assignment = True validate_all = True @@ -59,26 +58,32 @@ def __setattr__(self, key, value): # and now pass upwards for pydantic to validate and set super().__setattr__(key, value) - def json(self, **kwargs): - return self.jsonld(**kwargs) - - def jsonld(self, **kwargs): + def json(self, exclude_context=False, **kwargs): # approach 6- use the pydantic .dict() function to get the dict with pydantic options, add the context at the top and dump to json with modified kwargs excluded_args = ["exclude_unset", "exclude_defaults", "exclude_none", "by_alias", "ensure_ascii", "default"] pydantic_args = ["include", "exclude", "encoder"] dict_kwargs = dict([(arg, kwargs[arg]) for arg in kwargs.keys() if arg in pydantic_args]) json_kwargs = dict([(arg, kwargs[arg]) for arg in kwargs.keys() if arg not in pydantic_args + excluded_args]) - return json.dumps({"@context": "http://iiif.io/api/presentation/3/context.json", - **self.dict(exclude_unset=False, - exclude_defaults=False, - exclude_none=True, - by_alias=True, - **dict_kwargs)}, + + dict_out = self.dict(exclude_unset=False, + exclude_defaults=False, + exclude_none=True, + by_alias=True, + **dict_kwargs) + + if not exclude_context: + dict_out = {"@context": "http://iiif.io/api/presentation/3/context.json", + **dict_out} + + return json.dumps(dict_out, ensure_ascii=False, default=pydantic_encoder, **json_kwargs) + def jsonld(self, **kwargs): + return self.json(exclude_context=False, **kwargs) + def jsonld_dict(self, **kwargs): pydantic_args = ["include", "exclude", "encoder"] dict_kwargs = dict([(arg, kwargs[arg]) for arg in kwargs.keys() if arg in pydantic_args]) From 356ad449ddd5ca1194f5e23630e31b90e0167837 Mon Sep 17 00:00:00 2001 From: Mike Bennett Date: Sun, 11 Jun 2023 11:19:25 +0100 Subject: [PATCH 3/3] Add an extra test for .jsonld() --- tests/test_basic.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/test_basic.py b/tests/test_basic.py index f29c47f..1aa1f54 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -108,6 +108,13 @@ def test_context_excluded(self): self.assertEqual(manifest_json[:49], '{"id": "http://iiif.example.org/prezi/Manifest/0"') + def text_jsonld_context_excluded(self): + """Test that @context is not excluded from .jsonld() calls, even when requested.""" + manifest = Manifest(id='http://iiif.example.org/prezi/Manifest/0', type='Manifest', label={'en': ['default label']}) + manifest_json = manifest.jsonld(exclude_context=True) + + self.assertEqual(manifest_json[:61], '{"@context": "http://iiif.io/api/presentation/3/context.json"') + if __name__ == '__main__': unittest.main()