Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aanil committed May 13, 2024
1 parent 72fecf7 commit 8e2a4a8
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 59 deletions.
9 changes: 8 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ def data_repo_full(
@pytest.fixture
def mock_project_data_record():
def _method(status):
config_values = config.Config()
config_values = config.Config(env_file_path=".test.env")
if status == "open":
mock_record = ngi_data.ProjectDataRecord(
"NGIS/2023/NGI123456.json",
Expand Down Expand Up @@ -441,3 +441,10 @@ def mocked_statusdb_conn_rows():
},
)
return [row1, row2, row3]


@pytest.fixture(autouse=True)
def get_env_file_path():
"""returns the test env file path"""

return ".test.env"
4 changes: 2 additions & 2 deletions tests/test_daily_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
from daily_read import daily_report, config, ngi_data, order_portal


def test_write_report_to_out_dir(data_repo_full, mock_project_data_record, create_report_path):
def test_write_report_to_out_dir(data_repo_full, mock_project_data_record, create_report_path, get_env_file_path):
"""Test existence of html report when provided with out_dir"""
orderer = "[email protected]"
order_id = "NGI123456"
config_values = config.Config()
config_values = config.Config(env_file_path=get_env_file_path)
daily_rep = daily_report.DailyReport()
with mock.patch("daily_read.statusdb.StatusDBSession"):
data_master = ngi_data.ProjectDataMaster(config_values)
Expand Down
80 changes: 40 additions & 40 deletions tests/test_ngi_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
####################################################### TESTS #########################################################


def test_create_project_data_master(data_repo_full):
def test_create_project_data_master(data_repo_full, get_env_file_path):
"""With existing git repo, test creation of a ProjectDataMaster class"""
config_values = config.Config()
config_values = config.Config(get_env_file_path)
with mock.patch("daily_read.statusdb.StatusDBSession"):
data_master = ngi_data.ProjectDataMaster(config_values)

Expand All @@ -21,9 +21,9 @@ def test_create_project_data_master(data_repo_full):
assert data_master._data_fetched == False


def test_create_project_data_master_no_commit(data_repo_no_commit):
def test_create_project_data_master_no_commit(data_repo_no_commit, get_env_file_path):
"""With existing git repo but without commits, test creation of a ProjectDataMaster class"""
config_values = config.Config()
config_values = config.Config(get_env_file_path)
with mock.patch("daily_read.statusdb.StatusDBSession"):
data_master = ngi_data.ProjectDataMaster(config_values)

Expand All @@ -35,8 +35,8 @@ def test_create_project_data_master_no_commit(data_repo_no_commit):
assert data_master.data_repo.commit().message == "Empty file as a first commit"


def test_modified_or_new_no_commit(data_repo_no_commit):
config_values = config.Config()
def test_modified_or_new_no_commit(data_repo_no_commit, get_env_file_path):
config_values = config.Config(get_env_file_path)
with mock.patch("daily_read.statusdb.StatusDBSession"):
data_master = ngi_data.ProjectDataMaster(config_values)

Expand All @@ -47,8 +47,8 @@ def test_modified_or_new_no_commit(data_repo_no_commit):
assert len(set(file_names)) == 0


def test_modified_or_new(data_repo_full):
config_values = config.Config()
def test_modified_or_new(data_repo_full, get_env_file_path):
config_values = config.Config(get_env_file_path)
with mock.patch("daily_read.statusdb.StatusDBSession"):
data_master = ngi_data.ProjectDataMaster(config_values)

Expand All @@ -59,8 +59,8 @@ def test_modified_or_new(data_repo_full):
assert len(set(file_names)) == 12


def test_modified_or_new_untracked(data_repo_untracked):
config_values = config.Config()
def test_modified_or_new_untracked(data_repo_untracked, get_env_file_path):
config_values = config.Config(get_env_file_path)
with mock.patch("daily_read.statusdb.StatusDBSession"):
data_master = ngi_data.ProjectDataMaster(config_values)

