Ejemplo n.º 1
0
    def testJsonLd(self):
        d = {
            "type": "resource",
            "value-type": "resource-jsonld",
            "value": "Douglas Adams",
            "graph": {
                "@context": "http://schema.org/",
                "@type": "Person",
                "name": {"@value": "Douglas Adams", "@language": "en"},
                "description": [
                    {"@value": "English writer and humorist", "@language": "en"},
                    {"@value": "écrivain anglais de science-fiction", "@language": "fr"}
                ],
                "sameAs": "http://www.wikidata.org/entity/Q42",
            }
        }
        o = AbstractNode.from_dict(d)
        self.assertEqual(o.value, 'Douglas Adams')
        self.assertEqual(o.graph['@context'], 'http://schema.org/')
        self.assertRaises(TypeError, hash, o)

        d2 = copy.deepcopy(d)
        d2['value'] = 'foo'
        o2 = AbstractNode.from_dict(d2)
        self.assertEqual(o, o2)
        d3 = copy.deepcopy(d)
        d3['graph']['sameAs'] = 'bar'
        o3 = AbstractNode.from_dict(d3)
        self.assertNotEqual(o, o3)
        self.assertNotEqual(o2, o3)
 def testTripleIntersection(self):
     t = Intersection([
         Triple(
             Resource('a'),
             Resource('b'),
             Missing()),
         Triple(
             Resource('c'),
             Resource('d'),
             Missing())])
     AbstractNode.from_dict(t.as_dict())
 def testListNodesType(self):
     d1 = {"type": "sort", "predicate": {"type": "resource", "value": "foo"},
           "list": [{"type": "resource", "value": "George Washington"},
                    {"type": "resource", "value": "Theodore Roosevelt"}]}
     self.assertRaises(TypeError, AbstractNode.from_dict, d1)
     d2 = {"type": "union",
           "list": [{"type": "resource", "value": "George Washington"},
                    {"type": "resource", "value": "Theodore Roosevelt"}]}
     AbstractNode.from_dict(d2)
     d3 = {"type": "sort", "predicate": {"type": "resource", "value": "foo"},
           "list": {"type": "resource", "value": "George Washington"}}
     AbstractNode.from_dict(d3)
 def testUnion(self):
     d1 = {"type": "union",
           "list": [{"type": "resource", "value": "George Washington"},
                    {"type": "resource", "value": "Theodore Roosevelt"}]}
     d2 = {"type": "union",
           "list": [{"type": "list", "list": [{"type": "resource", "value": "George Washington"}]},
                    {"type": "list", "list": [{"type": "resource", "value": "Theodore Roosevelt"}]}]}
     o1 = AbstractNode.from_dict(d1)
     o2 = AbstractNode.from_dict(d2)
     self.assertEqual(o1.as_dict(), d1)
     self.assertEqual(o2.as_dict(), d2)
     self.assertEqual(o1.list[0], Resource('George Washington'))
     o1.as_json()
 def testFromJsonNone(self):
     d = {'type': 'triple',
          'subject': r('s'), 'predicate': r('p'), 'object': m()}
     n = AbstractNode.from_dict(d)
     self.assertEqual(d, json.loads(n.as_json()))
     self.assertIn('object', n)
     self.assertEqual(n.object, M())
 def testFromJsonMissing(self):
     d = {'type': 'triple',
         'subject': r('s'), 'predicate': r('p')}
     n = AbstractNode.from_dict(d)
     self.assertEqual(d, json.loads(n.as_json()))
     self.assertEqual(n.predicate, r('p'))
     self.assertNotIn('object', n)
Ejemplo n.º 7
0
 def testList(self):
     r1 = {'type': 'resource', 'value': 'foo'}
     r2 = {'type': 'resource', 'value': 'bar'}
     d = {'type': 'list', 'list': [r1, r2]}
     o = AbstractNode.from_dict(d)
     self.assertEqual(o.list, [Resource('foo'), Resource('bar')])
     self.assertIsInstance(o.list[1], Resource)
     self.assertEqual(o.as_dict(), d)
Ejemplo n.º 8
0
 def testSerialize(self):
     d = {
         'type': 'resource',
         'value': 'foo',
         'value-type': 'bar',
         'baz-qux': 'quux'
     }
     self.assertEqual(AbstractNode.from_dict(d).as_dict(), d)
 def testList(self):
     r1 = {'type': 'resource', 'value': 'foo'}
     r2 = {'type': 'resource', 'value': 'bar'}
     d = {'type': 'list', 'list': [r1, r2]}
     o = AbstractNode.from_dict(d)
     self.assertEqual(o.list, [Resource('foo'), Resource('bar')])
     self.assertIsInstance(o.list[1], Resource)
     self.assertEqual(o.as_dict(), d)
