def test_transmission_order(self, db_session): net = models.Network() db_session.add(net) agent1 = nodes.ReplicatorAgent(network=net) agent2 = nodes.ReplicatorAgent(network=net) agent3 = nodes.ReplicatorAgent(network=net) self.add(db_session, agent1, agent2, agent3) db_session.commit() agent1.connect(whom=agent2) agent1.connect(whom=agent3) self.add(db_session, agent1, agent2, agent3) info1 = models.Info(origin=agent1, contents="foo") info2 = models.Info(origin=agent1, contents="bar") info3 = models.Info(origin=agent1, contents="baz") info4 = models.Info(origin=agent1, contents="spam") self.add(db_session, info1, info2, info3, info4) agent1.transmit(what=info1, to_whom=agent2) agent2.receive() agent1.transmit(what=info2, to_whom=agent3) agent3.receive() agent1.transmit(what=info3, to_whom=agent2) agent2.receive() agent1.transmit(what=info4, to_whom=agent3) agent3.receive() db_session.commit() transmissions = agent1.transmissions() assert len(transmissions) == 4 assert transmissions[0].receive_time < transmissions[1].receive_time assert transmissions[1].receive_time < transmissions[2].receive_time assert transmissions[2].receive_time < transmissions[3].receive_time
def test_transmit_raises_if_no_connection_to_destination(self, db_session): net1 = models.Network() net2 = models.Network() agent1 = nodes.ReplicatorAgent(network=net1) agent2 = nodes.ReplicatorAgent(network=net2) info1 = models.Info(origin=agent1, contents="foo") info2 = models.Info(origin=agent1, contents="bar") self.add(db_session, info1, info2) with raises(ValueError) as excinfo: agent1.transmit(what=None, to_whom=agent2) assert excinfo.match("cannot transmit to {}".format(agent2))
def test_transmit_none_finds_all_infos(self, db_session): net = models.Network() agent1 = nodes.ReplicatorAgent(network=net) agent2 = nodes.ReplicatorAgent(network=net) agent1.connect(whom=agent2) info1 = models.Info(origin=agent1, contents="foo") info2 = models.Info(origin=agent1, contents="bar") self.add(db_session, info1, info2) transmissions = agent1.transmit(what=None, to_whom=agent2) assert len(transmissions) == 2 for t in transmissions: assert t.origin is agent1 assert t.destination is agent2
def test_agent_transmit_no_connection(self): net = models.Network() self.db.add(net) agent1 = nodes.ReplicatorAgent(network=net) agent2 = nodes.ReplicatorAgent(network=net) info = models.Info(origin=agent1, contents="foo") agent1.transmit(what=info, to_whom=agent2)
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_create_transmission(self): """Try creating a transmission""" net = models.Network() self.db.add(net) node1 = models.Node(network=net) node2 = models.Node(network=net) self.add(node1, node2) self.db.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(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_agent_transmit_no_connection(self, a): net = a.network() agent1 = a.replicator(network=net) agent2 = a.replicator(network=net) info = models.Info(origin=agent1) with raises(ValueError): agent1.transmit(what=info, to_whom=agent2)
def test_agent_transmit_everything_to_everyone(self): net = models.Network() self.db.add(net) self.db.commit() agent1 = nodes.ReplicatorAgent(network=net) agent2 = nodes.ReplicatorAgent(network=net) agent3 = nodes.ReplicatorAgent(network=net) agent1.connect(direction="to", whom=agent2) agent1.connect(direction="to", whom=agent3) info = models.Info(origin=agent1, contents="foo") agent1.transmit(what=models.Info, to_whom=nodes.Agent) agent2.receive() agent3.receive() assert agent1.infos()[0].contents == agent2.infos()[0].contents assert agent1.infos()[0].contents == agent3.infos()[0].contents assert agent1.infos()[0].id != agent2.infos()[0].id != agent3.infos( )[0].id transmissions = info.transmissions() assert len(transmissions) == 2
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_create_two_infos(self): """Try creating two infos""" net = models.Network() self.db.add(net) node = models.Node(network=net) info1 = models.Info(origin=node, contents="bar") info2 = models.Info(origin=node, contents="foo") self.add(node, info1, info2) assert info1.id != info2.id assert info1.origin_id == info2.origin_id assert info1.creation_time != info2.creation_time assert info1.contents != info2.contents assert len(info1.transmissions()) == 0 assert len(info2.transmissions()) == 0 assert len(node.infos()) == 2 assert info1 in node.infos() assert info2 in node.infos()
def test_agent_transmit_invalid_info(self): net = models.Network() self.db.add(net) agent1 = nodes.ReplicatorAgent(network=net) agent2 = nodes.ReplicatorAgent(network=net) agent1.connect(direction="to", whom=agent2) info = models.Info(origin=agent2, contents="foo") agent1.transmit(what=info, to_whom=agent2)
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_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_transmit_to_class_finds_nodes_in_network(self, db_session): net = models.Network() agent1 = nodes.ReplicatorAgent(network=net) agent2 = nodes.ReplicatorAgent(network=net) agent1.connect(whom=agent2) info1 = models.Info(origin=agent1, contents="foo") self.add(db_session, info1) transmissions = agent1.transmit(what=info1, to_whom=nodes.ReplicatorAgent) assert len(transmissions) == 1 assert transmissions[0].origin is agent1 assert transmissions[0].destination is agent2
def test_node_incoming_transmissions(self): net = models.Network() self.db.add(net) agent1 = nodes.ReplicatorAgent(network=net) agent2 = nodes.ReplicatorAgent(network=net) agent3 = nodes.ReplicatorAgent(network=net) self.add(agent1, agent2, agent3) self.db.commit() agent1.connect(direction="from", whom=[agent2, agent3]) self.add(agent1, agent2, agent3) info1 = models.Info(origin=agent2, contents="foo") info2 = models.Info(origin=agent3, contents="bar") self.add(info1, info2) agent2.transmit(what=info1, to_whom=agent1) agent3.transmit(what=info2, to_whom=agent1) self.db.commit() assert len(agent1.transmissions(direction="incoming")) == 2 assert len(agent2.transmissions(direction="incoming")) == 0 assert len(agent3.transmissions(direction="incoming")) == 0
def test_node_outgoing_transmissions(self, db_session): net = models.Network() db_session.add(net) agent1 = nodes.ReplicatorAgent(network=net) agent2 = nodes.ReplicatorAgent(network=net) agent3 = nodes.ReplicatorAgent(network=net) self.add(db_session, agent1, agent2, agent3) db_session.commit() agent1.connect(whom=agent2) agent1.connect(whom=agent3) self.add(db_session, agent1, agent2, agent3) info1 = models.Info(origin=agent1, contents="foo") info2 = models.Info(origin=agent1, contents="bar") self.add(db_session, info1, info2) agent1.transmit(what=info1, to_whom=agent2) agent1.transmit(what=info2, to_whom=agent3) db_session.commit() assert len(agent1.transmissions(direction="outgoing")) == 2 assert len(agent2.transmissions(direction="outgoing")) == 0 assert len(agent3.transmissions(direction="outgoing")) == 0
def test_create_info(self): """Try creating an info""" net = models.Network() self.db.add(net) node = models.Node(network=net) info = models.Info(origin=node, contents="foo") self.add(node, info) assert isinstance(info.id, int) assert info.type == "info" assert info.origin_id == node.id assert info.creation_time assert info.contents == "foo" assert len(info.transmissions()) == 0 assert node.infos() == [info]
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_agent_transmit(self): net = models.Network() self.db.add(net) agent1 = nodes.ReplicatorAgent(network=net) agent2 = nodes.ReplicatorAgent(network=net) agent1.connect(direction="to", whom=agent2) info = models.Info(origin=agent1, contents="foo") agent1.transmit(what=agent1.infos()[0], to_whom=agent2) agent2.receive() assert agent1.infos()[0].contents == agent2.infos()[0].contents assert agent1.infos()[0].id != agent2.infos()[0].id transmission = info.transmissions()[0] assert transmission.info_id == info.id assert transmission.origin_id == agent1.id assert transmission.destination_id == agent2.id
def test_models(self): """#################### #### Test Network #### ####################""" print("") print("Testing models: Network", end="\r") sys.stdout.flush() # create test network: net = models.Network() self.db.add(net) self.db.commit() net = models.Network.query.one() # create a participant participant = models.Participant(worker_id=str(1), hit_id=str(1), assignment_id=str(1), mode="test") self.db.add(participant) self.db.commit() # create some nodes node = models.Node(network=net) agent = Agent(network=net, participant=participant) source = Source(network=net) # create vectors source.connect(direction="to", whom=agent) agent.connect(direction="both", whom=node) # create some infos info = models.Info(origin=agent, contents="ethwth") gene = Gene(origin=source, contents="hkhkhkh") # conditionally transmit and transform source.transmit(what=models.Info) agent.receive() agent.transmit(what=Gene) models.Transformation(info_in=gene, info_out=info) # Test attributes assert net.id == 1 assert isinstance(net.creation_time, datetime) assert net.property1 is None assert net.property2 is None assert net.property3 is None assert net.property4 is None assert net.property5 is None assert net.failed is False assert net.time_of_death is None assert net.type == "network" assert isinstance(net.max_size, int) assert net.max_size == 1e6 assert isinstance(net.full, bool) assert net.full is False assert isinstance(net.role, unicode) assert net.role == "default" # test __repr__() assert repr( net ) == "<Network-1-network with 3 nodes, 3 vectors, 2 infos, 1 transmissions and 1 transformations>" # test __json__() assert net.__json__() == { "id": 1, "type": "network", "max_size": 1e6, "full": False, "role": "default", "creation_time": net.creation_time, "failed": False, "time_of_death": None, "property1": None, "property2": None, "property3": None, "property4": None, "property5": None } # test nodes() for n in [node, agent, source]: assert n in net.nodes() assert net.nodes(type=Agent) == [agent] assert net.nodes(failed=True) == [] for n in [node, agent, source]: assert n in net.nodes(failed="all") assert net.nodes(participant_id=1) == [agent] # test size() assert net.size() == 3 assert net.size(type=Source) == 1 assert net.size(type=Agent) == 1 assert net.size(failed=True) == 0 assert net.size(failed="all") == 3 # test infos() assert len(net.infos(failed="all")) == 2 assert len(net.infos(type=models.Info, failed="all")) == 2 assert len(net.infos(type=Gene, failed="all")) == 1 assert len(net.infos(type=Gene)) == 1 assert len(net.infos(failed=True)) == 0 # test Network.transmissions() assert len(net.transmissions(failed="all")) == 1 assert len(net.transmissions(failed=True)) == 0 assert len(net.transmissions(failed=False)) == 1 assert len(net.transmissions(status="pending", failed="all")) == 0 assert len(net.transmissions(status="received", failed="all")) == 1 # test Network.transformations() assert len(net.transformations(failed="all")) == 1 assert len(net.transformations(failed="all", type=Mutation)) == 0 assert len( net.transformations(failed="all", type=models.Transformation)) == 1 for t in net.transformations(failed="all"): assert type(t.node) == Agent # test latest_transmission_recipient assert net.latest_transmission_recipient() == agent # test Network.vectors() assert len(net.vectors(failed="all")) == 3 assert len(net.vectors(failed=False)) == 3 assert len(net.vectors(failed=True)) == 0 # test fail() net.fail() assert net.nodes() == [] assert len(net.nodes(failed=True)) == 3 assert len(net.nodes(failed="all")) == 3 assert net.infos() == [] assert net.transmissions() == [] assert net.vectors() == [] assert net.transformations() == [] print("Testing models: Network passed!") sys.stdout.flush()