Repository: fga-eps-mds/2022-2-ez-docs (language: python)

Issue:
Create method and test to filter format
Create a method and test cases to filter format cases on data cleaning module. The module will need to verify the following formats:

- .csv;
- .json; 
- .xml; 
- .xlsx;
- .html.


Discussion / hints:

Fix patch:
diff --git a/src/ez_docs/modules/data_cleaning.py b/src/ez_docs/modules/data_cleaning.py
index 6f514fa..6c59cbd 100644
--- a/src/ez_docs/modules/data_cleaning.py
+++ b/src/ez_docs/modules/data_cleaning.py
@@ -21,26 +21,6 @@ def find_delimiter(location: str) -> str:
     delimiters_dict = sorted(delimiters_dict.items(), key=lambda item: item[1])
     return delimiters_dict[0][0]
 
-# Returns a pandas DataFrame according to an extension
- def filter_format(location: str) -> pd.DataFrame:
-     extension = location.split(".")[-1]
-     if extension == "csv":
-         return pd.read_csv(location, delimiter=find_delimiter(location))
-     elif extension == "html":
-         return pd.read_html(location)
-     elif extension == "json":
-         return pd.read_json(location)
-     elif extension == "xlsx":
-         return pd.read_excel(location)
-     elif extension == "xml":
-         return pd.read_xml(location)
-     else:
-         raise Exception(
-                 f"""
-                 \033[0;31mThe extension '{extension}' is not accepted.\n
-                 Valid: csv, html, json, xlsx, xml.\033[0m
-                 """
-             )
 
 # Converts the data_set to the format specified in the architecture
 def filter_data(location: str, constraint: str = "") -> list:
@@ -71,3 +51,18 @@ def filter_data(location: str, constraint: str = "") -> list:
             """
         )
 
+def filter_format(file_path):
+    supported_extensions = {
+        'csv': pd.read_csv,
+        'xlm': pd.read_xml,
+        'json': pd.read_json,
+        'html': pd.read_html,
+        'xlsx': pd.read_excel
+    }
+    
+    extension = file_path.split('.')[-1]
+    
+    if extension in supported_extensions:
+        return supported_extensions[extension](file_path)
+    else:
+        raise ValueError("The extension", '{extension}', "is not accepted.\n Valid: csv, html, json, xlsx, xml.")


Test patch:
diff --git a/test/test_dataCleaning.py b/test/test_dataCleaning.py
index bc8b3ef..294c2a3 100644
--- a/test/test_dataCleaning.py
+++ b/test/test_dataCleaning.py
@@ -34,32 +34,24 @@ def test_data_cleaning():
 def test_data_cleaning_error():
     assert filter_data("test/example.csv") != final_data2
 
-
-def test_filter_format():
-     # Test with a CSV file
-     with tempfile.NamedTemporaryFile(
-         mode='w',
-         delete=False,
-         suffix='.csv'
-     ) as temp:
-
-         temp.write("col1,col2\n1,2\n3,4")
-         temp.seek(0)
-         result = filter_format(temp.name)
-         assert isinstance(result, pd.DataFrame)
-
-     # Test with a JSON file
-     with tempfile.NamedTemporaryFile(
-         mode='w',
-         delete=False,
-         suffix='.json'
-     ) as temp:
-         temp.write('{"col1": [1, 3], "col2": [2, 4]}')
-         temp.seek(0)
-         result = filter_format(temp.name)
-         assert isinstance(result, pd.DataFrame)
-
-     # Test with an invalid extension
-     location = "path/to/invalid/file.invalid"
-     with pytest.raises(Exception):
-         filter_format(location)
+def test_filter_format_csv():
+    file_path = 'test/data.csv'
+    expected_df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
+    
+    result_df = filter_format(file_path)
+    
+    assert result_df.equals(expected_df)
+
+def test_filter_format_json():
+    file_path = 'test/data.json'
+    expected_df = pd.DataFrame({'C': [7, 8, 9], 'D': [10, 11, 12]})
+        
+    result_df = filter_format(file_path)
+        
+    assert result_df.equals(expected_df)
+
+def test_filter_format_invalid():
+    file_path = 'data.txt'
+    
+    with pytest.raises(ValueError):
+        filter_format(file_path)