def setup_bucket(add_model=False, add_mm=False, add_tests=False, add_results=False, add_model_stats=False, add_test_stats=False): """ This function creates a new (local) bucket mocking S3 bucket at each call. Then all calls to S3 are calling this bucket instead of real S3 bucket. Depending on the test we might or might not need the bucket to contain different files. For faster computation, only required files for the test are generated and stored in the bucket. Files can be added by setting corresponding arguments to True when calling this function. """ # Local imports are recommended when using moto from emmaa.util import get_s3_client from emmaa.model import save_config_to_s3 from emmaa.model_tests import ModelManager, save_model_manager_to_s3, \ StatementCheckingTest # Create a mock s3 bucket client = get_s3_client() bucket = client.create_bucket(Bucket=TEST_BUCKET_NAME, ACL='public-read') date_str = make_date_str() emmaa_model = None if add_model: # Put config and model files into empty bucket config_dict = { 'ndex': { 'network': 'a08479d1-24ce-11e9-bb6a-0ac135e8bacf' }, 'search_terms': [{ 'db_refs': { 'HGNC': '20974' }, 'name': 'MAPK1', 'search_term': 'MAPK1', 'type': 'gene' }], 'test': { 'test_corpus': 'simple_tests', 'default_test_corpus': 'simple_tests' }, 'human_readable_name': 'Test Model', 'assembly': [{ 'function': 'filter_no_hypothesis' }, { 'function': 'map_grounding' }, { 'function': 'filter_grounded_only' }, { 'function': 'filter_human_only' }, { 'function': 'map_sequence' }, { 'function': 'run_preassembly', 'kwargs': { 'return_toplevel': False } }, { 'function': 'filter_top_level' }] } save_config_to_s3('test', config_dict, bucket=TEST_BUCKET_NAME) emmaa_model = create_model() emmaa_model.save_to_s3(bucket=TEST_BUCKET_NAME) if add_mm: # Add a ModelManager to bucket if not emmaa_model: emmaa_model = create_model() mm = ModelManager(emmaa_model) mm.date_str = date_str mm.save_assembled_statements(upload_to_db=False, bucket=TEST_BUCKET_NAME) save_model_manager_to_s3('test', mm, bucket=TEST_BUCKET_NAME) if add_tests: tests = [ StatementCheckingTest(Activation(Agent('BRAF'), Agent('MAPK1'))) ] test_dict = { 'test_data': { 'description': 'Tests for functionality testing' }, 'tests': tests } client.put_object(Body=pickle.dumps(test_dict), Bucket=TEST_BUCKET_NAME, Key=f'tests/simple_tests.pkl') if add_results: client.put_object( Body=json.dumps(previous_results, indent=1), Bucket=TEST_BUCKET_NAME, Key=f'results/test/results_simple_tests_{date_str}.json') if add_model_stats: client.put_object(Body=json.dumps(previous_model_stats, indent=1), Bucket=TEST_BUCKET_NAME, Key=f'model_stats/test/model_stats_{date_str}.json') if add_test_stats: client.put_object( Body=json.dumps(previous_test_stats, indent=1), Bucket=TEST_BUCKET_NAME, Key=f'stats/test/test_stats_simple_tests_{date_str}.json') return client
'/evidence?stmt_hash=-34603994586320440&source=' 'model_statement&model=test&date=2020-01-01', 'Active MAP2K1 activates MAPK1.', '' ]] }] } } query_not_appl = {'2413475507': 'Query is not applicable for this model'} fail_response = { '521653329': 'No path found that satisfies the test statement' } # Create a new EmmaaModel and ModelManager for tests instead of depending # on S3 version test_model = create_model() test_mm = ModelManager(test_model) test_mm.date_str = '2020-01-01-00-00-00' test_email = '*****@*****.**' def test_format_results(): date = datetime.now() results = [ ('test', query_object, 'pysb', test_response, {'3801854542'}, date), ('test', query_object, 'signed_graph', fail_response, {}, date), ('test', query_object, 'unsigned_graph', test_response, {}, date) ] formatted_results = format_results(results) assert len(formatted_results) == 1 qh = query_object.get_hash_with_model('test') assert qh in formatted_results assert formatted_results[qh]['model'] == 'test'