Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
okaycj committed Aug 7, 2023
1 parent 3d05c28 commit a1a8290
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 281 deletions.
118 changes: 118 additions & 0 deletions exp/tests/test_runner_forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import json

from django.test import TestCase

from studies.forms import EFPForm, ExternalForm, ScheduledChoice
from studies.models import default_study_structure


class EFPFormTestCase(TestCase):
def test_successful(self):
form = EFPForm(
data={
"last_known_player_sha": "862604874f7eeff8c9d72adcb8914b21bfb5427e",
"player_repo_url": "https://github.com/lookit/ember-lookit-frameplayer",
"structure": json.dumps(default_study_structure()),
}
)
self.assertDictEqual(form.errors, {})
self.assertTrue(form.is_valid())

def test_failed_structure(self):
form = EFPForm(
data={
"last_known_player_sha": "862604874f7eeff8c9d72adcb8914b21bfb5427e",
"player_repo_url": "https://github.com/lookit/ember-lookit-frameplayer",
"structure": "{this is not valid json}",
}
)
self.assertDictEqual(
form.errors,
{
"structure": [
"Saving protocol configuration failed due to invalid JSON! Please use valid JSON and save again. If you reload this page, all changes will be lost."
]
},
)
self.assertFalse(form.is_valid())

def test_failed_generator(self):
form = EFPForm(
data={
"last_known_player_sha": "862604874f7eeff8c9d72adcb8914b21bfb5427e",
"player_repo_url": "https://github.com/lookit/ember-lookit-frameplayer",
"structure": json.dumps(default_study_structure()),
"generator": "This is not valid Javascript.",
}
)

self.assertDictEqual(
form.errors,
{
"generator": [
"Generator javascript seems to be invalid. Please edit and save again. If you reload this page, all changes will be lost."
]
},
)
self.assertFalse(form.is_valid())

def test_failed_player_repo_url(self):
data = {
"last_known_player_sha": "862604874f7eeff8c9d72adcb8914b21bfb5427e",
"structure": json.dumps(default_study_structure()),
}

# Check completely invalid url
data.update(player_repo_url="https://not-a-valid.url")
form = EFPForm(data=data)
self.assertDictEqual(
form.errors,
{
"player_repo_url": [
f'Frameplayer repo url {data["player_repo_url"]} does not work.'
]
},
)
self.assertFalse(form.is_valid())

# Check slightly off url
data.update(player_repo_url="https://github.com/lookit/not-a-valid-project")
form = EFPForm(data=data)
self.assertDictEqual(
form.errors,
{
"player_repo_url": [
f'Frameplayer repo url {data["player_repo_url"]} does not work.'
]
},
)
self.assertFalse(form.is_valid())

def test_failed_last_known_player_sha(self):
data = {
"last_known_player_sha": "not a valid sha",
"player_repo_url": "https://github.com/lookit/ember-lookit-frameplayer",
"structure": json.dumps(default_study_structure()),
}
form = EFPForm(data=data)
self.assertDictEqual(
form.errors,
{
"last_known_player_sha": [
f'Frameplayer commit {data["last_known_player_sha"]} does not exist.'
]
},
)
self.assertFalse(form.is_valid())


class ExternalFormTestCase(TestCase):
def test_successful(self):
form = ExternalForm(
data={
"scheduled": ScheduledChoice.scheduled.value,
"url": "https://google.com",
}
)
self.assertDictEqual(form.errors, {})
self.assertTrue(form.is_valid())
112 changes: 0 additions & 112 deletions exp/tests/test_study_forms.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
from typing import Dict, Text
from unittest.case import skip
from unittest.mock import Mock, patch

from django.core.files.uploadedfile import SimpleUploadedFile
from django.forms.models import model_to_dict
from django.test import TestCase
from django_dynamic_fixture import G
from guardian.shortcuts import assign_perm
from parameterized import parameterized

from accounts.models import User
from exp.views.mixins import StudyTypeMixin
from studies.forms import StudyCreateForm, StudyEditForm
from studies.models import Lab, Study, StudyType
from studies.permissions import LabPermission, StudyPermission
Expand Down Expand Up @@ -86,44 +82,6 @@ def setUp(self):
self.study.save()
self.age_error_message = "The maximum age must be greater than the minimum age."

