コード例 #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.")
コード例 #2
0
    def Test_DAGStructure_VertexEdgeSetQueries(self):

        #multiple users so we can test this feature
        u1 = MyUser(user="******", key_name="Bill")
        u1.put()
        u2 = MyUser(user="******", key_name="Ted")
        u2.put()

        #first insert all known tags
        t1 = TagVertex(u1, "Tag1")
        t1.ttlCount = 1
        t1.put()
        t2 = TagVertex(u1, "Tag2")
        t2.ttlCount = 2
        t2.put()
        t3 = TagVertex(u1, "Tag3")
        t3.ttlCount = 3
        t3.put()
        t4 = TagVertex(u1, "Tag4")
        t4.ttlCount = 4
        t4.put()
        t5 = TagVertex(u1, "Tag5")
        t5.ttlCount = 5
        t5.put()
        t6 = TagVertex(u1, "Tag6")
        t6.ttlCount = 6
        t6.put()
        t1.AddEdge(t2, 11)
        t1.AddEdge(t3, 12)
        t2.AddEdge(t3, 13)
        t2.AddEdge(t4, 14)
        t2.AddEdge(t6, 15)
        t3.AddEdge(t4, 16)
        
        #some duplicate tags for the second user
        #(just noise - the calls here work from vertex references
        #and so should never see these 'other' tags
        t22 = TagVertex(u2, "Tag2")
        t22.ttlCount = 22
        t22.put()
        t33 = TagVertex(u2, "Tag3")
        t33.ttlCount = 33
        t33.put()
        t44 = TagVertex(u2, "Tag4")
        t44.ttlCount = 44
        t44.put()
        t22.AddEdge(t33, 13)
        t22.AddEdge(t44, 14)
        t33.AddEdge(t44, 16)

        # test getting the set of outgoing edges
        myEdges = t2.GetMyEdges()
        check.ok_(myEdges != None)
        check.ok_(len(myEdges) == 3)
        check.ok_(myEdges.has_key("Tag3"))
        check.ok_(myEdges.has_key("Tag4"))
        check.ok_(myEdges.has_key("Tag6"))
        check.ok_(myEdges["Tag3"].edgeCount == 13)
        check.ok_(myEdges["Tag4"].edgeCount == 14)
        check.ok_(myEdges["Tag6"].edgeCount == 15)
        
        #get the set of connected vertices recorded in a vertex
        myConectees = t3.GetMyAdjacentVertices()
        check.ok_(myConectees != None)
        check.ok_(len(myConectees) == 1)
        check.ok_(myConectees.has_key("Tag4"))
        
        #test getting the set of incoming edges
        otherConectees = t3.GetAdjacentVerticesRecordedElsewhere()
        check.ok_(otherConectees != None)
        check.ok_(len(otherConectees) == 2)
        check.ok_(otherConectees.has_key("Tag1"))
        check.ok_(otherConectees.has_key("Tag2"))

        #get the total set of connected vertices
        connectees = t3.GetAllAdjacentVertices()
        check.ok_(connectees != None)
        check.ok_(len(connectees) == 3)
        check.ok_(connectees.has_key("Tag1"))
        check.ok_(connectees.has_key("Tag2"))
        check.ok_(connectees.has_key("Tag4"))