Expand All @@ -73,8 +73,8 @@ def test_modified_or_new_untracked(data_repo_untracked):
assert "untracked_file" in file_names[0]


def test_modified_or_new_staged(data_repo_new_staged):
config_values = config.Config()
def test_modified_or_new_staged(data_repo_new_staged,get_env_file_path):
config_values = config.Config(get_env_file_path)
with mock.patch("daily_read.statusdb.StatusDBSession"):
data_master = ngi_data.ProjectDataMaster(config_values)

Expand All @@ -86,8 +86,8 @@ def test_modified_or_new_staged(data_repo_new_staged):
assert any("staged_file" in s for s in file_names)


def test_modified_or_new_modified_not_staged(data_repo_modified_not_staged):
config_values = config.Config()
def test_modified_or_new_modified_not_staged(data_repo_modified_not_staged, get_env_file_path):
config_values = config.Config(get_env_file_path)
with mock.patch("daily_read.statusdb.StatusDBSession"):
data_master = ngi_data.ProjectDataMaster(config_values)

Expand All @@ -100,8 +100,8 @@ def test_modified_or_new_modified_not_staged(data_repo_modified_not_staged):
assert "modified_file" in file_names[0]


def test_modified_or_new_modified_staged(data_repo_modified_staged):
config_values = config.Config()
def test_modified_or_new_modified_staged(data_repo_modified_staged, get_env_file_path):
config_values = config.Config(get_env_file_path)
with mock.patch("daily_read.statusdb.StatusDBSession"):
data_master = ngi_data.ProjectDataMaster(config_values)

Expand All @@ -114,8 +114,8 @@ def test_modified_or_new_modified_staged(data_repo_modified_staged):
assert "modified_staged_file" in file_names[0]


def test_modified_or_new_tracked(data_repo_tracked):
config_values = config.Config()
def test_modified_or_new_tracked(data_repo_tracked, get_env_file_path):
config_values = config.Config(get_env_file_path)
with mock.patch("daily_read.statusdb.StatusDBSession"):
data_master = ngi_data.ProjectDataMaster(config_values)

Expand All @@ -127,19 +127,19 @@ def test_modified_or_new_tracked(data_repo_tracked):
assert len(set(file_names)) == 0


def test_get_unique_orderers(data_repo_full):
def test_get_unique_orderers(data_repo_full, get_env_file_path):
"""Test getting unique orders in the project data from statusdb"""
config_values = config.Config()
config_values = config.Config(get_env_file_path)
with mock.patch("daily_read.statusdb.StatusDBSession"):
data_master = ngi_data.ProjectDataMaster(config_values)
data_master.get_data()
orderers = data_master.find_unique_orderers()
assert orderers == set(["[email protected]"])


def test_user_list(data_repo_full, tmp_path, mocked_statusdb_conn_rows):
def test_user_list(data_repo_full, tmp_path, mocked_statusdb_conn_rows, get_env_file_path):
"""Test getting and reading users from the user list url"""
config_values = config.Config()
config_values = config.Config(get_env_file_path)
temp_file = tmp_path / "test_file.txt"
temp_file.write_text("[email protected]\n[email protected]")
config_values.USERS_LIST_LOCATION = temp_file
Expand All @@ -154,9 +154,9 @@ def test_user_list(data_repo_full, tmp_path, mocked_statusdb_conn_rows):
assert orderers == set(["[email protected]", "[email protected]"])


