def equal(expect, actual): _expect = base.deserialize(expect) _actual = base.deserialize(actual) if _expect != _actual: raise Json.NotExpectedError(_expect, _actual)
def compact(jsonld, context=None): ldobj = base.deserialize(jsonld) _context = model.Context(context or ldobj.get('@context')) result = functor.compose([ compaction.contextualize_props(_context), compaction.contextualize_types(_context), compaction.normalize_empty_lists, compaction.drop_null, compaction.squeeze_lists, compaction.drop_unmapped(_context), compaction.nullify_nonetype, compaction.opt_empty_collection(_context), compaction.add_singleton_sets(_context), compaction.shorten_prop_prefixes(_context), compaction.shorten_type_prefixes(_context), compaction.squeeze_id_only_nodes(_context), compaction.contextualize_props(_context), compaction.squeeze_redundant_types(_context), compaction.sort_arrays_by_id ]).apply(ldobj) if '@id' in result and 2 > len(result): del (result['@id']) if _context: if isinstance(result, list): result[0]['@context'] = (base.deserialize(context)['@context']) else: result['@context'] = (base.deserialize(context)['@context']) if isinstance(result, list) and 1 == len(result): result = result[0] return str(result).replace("'", '"')
def test_term_identifies_all_parts_simple_iri(self): context = base.deserialize(""" { "@context": { "ex": "http://example.com/", "term3": "http://example.org/Thing3" } } """)['@context'] term3 = model.Context.Term('term3', context['term3'], context) self.assertEqual('term3', term3.name) self.assertTrue(term3.prefix is None) self.assertEqual(term3.ident, term3.expanded_url)
def test_term_identifies_all_parts_prefixed_term(self): context = base.deserialize(""" { "@context": { "ex": "http://example.com/", "term2": "ex:Thing2" } } """)['@context'] term2 = model.Context.Term('term2', context['term2'], context) self.assertEqual('term2', term2.name) self.assertEqual('ex:Thing2', term2.ident) self.assertEqual('ex', term2.prefix) self.assertEqual('http://example.com/Thing2', term2.expanded_url)
def test_term_identifies_all_parts_expanded_object(self): context = base.deserialize(""" { "@context": { "ex": "http://example.com/", "term1": { "@type": "ex:Type1", "@id": "ex:Thing1" } } } """)['@context'] term1 = model.Context.Term('term1', context['term1'], context) self.assertEqual('term1', term1.name) self.assertEqual('ex:Thing1', term1.ident) self.assertEqual('ex', term1.prefix) self.assertEqual('ex:Type1', term1.ldtype) self.assertEqual('http://example.com/Thing1', term1.expanded_url)