Skip to content

Commit

Permalink
refactor: updated None objects in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvanr committed Nov 28, 2023
1 parent 03ce3f5 commit f2f9489
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
13 changes: 7 additions & 6 deletions tests/direct_indexing/custom_fields/test_custom_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,25 @@ def test_add_all(mocker):
mock_pa = mocker.patch('direct_indexing.custom_fields.custom_fields.process_activity')
mock_ca = mocker.patch('direct_indexing.custom_fields.custom_fields.currency_aggregation')
mock_h2 = mocker.patch('direct_indexing.custom_fields.custom_fields.raise_h2_budget_data_to_h1')

mock = mocker.MagicMock()
# Test that the h2 function is not called when fcdo instance is false,
# and that the process_activity and currency_aggregation functions are called once
mocker.patch(FCDO_IN, False)
data = {}
add_all(data, None, None, None)
add_all(data, mock, mock, {})
mock_pa.assert_called_once()
mock_ca.assert_called_once()
mock_h2.assert_not_called()

# Test that the process_activity function is called len(data) times
data = [{}, {}]
add_all(data, None, None, None)
add_all(data, mock, mock, {})
assert mock_pa.call_count == len(data) + 1 # +1 for the previous test

# Test that the h2 function is called when fcdo instance is true
mocker.patch(FCDO_IN, True)
data = {}
add_all(data, None, None, None)
add_all(data, mock, mock, {})
mock_h2.assert_called_once()


Expand All @@ -41,11 +41,12 @@ def test_process_activity(mocker):
mock_ajd = mocker.patch('direct_indexing.custom_fields.custom_fields.add_json_dumps')
mock_adq = mocker.patch('direct_indexing.custom_fields.custom_fields.add_date_quarter_fields')
mock_dlcc = mocker.patch('direct_indexing.custom_fields.custom_fields.document_link_category_combined')
mock = mocker.MagicMock()

# Test that all subfunctions are called once
mocker.patch(FCDO_IN, False)
activity = {}
process_activity(activity, None, None, None)
process_activity(activity, mock, mock, {})
mock_ac.assert_called_once()
mock_tn.assert_called_once()
mock_ad.assert_called_once()
Expand All @@ -60,7 +61,7 @@ def test_process_activity(mocker):

# Test that the remaining functions are called when FCDO_INSTANCE is True
mocker.patch(FCDO_IN, True)
process_activity(activity, None, None, None)
process_activity(activity, mock, mock, {})
mock_ajd.assert_called_once()
mock_adq.assert_called_once()
mock_dlcc.assert_called_once()
Expand Down
11 changes: 6 additions & 5 deletions tests/direct_indexing/processing/test_activity_subtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,26 @@ def test_extract_subtype(mocker):
assert mock_process.call_count == len(data.keys()) # once for each key in data


def test_process_subtype_dict():
def test_process_subtype_dict(mocker):
tvu = 'transaction.value-usd'
bvu = 'budget.value-usd'
# Test if key is in AVAILABLE_SUBTYPES, it is nothing changes in subtype_dict
subtype_dict = {'transaction': {'value': 1}}
expected_res = subtype_dict.copy()
key = 'transaction'
res = process_subtype_dict(subtype_dict, key, None, None, None, None)
mock = mocker.MagicMock()
res = process_subtype_dict(subtype_dict, key, mock, None, None, None)
assert res == expected_res

# Test if key is in exclude fields we do not include it in the subtype dict
res = process_subtype_dict(subtype_dict, bvu, None, None, [bvu], None)
res = process_subtype_dict(subtype_dict, bvu, mock, None, [bvu], None)
assert res == expected_res

# Test that a specific value which is a dict can be retrieved
data = {'title': 'title', 'budget': {'value': 1}, tvu: 1.1}
expected_res = subtype_dict.copy()
expected_res[tvu] = 1.1
res = process_subtype_dict(subtype_dict, tvu, None, data, [], [tvu])
res = process_subtype_dict(subtype_dict, tvu, mock, data, [], [tvu])
assert res == expected_res

# Test that the value of a specific element is extracted from the list
Expand All @@ -53,7 +54,7 @@ def test_process_subtype_dict():

# Test that additional fields are kept without modification
expected_res['title'] = 'title'
res = process_subtype_dict(subtype_dict, 'title', None, data, [], [])
res = process_subtype_dict(subtype_dict, 'title', mock, data, [], [])
assert res == expected_res


Expand Down

0 comments on commit f2f9489

Please sign in to comment.