Exemplo n.º 1
0
    def test_voucher_pool_unassigned(self) -> None:
        experiment = ExperimentFactory()
        participant_1 = ParticipantFactory(
            experiment=experiment,
            started_at=timezone.now(),
            finished_at=timezone.now(),
        )

        post_data = {
            "participant": participant_1.participant_id,
        }

        resp = self.client.post(self.url, post_data)

        self.assertEqual(400, resp.status_code)

        self.assertEqual(
            resp.json(),
            {
                "status": "error",
                "error_code": "pool_unassigned",
                "error_message":
                "This experiment is not assigned a voucher pool",
            },
        )
Exemplo n.º 2
0
    def test_export(self) -> None:
        pool = VoucherPoolFactory()
        vouchers = VoucherFactory.create_batch(10, pool=pool)
        experiment = ExperimentFactory(voucher_pool=pool)
        participant = ParticipantFactory(experiment=experiment)
        vouchers[0].participant = participant
        vouchers[0].save()

        url = reverse("reimbursement:voucher_export", kwargs={"pk": pool.pk})

        resp = self.client.get(url)

        reader = csv.DictReader(io.StringIO(resp.content.decode("utf-8")))

        rows = [row for row in reader]

        self.assertEqual(10, len(rows))

        for index, voucher in enumerate(vouchers):
            self.assertEqual(voucher.code, rows[index]["voucher_code"])

        self.assertEqual(
            rows[0],
            {
                "voucher_code": vouchers[0].code,
                "experiment_code": experiment.code,
                "participant_id": participant.participant_id,
            },
        )
Exemplo n.º 3
0
    def test_get_voucher(self) -> None:
        voucher = VoucherFactory(pool__success_message="Use this on Amazon")
        experiment = ExperimentFactory(voucher_pool=voucher.pool)
        participant = ParticipantFactory(
            experiment=experiment,
            started_at=timezone.now(),
            finished_at=timezone.now(),
        )

        post_data = {
            "participant": participant.participant_id,
        }

        resp = self.client.post(self.url, post_data)

        self.assertEqual(200, resp.status_code)

        self.assertEqual(
            resp.json(),
            {
                "status": "success",
                "voucher": voucher.code,
                "success_message": voucher.pool.success_message,
            },
        )

        voucher.refresh_from_db()

        self.assertEqual(voucher.participant, participant)
Exemplo n.º 4
0
    def test_question_validation(self) -> None:
        # Question should belong to the module
        experiment = ExperimentFactory()
        participant = ParticipantFactory(experiment=experiment)
        module = CriterionModuleFactory(experiment=experiment)

        question_1 = CriterionQuestionFactory()

        url = reverse("api:criterion_data")

        # Participant can only submit one answer to a question
        json_data = {
            "participant": participant.participant_id,
            "module": module.pk,
            "question": question_1.pk,
            "answer": True,
        }

        resp = self.client.post(url,
                                json_data,
                                content_type="application/json")

        self.assertEqual(400, resp.status_code)

        self.assertEqual(
            resp.json(),
            {"question": ["This question does not belong to that module."]})
Exemplo n.º 5
0
    def test_post(self) -> None:
        experiment = ExperimentFactory()
        participant = ParticipantFactory(experiment=experiment)
        module = AffectiveRatingModuleFactory(experiment=experiment)

        url = reverse("api:affective_rating_data")

        json_data = {
            "participant": participant.participant_id,
            "module": module.pk,
            "rating": 5,
            "stimulus": "csa",
        }

        resp = self.client.post(url,
                                json_data,
                                content_type="application/json")

        # Check valid submission
        self.assertEqual(201, resp.status_code)

        # Check submitted data is valid
        data = module.data.first()
        self.assertEqual(data.rating, json_data["rating"])
        self.assertEqual(data.stimulus, json_data["stimulus"])
Exemplo n.º 6
0
def get_example_experiment() -> Experiment:
    us_file = SimpleUploadedFile("file.wav",
                                 b"wav content",
                                 content_type="audio/wav")
    csa_file = SimpleUploadedFile("csa.png",
                                  open(test_file, "rb").read(),
                                  content_type="image/png")
    csb_file = SimpleUploadedFile("csa.png",
                                  open(test_file, "rb").read(),
                                  content_type="image/png")
    context_a_file = SimpleUploadedFile(
        "context_a.png",
        open(test_file, "rb").read(),
        content_type="image/png",
    )
    context_b_file = SimpleUploadedFile(
        "context_b.png",
        open(test_file, "rb").read(),
        content_type="image/png",
    )
    return ExperimentFactory(
        us=us_file,
        csa=csa_file,
        csb=csb_file,
        context_a=context_a_file,
        context_b=context_b_file,
    )
