Example #1
0
    def test_identity_transformation(self):
        net = models.Network()
        self.add(net)
        node = models.Node(network=net)
        self.db.add(node)
        self.db.commit()

        info_in = models.Info(origin=node, contents="foo")
        self.db.add(info_in)
        self.db.commit()

        node.replicate(info_in)

        # # Create a new info based on the old one.
        # info_out = models.Info(origin=node, contents=info_in.contents)
        # self.db.add(info_in)
        # self.db.commit()

        # # Register the transformation.
        # transformation = transformations.Replication(
        #     info_out=info_out,
        #     info_in=info_in)

        # self.db.add(transformation)
        # self.db.commit()

        assert node.infos()[-1].contents == "foo"
        assert len(node.infos()) == 2
Example #2
0
    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]
Example #3
0
 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)
Example #4
0
    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
Example #5
0
    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"]
Example #6
0
    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()
Example #7
0
    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)
Example #8
0
    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"
Example #9
0
    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
Example #10
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]
Example #11
0
    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)
        ])
Example #12
0
    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
Example #13
0
    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()