def test_network_downstream_nodes(self, db_session): net = networks.Network() db_session.add(net) node1 = models.Node(network=net) node2 = models.Node(network=net) agent1 = nodes.Agent(network=net) agent2 = nodes.ReplicatorAgent(network=net) source1 = nodes.Source(network=net) nodes.Source(network=net) node1.connect(whom=[node2, agent1, agent2]) assert pytest.raises(TypeError, node1.connect, whom=source1) assert set(node1.neighbors(direction="to")) == set( [node2, agent1, agent2]) assert len(node1.vectors(direction="outgoing")) == 3 assert set(node1.neighbors(direction="to", type=nodes.Agent)) == set([agent1, agent2]) agent1.fail() agent2.fail() assert pytest.raises(ValueError, node1.neighbors, direction="ghbhfgjd")
def test_create_transmission(self, db_session): """Try creating a transmission""" net = models.Network() db_session.add(net) node1 = models.Node(network=net) node2 = models.Node(network=net) self.add(db_session, node1, node2) db_session.commit() node1.connect(whom=node2) info = models.Info(origin=node1) node1.transmit(what=node1.infos()[0], to_whom=node2) # transmission = models.Transmission(info=info, destination=node2) # self.add(db_session, node1, node2, vector, info, transmission) transmission = node1.transmissions()[0] vector = node1.vectors()[0] assert isinstance(transmission.id, int) assert transmission.info_id == info.id assert transmission.origin_id == vector.origin_id assert transmission.destination_id == vector.destination_id assert transmission.creation_time assert transmission.vector == vector assert vector.transmissions() == [transmission]
def test_create_bidirectional_vectors(self, db_session): """Test creating a bidirectional connection between nodes""" net = models.Network() db_session.add(net) node1 = models.Node(network=net) node2 = models.Node(network=net) vector1 = models.Vector(origin=node1, destination=node2) vector2 = models.Vector(origin=node2, destination=node1) self.add(db_session, node1, node2, vector1, vector2) assert vector1.origin_id == node1.id assert vector1.destination_id == node2.id assert vector2.origin_id == node2.id assert vector2.destination_id == node1.id assert node1.vectors(direction="incoming") == [vector2] assert node1.vectors(direction="outgoing") == [vector1] assert node2.vectors(direction="incoming") == [vector1] assert node2.vectors(direction="outgoing") == [vector2] assert node1.is_connected(direction="to", whom=node2) assert node1.is_connected(direction="from", whom=node2) assert node2.is_connected(direction="to", whom=node1) assert node2.is_connected(direction="from", whom=node1) assert len(node1.vectors(direction="incoming")) == 1 assert len(node2.vectors(direction="incoming")) == 1 assert len(node1.vectors(direction="outgoing")) == 1 assert len(node2.vectors(direction="outgoing")) == 1 assert len(vector1.transmissions()) == 0 assert len(vector2.transmissions()) == 0
def test_node_connect(self, db_session): """Test connecting one node to another""" net = models.Network() db_session.add(net) db_session.commit() node1 = models.Node(network=net) node2 = models.Node(network=net) node3 = models.Node(network=net) node4 = models.Node(network=net) node1.connect(whom=node2) assert node1.neighbors(direction="to") == [node2] assert node2.neighbors(direction="from") == [node1] node2.connect(whom=[node3, node4]) for n in node2.neighbors(direction="to"): assert n in [node3, node4] assert node3.neighbors(direction="from") == [node2] raises(ValueError, node1.connect, whom=node1) net = models.Network() self.add(db_session, net) raises(TypeError, node1.connect, whom=net)
def test_different_node_ids(self): """Test that two nodes have different ids""" net = models.Network() self.db.add(net) node1 = models.Node(network=net) node2 = models.Node(network=net) self.add(node1, node2) assert node1.id != node2.id
def test_create_vector(self, db_session): """Test creating a vector between two nodes""" net = models.Network() db_session.add(net) node1 = models.Node(network=net) node2 = models.Node(network=net) self.add(db_session, node1, node2) db_session.commit() node1.connect(whom=node2) self._check_single_connection(node1, node2)
def test_kill_vector(self): net = models.Network() self.db.add(net) node1 = models.Node(network=net) node2 = models.Node(network=net) vector = models.Vector(origin=node1, destination=node2) self.add(node1, node2, vector) assert vector.failed is False vector.fail() assert vector.failed is True
def test_node_has_connection_to(self, db_session): net = models.Network() self.add(db_session, net) node1 = models.Node(network=net) node2 = models.Node(network=net) self.add(db_session, node1, node2) db_session.commit() node1.connect(whom=node2) self.add(db_session, node1, node2) assert node1.is_connected(direction="to", whom=node2) assert not node2.is_connected(direction="to", whom=node1)
def test_node_has_connection_from(self): net = models.Network() self.add(net) node1 = models.Node(network=net) node2 = models.Node(network=net) self.add(node1, node2) self.db.commit() node1.connect(whom=node2) self.add(node1, node2) assert not node1.is_connected(direction="from", whom=node2) assert node2.is_connected(direction="from", whom=node1)
def test_create_vector(self): """Test creating a vector between two nodes""" net = models.Network() self.db.add(net) node1 = models.Node(network=net) node2 = models.Node(network=net) #vector = models.Vector(origin=node1, destination=node2) #self.add(node1, node2, vector) self.add(node1, node2) self.db.commit() node1.connect(whom=node2) self._check_single_connection(node1, node2)
def test_identity_transformation(self, db_session): net = models.Network() db_session.add(net) node = models.Node(network=net) db_session.add(node) db_session.commit() info_in = models.Info(origin=node, contents="foo") db_session.add(info_in) db_session.commit() node.replicate(info_in) # # Create a new info based on the old one. # info_out = models.Info(origin=node, contents=info_in.contents) # db_session.add(info_in) # db_session.commit() # # Register the transformation. # transformation = transformations.Replication( # info_out=info_out, # info_in=info_in) # db_session.add(transformation) # db_session.commit() assert node.infos()[-1].contents == "foo" assert len(node.infos()) == 2
def test_vector_repr(self): """Test the repr of a vector""" net = models.Network() self.db.add(net) node1 = models.Node(network=net) node2 = models.Node(network=net) vector1 = models.Vector(origin=node1, destination=node2) vector2 = models.Vector(origin=node2, destination=node1) self.add(node1, node2, vector1, vector2) assert (repr(vector1).split("-") == [ "Vector", str(node1.id), str(node2.id) ]) assert (repr(vector2).split("-") == [ "Vector", str(node2.id), str(node1.id) ])
def test_details(self, db_session): net = models.Network() db_session.add(net) node = models.Node(network=net) node.details = {"my_data": [1, 2, 3]} self.add(db_session, node) assert tuple(node.details["my_data"]) == (1, 2, 3)
def test_fail_participant(self, db_session): net = models.Network() db_session.add(net) participant = models.Participant( recruiter_id="hotair", worker_id=str(1), hit_id=str(1), assignment_id=str(1), mode="test", ) db_session.add(participant) db_session.commit() node = models.Node(network=net, participant=participant) db_session.add(node) question = models.Question(participant=participant, number=1, question="what?", response="???") db_session.add(question) assert len(participant.nodes()) == 1 assert len(participant.questions()) == 1 assert participant.failed is False participant.fail() assert participant.failed is True assert len(participant.nodes()) == 0 assert len(participant.nodes(failed=True)) == 1 assert len(participant.questions()) == 1 assert participant.questions()[0].failed is True
def test_network_base_add_node_not_implemented(self, db_session): net = models.Network() db_session.add(net) node = models.Node(network=net) db_session.add(net) with pytest.raises(NotImplementedError): net.add_node(node)
def test_property_node(self, db_session): net = models.Network() db_session.add(net) node = models.Node(network=net) node.property1 = "foo" self.add(db_session, node) assert node.property1 == "foo"
def test_transmission_repr(self): net = models.Network() self.db.add(net) node1 = models.Node(network=net) node2 = models.Node(network=net) self.add(node1, node2) node1.connect(whom=node2) models.Info(origin=node1) node1.transmit(what=node1.infos()[0], to_whom=node2) transmission = node1.transmissions()[0] node1.vectors()[0] assert (repr(transmission).split("-") == [ "Transmission", str(transmission.id) ])
def test_property_node(self): net = models.Network() self.db.add(net) node = models.Node(network=net) node.property1 = "foo" self.add(node) assert node.property1 == "foo"
def test_network_base_add_node_not_implemented(self): net = models.Network() self.db.add(net) self.db.commit() node = models.Node(network=net) self.db.add(net) self.db.commit() net.add_node(node)
def test_node_repr(self): """Test the repr of a node""" net = models.Network() self.db.add(net) node = models.Node(network=net) self.add(node) assert repr(node).split("-") == ["Node", str(node.id), "node"]
def test_info_repr(self): """Check the info repr""" net = models.Network() self.db.add(net) node = models.Node(network=net) info = models.Info(origin=node) self.add(info) assert repr(info).split("-") == ["Info", str(info.id), "info"]
def test_network_base_add_node_not_implemented(self): net = models.Network() self.db.add(net) self.db.commit() node = models.Node(network=net) self.db.add(net) self.db.commit() with raises(NotImplementedError): net.add_node(node)
def test_create_memome(self): net = models.Network() self.db.add(net) node = models.Node(network=net) info = information.Meme(origin=node) self.db.commit() assert info.type == "meme" assert info.contents is None
def test_create_genome(self, db_session): net = models.Network() db_session.add(net) node = models.Node(network=net) info = information.Gene(origin=node) db_session.commit() assert info.type == "gene" assert info.contents is None
def test_info_write_twice(self): """Overwrite an info's contents.""" net = models.Network() self.db.add(net) node = models.Node(network=net) info = models.Info(origin=node, contents="foo") self.add(node, info) assert info.contents == "foo" info.contents = "ofo"
def test_network_nodes(self, db_session): net = models.Network() db_session.add(net) node1 = models.Node(network=net) node2 = models.Node(network=net) agent1 = nodes.Agent(network=net) agent2 = nodes.Agent(network=net) agent3 = nodes.Agent(network=net) assert set([node1, node2, agent1, agent2, agent3]) == set(net.nodes()) assert set([agent1, agent2, agent3]) == set(net.nodes(type=nodes.Agent)) node1.fail() agent1.fail() assert set(net.nodes()) == set([node2, agent2, agent3]) assert set(net.nodes(failed="all")) == set([node1, node2, agent1, agent2, agent3]) assert set(net.nodes(failed=True)) == set([node1, agent1]) assert set(net.nodes(type=nodes.Agent, failed="all")) == set([agent1, agent2, agent3])
def test_info_write_twice(self, db_session): """Overwrite an info's contents.""" net = models.Network() db_session.add(net) node = models.Node(network=net) info = models.Info(origin=node, contents="foo") self.add(db_session, node, info) assert info.contents == "foo" with raises(ValueError): info.contents = "ofo"
def test_node_indegree(self): net = models.Network() self.add(net) node1 = models.Node(network=net) self.db.add(node1) self.db.commit() for i in xrange(5): assert len(node1.vectors(direction="incoming")) == i new_node = models.Node(network=net) self.db.add(new_node) self.db.commit() node1.connect(direction="from", whom=new_node) self.add(new_node) assert len(node1.vectors(direction="incoming")) == 5 nodes = self.db.query(models.Node).all() node5 = [ n for n in nodes if len(n.vectors(direction="incoming")) == 5 ][0] assert node5 == node1
def test_node_outdegree(self, db_session): net = models.Network() self.add(db_session, net) node1 = models.Node(network=net) db_session.add(node1) for i in range(5): assert len(node1.vectors(direction="outgoing")) == i new_node = models.Node(network=net) self.add(db_session, new_node) db_session.commit() node1.connect(whom=new_node) self.add(db_session, new_node) assert len(node1.vectors(direction="outgoing")) == 5 nodes = db_session.query(models.Node).all() node5 = [ n for n in nodes if len(n.vectors(direction="outgoing")) == 5 ][0] assert node5 == node1
def test_create_node(self): """Create a basic node""" net = models.Network() self.db.add(net) node = models.Node(network=net) self.add(node) assert isinstance(node.id, int) assert node.type == "node" assert node.creation_time assert len(node.infos()) == 0 assert len(node.vectors(direction="outgoing")) == 0 assert len(node.vectors(direction="incoming")) == 0 assert len(node.vectors(direction="outgoing")) == 0 assert len(node.vectors(direction="incoming")) == 0