Ejemplo n.º 1
0
    def testAddRelation(self):
        for rel in self.ot.queryRelations():
            rel.remove()
        # Remove the default relations
        db.flush()

        relA = self.addRelation(name=u"relA")
        relB = self.addRelation(name=u"relB", inverse=relA.name)
        assert_equal(relA, relB.inverse)

        # Ensure that the inverse is properly created
        relC = self.addRelation(name=u"relC", types=["transitive"], weight=0.5, inverse=u"relD")
        relD = self.ot.getRelation(u"relD")
        ok_(relD)
        assert_equal(relD.inverse, relC)
        assert_equal(relD.types, ["transitive"])
        assert_equal(relD.weight, 0.5)

        # Test that adding a duplicate relation will cause an update.
        self.addRelation(name=u"relD", weight=0)
        relD = self.ot.getRelation(u"relD")
        assert_equal(relD.inverse, None)
        assert_equal(relD.types, [])
        assert_equal(relD.weight, 0)

        # Tests for removing and querying relations are also snuck in here
        self.ot.delRelation(u"relD")
        db.flush()
        assert_raises(PyolsNotFound, self.ot.getRelation, name=u"relD")

        assert_equal(set(["relA", "relB", "relC"]), set([r.name for r in self.ot.queryRelations()]))
Ejemplo n.º 2
0
 def addRelation(self, name=u"testRel", weight=1.0, types=[], inverse=None):
     newRel = self.ot.addRelation(name=name,
                                  weight=weight,
                                  types=types,
                                  inverse=inverse)
     db.flush()  # Simulate a web request -- flush
     return newRel
Ejemplo n.º 3
0
    def testAddRelation(self):
        for rel in self.ot.queryRelations():
            rel.remove()
        # Remove the default relations
        db.flush()

        relA = self.addRelation(name=u"relA")
        relB = self.addRelation(name=u"relB", inverse=relA.name)
        assert_equal(relA, relB.inverse)

        # Ensure that the inverse is properly created
        relC = self.addRelation(name=u"relC",
                                types=['transitive'],
                                weight=0.5,
                                inverse=u"relD")
        relD = self.ot.getRelation(u"relD")
        ok_(relD)
        assert_equal(relD.inverse, relC)
        assert_equal(relD.types, ['transitive'])
        assert_equal(relD.weight, 0.5)

        # Test that adding a duplicate relation will cause an update.
        self.addRelation(name=u"relD", weight=0)
        relD = self.ot.getRelation(u"relD")
        assert_equal(relD.inverse, None)
        assert_equal(relD.types, [])
        assert_equal(relD.weight, 0)

        # Tests for removing and querying relations are also snuck in here
        self.ot.delRelation(u"relD")
        db.flush()
        assert_raises(PyolsNotFound, self.ot.getRelation, name=u"relD")

        assert_equal(set(["relA", "relB", "relC"]),
                     set([r.name for r in self.ot.queryRelations()]))
Ejemplo n.º 4
0
    def testSetInverse(self):
        relA = self.relation_new(name=u"relA")
        relB = self.relation_new(name=u"relB")
        relC = self.relation_new(name=u"relC")
        relD = self.relation_new(name=u"relD")
        db.flush()

        relA.inverse = None # Nothing should happen

        relA.inverse = relB
        assert_equal(relA.inverse, relB)
        assert_equal(relB.inverse, relA)

        # Get rid of A's inverse
        relA.inverse = None
        assert_equal(relA.inverse, None)
        assert_equal(relB.inverse, None)

        relA.inverse = relB
        # Change A's inverse to C
        relA.inverse = relC
        assert_equal(relA.inverse, relC)
        assert_equal(relB.inverse, None)
        assert_equal(relC.inverse, relA)

        relA.inverse = relB
        relC.inverse = relD
        # In this case, both B and C have different
        # inverses before they are paired.
        relB.inverse = relC
        assert_equal(relA.inverse, None)
        assert_equal(relB.inverse, relC)
        assert_equal(relC.inverse, relB)
        assert_equal(relD.inverse, None)
