Skip to content

Commit

Permalink
adapted test
Browse files Browse the repository at this point in the history
  • Loading branch information
burnout87 committed Mar 25, 2024
1 parent 5b6d7a8 commit e2295fa
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 12 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,6 @@ data/dummy_prods**
.venv
js9.fits
test-dispatcher-conf-with-gallery.yaml
tests/oda-ontology.ttl
tests/oda-ontology.ttl
tests/request_files
tests/local_request_files
45 changes: 44 additions & 1 deletion cdci_data_analysis/pytest_fixtures.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# this could be a separate package or/and a pytest plugin
import pathlib
from json import JSONDecodeError

import sentry_sdk
Expand All @@ -7,7 +8,7 @@
import cdci_data_analysis.flask_app.app
from cdci_data_analysis.analysis.exceptions import BadRequest
from cdci_data_analysis.flask_app.dispatcher_query import InstrumentQueryBackEnd
from cdci_data_analysis.analysis.hash import make_hash
from cdci_data_analysis.analysis.hash import make_hash, make_hash_file
from cdci_data_analysis.configurer import ConfigEnv
from cdci_data_analysis.analysis.email_helper import textify_email

Expand Down Expand Up @@ -935,6 +936,38 @@ def empty_products_files_fixture(default_params_dict):
yield scratch_params


@pytest.fixture
def request_files_fixture(default_params_dict):
DispatcherJobState.empty_request_files_folders()
request_file_info_obj = {
'file_path': 'request_files/test.fits.gz',
'content': os.urandom(20)
}

with open(request_file_info_obj['file_path'], 'wb') as f:
f.write(request_file_info_obj['content'])

request_file_info_obj['file_hash'] = make_hash_file(request_file_info_obj['file_path'])

f_path_obj = pathlib.Path(request_file_info_obj['file_path'])
f_name = f_path_obj.name.split('.')[0]
f_ext = ''.join(f_path_obj.suffixes)

new_file_name = f_name + '_' + request_file_info_obj['file_hash'] + f_ext
new_file_path = os.path.join('request_files', new_file_name)
os.rename(request_file_info_obj['file_path'], new_file_path)
request_file_info_obj['file_path'] = new_file_path

ownership_file_path = 'request_files/.file_ownerships.json'
with open(ownership_file_path) as ownership_file:
ownerships = json.load(ownership_file)
ownerships[new_file_name] = ['public']
with open(ownership_file_path, 'w') as ownership_file:
json.dump(ownerships, ownership_file)

yield request_file_info_obj


@pytest.fixture
def empty_products_user_files_fixture(default_params_dict, default_token_payload):
sub = default_token_payload['sub']
Expand Down Expand Up @@ -1441,6 +1474,16 @@ def remove_download_folders(id=None):
for d in dir_list:
shutil.rmtree(d)

@staticmethod
def empty_request_files_folders():
dir_list = glob.glob('request_files/*')
for d in dir_list:
os.remove(d)

ownership_file_path = 'request_files/.file_ownerships.json'
with open(ownership_file_path, 'w') as ownership_file:
json.dump({}, ownership_file)

@staticmethod
def create_p_value_file(p_value):
# generate ScWs list file
Expand Down
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
cleanup,
empty_products_files_fixture,
empty_products_user_files_fixture,
request_files_fixture,
default_params_dict,
default_token_payload,
dispatcher_live_fixture_empty_sentry,
Expand Down
16 changes: 6 additions & 10 deletions tests/test_server_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,22 +496,18 @@ def test_download_products_outside_dir(dispatcher_long_living_fixture,
@pytest.mark.fast
@pytest.mark.parametrize('return_archive', [True, False])
@pytest.mark.parametrize('matching_file_name', [True, False])
def test_download_file_public(dispatcher_long_living_fixture, empty_products_files_fixture, return_archive, matching_file_name):
def test_download_file_public(dispatcher_long_living_fixture, request_files_fixture, return_archive, matching_file_name):

server = dispatcher_long_living_fixture

logger.info("constructed server: %s", server)

session_id = empty_products_files_fixture['session_id']
job_id = empty_products_files_fixture['job_id']

params = {
# since we are passing a job_id
'query_status': 'ready',
'file_list': 'test.fits.gz',
'file_list': os.path.basename(request_files_fixture['file_path']),
'download_file_name': 'output_test',
'session_id': session_id,
'return_archive': return_archive,
'job_id': job_id
}

if matching_file_name:
Expand All @@ -523,16 +519,16 @@ def test_download_file_public(dispatcher_long_living_fixture, empty_products_fil
assert c.status_code == 200

# download the output, read it and then compare it
with open(f'scratch_sid_{session_id}_jid_{job_id}/output_test', 'wb') as fout:
with open('local_request_files/output_test', 'wb') as fout:
fout.write(c.content)

if return_archive:
with gzip.open(f'scratch_sid_{session_id}_jid_{job_id}/output_test', 'rb') as fout:
with gzip.open('local_request_files/output_test', 'rb') as fout:
data_downloaded = fout.read()
else:
data_downloaded = c.content

assert data_downloaded == empty_products_files_fixture['content']
assert data_downloaded == request_files_fixture['content']

def test_query_restricted_instrument(dispatcher_live_fixture):
server = dispatcher_live_fixture
Expand Down

0 comments on commit e2295fa

Please sign in to comment.