def test_serialise_object(self): element = Element('object', content=[ Element('member', content=KeyValuePair( key=Element('string', content='id'), value=Element('string', content='Hello World') )) ]) self.assertEqual(self.serialise(element), { 'element': 'object', 'content': [ { 'element': 'member', 'content': { 'key': { 'element': 'string', 'content': 'id', }, 'value': { 'element': 'string', 'content': 'Hello World', } } } ] })
def test_serialise_object_empty_value(self): element = Element('object', content=[ Element('member', content=KeyValuePair( key=Element('string', content='id'), value=Array() )) ]) self.assertEqual(self.serialise(element), { 'element': 'object', 'content': [ { 'element': 'member', 'content': { 'key': { 'element': 'string', 'content': 'id', }, 'value': { 'element': 'array', 'content': [], } } } ] })
def test_equality(self): element1 = Element('string', content='Hello') element2 = Element('string', content='Hello') self.assertEqual(element1, element2) self.assertNotEqual(element1, Element('string', content='Hello1')) self.assertNotEqual(element1, Element('strings', content='Hello'))
def test_recursive_children_array(self): element = Element('things', content=[ Element( 'morethings', content=[Element('string', content='hi')]) ]) self.assertEqual(len(list(element.recursive_children)), 2)
def test_serailise_custom(self): element = Element('custom', content=Element('string', content='Hello')) self.assertEqual(self.serialise(element), { 'element': 'custom', 'content': { 'element': 'string', 'content': 'Hello' } })
def test_repr(self): element = Element('string', content='Hello World') self.assertEqual(repr(element), "<Element(string) content='Hello World'>") element = Element('array', content=[String('Hello World')]) desc = "<Element(array) content=[<String content=None>]>" self.assertEqual(repr(element), desc)
def test_serialise_attributes(self): element = Element('string', attributes={'test': Element('example')}) self.assertEqual(self.serialise(element), { 'element': 'string', 'attributes': { 'test': { 'element': 'example' } } })
def test_serialise_meta_ref(self): pointer = Element('ref', content='Test') element = Element('string', meta=Metadata(ref=pointer)) self.assertEqual(self.serialise(element), { 'element': 'string', 'meta': { 'ref': { 'element': 'ref', 'content': 'Test' } } })
def test_serialise_array(self): element = Element('array', content=[ Element('string', content='Hello World') ]) self.assertEqual(self.serialise(element), { 'element': 'array', 'content': [ { 'element': 'string', 'content': 'Hello World', } ] })
def test_object_value(self): element = Element('object', content=[ Member(key=String(content='key'), value=String(content='value')) ]) self.assertEqual(element.defract, {'key': 'value'})
def test_serialise_meta_links(self): element = Element('string', meta=Metadata(links=Array(content=[ Element('link', content='https://example.com') ]))) self.assertEqual(self.serialise(element), { 'element': 'string', 'meta': { 'links': { 'element': 'array', 'content': [ { 'element': 'link', 'content': 'https://example.com' } ] } } })
def test_serialise_key_value_pair_element(self): pair = KeyValuePair( key=String(content='name'), value=String(content='Doe') ) payload = self.serialiser.serialise(Element('member', content=pair)) self.assertEqual(payload, '["member", null, null, ["pair", ' '["string", null, null, "name"], ' '["string", null, null, "Doe"]' ']]')
def test_serialise_meta_id(self): element = Element('string', meta=Metadata(id=String(content='Test'))) self.assertEqual(self.serialise(element), { 'element': 'string', 'meta': { 'id': { 'element': 'string', 'content': 'Test' } } })
def test_serialise_empty_meta(self): element = Element('string', meta=Metadata(classes=Array(content=[]))) self.assertEqual(self.serialise(element), { 'element': 'string', 'meta': { 'classes': { 'element': 'array', 'content': [] } } })
def test_serialise_meta_description(self): meta = Metadata(description=String(content='Test')) element = Element('string', meta=meta) self.assertEqual(self.serialise(element), { 'element': 'string', 'meta': { 'description': { 'element': 'string', 'content': 'Test' } } })
def test_serialise_meta_title(self): meta = Metadata(title=String(content='Test')) element = Element('string', meta=meta) self.assertEqual(self.serialise(element), { 'element': 'string', 'meta': { 'title': { 'element': 'string', 'content': 'Test' } } })
def test_serialise_meta_classes(self): element = Element('string', meta=Metadata(classes=Array(content=[ String(content='warning') ]))) self.assertEqual(self.serialise(element), { 'element': 'string', 'meta': { 'classes': { 'element': 'array', 'content': [ { 'element': 'string', 'content': 'warning' } ] } } })
def test_serialise_null(self): element = Element('null', content=None) self.assertEqual(self.serialise(element), { 'element': 'null', 'content': None, })
def test_children_direct_element(self): element = Element('thing', content=Element('string', content='hello world')) self.assertEqual(len(element.children), 1)
def test_children_array(self): element = Element('things', content=[Element('string', content='hello world')]) self.assertEqual(len(element.children), 1)
def test_serialise_boolean(self): element = Element('boolean', content=True) self.assertEqual(self.serialise(element), { 'element': 'boolean', 'content': True, })
def test_serialise_element_element(self): payload = self.serialiser.serialise(Element('test', content=String(content='value'))) self.assertEqual(payload, '["test", null, null, ' '["string", null, null, "value"]]')
def test_serialise_array_element(self): payload = self.serialiser.serialise(Element('array', content=[String(content='value')])) self.assertEqual(payload, '["array", null, null, ' '[["string", null, null, "value"]]]')
def test_serialise_number(self): element = Element('number', content=3) self.assertEqual(self.serialise(element), { 'element': 'number', 'content': 3, })
def test_setting_attributes(self): element = Element() element.attributes['key'] = 'value' self.assertIsInstance(element.attributes['key'], String) self.assertEqual(element.attributes['key'].content, 'value')
def test_serialise_element_as_json(self): element = Element('string') serialised = self.serialiser.serialise(element) self.assertEqual(serialised, '{"element": "string"}')
def test_key_value_pair_value(self): element = Element('element', content=KeyValuePair(key=String(content='key'), value=String(content='value'))) self.assertEqual(element.defract, ('key', 'value'))
def test_serialise_string(self): element = Element('string', content='Hello World') self.assertEqual(self.serialise(element), { 'element': 'string', 'content': 'Hello World', })
def test_children_string(self): element = Element('string', content='hello world') self.assertEqual(element.children, [])
def test_recursive_children_string(self): element = Element('string', content='hello world') self.assertEqual(len(list(element.recursive_children)), 0)