Ejemplo n.º 5
0
    def testSetInverse(self):
        relA = self.relation_new(name=u"relA")
        relB = self.relation_new(name=u"relB")
        relC = self.relation_new(name=u"relC")
        relD = self.relation_new(name=u"relD")
        db.flush()

        relA.inverse = None  # Nothing should happen

        relA.inverse = relB
        assert_equal(relA.inverse, relB)
        assert_equal(relB.inverse, relA)

        # Get rid of A's inverse
        relA.inverse = None
        assert_equal(relA.inverse, None)
        assert_equal(relB.inverse, None)

        relA.inverse = relB
        # Change A's inverse to C
        relA.inverse = relC
        assert_equal(relA.inverse, relC)
        assert_equal(relB.inverse, None)
        assert_equal(relC.inverse, relA)

        relA.inverse = relB
        relC.inverse = relD
        # In this case, both B and C have different
        # inverses before they are paired.
        relB.inverse = relC
        assert_equal(relA.inverse, None)
        assert_equal(relB.inverse, relC)
        assert_equal(relC.inverse, relB)
        assert_equal(relD.inverse, None)
Ejemplo n.º 6
0
    def testLongNames(self):
        long = u'this string has more than 30 characters'
        long_trunc = long[:27] + '...'
        short = u'this string has exactly 26'

        self.ot.addKeyword(long)
        self.gc.addKeyword(long_trunc)
        self.ot.addKeyword(short)
        self.gc.addKeyword(short)

        self.ot.addRelation(long)
        self.ot.addRelation(short)

        db.flush()

        self.ot.addKeywordRelationship(long, long, long)
        self.gc.addKeywordRelationship(long_trunc, long_trunc, long_trunc)

        self.ot.addKeywordRelationship(short, short, short)
        self.gc.addKeywordRelationship(short, short, short)

        self.ot.addKeywordRelationship(long, short, short)
        self.gc.addKeywordRelationship(long_trunc, short, short)

        self.ot.addKeywordRelationship(short, long, short)
        self.gc.addKeywordRelationship(short, long_trunc, short)

        db.flush()
        dot = self.ot.getOntologyDotSource()
        self.gc.checkDot(dot)
Ejemplo n.º 7
0
 def addKeyword(self,
                name=u"testKW",
                disambiguation=u"dis",
                description=u"desc"):
     """ Add a keyword using a call to the OT. """
     kw = self.ot.addKeyword(name,
                             disambiguation=disambiguation,
                             description=description)
     db.flush()  # Mimic the flush that hapens at the end of each request
     return kw
Ejemplo n.º 8
0
    def testAddDuplicateInstance(self):
        kw = self.addKeyword()
        kw = self.addKeyword()
        # These should both succeed

        self.ot.addKeywordAssociation(kw.name, u'0')
        db.flush()
        self.ot.addKeywordAssociation(kw.name, u'1')
        db.flush()

        # The original keyword association should have been updated.
        ka = KeywordAssociation.get_by(path=u'1')
        ok_(ka)
        assert_equal(ka.keyword.name, kw.name)
Ejemplo n.º 9
0
    def testAddDuplicateInstance(self):
        kw = self.addKeyword()
        kw = self.addKeyword()
        # These should both succeed

        self.ot.addKeywordAssociation(kw.name, u"0")
        db.flush()
        self.ot.addKeywordAssociation(kw.name, u"1")
        db.flush()

        # The original keyword association should have been updated.
        ka = KeywordAssociation.get_by(path=u"1")
        ok_(ka)
        assert_equal(ka.keyword.name, kw.name)
