コード例 #1
0
    def Test_DAGStructureFromGoogleClasses(self):

        #first insert all known tags
        t1 = TagVertex(None, "Tag1")
        t1.ttlCount = 1
        t1.put()

        t2 = TagVertex(None, "Tag2")
        t2.ttlCount = 2
        t2.put()

        t3 = TagVertex(None, "Tag3")
        t3.ttlCount = 3
        t3.put()

        t4 = TagVertex(None, "Tag4")
        t4.ttlCount = 4
        t4.put()

        t5 = TagVertex(None, "Tag5")
        t5.ttlCount = 5
        t5.put()

        t6 = TagVertex(None, "Tag6")
        t6.ttlCount = 6
        t6.put()

        #now make an edge
        t1.AddEdge(t2, 11)
        #test what we've done...
        check.ok_(len(t1.edges) == 1)
        edgeKey = t1.edges[0]
        edge = db.get(edgeKey)
        check.ok_(edge != None)
        check.ok_(edge.edgeCount == 11)
        check.ok_(edge.myOwningVertex != None)
        check.ok_(edge.myOwningVertex.key() == t1.key())
        
        v = edge.myOtherVertex
        check.ok_(v != None)
        check.ok_(v.key() == t2.key())
        check.ok_(v.ttlCount == 2)
        check.ok_(t1.HasEdgeToVertex(t2.key()) == True)
        check.ok_(t1.HasEdgeToVertex(t3.key()) == False)
        check.ok_(t1.GetEdgeToVertex(t2.key()).myOtherVertex.key() == t2.key())
        
        # check that trying to get a non-existent edge chucks
        try:
            t1.GetEdgeToVertex(t3.key())
        except Exception:   
            pass
        except:
            check.ok_(0==1, "Wrong type of exception thrown.")
        else:
            check.ok_(0==1, "Expected exception upchuck; none occurred.")
        #end try/catch            

        #make the other edges
        t1.AddEdge(t3, 12)
        t2.AddEdge(t3, 13)
        t2.AddEdge(t4, 14)
        t2.AddEdge(t6, 15)
        t3.AddEdge(t4, 16)

        #double add some edges to test all paths
        #shouldn't happen in the real app but we'll
        #cover the path anyway...
        t1.AddEdge(t3, 12)
        t2.AddEdge(t3, 15)

        #check.ok_ the structure we've built
        check.ok_(len(t1.edges) == 2)
        check.ok_(len(t2.edges) == 3)
        check.ok_(len(t3.edges) == 1)
        check.ok_(len(t4.edges) == 0)
        check.ok_(len(t5.edges) == 0)
        check.ok_(len(t6.edges) == 0)

        edge = db.get(t1.edges[0])
        check.ok_(edge != None)
        check.ok_(edge.myOwningVertex.key() == t1.key())
        check.ok_(edge.myOtherVertex.key() == t2.key())
        check.ok_(edge.edgeCount == 11)

        edge = db.get(t1.edges[1])
        check.ok_(edge != None)
        check.ok_(edge.myOwningVertex.key() == t1.key())
        check.ok_(edge.myOtherVertex.key() == t3.key())
        check.ok_(edge.edgeCount == 12)

        edge = db.get(t2.edges[0])
        check.ok_(edge != None)
        check.ok_(edge.myOwningVertex.key() == t2.key())
        check.ok_(edge.myOtherVertex.key() == t3.key())
        check.ok_(edge.edgeCount == 13)

        edge = db.get(t2.edges[1])
        check.ok_(edge != None)
        check.ok_(edge.myOwningVertex.key() == t2.key())
        check.ok_(edge.myOtherVertex.key() == t4.key())
        check.ok_(edge.edgeCount == 14)

        edge = db.get(t2.edges[2])
        check.ok_(edge != None)
        check.ok_(edge.myOwningVertex.key() == t2.key())
        check.ok_(edge.myOtherVertex.key() == t6.key())
        check.ok_(edge.edgeCount == 15)

        edge = db.get(t3.edges[0])
        check.ok_(edge != None)
        check.ok_(edge.myOwningVertex.key() == t3.key())
        check.ok_(edge.myOtherVertex.key() == t4.key())
        check.ok_(edge.edgeCount == 16)

        #test negative query paths
        check.ok_(t5.HasEdgeToVertex(t1.key()) == False)
        
        try:
            t6.GetEdgeToVertex(t2.key())
        except Exception:   
            pass
        except:
            check.ok_(0==1, "Wrong type of exception thrown.")
        else:
            check.ok_(0==1, "Expected exception upchuck; none occurred.")