def test_writeTempOverride_diffDir(self, mock_open, mock_os_open, mock_os_close, mock_fcntl, mock_scd): source_filepath = '/path/to/file.png' target_dir = '/some/other/dir' d = DataFileElement(source_filepath) fp = d.write_temp(temp_dir=target_dir) ntools.assert_not_equal(fp, source_filepath) ntools.assert_equal(os.path.dirname(fp), target_dir) # subsequent call to write temp should not invoke creation of a new file fp2 = d.write_temp() ntools.assert_equal(fp2, source_filepath) # request in same dir should return same path as first request with that # directory fp3 = d.write_temp(target_dir) ntools.assert_equal(fp, fp3) # request different target dir target2 = '/even/different/path' fp4 = d.write_temp(target2) ntools.assert_equal(os.path.dirname(fp4), target2) ntools.assert_not_equal(fp, fp4) ntools.assert_equal(len(d._temp_filepath_stack), 2)
def test_writeTempOverride(self, mock_DataElement_wt): # no manual directory, should return the base filepath expected_filepath = '/path/to/file.txt' d = DataFileElement(expected_filepath) fp = d.write_temp() ntools.assert_false(mock_DataElement_wt.called) ntools.assert_equal(expected_filepath, fp)
def test_cleanTemp(self): # a write temp and clean temp should not affect original file source_file = os.path.join(TEST_DATA_DIR, 'test_file.dat') ntools.assert_true(os.path.isfile(source_file)) d = DataFileElement(source_file) d.write_temp() ntools.assert_equal(len(d._temp_filepath_stack), 0) d.clean_temp() ntools.assert_true(os.path.isfile(source_file))
def test_writeTempOverride_sameDir(self, mock_DataElement_wt): expected_filepath = '/path/to/file.txt' target_dir = '/path/to' d = DataFileElement(expected_filepath) fp = d.write_temp(temp_dir=target_dir) ntools.assert_false(mock_DataElement_wt.called) ntools.assert_equal(fp, expected_filepath)
def iqr_ingest_file(): """ Ingest the file with the given UID, getting the path from the uploader. :return: status message :rtype: str """ # TODO: Add status dict with a "GET" method branch for getting that # status information. # Start the ingest of a FID when POST if flask.request.method == "POST": iqr_sess = self.get_current_iqr_session() fid = flask.request.form['fid'] self.log.debug("[%s::%s] Getting temporary filepath from " "uploader module", iqr_sess.uuid, fid) upload_filepath = self.mod_upload.get_path_for_id(fid) self.mod_upload.clear_completed(fid) # Extend session ingest -- modifying with iqr_sess: self.log.debug("[%s::%s] Adding new file to extension " "ingest", iqr_sess.uuid, fid) sess_upload = osp.join(iqr_sess.work_dir, osp.basename(upload_filepath)) os.rename(upload_filepath, sess_upload) upload_data = DataFileElement(sess_upload) iqr_sess.extension_ds.add_data(upload_data) # Compute feature for data -- non-modifying self.log.debug("[%s::%s] Computing feature for file", iqr_sess.uuid, fid) feat = iqr_sess.descriptor.compute_descriptor(upload_data) # Extend indexer model with feature data -- modifying with iqr_sess: self.log.debug("[%s::%s] Extending indexer model with " "feature", iqr_sess.uuid, fid) iqr_sess.indexer.extend_model({upload_data.uuid(): feat}) # of course, add the new data element as a positive iqr_sess.adjudicate((upload_data.uuid(),)) return "Finished Ingestion"
def test_content_type(self): d = DataFileElement('foo.txt') ntools.assert_equal(d.content_type(), 'text/plain')