Ejemplo n.º 10
0
    def testRemove(self):
        self.kw0.remove()
        db.flush()
        
        # No KeywordRelationships should be left
        assert_equal(len(list(KeywordRelationship.query_by())), 0)
        # And one KeywordAssociation is left
        assert_equal(len(list(KeywordAssociation.query_by())), 1)

        self.kw2.remove()
        db.flush()
        assert_equal(len(list(KeywordAssociation.query_by())), 0)
        
        # And only one keyword
        assert_equal(len(list(Keyword.query_by())), 1)
Ejemplo n.º 11
0
    def testRemove(self):
        self.kw0.remove()
        db.flush()

        # No KeywordRelationships should be left
        assert_equal(len(list(KeywordRelationship.query_by())), 0)
        # And one KeywordAssociation is left
        assert_equal(len(list(KeywordAssociation.query_by())), 1)

        self.kw2.remove()
        db.flush()
        assert_equal(len(list(KeywordAssociation.query_by())), 0)

        # And only one keyword
        assert_equal(len(list(Keyword.query_by())), 1)
Ejemplo n.º 12
0
 def testSetTypes(self):
     rel = self.relation_new(types=['transitive'])
     assert_equal(rel.types, ['transitive'])
     # Use a tuple (instead of a list) here to ensure
     # that it is not modified by _set_types
     to_check = (('transitive', 'symmetric'), ('functional', ),
                 ('inverse_functional', 'transitive'), [])
     for check in to_check:
         db.flush()  # Pretend we're in a web request
         rel.types = check
         # Make sure that we can flush the relation here
         # (ie, that there are no broken references from types)
         rel.flush()
         # It's imortant to wrap each one in set(...) because
         # their order is not guarenteed
         assert_equal(set(rel.types), set(check))
Ejemplo n.º 13
0
    def testAdd(self):
        kw0 = self.addKeyword(u"kw0")
        kw1 = self.addKeyword(u"kw1")
        rel = self.relation_new()
        self.ot.addKeywordRelationship(kw0.name, rel.name, kw1.name)
        db.flush()

        kwr = list(KeywordRelationship.query_by())[0]
        ok_(kwr)
        assert_equal(kwr.left.name, kw0.name)
        assert_equal(kwr.right.name, kw1.name)
        assert_equal(kwr.relation.name, rel.name)

        self.ot.namespace = u"new_ns"
        ok_(not self.keyword_getby(u"kw0"))
        self.addKeyword(name=u"kw0")
        ok_(self.keyword_getby(name=u"kw0"))
Ejemplo n.º 14
0
    def testAdd(self):
        kw0 = self.addKeyword(u"kw0")
        kw1 = self.addKeyword(u"kw1")
        rel = self.relation_new()
        self.ot.addKeywordRelationship(kw0.name, rel.name, kw1.name)
        db.flush()

        kwr = list(KeywordRelationship.query_by())[0]
        ok_(kwr)
        assert_equal(kwr.left.name, kw0.name)
        assert_equal(kwr.right.name, kw1.name)
        assert_equal(kwr.relation.name, rel.name)

        self.ot.namespace = u"new_ns"
        ok_(not self.keyword_getby(u"kw0"))
        self.addKeyword(name=u"kw0")
        ok_(self.keyword_getby(name=u"kw0"))
Ejemplo n.º 15
0
    def testUpdate(self):
        kw = rpcify(self.keyword_new())
        kw["name"] = u"new_name"
        self.ot.updateKeyword(kw)
        db.flush()
        ok_(self.keyword_getby(name=u"new_name"))

        rel = rpcify(self.relation_new())
        rel["inverse"] = rel["name"]
        rel["name"] = u"new_name"
        rel["bad_value"] = u"ohnoes!"
        self.ot.updateRelation(rel)
        db.flush()
        rel = Relation.get_by(name=u"new_name")
        ok_(rel)
        assert_equal(rel.inverse, rel)
        # It should be impossible to set "private" members this way
        ok_(not getattr(rel, "bad_value", None))