Exemplo n.º 7
0
    def test_participant_not_completed_experiment(self) -> None:
        voucher = VoucherFactory()
        experiment = ExperimentFactory(voucher_pool=voucher.pool)
        participant = ParticipantFactory(experiment=experiment)

        post_data = {
            "participant": participant.participant_id,
        }

        resp = self.client.post(self.url, post_data)

        self.assertEqual(400, resp.status_code)
Exemplo n.º 8
0
    def test_unique_trial(self) -> None:
        experiment: Experiment = ExperimentFactory()
        module: FearConditioningModule = FearConditioningModuleFactory(
            experiment=experiment)
        participant: Participant = ParticipantFactory(experiment=experiment)

        url = reverse("api:fear_conditioning_data")

        json_data = {
            "participant": participant.participant_id,
            "module": module.pk,
            "trial": 1,
            "trial_by_stimulus": 1,
            "rating": 5,
            "stimulus": "CSA",
            "normalised_stimulus": "CS+",
            "reinforced_stimulus": "CSA",
            "unconditional_stimulus": True,
            "trial_started_at": parse_datetime("2020-01-01T00:00Z"),
            "response_recorded_at": parse_datetime("2020-01-01T00:00Z"),
            "volume_level": "0.50",
            "calibrated_volume_level": "0.85",
            "headphones": True,
            "did_leave_iti": False,
            "did_leave_trial": False,
        }

        resp = self.client.post(url,
                                json_data,
                                content_type="application/json")

        self.assertEqual(201, resp.status_code)

        # Send the same data again (same trial, participant, and module)
        resp = self.client.post(url,
                                json_data,
                                content_type="application/json")

        self.assertEqual(400, resp.status_code)

        self.assertEqual(
            resp.json(),
            {
                "non_field_errors": [
                    "The fields trial, module, participant must make a unique set."
                ]
            },
        )
Exemplo n.º 9
0
    def test_unique_answer(self) -> None:
        experiment = ExperimentFactory()
        participant = ParticipantFactory(experiment=experiment)
        module = CriterionModuleFactory(experiment=experiment)

        question_1 = CriterionQuestionFactory(module=module)

        url = reverse("api:criterion_data")

        # Participant can only submit one answer to a question
        json_data = {
            "participant": participant.participant_id,
            "module": module.pk,
            "question": question_1.pk,
            "answer": True,
        }

        resp = self.client.post(url,
                                json_data,
                                content_type="application/json")

        self.assertEqual(201, resp.status_code)

        json_data = {
            "participant": participant.participant_id,
            "module": module.pk,
            "question": question_1.pk,
            "answer": True,
        }

        resp = self.client.post(url,
                                json_data,
                                content_type="application/json")

        # Error
        self.assertEqual(400, resp.status_code)
        self.assertEqual(
            resp.json(),
            {
                "non_field_errors":
                ["The fields participant, question must make a unique set."]
            },
        )
Exemplo n.º 10
0
    def test_post(self) -> None:
        experiment = ExperimentFactory()
        participant = ParticipantFactory(experiment=experiment)
        module = CriterionModuleFactory(experiment=experiment)

        question_1 = CriterionQuestionFactory(module=module)
        question_2 = CriterionQuestionFactory(module=module)

        url = reverse("api:criterion_data")

        json_data = {
            "participant": participant.participant_id,
            "module": module.pk,
            "question": question_1.pk,
            "answer": True,
        }

        resp = self.client.post(url,
                                json_data,
                                content_type="application/json")

        self.assertEqual(201, resp.status_code)

        json_data = {
            "participant": participant.participant_id,
            "module": module.pk,
            "question": question_2.pk,
            "answer": False,
        }

        resp = self.client.post(url,
                                json_data,
                                content_type="application/json")

        self.assertEqual(201, resp.status_code)

        answers = module.data.all()

        self.assertEqual(answers[0].question, question_1)
        self.assertEqual(answers[0].answer, True)

        self.assertEqual(answers[1].question, question_2)
        self.assertEqual(answers[1].answer, False)