def test_save_data_to_disk(data_repo_full, mocked_statusdb_conn_rows):
def test_save_data_to_disk(data_repo_full, mocked_statusdb_conn_rows, get_env_file_path):
"""Test saving in git repo the data gotten from statusdb"""
config_values = config.Config()
config_values = config.Config(get_env_file_path)
with mock.patch("daily_read.statusdb.StatusDBSession"):
data_master = ngi_data.ProjectDataMaster(config_values)
data_master.sources[0].statusdb_session.rows.return_value = mocked_statusdb_conn_rows
Expand All @@ -166,9 +166,9 @@ def test_save_data_to_disk(data_repo_full, mocked_statusdb_conn_rows):
assert os.path.exists(os.path.join(config_values.DATA_LOCATION, "NGIS/2023/NGI123458.json"))


def test_get_data_with_project(data_repo_full, mocked_statusdb_conn_rows):
def test_get_data_with_project(data_repo_full, mocked_statusdb_conn_rows, get_env_file_path):
"""Test getting data for a specific order"""
config_values = config.Config()
config_values = config.Config(get_env_file_path)
order_id = "NGI123457"
with mock.patch("daily_read.statusdb.StatusDBSession"):
data_master = ngi_data.ProjectDataMaster(config_values)
Expand All @@ -179,9 +179,9 @@ def test_get_data_with_project(data_repo_full, mocked_statusdb_conn_rows):
assert data_master.data[order_id].internal_id_or_portal_id == "P123457"


