示例#1
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
示例#2
0
def create_participant(worker_id, hit_id, assignment_id, mode):
    """Create a participant.

    This route will be hit very early on as any nodes the participant creates
    will be defined in reference to the participant object.
    You must specify the worker_id, hit_id, assignment_id and mode in the url.
    """
    # check this worker hasn't already taken part
    parts = models.Participant.query.filter_by(worker_id=worker_id).all()
    if parts:
        print("participant already exists!")
        return Response(status=200)

    # make the participant
    participant = models.Participant(worker_id=worker_id,
                                     assignment_id=assignment_id,
                                     hit_id=hit_id,
                                     mode=mode)
    session.add(participant)
    session.commit()

    # make a psiturk participant too, for now
    from psiturk.models import Participant as PsiturkParticipant
    psiturk_participant = PsiturkParticipant(workerid=worker_id,
                                             assignmentid=assignment_id,
                                             hitid=hit_id)
    session_psiturk.add(psiturk_participant)
    session_psiturk.commit()

    # return the data
    return success_response(field="participant",
                            data=participant.__json__(),
                            request_type="participant post")
示例#3
0
    def test_create_participant(self, db_session):
        participant = models.Participant(worker_id=str(1),
                                         hit_id=str(1),
                                         assignment_id=str(1),
                                         mode="test")
        db_session.add(participant)
        db_session.commit()

        assert isinstance(participant.id, int)
        assert participant.type == "participant"
示例#4
0
    def test_participant_json(self, db_session):
        participant = models.Participant(worker_id=str(1),
                                         hit_id=str(1),
                                         assignment_id=str(1),
                                         mode="test")
        db_session.add(participant)
        db_session.commit()

        # make sure private data is not in there
        assert 'unique_id' not in participant.__json__()
        assert 'worker_id' not in participant.__json__()
示例#5
0
    def test_participant_json(self, db_session):
        participant = models.Participant(
            recruiter_id="hotair",
            worker_id=str(1),
            hit_id=str(1),
            assignment_id=str(1),
            mode="test",
        )
        participant.details = {"data": "something"}
        db_session.add(participant)
        db_session.commit()

        participant_json = participant.__json__()
        assert "details" in participant_json
        assert participant_json["details"].get("data") == "something"

        # make sure private data is not in there
        assert "unique_id" not in participant_json
        assert "worker_id" not in participant_json
示例#6
0
def create_participant(worker_id, hit_id, assignment_id, mode):
    """Create a participant.

    This route is hit early on. Any nodes the participant creates will be
    defined in reference to the participant object. You must specify the
    worker_id, hit_id, assignment_id, and mode in the url.
    """
    already_participated = models.Participant.query.\
        filter_by(worker_id=worker_id).one_or_none()

    if already_participated:
        db.logger.warning("Worker has already participated.")
        return error_response(
            error_type="/participant POST: worker has already participated.",
            status=403)

    duplicate = models.Participant.query.\
        filter_by(
            assignment_id=assignment_id,
            status="working")\
        .one_or_none()

    if duplicate:
        msg = """
            AWS has reused assignment_id while existing participant is
            working. Replacing older participant {}.
        """
        app.logger.warning(msg.format(duplicate.id))
        q.enqueue(worker_function, "AssignmentReassigned", None, duplicate.id)

    # Create the new participant.
    participant = models.Participant(worker_id=worker_id,
                                     assignment_id=assignment_id,
                                     hit_id=hit_id,
                                     mode=mode)
    session.add(participant)
    session.commit()

    # return the data
    return success_response(field="participant",
                            data=participant.__json__(),
                            request_type="participant post")
示例#7
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()