def test_download_exercise_csv(self): # Given a patched exercise instrument = InstrumentModel() seft_file = SEFTModel(instrument_id=instrument.instrument_id, file_name="file_name", data="test_data", length=999) instrument.seft_file = seft_file exercise = ExerciseModel( exercise_id="cb0711c3-0ac8-41d3-ae0e-567e5ea1ef87") business = BusinessModel(ru_ref="test_ru_ref") instrument.exercises.append(exercise) instrument.businesses.append(business) with patch( "application.controllers.collection_instrument.query_exercise_by_id", return_value=exercise): # When a call is made to the download_csv end point response = self.client.get( "/collection-instrument-api/1.0.2/download_csv/cb0711c3-0ac8-41d3-ae0e-567e5ea1ef87", headers=self.get_auth_headers(), ) # Then the response contains the correct data self.assertStatus(response, 200) self.assertIn('"Count","File Name","Length","Time Stamp"', response.data.decode()) self.assertIn('"1","file_name","999"', response.data.decode())
def upload_instrument_with_no_collection_exercise(self, survey_id, classifiers=None, session=None): """ Upload a collection instrument to the db without a collection exercise :param classifiers: Classifiers associated with the instrument :param session: database session :param survey_id: database session :return: a collection instrument instance """ log.info('Upload instrument', survey_id=survey_id) validate_uuid(survey_id) instrument = InstrumentModel(ci_type='EQ') survey = self._find_or_create_survey_from_survey_id(survey_id, session) instrument.survey = survey if classifiers: deserialized_classifiers = loads(classifiers) instruments = self._get_instruments_by_classifier(deserialized_classifiers, None, session) for instrument in instruments: if instrument.classifiers == deserialized_classifiers: raise RasError("Cannot upload an instrument with an identical set of classifiers", 400) instrument.classifiers = deserialized_classifiers session.add(instrument) return instrument
def add_instrument_without_exercise(session=None): instrument = InstrumentModel(ci_type="EQ", classifiers={ "form_type": "001", "geography": "EN" }) survey = SurveyModel(survey_id="cb0711c3-0ac8-41d3-ae0e-567e5ea1ef87") instrument.survey = survey business = BusinessModel(ru_ref="test_ru_ref") instrument.businesses.append(business) session.add(instrument) return instrument.instrument_id
def add_instrument_data(session=None): instrument = InstrumentModel(ci_type="SEFT") seft_file = SEFTModel(instrument_id=instrument.instrument_id, file_name="test_file") exercise = ExerciseModel(exercise_id="db0711c3-0ac8-41d3-ae0e-567e5ea1ef87") business = BusinessModel(ru_ref="test_ru_ref") instrument.seft_file = seft_file instrument.exercises.append(exercise) instrument.businesses.append(business) survey = SurveyModel(survey_id="cb0711c3-0ac8-41d3-ae0e-567e5ea1ef87") instrument.survey = survey session.add(instrument) return instrument.instrument_id
def test_patch_seft_instrument_instrument_not_seft(self, get_instrument): instrument_model = InstrumentModel() instrument_model.type = "EQ" get_instrument.return_value = instrument_model session = MagicMock() instrument = CollectionInstrument() with self.assertRaises(RasError) as error: instrument.patch_seft_instrument.__wrapped__(instrument, self.instrument_id, self.file, session) expected = ["Not a SEFT instrument"] expected_status = 400 self.assertEqual(expected, error.exception.errors) self.assertEqual(expected_status, error.exception.status_code)
def upload_to_bucket(self, exercise_id, file, ru_ref=None, classifiers=None, session=None): """ Encrypt and upload a collection instrument to the bucket and db :param exercise_id: An exercise id (UUID) :param ru_ref: The name of the file we're receiving :param classifiers: Classifiers associated with the instrument :param file: A file object from which we can read the file contents :param session: database session :return: a collection instrument instance """ log.info("Upload exercise", exercise_id=exercise_id) validate_uuid(exercise_id) self.validate_non_duplicate_instrument(file, exercise_id, session) instrument = InstrumentModel(ci_type="SEFT") seft_file = self._create_seft_file(instrument.instrument_id, file, encrypt_and_save_to_db=False) instrument.seft_file = seft_file exercise = self._find_or_create_exercise(exercise_id, session) instrument.exercises.append(exercise) survey = self._find_or_create_survey_from_exercise_id(exercise_id, session) instrument.survey = survey if ru_ref: business = self._find_or_create_business(ru_ref, session) self.validate_one_instrument_for_ru_specific_upload(exercise, business, session) instrument.businesses.append(business) if classifiers: instrument.classifiers = loads(classifiers) try: survey_ref = get_survey_ref(instrument.survey.survey_id) file.filename = survey_ref + "/" + exercise_id + "/" + file.filename seft_ci_bucket = GoogleCloudSEFTCIBucket(current_app.config) seft_ci_bucket.upload_file_to_bucket(file=file) instrument.seft_file.gcs = True except Exception as e: log.exception("An error occurred when trying to put SEFT CI in bucket") raise e session.add(instrument) return instrument
def add_instrument_data(session=None): instrument = InstrumentModel(classifiers={ "form_type": "001", "geography": "EN" }, ci_type="SEFT") crypto = Cryptographer() data = BytesIO(b"test data") data = crypto.encrypt(data.read()) seft_file = SEFTModel(instrument_id=instrument.instrument_id, file_name="test_file", length="999", data=data) instrument.seft_file = seft_file exercise = ExerciseModel(exercise_id=linked_exercise_id) business = BusinessModel(ru_ref="test_ru_ref") instrument.exercises.append(exercise) instrument.businesses.append(business) survey = SurveyModel(survey_id="cb0711c3-0ac8-41d3-ae0e-567e5ea1ef87") instrument.survey = survey session.add(instrument) return instrument.instrument_id
def upload_instrument(self, exercise_id, file, ru_ref=None, classifiers=None, session=None): """ Encrypt and upload a collection instrument to the db :param exercise_id: An exercise id (UUID) :param ru_ref: The name of the file we're receiving :param classifiers: Classifiers associated with the instrument :param file: A file object from which we can read the file contents :param session: database session :return: a collection instrument instance """ log.info('Upload exercise', exercise_id=exercise_id) validate_uuid(exercise_id) instrument = InstrumentModel(ci_type='SEFT') seft_file = self._create_seft_file(instrument.instrument_id, file) instrument.seft_file = seft_file exercise = self._find_or_create_exercise(exercise_id, session) instrument.exercises.append(exercise) survey = self._find_or_create_survey_from_exercise_id(exercise_id, session) instrument.survey = survey if ru_ref: business = self._find_or_create_business(ru_ref, session) self.validate_one_instrument_for_ru_specific_upload(exercise, business, session) instrument.businesses.append(business) if classifiers: instrument.classifiers = loads(classifiers) session.add(instrument) return instrument