def setUp(self): self.client = APIClient() self.concept = ConceptFactory(name="test-concept") self.topic = TopicFactory(name="test-topic") self.concept.topics.add(self.topic) self.concept.save() return super().setUp()
def test_import_variable_method_with_concept_name(self, variable_importer, variable): concept = ConceptFactory(name="some-concept") concept.save() element = dict( dataset_name=variable.dataset.name, name=variable.name, concept_name=concept.name, description="some-description", ) variable_importer._import_variable(element) # pylint: disable=protected-access variable = Variable.objects.get(id=variable.id) assert variable.description == element["description"] assert variable.concept.name == element["concept_name"]
def test_variable_import(self): some_dataset = DatasetFactory(name="some-dataset") some_dataset.save() ConceptFactory(name="some-concept").save() ConceptFactory(name="orphaned-concept").save() variable_path = Path( "tests/functional/test_data/some-study/ddionrails/variables.csv") variable_path = variable_path.absolute() VariableImport.run_import(variable_path, study=some_dataset.study) with open(variable_path, "r", encoding="utf8") as csv_file: variable_names = {row["name"] for row in csv.DictReader(csv_file)} result = Variable.objects.filter(name__in=list(variable_names)) TEST_CASE.assertNotEqual(0, len(result)) TEST_CASE.assertEqual(len(variable_names), len(result))
def test_POST_basket_variables_by_concept_name(self): """Define how basket variable creation by concept should work.""" topic = TopicFactory(name="parent-topic") child_topic = TopicFactory(name="parent-topic", parent=topic) concept = ConceptFactory(name="some-concept") concept.topics.set([child_topic]) concept.save() variables = [VariableFactory(name="1"), VariableFactory(name="2")] variables[1].concept = concept variables[1].save() post_data = {"basket": str(self.basket.id), "concept": concept.name} self.client.force_authenticate(user=self.user) post_response = self.client.post(self.API_PATH, post_data, format="json") self.assertEqual(201, post_response.status_code) get_response = self.client.get(self.API_PATH) content = json.loads(get_response.content) result_ids = [result["variable_id"] for result in content["results"]] self.assertIn(str(variables[1].id), result_ids) self.assertNotIn(str(variables[0].id), result_ids)
def test_basket_variable_limit_topic_and_concept_POST(self): """Define how the basket limit should work.""" too_many_variables = [] topic = TopicFactory(name="test-topic") concept = ConceptFactory(name="test-concept") concept.topics.set([topic]) concept.save() for number in range(1, 12): variable = VariableFactory(name=str(number)) variable.concept = concept variable.save() too_many_variables.append(variable) with patch( "ddionrails.api.views.user_tools.BasketVariableSet.basket_limit", new_callable=PropertyMock, ) as basket_limit: basket_limit.return_value = 10 post_data = {"basket": str(self.basket.id), "topic": topic.name} self.client.force_authenticate(user=self.user) post_response = self.client.post(self.API_PATH, post_data, format="json") self.assertEqual(406, post_response.status_code) self.assertIn(b"basket size limit", post_response.content) BasketVariable.objects.all().delete() post_data = { "basket": str(self.basket.id), "concept": concept.name } self.client.force_authenticate(user=self.user) post_response = self.client.post(self.API_PATH, post_data, format="json") self.assertEqual(406, post_response.status_code) self.assertIn(b"basket size limit", post_response.content)
def concept(db, topic, request): """A concept in the database""" concept = ConceptFactory( name="some-concept", label="Some Concept", label_de="Ein Konzept", description="This is some concept", ) root_topic = TopicFactory( name="root-topic", label="root-topic", label_de="root-topic", description="root-topic", description_de="root-topic", ) topic.parent = root_topic topic.save() concept.topics.add(topic) concept.save() if request.instance: request.instance.concept = concept yield concept
def test_variable_import_with_orphaned_concept(self): csv_path = Study().import_path() concept_path = csv_path.joinpath("concepts.csv") some_dataset = DatasetFactory(name="some-dataset") some_dataset.save() StudyImportManager(study=some_dataset.study).fix_concepts_csv() ConceptFactory(name="some-concept").save() variable_path = csv_path.joinpath("variables.csv") variable_path = variable_path.absolute() ConceptImport(concept_path).run_import(filename=concept_path) VariableImport.run_import(variable_path, study=some_dataset.study) with open(variable_path, "r", encoding="utf8") as csv_file: variable_names = {row["name"] for row in csv.DictReader(csv_file)} result = Variable.objects.filter(name__in=list(variable_names)) TEST_CASE.assertNotEqual(0, len(result)) TEST_CASE.assertEqual(len(variable_names), len(result))
def concept(db): """ A concept in the database """ return ConceptFactory(name="some-concept", label="Some Concept", description="This is some concept")
class TestVariableViewSet(unittest.TestCase): API_PATH = "/api/variables/" client: APIClient def setUp(self): self.client = APIClient() self.concept = ConceptFactory(name="test-concept") self.topic = TopicFactory(name="test-topic") self.concept.topics.add(self.topic) self.concept.save() return super().setUp() def test_query_parameter_conflict(self): concept_name = self.concept.name topic_name = self.topic.name response = self.client.get( self.API_PATH + f"?concept={concept_name}&topic={topic_name}") self.assertEqual(406, response.status_code) content = json.loads(response.content) self.assertIn("mutually exclusive", content["detail"]) response = self.client.get(self.API_PATH + f"?topic={topic_name}") self.assertEqual(406, response.status_code) content = json.loads(response.content) self.assertIn("requires study parameter", content["detail"]) def test_404_errors(self): study = "some-nonexistent-study" call_string = f"{self.API_PATH}?study={study}" self.assertEqual(404, self.client.get(call_string).status_code) concept = "some-nonexistent-concept" call_string = f"{self.API_PATH}?concept={concept}" self.assertEqual(404, self.client.get(call_string).status_code) topic = "some-nonexistent-topic" call_string = f"{self.API_PATH}?topic={topic}&study=dummy" self.assertEqual(404, self.client.get(call_string).status_code) def test_query_parameter_concept(self): concept_name = self.concept.name variable_list = [] for number in range(1, 11): _variable = VariableFactory(name=str(number)) _variable.concept = self.concept _variable.save() variable_list.append(_variable) for number in range(11, 21): _variable = VariableFactory(name=str(number)) variable_list.append(_variable) response = self.client.get(self.API_PATH + f"?concept={concept_name}") content = json.loads(response.content) self.assertEqual(10, len(content)) def test_query_parameter_study(self): """Define study parameter behavior.""" dataset = DatasetFactory(name="different-dataset") study = StudyFactory(name="different-study") study_name = study.name dataset.study = study dataset.save() variable_list = [] for number in range(1, 11): _variable = VariableFactory(name=str(number)) _variable.dataset = dataset _variable.save() variable_list.append(_variable) for number in range(11, 21): _variable = VariableFactory(name=str(number)) variable_list.append(_variable) response = self.client.get(self.API_PATH + f"?study={study_name}") content = json.loads(response.content) self.assertEqual(10, len(content)) def test_returned_fields(self): """Define fields that should be provided.""" expected_fields = [ "id", "name", "label", "label_de", "dataset_name", "study_name", "study_label", "dataset", "study", "position", ] VariableFactory(name="test_variable") response = self.client.get(self.API_PATH) results = json.loads(response.content) variable = results[0] self.assertListEqual(expected_fields, list(variable.keys())) def test_get_variable_GET_data(self): """Is the get response as expected?""" variable_amount = 10 variables = [] for number in range(1, variable_amount + 1): variables.append(VariableFactory(name=f"{number}")) response = self.client.get(self.API_PATH) self.assertEqual(200, response.status_code) content = json.loads(response.content) self.assertEqual(variable_amount, len(content)) result_ids = [result["id"] for result in content] for variable in variables: self.assertIn(str(variable.id), result_ids) def test_scroll_limit(self): """There should be no scroll limit""" variable_amount = 101 variables = [] for number in range(1, variable_amount + 1): variables.append(VariableFactory(name=f"{number}")) response = self.client.get(self.API_PATH + "?paginate=False") self.assertEqual(200, response.status_code) content = json.loads(response.content) self.assertEqual(variable_amount, len(content))