Skip to content

Commit

Permalink
Added .bamboo folder
Browse files Browse the repository at this point in the history
updated test_downloadable to properly check if regimen already tracked
  • Loading branch information
nicain committed Mar 15, 2019
1 parent af3687c commit cdbaddb
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 18 deletions.
12 changes: 12 additions & 0 deletions .bamboo/run_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export PATH=${bamboo_build_working_directory}/miniconda/bin:$PATH
export HOME=${bamboo_build_working_directory}/.home
export TMPDIR=${bamboo_build_working_directory}
export CONDA_PATH_BACKUP=${CONDA_PATH_BACKUP:-$PATH}
export CONDA_PREFIX=${CONDA_PREFIX:-}
export CONDA_PS1_BACKUP=${CONDA_PS1_BACKUP:-}

source activate ${bamboo_build_working_directory}/.conda/conda_test_env
cd ${bamboo_build_working_directory}
coverage run --source ./ -m pytest --junitxml=test-reports/tests.xml --html=test-reports/report.html --self-contained-html
coverage html --omit="tests/*,setup.py" --directory=htmlcov
source deactivate
16 changes: 16 additions & 0 deletions .bamboo/setup_env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
set -eu

wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh
bash miniconda.sh -b -p ${bamboo_build_working_directory}/miniconda
export PATH=${bamboo_build_working_directory}/miniconda/bin:$PATH
export HOME=${bamboo_build_working_directory}/.home
export TMPDIR=${bamboo_build_working_directory}
export CONDA_PATH_BACKUP=${CONDA_PATH_BACKUP:-$PATH}
export CONDA_PREFIX=${CONDA_PREFIX:-}
export CONDA_PS1_BACKUP=${CONDA_PS1_BACKUP:-}
conda create -y -v --prefix ${bamboo_build_working_directory}/.conda/conda_test_env python=3.6
source activate ${bamboo_build_working_directory}/.conda/conda_test_env
cd ${bamboo_build_working_directory}
pip install -r requirements.txt
pip install -r requirements_dev.txt
source deactivate
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pyyaml
requests
7 changes: 7 additions & 0 deletions requirements_dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pytest
bumpversion
flake8
pytest-cov
pytest-html
coverage
pandas
4 changes: 3 additions & 1 deletion tests/utils.py → tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import pytest
import os
import yaml

def load_regimen():
@pytest.fixture
def regimen_dict():
regimen_path = os.path.join(
os.path.dirname(__file__),
'..',
Expand Down
55 changes: 43 additions & 12 deletions tests/test_downloadable.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,47 @@
import hashlib
import warnings
import requests
from .utils import load_regimen
import sys
import itertools
import pandas as pd

regimen_data = load_regimen()
'''
Copy-pasted get_page and get_df; This is code duplication, but I dont want this to be an installed package, and both tests and scripts need this code
'''

def test_script_files_download():
def get_page(table_name, api_base='http://prodmtrain1:5000', get_obj=None, **kwargs):

for stage_name, stage_dict in regimen_data['stages'].items():
if get_obj is None:
get_obj = requests

data = {'total_pages':'--'}
for ii in itertools.count(1):
print('Downloading page: %s/%s ' % (ii , data['total_pages']), end='')
sys.stdout.flush()

tmp = get_obj.get(os.path.join(api_base, "api/v1/%s?page=%i" % (table_name, ii)), **kwargs)
try:
data = tmp.json()
except TypeError:
data = tmp.json
print('... ', end='')
if 'message' not in data:
df = pd.DataFrame(data["objects"])
print('done')
sys.stdout.flush()
yield df

if 'total_pages' not in data or data['total_pages'] == ii:
print('done')
return

def get_df(table_name, api_base='http://prodmtrain1:5000', get_obj=None, **kwargs):
return pd.concat([df for df in get_page(table_name, api_base=api_base, get_obj=get_obj, **kwargs)], axis=0)


def test_script_files_download(regimen_dict):

for stage_name, stage_dict in regimen_dict['stages'].items():
if stage_dict['script'] == 'NotImplemented':
warnings.warn('Stage "%s" Not Implemented' % stage_name)
else:
Expand All @@ -25,12 +59,9 @@ def test_script_files_download():
downloaded_md5 = hashlib.md5(result.content).hexdigest()
assert downloaded_md5 == stage_dict['script_md5'], "{}:{}".format(stage_name,downloaded_md5)

def test_add_already_tracked_regimen():
def test_add_already_tracked_regimen(regimen_dict):

api_endpoint = 'http://prodmtrain1:5000/api/v1/regimens'
result = requests.get(api_endpoint)
assert result.status_code == 200
data = result.json()
assert data['total_pages'] >= 1
if regimen_data['name'] in [x['name'] for x in data['objects']]:
warnings.warn('Regimen name %s already tracked on server %s' % (regimen_data['name'], api_endpoint))
api_base = 'http://prodmtrain1:5000'
regimen_list = get_df('regimens', api_base=api_base)['name'].values
if regimen_dict['name'] in regimen_list:
raise Exception('Regimen name %s already tracked on server %s' % (regimen_dict['name'], api_base))
18 changes: 13 additions & 5 deletions tests/test_stage_name_in_params.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
from .utils import load_regimen

regimen_data = load_regimen()

def test_stage_name_in_params():
for stage_name, stage_dict in regimen_data['stages'].items():
def test_stage_name_in_params(regimen_dict):
for stage_name, stage_dict in regimen_dict['stages'].items():
stage_param = stage_dict['parameters']['stage']
assert stage_param==stage_name, "stage: {}, param: {}".format(stage_name,stage_param)

def test_transitions_stages_match(regimen_dict):

# Double-check regimen stages and transitions match:
transition_source_target_set = set()
for t in regimen_dict['transitions']:
transition_source_target_set.add(t['source'])
transition_source_target_set.add(t['dest'])
stage_set = set(regimen_dict['stages'].keys())
if not all([curr_stage in stage_set for curr_stage in transition_source_target_set]):
raise Exception('Stages and source/dest of transitions are not consistent')

0 comments on commit cdbaddb

Please sign in to comment.