Ejemplo n.º 16
0
    def testUpdate(self):
        kw = rpcify(self.keyword_new())
        kw['name'] = u'new_name'
        self.ot.updateKeyword(kw)
        db.flush()
        ok_(self.keyword_getby(name=u'new_name'))

        rel = rpcify(self.relation_new())
        rel['inverse'] = rel['name']
        rel['name'] = u'new_name'
        rel['bad_value'] = u'ohnoes!'
        self.ot.updateRelation(rel)
        db.flush()
        rel = Relation.get_by(name=u'new_name')
        ok_(rel)
        assert_equal(rel.inverse, rel)
        # It should be impossible to set "private" members this way
        ok_(not getattr(rel, 'bad_value', None))
Ejemplo n.º 17
0
 def testSetTypes(self):
     rel = self.relation_new(types=['transitive'])
     assert_equal(rel.types, ['transitive'])
     # Use a tuple (instead of a list) here to ensure
     # that it is not modified by _set_types
     to_check = (('transitive', 'symmetric'),
                 ('functional', ),
                 ('inverse_functional', 'transitive'),
                 [])
     for check in to_check:
         db.flush() # Pretend we're in a web request
         rel.types = check
         # Make sure that we can flush the relation here
         # (ie, that there are no broken references from types)
         rel.flush()
         # It's imortant to wrap each one in set(...) because
         # their order is not guarenteed
         assert_equal(set(rel.types), set(check))
Ejemplo n.º 18
0
    def testGetRelatedKeywords(self):
        rs = {}
        rs["k"] = self.relation_new(u"kind of", weight=0.5, types=["symmetric"])
        rs["s"] = self.relation_new(u"synonym of", weight=1, types=["symmetric"])
        rs["b"] = self.relation_new(u"breed of", weight=0.75)
        kwrs = (
            ("animal", "kind of", "living thing"),
            ("dog", "kind of", "animal"),
            ("cat", "kind of", "animal"),
            ("cachorro", "synonym of", "dog"),
            ("egyptian", "breed of", "cat"),
            ("collie", "breed of", "dog"),
        )
        ks = {}
        for kwr in kwrs:
            for kw in (kwr[0], kwr[2]):
                if kw in ks:
                    continue
                ks[kw] = self.keyword_new(unicode(kw))
            KeywordRelationship.new(left=ks[kwr[0]], relation=rs[kwr[1][0]], right=ks[kwr[2]])
        db.flush()

        queries = (
            (("animal", 0.5, None), {"cat": 0.5, "living thing": 0.5, "dog": 0.5, "animal": 1, "cachorro": 0.5}),
            (("collie", 0.75, None), {"collie": 1, "dog": 0.75, "cachorro": 0.75}),
            (
                ("collie", 0, None),
                {
                    "collie": 1,
                    "living thing": 0.1875,
                    "egyptian": 0.140625,
                    "dog": 0.75,
                    "cat": 0.1875,
                    "animal": 0.375,
                    "cachorro": 0.75,
                },
            ),
            (("living thing", 0, [u"kind of"]), {"living thing": 1, "dog": 0.25, "animal": 0.5, "cat": 0.25}),
            (("dog", 0, [u"synonym of", u"breed of"]), {"collie": 0.75, "dog": 1, "cachorro": 1}),
        )

        for (query, expected) in queries:
            actual = self.ot.getRelatedKeywords(unicode(query[0]), cutoff=query[1], links=query[2])
            assert_equal(actual, expected)
Ejemplo n.º 19
0
    def testSimpleGraph(self):
        for kw in (u"kw0", u"kw1", u"kw2"):
            self.ot.addKeyword(kw)
            self.gc.addKeyword(kw)

        for rel in (u"rel0", u"rel1"):
            self.ot.addRelation(rel)

        db.flush()

        for kwr in ((u"kw0", u"rel0", u"kw1"),
                    (u"kw0", u"rel1", u"kw2"),
                    (u"kw2", u"rel0", u"kw0")):
            self.ot.addKeywordRelationship(*kwr)
            self.gc.addKeywordRelationship(*kwr)

        db.flush()

        dot = self.ot.getOntologyDotSource()
        self.gc.checkDot(dot)
