Example #1
0
    def test_node_connect(self):
        """Test connecting one node to another"""
        net = models.Network()
        self.db.add(net)
        self.db.commit()

        node1 = models.Node(network=net)
        node2 = models.Node(network=net)
        node3 = models.Node(network=net)
        node4 = models.Node(network=net)
        # self.add(node1, node2, node3, node4)
        # self.db.commit()

        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]

        assert_raises(ValueError, node1.connect, whom=node1)

        net = models.Network()
        self.add(net)

        assert_raises(TypeError, node1.connect, whom=net)
Example #2
0
    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))
Example #3
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 #4
0
    def test_create_random_binary_string_source(self, db_session):
        net = models.Network()
        self.add(db_session, net)
        source = nodes.RandomBinaryStringSource(network=net)
        self.add(db_session, source)

        assert source
Example #5
0
    def test_transmit_selector_all_of_type(self):
        net = models.Network()
        self.db.add(net)

        # Create a network of two biological nodes.
        agent1 = nodes.ReplicatorAgent(network=net)
        agent2 = nodes.ReplicatorAgent(network=net)

        agent1.connect(direction="to", whom=agent2)

        information.Meme(origin=agent1, contents="foo1")
        information.Meme(origin=agent1, contents="foo2")
        information.Meme(origin=agent1, contents="foo3")
        information.Gene(origin=agent1, contents="bar")

        assert len(agent1.infos(type=Meme)) == 3
        assert len(agent2.infos(type=Meme)) == 0
        assert len(agent1.infos(type=Gene)) == 1
        assert len(agent2.infos(type=Gene)) == 0

        # Transmit memes from agent 1 to 2.
        agent1.transmit(what=information.Meme, to_whom=agent2)

        # Receive the transmission.
        agent2.receive()

        # Make sure that Agent 2 has a blank memome and the right gene.
        assert not agent2.infos(type=Gene)
        assert len(agent2.infos(type=Meme)) == 3
Example #6
0
 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)
Example #7
0
    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
Example #8
0
    def test_random_walk_from_source(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(whom=agent2)
        agent2.connect(whom=agent3)

        source = nodes.RandomBinaryStringSource(network=net)

        from operator import attrgetter
        source.connect(
            whom=min(net.nodes(type=Agent), key=attrgetter('creation_time')))
        source.create_information()

        processes.random_walk(net)

        agent1.receive()
        msg = agent1.infos()[0].contents

        processes.random_walk(net)
        agent2.receive()
        agent2.infos()[0].contents

        processes.random_walk(net)
        agent3.receive()
        agent3.infos()[0].contents

        assert msg == agent3.infos()[0].contents
Example #9
0
    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
Example #10
0
    def test_create_bidirectional_vectors(self):
        """Test creating a bidirectional connection between nodes"""
        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 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
Example #11
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 #12
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 #13
0
    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
Example #14
0
 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)
Example #15
0
    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"
Example #16
0
 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)
Example #17
0
    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"]
Example #18
0
    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"
Example #19
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 #20
0
    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
Example #21
0
    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
Example #22
0
 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)
Example #23
0
    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
Example #24
0
    def test_create_agent_generic_transmit_to_all(self):
        net = models.Network()
        self.db.add(net)
        agent1 = nodes.Agent(network=net)
        agent2 = nodes.Agent(network=net)
        agent3 = nodes.Agent(network=net)

        agent1.connect(direction="to", whom=agent2)
        agent1.connect(direction="to", whom=agent3)
        agent1.transmit(to_whom=models.Node)
Example #25
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 #26
0
    def test_environment_update(self, db_session):
        net = models.Network()
        db_session.add(net)
        environment = nodes.Environment(network=net)
        environment.update("some content")
        db_session.commit()

        state = environment.state()

        assert state.contents == u"some content"
Example #27
0
    def test_create_replicator_agent(self):
        net = models.Network()
        self.db.add(net)
        agent = nodes.ReplicatorAgent(network=net)

        assert len(agent.infos()) is 0

        info = information.Info(origin=agent, contents="foo")

        assert agent.infos()[0] == info
Example #28
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 #29
0
    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"
Example #30
0
    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