-
Notifications
You must be signed in to change notification settings - Fork 14
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 #116 from stfc/dictionaries-to-dataclass
Dictionaries to dataclass
- Loading branch information
Showing
8 changed files
with
147 additions
and
13 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import csv | ||
from typing import List | ||
from lib.utils.dataclass_data import Device | ||
|
||
|
||
def open_file(file_path: str) -> csv.DictReader: | ||
""" | ||
This function opens the specified csv file and returns the DictReader class on it. | ||
:param file_path: The file path to the csv file. | ||
:return: Returns an instance of the DictReader Class. | ||
""" | ||
with open(file_path, encoding="UTF-8") as file: | ||
csv_reader_obj = csv.DictReader(file) | ||
return csv_reader_obj | ||
|
||
|
||
def separate_data(csv_reader_obj: csv.DictReader) -> List[Device]: | ||
""" | ||
This method separates the data from the iterator object into a list of dataclasses for each device. | ||
:param csv_reader_obj: The DictReader class with the data. | ||
:return: Returns a list of dataclass objects. | ||
""" | ||
devices = [] | ||
for row in csv_reader_obj: | ||
device = Device( | ||
tenant=row["tenant"], | ||
device_role=row["device_role"], | ||
manufacturer=row["manufacturer"], | ||
device_type=row["device_type"], | ||
status=row["status"], | ||
site=row["site"], | ||
location=row["location"], | ||
rack=row["rack"], | ||
face=row["face"], | ||
airflow=row["airflow"], | ||
position=row["position"], | ||
name=row["name"], | ||
serial=row["serial"], | ||
) | ||
devices.append(device) | ||
return devices |
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,24 @@ | ||
from dataclasses import dataclass | ||
from typing import Optional | ||
|
||
|
||
# pylint: disable = R0902 | ||
@dataclass | ||
class Device: | ||
""" | ||
This class instantiates device objects with the device data. | ||
""" | ||
|
||
tenant: str | ||
device_role: str | ||
manufacturer: str | ||
device_type: str | ||
status: str | ||
site: str | ||
location: str | ||
rack: str | ||
position: str | ||
name: str | ||
serial: str | ||
face: Optional[str] = None | ||
airflow: Optional[str] = None |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from csv import DictReader | ||
from unittest.mock import patch, mock_open | ||
from lib.utils.csv_to_dataclass import separate_data, open_file | ||
from lib.utils.dataclass_data import Device | ||
|
||
|
||
def test_separate_data(): | ||
""" | ||
This test checks that when csv data is inputted the dataclass devices are created properly. | ||
""" | ||
mock_data = [ | ||
"tenant,device_role,manufacturer,device_type,status," | ||
"site,location,rack,face,airflow,position,name,serial", | ||
"t1,dr1,m1,dt1,st1,si1,l1,r1,f1,a1,p1,n1,se1", | ||
"t2,dr2,m2,dt2,st2,si2,l2,r2,f2,a2,p2,n2,se2", | ||
] | ||
mock_reader_obj = DictReader(mock_data) | ||
res = separate_data(mock_reader_obj) | ||
assert res[0] == Device( | ||
tenant="t1", | ||
device_role="dr1", | ||
manufacturer="m1", | ||
device_type="dt1", | ||
status="st1", | ||
site="si1", | ||
location="l1", | ||
rack="r1", | ||
face="f1", | ||
airflow="a1", | ||
position="p1", | ||
name="n1", | ||
serial="se1", | ||
) | ||
assert res[1] == Device( | ||
tenant="t2", | ||
device_role="dr2", | ||
manufacturer="m2", | ||
device_type="dt2", | ||
status="st2", | ||
site="si2", | ||
location="l2", | ||
rack="r2", | ||
face="f2", | ||
airflow="a2", | ||
position="p2", | ||
name="n2", | ||
serial="se2", | ||
) | ||
|
||
|
||
def test_open_file(): | ||
""" | ||
This test ensures the csv file is opened appropriately and the DictReader method is called. | ||
""" | ||
with patch("builtins.open", mock_open(read_data="mock_file_path")) as mock_file: | ||
with patch("csv.DictReader") as mock_dict_reader: | ||
res = open_file("mock_file_path") | ||
mock_file.assert_called_once_with("mock_file_path", encoding="UTF-8") | ||
mock_dict_reader.assert_called_once_with(mock_file.return_value) | ||
assert res == mock_dict_reader.return_value |
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