Skip to content

Commit

Permalink
Fixed testing
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoooo committed Apr 17, 2023
1 parent a990005 commit c6ab0ee
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 34 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.0.0
2.0.0
4 changes: 1 addition & 3 deletions src/ensembl/production/datacheck/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
es_user = app.config['ES_USER']
es_password = app.config['ES_PASSWORD']
es_ssl = app.config['ES_SSL']

app_es_data_source = app.config['APP_ES_DATA_SOURCE']

if app.env == 'development':
Expand Down Expand Up @@ -343,7 +342,6 @@ def job_details():
jsonfile = request.args.get('jsonfile', None)
if jsonfile is None:
raise Exception('jsonfile needed ')

if app_es_data_source:
ensembl_division = f"Ensembl{DatacheckConfig.DATACHECK_TYPE.capitalize()}"
res = get_datacheck_results(division=ensembl_division,
Expand All @@ -361,7 +359,7 @@ def job_details():
file_data = open(jsonfile, 'r').read()
return jsonify(json.loads(file_data))
except Exception as e:
return jsonify({'error': f"Failed to retrive the details : {str(e)}"}), 404
return jsonify({'error': f"Failed to retrieve the details : {str(e)}"}), 404


@app.route('/jobs/<int:job_id>', methods=['GET'])
Expand Down
9 changes: 5 additions & 4 deletions src/ensembl/production/datacheck/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@

def get_app_version():
try:
version = pkg_resources.require("datacheck")[0].version
from importlib.metadata import version
version = version("datacheck")
except Exception as e:
with open(Path(__file__).parents[4] / 'VERSION') as f:
version = f.read()
Expand Down Expand Up @@ -126,11 +127,11 @@ class DatacheckConfig(EnsemblConfig):
APP_ES_DATA_SOURCE = os.environ.get('APP_ES_DATA_SOURCE', EnsemblConfig.file_config.get('app_es_data_source', True))

ES_HOST = os.environ.get('ES_HOST', EnsemblConfig.file_config.get('es_host', 'localhost'))
ES_USER = os.getenv("ES_USER", EnsemblConfig.file_config.get("es_user", "elastic")),
ES_PASSWORD = os.getenv("ES_PASSWORD", EnsemblConfig.file_config.get("es_password", "password")),
ES_USER = "" # os.getenv("ES_USER", EnsemblConfig.file_config.get("es_user", "")),
ES_PASSWORD = "" # os.getenv("ES_PASSWORD", EnsemblConfig.file_config.get("es_password", "")),
ES_PORT = os.environ.get('ES_PORT', EnsemblConfig.file_config.get('es_port', '9200'))
ES_SSL = os.environ.get('ES_SSL', EnsemblConfig.file_config.get('es_ssl', "f")).lower() in ['true', '1']
ES_INDEX = os.environ.get('ES_INDEX', EnsemblConfig.file_config.get('es_index', f"datacheck_results_{EnsemblConfig.ENS_VERSION}"))
ES_INDEX = os.environ.get('ES_INDEX', EnsemblConfig.file_config.get('es_index', "datacheck_results"))

GET_SERVER_NAMES = os.environ.get('GET_SERVER_NAMES', EnsemblConfig.file_config.get('get_server_names', 0))

Expand Down
37 changes: 19 additions & 18 deletions src/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,24 +101,25 @@ def wait_for(url: str, retries: int = 2, backoff: float = 0.2):
@pytest.fixture(scope="session")
def elastic_search():
wait_for(f"http://localhost:9200/")
es = Elasticsearch([{"host": "localhost", "port": 9200}])
print(es.info())
if not es.ping():
raise(
f"Cannot connect to Elasticsearch server. Host: localhost, Port: 9200"
)
def search(body: dict) -> None:
es.indices.flush()
es.indices.refresh()
return es.search(index="datacheck_results", body=body)
try:
#set mock es data
es.index(index="datacheck_results", body=dc_success_result_es_doc, doc_type="report")
es.index(index="datacheck_results", body=dc_failed_result_es_doc, doc_type="report")
yield search
finally:
if es.indices.exists("datacheck_results"):
es.indices.delete("datacheck_results")
with ElasticsearchConnectionManager("localhost", "9200", "", "", False) as es_client:
es = es_client.client
print("EsInfo", es.info())
def search(body: dict) -> None:
es.indices.flush()
es.indices.refresh()
return es.search(index="datacheck_results", body=body)

try:
#set mock es data
es.index(index="datacheck_results", body=dc_success_result_es_doc, doc_type="report")
es.index(index="datacheck_results", body=dc_failed_result_es_doc, doc_type="report")
print("Index created")
yield search
except:
raise RuntimeWarning("Unable to create indexes!")
finally:
if es.indices.exists("datacheck_results"):
es.indices.delete("datacheck_results")

@pytest.fixture()
def es_query():
Expand Down
15 changes: 10 additions & 5 deletions src/tests/test_config_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,17 @@ def test_config_load_fallback(self):
class TestAPPVersion(unittest.TestCase):

def test_config_app_version(self):

try:
from importlib.metadata import version
version = version("datacheck")
version_pkg = True
except Exception as e:
version = "unknown"
version_pkg = False
with open(Path(__file__).parent.parent.parent / 'VERSION') as f:
version_file = f.read().strip('\n')

version = pkg_resources.require("datacheck")[0].version
version_config = DatacheckConfig.APP_VERSION
self.assertEqual(version, version_config)
self.assertEqual(version, version_file)
if version_pkg :
self.assertEqual(version, version_config)
self.assertEqual(version, version_file)
self.assertEqual(version_file, version_config )
4 changes: 1 addition & 3 deletions src/tests/test_dc_results_from_es.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,19 @@ def test_without_jsonfile_param(appclient):
response = appclient.get('/jobs/details')
data = json.loads(response.data)
assert response.status_code == 404
assert data['error'] == 'Failed to retrive the details : jsonfile needed '
assert data['error'] == 'Failed to retrieve the details : jsonfile needed '

def test_get_dc_results_success(appclient, elastic_search, es_query):
elastic_search(es_query)
response = appclient.get('/jobs/details?jsonfile=/homes/user/test_es_output/user_sL2nmrNTRkjE/results_by_species.json')
data = json.loads(response.data)
print(response.data)
assert response.status_code == 200
assert data == {}

def test_get_dc_results_failed(appclient, elastic_search, es_query):
elastic_search(es_query)
response = appclient.get('/jobs/details?jsonfile=/homes/user/test_es_output/user_sL3mnrNTRrr1/results_by_species.json')
data = json.loads(response.data)
print(response.data)
assert response.status_code == 200
assert len(data.keys()) == 2

0 comments on commit c6ab0ee

Please sign in to comment.