Ejemplo n.º 20
0
    def testRemove(self):
        # Make sure a "naked" relation with no dependencies works
        for types in ([], ['transitive', 'symmetric']):
            rel = self.relation_new(types=types)
            rel.remove()
            db.flush()

        rel = self.relation_new(types=['transitive', 'symmetric'])
        kw0 = Keyword.new(namespace=self.ns, name=u"kw0")
        kw1 = Keyword.new(namespace=self.ns, name=u"kw1")
        kwr = KeywordRelationship.new(left=kw0, relation=rel, right=kw1)
        db.flush()

        rel.remove()
        db.flush()

        # No instances of these types should exist...
        empty_types = (KeywordRelationship, RelationType)
        for type in empty_types:
            assert_equal(list(type.query_by()), [])

        assert_equal(len(list(Relation.query_by())),
                     len(Relation.default_relations))

        # ... but the two keywords should remain.
        assert_equal(set([kw.name for kw in Keyword.query_by()]),
                     set(["kw0", "kw1"]))
Ejemplo n.º 21
0
    def testRemove(self):
        # Make sure a "naked" relation with no dependencies works
        for types in ([], ['transitive', 'symmetric']):
            rel = self.relation_new(types=types)
            rel.remove()
            db.flush()

        rel = self.relation_new(types=['transitive', 'symmetric'])
        kw0 = Keyword.new(namespace=self.ns, name=u"kw0")
        kw1 = Keyword.new(namespace=self.ns, name=u"kw1")
        kwr = KeywordRelationship.new(left=kw0, relation=rel, right=kw1)
        db.flush()

        rel.remove()
        db.flush()

        # No instances of these types should exist...
        empty_types = (KeywordRelationship, RelationType)
        for type in empty_types:
            assert_equal(list(type.query_by()), [])

        assert_equal(len(list(Relation.query_by())),
                     len(Relation.default_relations))

        # ... but the two keywords should remain.
        assert_equal(set([kw.name for kw in Keyword.query_by()]),
                     set(["kw0", "kw1"]))
Ejemplo n.º 22
0
    def testDeletingThings(self):
        # These are not exhaustive tests -- see test_model.py for those
        kw0 = self.keyword_new(u"kw0")
        kw1 = self.keyword_new(u"kw1")
        kw2 = self.keyword_new(u"kw2")
        rel = self.relation_new()
        kwr = KeywordRelationship.new(left=kw0, relation=rel, right=kw1)
        kwa = KeywordAssociation.new(keyword=kw2, path=u"/asdf/123")
        db.flush()

        # Removing the keyword should kill the KWA
        self.ot.delKeyword(kw2.name)
        db.flush()
        assert_equal(KeywordAssociation.get_by(keyword=kw2), None)

        # Deleting the relationship should cause the KWR to be deleted
        # but the keywords should stick around
        self.ot.delRelation(rel.name)
        db.flush()
        ok_(self.keyword_getby(name=kw0.name))
        ok_(self.keyword_getby(name=kw1.name))
        assert_equal(KeywordRelationship.get_by(left=kw0), None)
Ejemplo n.º 23
0
    def testDeletingThings(self):
        # These are not exhaustive tests -- see test_model.py for those
        kw0 = self.keyword_new(u"kw0")
        kw1 = self.keyword_new(u"kw1")
        kw2 = self.keyword_new(u"kw2")
        rel = self.relation_new()
        kwr = KeywordRelationship.new(left=kw0, relation=rel, right=kw1)
        kwa = KeywordAssociation.new(keyword=kw2, path=u"/asdf/123")
        db.flush()

        # Removing the keyword should kill the KWA
        self.ot.delKeyword(kw2.name)
        db.flush()
        assert_equal(KeywordAssociation.get_by(keyword=kw2), None)

        # Deleting the relationship should cause the KWR to be deleted
        # but the keywords should stick around
        self.ot.delRelation(rel.name)
        db.flush()
        ok_(self.keyword_getby(name=kw0.name))
        ok_(self.keyword_getby(name=kw1.name))
        assert_equal(KeywordRelationship.get_by(left=kw0), None)