Exemplo n.º 11
0
    def test_voucher_pool_empty(self) -> None:
        voucher = VoucherFactory(pool__empty_pool_message="No codes left.")
        pool = voucher.pool
        experiment = ExperimentFactory(voucher_pool=pool)
        participant_1 = ParticipantFactory(
            experiment=experiment,
            started_at=timezone.now(),
            finished_at=timezone.now(),
        )
        participant_2 = ParticipantFactory(
            experiment=experiment,
            started_at=timezone.now(),
            finished_at=timezone.now(),
        )

        # Fetch voucher
        post_data = {
            "participant": participant_1.participant_id,
        }

        resp = self.client.post(self.url, post_data)

        self.assertEqual(200, resp.status_code)

        # Fetch voucher again, this time no more vouchers available
        post_data = {
            "participant": participant_2.participant_id,
        }

        resp = self.client.post(self.url, post_data)

        self.assertEqual(200, resp.status_code)

        self.assertEqual(
            resp.json(),
            {
                "status": "error",
                "error_code": "pool_empty",
                "error_message": pool.empty_pool_message,
            },
        )
Exemplo n.º 12
0
    def test_participant_already_claimed_voucher(self) -> None:
        pool = VoucherPoolFactory()
        VoucherFactory.create_batch(5, pool=pool)
        experiment = ExperimentFactory(voucher_pool=pool)
        participant = ParticipantFactory(
            experiment=experiment,
            started_at=timezone.now(),
            finished_at=timezone.now(),
        )

        # Fetch voucher
        post_data = {
            "participant": participant.participant_id,
        }

        resp = self.client.post(self.url, post_data)

        self.assertEqual(200, resp.status_code)

        # Fetch voucher again, should return error
        resp = self.client.post(self.url, post_data)

        self.assertEqual(400, resp.status_code)
Exemplo n.º 13
0
    def test_post(self) -> None:
        experiment: Experiment = ExperimentFactory()
        module: FearConditioningModule = FearConditioningModuleFactory(
            experiment=experiment)
        participant: Participant = ParticipantFactory(experiment=experiment)

        url = reverse("api:fear_conditioning_data")

        json_data = {
            "participant": participant.participant_id,
            "module": module.pk,
            "trial": 1,
            "trial_by_stimulus": 1,
            "rating": 5,
            "stimulus": "CSA",
            "normalised_stimulus": "CS+",
            "reinforced_stimulus": "CSA",
            "unconditional_stimulus": True,
            "trial_started_at": parse_datetime("2020-01-01T00:00Z"),
            "response_recorded_at": parse_datetime("2020-01-01T00:00Z"),
            "volume_level": "0.50",
            "calibrated_volume_level": "0.85",
            "headphones": True,
            "did_leave_iti": False,
            "did_leave_trial": False,
        }

        resp = self.client.post(url,
                                json_data,
                                content_type="application/json")

        self.assertEqual(201, resp.status_code)

        # Verify data posted
        data = module.data.get()

        response_data = {
            **resp.json(),
            "trial_started_at":
            parse_datetime(resp.json()["trial_started_at"]),
            "response_recorded_at":
            parse_datetime(resp.json()["response_recorded_at"]),
        }

        self.assertEqual(response_data, {**json_data, "id": data.pk})

        self.assertEqual(data.participant.participant_id,
                         json_data["participant"])
        self.assertEqual(data.module_id, json_data["module"])
        self.assertEqual(data.trial, json_data["trial"])
        self.assertEqual(data.trial_by_stimulus,
                         json_data["trial_by_stimulus"])
        self.assertEqual(data.rating, json_data["rating"])
        self.assertEqual(data.stimulus, json_data["stimulus"])
        self.assertEqual(data.normalised_stimulus,
                         json_data["normalised_stimulus"])
        self.assertEqual(data.reinforced_stimulus,
                         json_data["reinforced_stimulus"])
        self.assertEqual(data.unconditional_stimulus,
                         json_data["unconditional_stimulus"])
        self.assertEqual(data.trial_started_at, json_data["trial_started_at"])
        self.assertEqual(data.response_recorded_at,
                         json_data["response_recorded_at"])
        self.assertEqual(data.volume_level, Decimal(json_data["volume_level"]))
        self.assertEqual(data.calibrated_volume_level,
                         Decimal(json_data["calibrated_volume_level"]))
        self.assertEqual(data.headphones, json_data["headphones"])