Exemplo n.º 1
0
    def test_remove_dangling_basket_variables_study_specific(self):
        """Can we clean up BasketVariables belonging to a specific study?"""
        other_study = StudyFactory(name="a_different_study")
        dataset = DatasetFactory(name="a_different_study")
        dataset.study = other_study
        dataset.save()
        new_variable = Variable()
        new_variable.name = "a_different_variable"
        new_variable.dataset = dataset
        basket_variable = BasketVariable(basket_id=self.basket.id,
                                         variable_id=self.variable.id)
        new_basket = Basket()
        new_basket.study = other_study
        new_basket.user = self.basket.user
        new_basket.save()
        new_basket_variable = BasketVariable()
        new_basket_variable.variable = new_variable
        new_basket_variable.basket = new_basket
        new_basket_variable.save()

        basket_variable.save()
        variable_id = basket_variable.variable.id
        basket_variable.variable.delete()
        new_variable.delete()
        basket_variable.clean_basket_variables(
            study_name=self.basket.study.name)
        with self.assertRaises(BasketVariable.DoesNotExist):
            BasketVariable.objects.get(variable__id=variable_id)
Exemplo n.º 2
0
    def test_variable_import_without_concept_csv(self):

        csv_path = Study().import_path()
        concept_path = csv_path.joinpath("concepts.csv")

        os.remove(concept_path)

        some_dataset = DatasetFactory(name="some-dataset")
        some_dataset.save()
        TEST_CASE.assertIsNone(
            StudyImportManager(study=some_dataset.study).fix_concepts_csv())
        TEST_CASE.assertFalse(concept_path.exists())
Exemplo n.º 3
0
    def test_no_parameter(self) -> None:
        other_study = StudyFactory(name="some-other-study")
        dataset = DatasetFactory(name="some-dataset")
        other_dataset = DatasetFactory(name="some-other-dataset",
                                       study=other_study)

        response = self.client.get(self.API_PATH)
        content = json.loads(response.content)
        self.assertEqual(2, content["count"])
        result_dataset_names = [
            result["name"] for result in content["results"]
        ]
        self.assertIn(getattr(dataset, "name"), result_dataset_names)
        self.assertIn(getattr(other_dataset, "name"), result_dataset_names)
Exemplo n.º 4
0
 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))
Exemplo n.º 5
0
def _dataset(request, db):
    """A dataset in the database"""
    _factory = DatasetFactory(
        name="some-dataset", label="Some Dataset", description="This is some dataset"
    )
    if request.instance:
        request.instance.dataset = _factory
    return _factory
Exemplo n.º 6
0
    def test_study_name_parameter(self) -> None:
        dataset = DatasetFactory(name="some-dataset")
        study_name = getattr(dataset.study, "name")

        response = self.client.get(self.API_PATH + f"?study={study_name}")
        content = json.loads(response.content)
        self.assertEqual(1, content["count"])
        self.assertEqual(getattr(dataset, "name"),
                         content["results"][0]["name"])
Exemplo n.º 7
0
    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))
Exemplo n.º 8
0
 def test_clean_method_fails(self, basket):
     """ BasketVariable clean method should raise an ValidationError when basket and variable study do not match """
     other_study = StudyFactory(name="some-other-study")
     other_dataset = DatasetFactory(name="some-other-dataset",
                                    study=other_study)
     other_variable = VariableFactory(name="some-other-variable",
                                      dataset=other_dataset)
     basket_variable = BasketVariable(basket_id=basket.id,
                                      variable_id=other_variable.id)
     with pytest.raises(ValidationError):
         basket_variable.clean()
     assert 0 == BasketVariable.objects.count()
Exemplo n.º 9
0
    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))
Exemplo n.º 10
0
    def test_clean_method_fails(self):
        """Ensure the correct error raising of the BasketVariable clean method.

        BasketVariable clean method should raise a ValidationError
        when basket and variable study do not match.
        """
        other_study = StudyFactory(name="some-other-study")
        other_dataset = DatasetFactory(name="some-other-dataset",
                                       study=other_study)
        other_variable = VariableFactory(name="some-other-variable",
                                         dataset=other_dataset)
        basket_variable = BasketVariable(basket_id=self.basket.id,
                                         variable_id=other_variable.id)
        with pytest.raises(ValidationError):
            basket_variable.clean()
        expected = 0
        self.assertEqual(expected, BasketVariable.objects.count())
Exemplo n.º 11
0
def dataset(db):
    """ A dataset in the database """
    return DatasetFactory(name="some-dataset",
                          label="Some Dataset",
                          description="This is some dataset")
Exemplo n.º 12
0
 def setUp(self):
     self.dataset = DatasetFactory(name="test-dataset")
     self.study = self.dataset.study
     return super().setUp()