def test_get_data_with_project_unknown(data_repo_full, mocked_statusdb_conn_rows):
def test_get_data_with_project_unknown(data_repo_full, mocked_statusdb_conn_rows, get_env_file_path):
"""Test error thrown when the order specified is not found in statusdb"""
config_values = config.Config()
config_values = config.Config(get_env_file_path)
with mock.patch("daily_read.statusdb.StatusDBSession"):
data_master = ngi_data.ProjectDataMaster(config_values)
data_master.sources[0].statusdb_session.rows.return_value = mocked_statusdb_conn_rows
Expand All @@ -190,9 +190,9 @@ def test_get_data_with_project_unknown(data_repo_full, mocked_statusdb_conn_rows


@mock.patch("daily_read.statusdb.StatusDBSession")
def test_data_loc_not_abs(mock_status):
def test_data_loc_not_abs(mock_status, get_env_file_path):
"""Test error thrown when given data location is not an absolute path"""
config_values = config.Config()
config_values = config.Config(get_env_file_path)
config_values.DATA_LOCATION = "tests/test_data_location"
with pytest.raises(
ValueError, match=f"Data location is not an absolute path: {config_values.DATA_LOCATION}"
Expand All @@ -201,9 +201,9 @@ def test_data_loc_not_abs(mock_status):


@mock.patch("daily_read.statusdb.StatusDBSession")
def test_data_loc_not_dir(mock_status, tmp_path):
def test_data_loc_not_dir(mock_status, tmp_path, get_env_file_path):
"""Test error thrown when data location is not a directory"""
config_values = config.Config()
config_values = config.Config(get_env_file_path)
temp_file = tmp_path / "test_file.txt"
temp_file.write_text("test")
config_values.DATA_LOCATION = temp_file
Expand All @@ -213,11 +213,11 @@ def test_data_loc_not_dir(mock_status, tmp_path):
ngi_data.ProjectDataMaster(config_values)


def test_get_data_with_no_project_dates(data_repo_full, mocked_statusdb_conn_rows, caplog):
def test_get_data_with_no_project_dates(data_repo_full, mocked_statusdb_conn_rows, caplog, get_env_file_path):
"""Test log output when no project dates are found in statusdb for a specific project"""
from copy import deepcopy

config_values = config.Config()
config_values = config.Config(get_env_file_path)
with mock.patch("daily_read.statusdb.StatusDBSession"):
data_master = ngi_data.ProjectDataMaster(config_values)
data_master.sources[0].statusdb_session.rows.return_value = mocked_statusdb_conn_rows
Expand All @@ -232,10 +232,10 @@ def test_get_data_with_no_project_dates(data_repo_full, mocked_statusdb_conn_row
assert "No project dates found for NGI123459" in caplog.text


def test_skip_order_with_no_year(data_repo_full, mocked_statusdb_conn_rows, caplog):
def test_skip_order_with_no_year(data_repo_full, mocked_statusdb_conn_rows, caplog, get_env_file_path):
"""Test that orders with no order year (i.e. with no contract signed) are skipped"""

config_values = config.Config()
config_values = config.Config(get_env_file_path)
with mock.patch("daily_read.statusdb.StatusDBSession"):
data_master = ngi_data.ProjectDataMaster(config_values)
data_master.sources[0].statusdb_session.rows.return_value = mocked_statusdb_conn_rows
Expand All @@ -247,20 +247,20 @@ def test_skip_order_with_no_year(data_repo_full, mocked_statusdb_conn_rows, capl


@mock.patch("daily_read.statusdb.StatusDBSession")
def test_no_source_specified(mock_status):
def test_no_source_specified(mock_status, get_env_file_path):
"""Test error thrown when no sources are specified"""

config_values = config.Config()
config_values = config.Config(get_env_file_path)
config_values.FETCH_FROM_NGIS = ""
config_values.FETCH_FROM_SNPSEQ = ""
config_values.FETCH_FROM_UGC = ""
with pytest.raises(ValueError, match="There are no sources specified to fetch data from!") as err:
ngi_data.ProjectDataMaster(config_values)


def test_commit_staged_data(data_repo_full, mocked_statusdb_conn_rows):
def test_commit_staged_data(data_repo_full, mocked_statusdb_conn_rows, get_env_file_path):
"""Test file is staged for commit and committed and then try adding it for staging again"""
config_values = config.Config()
config_values = config.Config(get_env_file_path)
with mock.patch("daily_read.statusdb.StatusDBSession"):
data_master = ngi_data.ProjectDataMaster(config_values)
data_master.sources[0].statusdb_session.rows.return_value = mocked_statusdb_conn_rows
Expand Down
28 changes: 14 additions & 14 deletions tests/test_order_portal.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
from daily_read import order_portal, config, ngi_data


def test_get_and_process_orders_open_upload_fail(data_repo_full, mock_project_data_record, caplog):
def test_get_and_process_orders_open_upload_fail(data_repo_full, mock_project_data_record, caplog, get_env_file_path):
"""Test getting and processing an open order and upload to Order portal failing"""
orderer = "[email protected]"
order_id = "NGI123456"
config_values = config.Config()
config_values = config.Config(get_env_file_path)
with mock.patch("daily_read.statusdb.StatusDBSession"):
data_master = ngi_data.ProjectDataMaster(config_values)

Expand All @@ -32,11 +32,11 @@ def test_get_and_process_orders_open_upload_fail(data_repo_full, mock_project_da
assert f"Report not uploaded for order with project id: {order_id}\nReason: 404" in caplog.text


def test_get_and_process_orders_open_and_upload(data_repo_full, mock_project_data_record):
def test_get_and_process_orders_open_and_upload(data_repo_full, mock_project_data_record, get_env_file_path):
"""Test getting and processing an open order and uploading its Project progress report and uploading the report to the Order portal"""
orderer = "[email protected]"
order_id = "NGI123456"
config_values = config.Config()
config_values = config.Config(get_env_file_path)
with mock.patch("daily_read.statusdb.StatusDBSession"):
data_master = ngi_data.ProjectDataMaster(config_values)

Expand Down Expand Up @@ -69,11 +69,11 @@ def test_get_and_process_orders_open_and_upload(data_repo_full, mock_project_dat
)


def test_get_and_process_orders_open_with_report_and_upload(data_repo_full, mock_project_data_record, caplog):
def test_get_and_process_orders_open_with_report_and_upload(data_repo_full, mock_project_data_record, caplog, get_env_file_path):
"""Test getting, processing an open order with an existing Project progress report and uploading the report to the Order portal"""
orderer = "[email protected]"
order_id = "NGI123453"
config_values = config.Config()
config_values = config.Config(get_env_file_path)
with mock.patch("daily_read.statusdb.StatusDBSession"):
data_master = ngi_data.ProjectDataMaster(config_values)

Expand Down Expand Up @@ -109,11 +109,11 @@ def test_get_and_process_orders_open_with_report_and_upload(data_repo_full, mock
assert f"Updated report for order with project id: {order_id}" in caplog.text


def test_get_and_process_orders_open_to_aborted_with_report_and_upload(data_repo_full, mock_project_data_record):
def test_get_and_process_orders_open_to_aborted_with_report_and_upload(data_repo_full, mock_project_data_record, get_env_file_path):
"""Test getting, processing an open order with an existing Project progress report and uploading the report to the Order portal"""
orderer = "[email protected]"
order_id = "NGI123461"
config_values = config.Config()
config_values = config.Config(get_env_file_path)
with mock.patch("daily_read.statusdb.StatusDBSession"):
data_master = ngi_data.ProjectDataMaster(config_values)

Expand Down Expand Up @@ -142,11 +142,11 @@ def test_get_and_process_orders_open_to_aborted_with_report_and_upload(data_repo
)


def test_get_and_process_orders_closed(data_repo_full, mock_project_data_record):
def test_get_and_process_orders_closed(data_repo_full, mock_project_data_record, get_env_file_path):
"""Test getting and processing an order closed within the timeframe of Project progress report deletion"""
orderer = "[email protected]"
order_id = "NGI123455"
config_values = config.Config()
config_values = config.Config(get_env_file_path)
with mock.patch("daily_read.statusdb.StatusDBSession"):
data_master = ngi_data.ProjectDataMaster(config_values)

Expand All @@ -160,11 +160,11 @@ def test_get_and_process_orders_closed(data_repo_full, mock_project_data_record)
assert modified_orders[orderer]["delete_report_for"]["All Raw data Delivered"][0] == data_master.data[order_id]


def test_get_and_process_orders_mult_reports(data_repo_full, mock_project_data_record):
def test_get_and_process_orders_mult_reports(data_repo_full, mock_project_data_record, get_env_file_path):
"""Test getting and processing orders with multiple Project progress reports"""
orderer = "[email protected]"
order_id = "NGI123454"
config_values = config.Config()
config_values = config.Config(get_env_file_path)
with mock.patch("daily_read.statusdb.StatusDBSession"):
data_master = ngi_data.ProjectDataMaster(config_values)

Expand All @@ -180,10 +180,10 @@ def test_get_and_process_orders_mult_reports(data_repo_full, mock_project_data_r
op.process_orders(config_values.STATUS_PRIORITY_REV)


def test_base_url_and_api_key_not_set(data_repo_full, mock_project_data_record):
def test_base_url_and_api_key_not_set(data_repo_full, mock_project_data_record, get_env_file_path):
"""Test the conditions when environment variables ORDER_PORTAL_URL and ORDER_PORTAL_API_KEY are not set"""
order_id = "NGI123456"
config_values = config.Config()
config_values = config.Config(get_env_file_path)

order_portal_url = config_values.ORDER_PORTAL_URL
api_key = config_values.ORDER_PORTAL_API_KEY
Expand Down
4 changes: 2 additions & 2 deletions tests/test_statusdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
from daily_read import config, statusdb


def test_no_statusdb_conn(data_repo_full):
def test_no_statusdb_conn(data_repo_full, get_env_file_path):
"""Test error thrown when statusdb conn fails"""
config_values = config.Config()
config_values = config.Config(get_env_file_path)
# Escape special chars because match uses regex
display_url_string = (
rf"https:\/\/{config_values.STHLM_STATUSDB_USERNAME}:\*\*\*\*\*\*\*\*\*@{config_values.STHLM_STATUSDB_URL}"
Expand Down

0 comments on commit 8e2a4a8

Please sign in to comment.