Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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]
def test_node_indegree(self, db_session):
net = models.Network()
self.add(db_session, net)
node1 = models.Node(network=net)
db_session.add(node1)
db_session.commit()
for i in range(5):
assert len(node1.vectors(direction="incoming")) == i
new_node = models.Node(network=net)
db_session.add(new_node)
db_session.commit()
node1.connect(direction="from", whom=new_node)
self.add(db_session, new_node)
assert len(node1.vectors(direction="incoming")) == 5
nodes = db_session.query(models.Node).all()
node5 = [n for n in nodes if len(n.vectors(direction="incoming")) == 5][0]
assert node5 == node1
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_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_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]
)
def test_kill_vector(self, db_session):
net = models.Network()
db_session.add(net)
node1 = models.Node(network=net)
node2 = models.Node(network=net)
vector = models.Vector(origin=node1, destination=node2)
self.add(db_session, node1, node2, vector)
assert vector.failed is False
vector.fail()
assert vector.failed is True
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(
def get_network_for_participant(self, participant):
"""Find a network for a participant.
If no networks are available, None will be returned. By default
participants can participate only once in each network and participants
first complete networks with `role="practice"` before doing all other
networks in a random order.
"""
key = participant.id
networks_with_space = Network.query.filter_by(full=False).all()
networks_participated_in = [
node.network_id for node in
Node.query.with_entities(Node.network_id)
.filter_by(participant_id=participant.id).all()
]
legal_networks = [
net for net in networks_with_space
if net.id not in networks_participated_in
]
if not legal_networks:
self.log("No networks available, returning None", key)
return None
self.log("{} networks out of {} available"
.format(len(legal_networks),
(self.practice_repeats + self.experiment_repeats)),
key)
This calls the neighbours method of the node
making the request and returns a list of descriptions of
the nodes (even if there is only one).
Required arguments: participant_id, node_id
Optional arguments: type, failed, connection
After getting the neighbours it also calls
exp.node_get_request()
"""
exp = Experiment(session)
# get the parameters
node_type = request_parameter(parameter="node_type",
parameter_type="known_class",
default=models.Node)
failed = request_parameter(parameter="failed",
parameter_type="bool",
default=False)
connection = request_parameter(parameter="connection", default="to")
for x in [node_type, failed, connection]:
if type(x) == Response:
return x
# make sure the node exists
node = models.Node.query.get(node_id)
if node is None:
return error_response(
error_type="/node/neighbors, node does not exist",
error_text="/node/{}/neighbors, node {} does not exist"
.format(node_id))