Ejemplo n.º 24
0
 def addRelation(self, name=u"testRel", weight=1.0, types=[], inverse=None):
     newRel = self.ot.addRelation(name=name, weight=weight, types=types, inverse=inverse)
     db.flush()  # Simulate a web request -- flush
     return newRel
Ejemplo n.º 25
0
 def relation_new(self, name=u"testRel", **kwargs):
     newRel = Relation.new(name=name,
                           namespace=self.ot._namespace,
                           **kwargs)
     db.flush()
     return newRel
Ejemplo n.º 26
0
 def testDelKeyword(self):
     self.keyword_new()
     self.ot.delKeyword(u"testKW")
     db.flush()
     # The keyword should not exist
     assert_equal(self.keyword_getby(), None)
Ejemplo n.º 27
0
 def relation_new(self, name=u"testRel", **kwargs):
     newRel = Relation.new(name=name, namespace=self.ot._namespace, **kwargs)
     db.flush()
     return newRel
Ejemplo n.º 28
0
 def addKeyword(self, name=u"testKW", disambiguation=u"dis", description=u"desc"):
     """ Add a keyword using a call to the OT. """
     kw = self.ot.addKeyword(name, disambiguation=disambiguation, description=description)
     db.flush()  # Mimic the flush that hapens at the end of each request
     return kw
Ejemplo n.º 29
0
 def testDelKeyword(self):
     self.keyword_new()
     self.ot.delKeyword(u"testKW")
     db.flush()
     # The keyword should not exist
     assert_equal(self.keyword_getby(), None)
Ejemplo n.º 30
0
    def testGetRelatedKeywords(self):
        rs = {}
        rs['k'] = self.relation_new(u"kind of",
                                    weight=0.5,
                                    types=['symmetric'])
        rs['s'] = self.relation_new(u"synonym of",
                                    weight=1,
                                    types=['symmetric'])
        rs['b'] = self.relation_new(u"breed of", weight=0.75)
        kwrs = (("animal", "kind of", "living thing"),
                ("dog", "kind of", "animal"), ("cat", "kind of", "animal"),
                ("cachorro", "synonym of", "dog"),
                ("egyptian", "breed of", "cat"), ("collie", "breed of", "dog"))
        ks = {}
        for kwr in kwrs:
            for kw in (kwr[0], kwr[2]):
                if kw in ks: continue
                ks[kw] = self.keyword_new(unicode(kw))
            KeywordRelationship.new(left=ks[kwr[0]],
                                    relation=rs[kwr[1][0]],
                                    right=ks[kwr[2]])
        db.flush()

        queries = (
            (("animal", 0.5, None), {
                'cat': 0.5,
                'living thing': 0.5,
                'dog': 0.5,
                'animal': 1,
                'cachorro': 0.5
            }),
            (("collie", 0.75, None), {
                'collie': 1,
                'dog': 0.75,
                'cachorro': 0.75
            }),
            (("collie", 0, None), {
                'collie': 1,
                'living thing': 0.1875,
                'egyptian': 0.140625,
                'dog': 0.75,
                'cat': 0.1875,
                'animal': 0.375,
                'cachorro': 0.75
            }),
            (("living thing", 0, [u"kind of"]), {
                'living thing': 1,
                'dog': 0.25,
                'animal': 0.5,
                'cat': 0.25
            }),
            (("dog", 0, [u"synonym of", u"breed of"]), {
                'collie': 0.75,
                'dog': 1,
                'cachorro': 1
            }),
        )

        for (query, expected) in queries:
            actual = self.ot.getRelatedKeywords(unicode(query[0]),
                                                cutoff=query[1],
                                                links=query[2])
            assert_equal(actual, expected)