diff --git a/nomad/api/raw.py b/nomad/api/raw.py index 4096ff669dbde82504081b2f70e238406420e0f7..51fe17f7d4eb95f09a9dfe4691705fcbcd16f596 100644 --- a/nomad/api/raw.py +++ b/nomad/api/raw.py @@ -379,7 +379,7 @@ class RawFileQueryResource(Resource): with zipfile_cache: for filename in list(upload_files.raw_file_manifest(path_prefix=os.path.dirname(mainfile))): - yield filename, upload_files + yield os.path.join(upload_id, filename), filename, upload_files return _streamed_zipfile(generator(), zipfile_name='nomad_raw_files.zip') @@ -399,12 +399,12 @@ def respond_to_get_raw_files(upload_id, files, compress=False): with zipfile_cache: return _streamed_zipfile( - [(filename, upload_files) for filename in files], + [(filename, filename, upload_files) for filename in files], zipfile_name='%s.zip' % upload_id, compress=compress) def _streamed_zipfile( - files: Iterable[Tuple[str, UploadFiles]], zipfile_name: str, compress: bool = False): + files: Iterable[Tuple[str, str, UploadFiles]], zipfile_name: str, compress: bool = False): def generator(): """ Stream a zip file with all files using zipstream. """ @@ -413,10 +413,10 @@ def _streamed_zipfile( Replace the directory based iter of zipstream with an iter over all given files. """ - for filename, upload_files in files: + for zipped_filename, upload_filename, upload_files in files: # Write a file to the zipstream. try: - with upload_files.raw_file(filename, 'rb') as f: + with upload_files.raw_file(upload_filename, 'rb') as f: def iter_content(): while True: data = f.read(100000) @@ -424,7 +424,7 @@ def _streamed_zipfile( break yield data - yield dict(arcname=filename, iterable=iter_content()) + yield dict(arcname=zipped_filename, iterable=iter_content()) except KeyError: # files that are not found, will not be returned pass diff --git a/nomad/datamodel/base.py b/nomad/datamodel/base.py index 9b416118227a8e6f53b16ddd0fe53e4cbb6a9c63..2808f1beab373f4c2a86806a614f6cfdc9e37acb 100644 --- a/nomad/datamodel/base.py +++ b/nomad/datamodel/base.py @@ -269,7 +269,10 @@ class Domain: mainfile=DomainQuantity(description='Search for the mainfile.'), datasets=DomainQuantity( elastic_field='datasets.name', multi=True, - description='Search for a particular dataset by name.')) + description='Search for a particular dataset by name.'), + doi=DomainQuantity( + elastic_field='datasets.doi', elastic_search_type='match', multi=True, + description='Search for a particular dataset by doi (incl. http://dx.doi.org).')) def __init__( self, name: str, domain_entry_class: Type[CalcWithMetadata], diff --git a/tests/conftest.py b/tests/conftest.py index 63d173d93d4d11641198d4bd45be89322c2d70c8..d322b9f5a162a4cfee3c301883762e2b56775b0d 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from typing import Tuple +from typing import Tuple, List import pytest import logging from sqlalchemy.orm import Session @@ -555,6 +555,18 @@ def processed(uploaded: Tuple[str, str], test_user: coe_repo.User, proc_infra) - return test_processing.run_processing(uploaded, test_user) +@pytest.mark.timeout(config.tests.default_timeout) +@pytest.fixture(scope='function') +def processeds(non_empty_example_upload: str, test_user: coe_repo.User, proc_infra) -> List[processing.Upload]: + result: List[processing.Upload] = [] + for i in range(2): + upload_id = '%s_%d' % (os.path.basename(non_empty_example_upload).replace('.zip', ''), i) + result.append( + test_processing.run_processing((upload_id, non_empty_example_upload), test_user)) + + return result + + @pytest.mark.timeout(config.tests.default_timeout) @pytest.fixture(scope='function') def non_empty_processed(non_empty_uploaded: Tuple[str, str], test_user: coe_repo.User, proc_infra) -> processing.Upload: diff --git a/tests/test_api.py b/tests/test_api.py index 53ccaf807a1f93726f26df48caedb57a84688448..d3323319b183a16dff59dfbf691e938b669f82ae 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -1030,7 +1030,7 @@ class TestRaw(UploadFilesBasedTests): assert zip_file.testzip() is None assert len(zip_file.namelist()) == len(example_file_contents) - def test_raw_files_from_query(self, client, non_empty_processed, test_user_auth): + def test_raw_files_from_query_upload_id(self, client, non_empty_processed, test_user_auth): url = '/raw/query?upload_id=%s' % non_empty_processed.upload_id rv = client.get(url, headers=test_user_auth) @@ -1040,6 +1040,21 @@ class TestRaw(UploadFilesBasedTests): assert zip_file.testzip() is None assert len(zip_file.namelist()) == len(example_file_contents) + @pytest.mark.parametrize('query_params', [ + {'atoms': 'Si'}, + {'authors': 'Cooper, Sheldon'} + ]) + def test_raw_files_from_query(self, client, processeds, test_user_auth, query_params): + + url = '/raw/query?%s' % urlencode(query_params) + rv = client.get(url, headers=test_user_auth) + + assert rv.status_code == 200 + assert len(rv.data) > 0 + with zipfile.ZipFile(io.BytesIO(rv.data)) as zip_file: + assert zip_file.testzip() is None + assert len(zip_file.namelist()) == len(example_file_contents) * len(processeds) + def test_raw_files_from_empty_query(self, client, elastic): url = '/raw/query?upload_id=doesNotExist' rv = client.get(url)