def update_single_study( # pylint: disable=R0913 study: Study, local: bool, entity: tuple = None, filename: str = None, clean_import=False, manager: StudyImportManager = None, ) -> None: """Update a single study""" backup_file = Path() if clean_import: backup_file = Basket.backup() study.delete() study.save() if not manager: StudyImportManager(study) if not local: manager.update_repo() if not entity: manager.import_all_entities() elif filename: manager.import_single_entity(entity[0], filename) else: update_study_partial(manager, entity) if backup_file.is_file(): call_command("loaddata", backup_file) BasketVariable.clean_basket_variables(study.name)
def test_backup(self, basket, variable): """Can we do a backup of existing Baskets.""" basket.save() basket_id = basket.id basket_variable = BasketVariable(basket=basket, variable=variable) other_variable = VariableFactory(name="test-variable") other_basket_variable = BasketVariable(basket=basket, variable=other_variable) other_basket_variable.basket = basket other_basket_variable.variable = other_variable basket_variable.save() other_basket_variable.save() backup_file = Basket.backup() variable.delete() basket.delete() call_command("loaddata", backup_file) basket = Basket.objects.get(id=basket_id) basket_variables = list(basket.variables.all()) TEST_CASE.assertIn(other_basket_variable.variable, basket_variables) TEST_CASE.assertNotIn(basket_variable.variable, basket_variables) remove(backup_file)
def test_get_datasets_and_variables_method(self, script_config, variable): # Add variable to basket basket_variable = BasketVariable(basket=script_config.basket, variable=variable) basket_variable.save() result = script_config.get_datasets_and_variables() assert len(result) == 1 assert result[variable.dataset.name] == [variable.name]
def test_to_csv_method_with_variable_in_basket(self, basket, variable, csv_heading): basket_variable = BasketVariable(basket=basket, variable=variable) basket_variable.save() result = basket.to_csv() assert csv_heading in result assert variable.name in result assert variable.label in result assert variable.dataset.name in result assert variable.dataset.study.name in result
def test_export(self, variable, basket): TEST_CASE.assertEqual(0, BasketVariable.objects.count()) basket_variable = BasketVariable(basket=basket, variable=variable) basket_variable.save() dataset = BasketVariableExportResource().export() TEST_CASE.assertEqual(basket.name, dataset["basket"][0]) TEST_CASE.assertEqual(basket.user.username, dataset["user"][0]) TEST_CASE.assertEqual(basket.study.name, dataset["study"][0]) TEST_CASE.assertEqual(variable.dataset.name, dataset["dataset"][0]) TEST_CASE.assertEqual(variable.name, dataset["variable"][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())
def test_basket_protection(self): """A clean update should leave baskets intact.""" clean_import = False basket = BasketFactory(name="study_basket") manager = StudyImportManager(self.study, redis=False) update.update_single_study(self.study, True, clean_import=clean_import, manager=manager) variable = Variable.objects.get(name="some-variable") outdated_variable = Variable.objects.get(name="some-third-variable") basket_variable = BasketVariable(basket=basket, variable=variable) outdated_basket_variable = BasketVariable(basket=basket, variable=outdated_variable) outdated_basket_variable.save() basket.save() basket_variable.save() outdated_id = outdated_variable.id variable_id = variable.id basket_id = basket.id import_files = Path(self.patch_argument_dict["return_value"]) new_variables = """study_name,dataset_name,name,concept_name,image_url some-study,some-dataset,some-variable,some-concept,https://variable-image.de some-study,some-dataset,some-other-variable,some-concept,https://variable-other-image.de """ with open(import_files.joinpath("variables.csv"), "w", encoding="utf8") as file: file.write(new_variables) clean_import = True manager = StudyImportManager(self.study, redis=False) update.update_single_study(self.study, True, clean_import=clean_import, manager=manager) with self.assertRaises(ObjectDoesNotExist): Variable.objects.get(name="some-third-variable") variable = Variable.objects.get(name="some-variable") self.assertEqual(1, BasketVariable.objects.all().count()) self.assertEqual( 1, BasketVariable.objects.filter(variable_id=variable_id).count()) self.assertEqual( 0, BasketVariable.objects.filter(variable_id=outdated_id).count()) self.assertEqual(1, Basket.objects.filter(id=basket_id).count())
def test_remove_dangling_basket_variables(self): """Can we clean up BasketVariables, that link to non existing variables?""" basket_variable = BasketVariable(basket_id=self.basket.id, variable_id=self.variable.id) basket_variable.save() variable_id = basket_variable.variable.id basket_variable.variable.delete() basket_variable.clean_basket_variables() with self.assertRaises(BasketVariable.DoesNotExist): BasketVariable.objects.get(variable__id=variable_id)
def test_get_basket_variable_GET_with_url_param(self): """Can we get a basket variable with url params""" self.client.force_authenticate(user=self.user) new_basket_variable = BasketVariable() new_basket_variable.basket = self.basket new_basket_variable.variable = VariableFactory(name="new_variable") new_basket_variable.save() response = self.client.get( self.API_PATH + f"?variable={new_basket_variable.variable.id}&basket={self.basket.id}" ) results = json.loads(response.content)["results"] basket = results[0] self.assertEqual(self.basket.id, basket["basket_id"]) self.assertEqual(str(new_basket_variable.variable.id), basket["variable_id"])
def add_variable( request: WSGIRequest, # pylint: disable=unused-argument basket_id: int, variable_id: uuid.UUID, ) -> HttpResponseRedirect: """ Add a variable to a basket """ # make sure everything is found in the database _ = get_object_or_404(Basket, id=basket_id) _ = get_object_or_404(Variable, id=variable_id) try: basket_variable = BasketVariable(basket_id=basket_id, variable_id=variable_id) basket_variable.clean() basket_variable.save() except: pass return redirect(request.META.get("HTTP_REFERER"))
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)
def test_clean_method(self): basket_variable = BasketVariable(basket_id=self.basket.id, variable_id=self.variable.id) basket_variable.clean() basket_variable.save() self.assertEqual(1, BasketVariable.objects.count())