def test_valid_structure_accepted(self):
data = model_to_dict(self.study)
structure_text = """{
"frames": {"frame-a": {}, "frame-b": {}},
"sequence": ["frame-a", "frame-b"]
}"""
data["structure"] = structure_text
form = StudyEditForm(data=data, instance=self.study, user=self.study_designer)
self.assertNotIn("structure", form.errors)
form.is_valid()
self.assertDictEqual(
form.cleaned_data["structure"],
{
"frames": {"frame-a": {}, "frame-b": {}},
"sequence": ["frame-a", "frame-b"],
"exact_text": structure_text,
},
)

def test_structure_with_extra_comma_invalid(self):
data = model_to_dict(self.study)
data[
"structure"
] = """
{
"frames": {"frame-a": {}, "frame-b": {}},
"sequence": ["frame-a", "frame-b"],
}
"""
form = StudyEditForm(data=data, instance=self.study, user=self.study_designer)
self.assertIn("structure", form.errors)

def test_empty_structure_invalid(self):
data = model_to_dict(self.study)
data["structure"] = ""
form = StudyEditForm(data=data, instance=self.study, user=self.study_designer)
self.assertIn("structure", form.errors)

def test_valid_criteria_expression(self):
data = model_to_dict(self.study)
data["criteria_expression"] = (
Expand Down Expand Up @@ -424,73 +382,3 @@ def test_lab_options_as_user_who_can_create_studies_in_two_labs(self):
self.assertIn(self.second_lab, edit_form.fields["lab"].queryset)
self.assertNotIn(self.other_lab, edit_form.fields["lab"].queryset)
self.assertNotIn("lab", edit_form.errors)

def test_scheduled_checkbox_enabled(self):
"""Checking that the Study Edit Form's sheduled field is always enabled. JavaScript will disable this field when study is internal."""
data = model_to_dict(self.study)
structure_text = '{"frames": {"frame-a": {}, "frame-b": {}}, "sequence": ["frame-a", "frame-b"]}'
data["structure"] = structure_text
form = StudyEditForm(data=data, instance=self.study, user=self.study_designer)
self.assertFalse(form.fields["scheduled"].disabled)


class StudyMixinsTestCase(TestCase):
@parameterized.expand(
[
("", "", False, "", "", "", ""),
("", "on", True, "scheduling value", "", "study platform value", ""),
("https://lookit.mit.edu", "", False, "", "", "", ""),
(
"https://lookit.mit.edu",
"on",
True,
"Other",
"Other value",
"Other",
"Other value",
),
]
)
@patch.object(StudyTypeMixin, "request", create=True)
def test_validate_and_fetch_metadata(
self,
url: Text,
post_scheduled: Text,
meta_scheduled: bool,
scheduling: Text,
other_scheduling: Text,
study_platform: Text,
other_study_platform: Text,
mock_request: Mock,
):
type(mock_request).POST = expected_metadata = {
"url": url,
"scheduled": post_scheduled,
"scheduling": scheduling,
"other_scheduling": other_scheduling,
"study_platform": study_platform,
"other_study_platform": other_study_platform,
}
external = StudyType.get_external()
metadata, errors = StudyTypeMixin().validate_and_fetch_metadata(external)

expected_metadata["scheduled"] = meta_scheduled

self.assertFalse(errors)
self.assertEqual(expected_metadata, metadata)

@parameterized.expand(
[
({},),
({"scheduled": False},),
({"scheduled": True},),
]
)
@patch.object(StudyTypeMixin, "request", create=True)
def test_validate_and_fetch_metadata_invalid_metadata(
self, post_data: Dict, mock_request: Mock
):
type(mock_request).POST = post_data
external = StudyType.get_external()
_, errors = StudyTypeMixin().validate_and_fetch_metadata(external)
self.assertTrue(errors)
Loading

0 comments on commit a1a8290

Please sign in to comment.