Ejemplo n.º 10
0
 def testFromJsonNone(self):
     d = {
         'type': 'triple',
         'subject': r('s'),
         'predicate': r('p'),
         'object': m()
     }
     n = AbstractNode.from_dict(d)
     self.assertEqual(d, json.loads(n.as_json()))
     self.assertIn('object', n)
     self.assertEqual(n.object, M())
 def testListNodesType(self):
     d1 = {
         "type":
         "sort",
         "predicate": {
             "type": "resource",
             "value": "foo"
         },
         "list": [{
             "type": "resource",
             "value": "George Washington"
         }, {
             "type": "resource",
             "value": "Theodore Roosevelt"
         }]
     }
     self.assertRaises(TypeError, AbstractNode.from_dict, d1)
     d2 = {
         "type":
         "union",
         "list": [{
             "type": "resource",
             "value": "George Washington"
         }, {
             "type": "resource",
             "value": "Theodore Roosevelt"
         }]
     }
     AbstractNode.from_dict(d2)
     d3 = {
         "type": "sort",
         "predicate": {
             "type": "resource",
             "value": "foo"
         },
         "list": {
             "type": "resource",
             "value": "George Washington"
         }
     }
     AbstractNode.from_dict(d3)
Ejemplo n.º 12
0
 def testValueType(self):
     d = {'type': 'resource', 'value': 'foo', 'value-type': 'bar',
          'extra': 'baz'}
     o = AbstractNode.from_dict(d)
     self.assertEqual(o.value_type, 'bar')
     self.assertFalse(hasattr(o, 'extra'))
     self.assertRaises(AttributeError, o.get, 'extra')
     self.assertEqual(o.get('extra', strict=False), 'baz')
     self.assertEqual(o.as_dict(), d)
     self.assertEqual(Resource('foo').traverse(lambda x:x),
             Resource('foo'))
     self.assertEqual(Resource('type').traverse(lambda x:x),
             Resource('type'))
 def testUnion(self):
     d1 = {
         "type":
         "union",
         "list": [{
             "type": "resource",
             "value": "George Washington"
         }, {
             "type": "resource",
             "value": "Theodore Roosevelt"
         }]
     }
     d2 = {
         "type":
         "union",
         "list": [{
             "type":
             "list",
             "list": [{
                 "type": "resource",
                 "value": "George Washington"
             }]
         }, {
             "type":
             "list",
             "list": [{
                 "type": "resource",
                 "value": "Theodore Roosevelt"
             }]
         }]
     }
     o1 = AbstractNode.from_dict(d1)
     o2 = AbstractNode.from_dict(d2)
     self.assertEqual(o1.as_dict(), d1)
     self.assertEqual(o2.as_dict(), d2)
     self.assertEqual(o1.list[0], Resource('George Washington'))
     o1.as_json()
 def testFromJsonPerfect(self):
     d = {'type': 'triple',
         'subject': r('s'), 'predicate': r('p'), 'object': r('o')}
     self.assertIsInstance(AbstractNode.from_dict(d), Triple)
     self.assertIsInstance(AbstractNode.from_json(json.dumps(d)), Triple)
     self.assertEqual(d, json.loads(AbstractNode.from_dict(d).as_json()))
     self.assertEqual(d, json.loads(AbstractNode.from_json(json.dumps(d)).as_json()))
     self.assertIsInstance(AbstractNode.from_json(json.dumps(d)).subject, Resource)
     self.assertEqual(AbstractNode.from_json(json.dumps(d)).subject.value, 's')
Ejemplo n.º 15
0
 def testFromJsonPerfect(self):
     d = {
         'type': 'triple',
         'subject': r('s'),
         'predicate': r('p'),
         'object': r('o')
     }
     self.assertIsInstance(AbstractNode.from_dict(d), Triple)
     self.assertIsInstance(AbstractNode.from_json(json.dumps(d)), Triple)
     self.assertEqual(d, json.loads(AbstractNode.from_dict(d).as_json()))
     self.assertEqual(
         d, json.loads(AbstractNode.from_json(json.dumps(d)).as_json()))
     self.assertIsInstance(
         AbstractNode.from_json(json.dumps(d)).subject, Resource)
     self.assertEqual(
         AbstractNode.from_json(json.dumps(d)).subject.value, 's')
Ejemplo n.º 16
0
 def testTime(self):
     d = {'type': 'resource', 'value': '2010-05-08T23:41:54.000Z', 'value-type': 'time'}
     o = AbstractNode.from_dict(d)
     self.assertEqual(o.value, '2010-05-08T23:41:54.000Z')
Ejemplo n.º 17
0
 def testBoolean(self):
     d = {'type': 'resource', 'value': 'true', 'value-type': 'boolean'}
     o = AbstractNode.from_dict(d)
     self.assertIsInstance(o, BooleanResource)
     self.assertEqual(o.value, True)
     self.assertEqual(o.as_dict(), d)
Ejemplo n.º 18
0
 def testFromJsonMissing(self):
     d = {'type': 'triple', 'subject': r('s'), 'predicate': r('p')}
     n = AbstractNode.from_dict(d)
     self.assertEqual(d, json.loads(n.as_json()))
     self.assertEqual(n.predicate, r('p'))
     self.assertNotIn('object', n)
Ejemplo n.º 19
0
 def testTripleIntersection(self):
     t = Intersection([
         Triple(Resource('a'), Resource('b'), Missing()),
         Triple(Resource('c'), Resource('d'), Missing())
     ])
     AbstractNode.from_dict(t.as_dict())
 def testSerialize(self):
     d = {'type': 'resource', 'value': 'foo', 'value-type': 'bar', 'baz-qux': 'quux'}
     self.assertEqual(AbstractNode.from_dict(d).as_dict(), d)