-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #104 from khalford/write_tests_for_untested_code_v2
Write tests for untested code.
- Loading branch information
Showing
14 changed files
with
218 additions
and
174 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
13 changes: 3 additions & 10 deletions
13
...loader/lib/netbox_api/netbox_existence.py → ...a_Uploader/lib/netbox_api/netbox_check.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
from typing import Dict, List | ||
from pandas import read_csv | ||
from lib.netbox_api.netbox_data import NetboxGetID | ||
|
||
|
||
class FormatDict: | ||
""" | ||
This class takes dictionaries with string values and changes those to ID values from Netbox. | ||
""" | ||
|
||
def __init__(self, netbox): | ||
""" | ||
This method initialises the class with the following parameters. | ||
:param netbox: The Netbox object to pass into NetboxGetID | ||
""" | ||
self.netbox = netbox | ||
|
||
def iterate_dicts(self, dicts: list) -> List: | ||
""" | ||
This method iterates through each dictionary and calls a format method on each. | ||
:return: Returns the formatted dictionaries. | ||
""" | ||
new_dicts = [] | ||
for dictionary in dicts: | ||
new_dicts.append(self.format_dict(dictionary)) | ||
return new_dicts | ||
|
||
def format_dict(self, dictionary) -> Dict: | ||
""" | ||
This method iterates through each value in the dictionary. | ||
If the value needs to be converted into a Pynetbox ID it calls the .get() method. | ||
:param dictionary: The dictionary to be formatted | ||
:return: Returns the formatted dictionary | ||
""" | ||
for key in dictionary: | ||
netbox_id = NetboxGetID(self.netbox).get_id_from_key(key=key, dictionary=dictionary) | ||
dictionary[key] = netbox_id | ||
return dictionary | ||
|
||
|
||
@staticmethod | ||
def csv_to_python(file_path: str) -> Dict: | ||
""" | ||
This method reads data from csv files and writes them to a dictionary. | ||
:param file_path: The file path of the utils file to be read from. | ||
:return: Returns the data from the csv as a dictionary. | ||
""" | ||
dataframe = read_csv(file_path) | ||
return dataframe.to_dict(orient="list") | ||
|
||
@staticmethod | ||
def separate_data(data: dict) -> List: | ||
""" | ||
This method reduces Pandas utils to Dict conversion to individual dictionaries. | ||
:param data: The data from the utils file. | ||
:return: Returns a list of dictionaries which each represent a row of data from utils. | ||
""" | ||
data_keys = list(data.keys()) | ||
len_rows = len(data[data_keys[0]]) | ||
dicts = [] | ||
for index in range(len_rows): | ||
new_dict = {} | ||
for key in data_keys: | ||
new_dict.update({key: data[key][index]}) | ||
dicts.append(new_dict) | ||
return dicts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
from unittest.mock import NonCallableMock, patch, MagicMock | ||
import pytest | ||
from lib.utils.format_dict import FormatDict | ||
|
||
|
||
@pytest.fixture(name="instance") | ||
def instance_fixture(): | ||
""" | ||
This fixture method calls the class being tested. | ||
:return: The class object. | ||
""" | ||
netbox = NonCallableMock() | ||
return FormatDict(netbox) | ||
|
||
|
||
def test_csv_to_python(instance): | ||
""" | ||
This test ensures that the csv_read method is called once with the file_path arg. | ||
""" | ||
file_path = NonCallableMock() | ||
with patch("lib.utils.format_dict.read_csv") as mock_read_csv: | ||
res = instance.csv_to_python(file_path) | ||
mock_read_csv.assert_called_once_with(file_path) | ||
mock_read_csv.return_value.to_dict.assert_called_once_with(orient="list") | ||
assert res == mock_read_csv.return_value.to_dict.return_value | ||
|
||
|
||
def test_separate_data(instance): | ||
""" | ||
This test ensures that the dictionaries from pandas formatted into row by row dictionaries. | ||
These are much more understandable and can be used individually or in bulk. | ||
""" | ||
test_data = {"key1": ["Adata1", "Bdata1"], "key2": ["Adata2", "Bdata2"]} | ||
format_data = instance.separate_data(test_data) | ||
assert format_data == [ | ||
{"key1": "Adata1", "key2": "Adata2"}, | ||
{"key1": "Bdata1", "key2": "Bdata2"}, | ||
] | ||
|
||
|
||
def test_iterate_dicts_no_items(instance): | ||
""" | ||
This test ensures that an empty list is returned when there are no dictionaries. | ||
""" | ||
mock_dictionary = MagicMock() | ||
with patch("lib.utils.format_dict.FormatDict.format_dict") as mock_format: | ||
res = instance.iterate_dicts([mock_dictionary]) | ||
mock_format.assert_called_once_with(mock_dictionary) | ||
assert res == [mock_format.return_value] | ||
|
||
|
||
def test_iterate_dicts_one_item(instance): | ||
""" | ||
This test ensures the format method is called on the only dictionary. | ||
""" | ||
mock_dictionary = MagicMock() | ||
with patch("lib.utils.format_dict.FormatDict.format_dict") as mock_format: | ||
res = instance.iterate_dicts([mock_dictionary]) | ||
mock_format.assert_called_once_with(mock_dictionary) | ||
assert res == [mock_format.return_value] | ||
|
||
|
||
def test_iterate_dicts_many_items(instance): | ||
""" | ||
This test ensures the format method is called each dictionary. | ||
""" | ||
mock_dictionary_1 = MagicMock() | ||
mock_dictionary_3 = MagicMock() | ||
mock_dictionary_2 = MagicMock() | ||
with patch("lib.utils.format_dict.FormatDict.format_dict") as mock_format: | ||
res = instance.iterate_dicts( | ||
[mock_dictionary_1, mock_dictionary_2, mock_dictionary_3] | ||
) | ||
mock_format.assert_any_call(mock_dictionary_1) | ||
mock_format.assert_any_call(mock_dictionary_2) | ||
mock_format.assert_any_call(mock_dictionary_3) | ||
expected = [ | ||
mock_format.return_value, | ||
mock_format.return_value, | ||
mock_format.return_value, | ||
] | ||
assert res == expected |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.