diff --git a/alfred/Untitled.ipynb b/alfred/Untitled.ipynb deleted file mode 100644 index 38d115c..0000000 --- a/alfred/Untitled.ipynb +++ /dev/null @@ -1,868 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "id": "464d00f1-5f35-44b0-b821-1dd19cea149c", - "metadata": {}, - "outputs": [], - "source": [ - "import etl\n", - "import vis" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "id": "21e2f1e2-149c-48ba-a97a-57f260c2f56d", - "metadata": {}, - "outputs": [ - { - "name": "stdin", - "output_type": "stream", - "text": [ - "Enter the name of the raw data folder: data\n", - "Enter the name of the zip file (include .zip): test_data.zip\n" - ] - } - ], - "source": [ - "etl.unpack_interactive()" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "id": "4dbfd4fa-c5c4-4943-8fc7-ae4091e48a59", - "metadata": { - "jupyter": { - "source_hidden": true - } - }, - "outputs": [], - "source": [ - "\n", - "# tests on alfred.vis\n", - "\n", - "import pandas as pd\n", - "\n", - "directory = 'test_data_after_sorted/'\n", - "\n", - "def test_build_bus_df_1():\n", - " '''\n", - " Tests to determine that the return is a Pandas Dataframe\n", - " '''\n", - " bus_df = build_bus_df(directory, 'bus_0039141908140233500710/', 'Current')\n", - " assert isinstance(bus_df, pd.DataFrame), 'Pandas DataFrame not returned'\n", - " return\n", - "\n", - "def test_build_module_df_1():\n", - " '''\n", - " Tests to determine that the return is a Pandas Dataframe\n", - " '''\n", - " module_df = build_module_df(directory, 'bus_0039141908140233500710/', 1)\n", - " assert isinstance(module_df, pd.DataFrame), 'Pandas DataFrame not returned'\n", - " return\n", - "\n", - "\n", - "directory = 'test_data_after_sorted/'\n", - "\n", - "def test_find_replaced_modules_1():\n", - " '''\n", - " Tests to determine that the returned object is a dictionary\n", - " '''\n", - " replaced_dict = find_replaced_modules(directory)\n", - " assert isinstance(replaced_dict, dict), 'Dictionary not returned'\n", - " return\n", - "\n", - "def test_find_replaced_modules_2():\n", - " '''\n", - " Tests to determine dictionary keys are of type strings.\n", - " '''\n", - " replaced_dict = find_replaced_modules(directory)\n", - " for key in replaced_dict.keys():\n", - " assert isinstance(key, str), 'Bus names are not strings.'\n", - " return\n", - "\n", - "def test_find_replaced_modules_3():\n", - " '''\n", - " Tests to determine that the dictionary values are lists.\n", - " '''\n", - " replaced_dict = find_replaced_modules(directory)\n", - " for value in replaced_dict.values():\n", - " assert isinstance(value, list), 'Returned values are not in a list format.'\n", - " return\n", - "\n", - "def test_find_replaced_modules_4():\n", - " '''\n", - " Tests to determine that the returned dictionary values' contents are of type string\n", - " '''\n", - " replaced_dict = find_replaced_modules(directory)\n", - " for value in replaced_dict.values():\n", - " for module in value:\n", - " assert isinstance(module, str), 'Returned serials are not of type string.'\n", - " return\n", - "\n", - "def test_swapped_mod_dataframes_1():\n", - " '''\n", - " Tests to determine that a list is returned.\n", - " '''\n", - " df_swap = swapped_mod_dataframes(directory, 'g2--..-.-.-.-', 'cell voltages')\n", - " assert isinstance(df_swap, list), \"A list of dataframes is not returned\"\n", - " return\n", - "\n", - "def test_swapped_mod_dataframes_2():\n", - " '''\n", - " Tests to determine that a list is returned.\n", - " '''\n", - " df_swap = swapped_mod_dataframes(directory, 'g2--..-.-.-.-', 'cell voltages')\n", - " for df in df_swap:\n", - " assert isinstance(df, pd.DataFrame), \"Not all contents of list are Pandas Dataframes\"\n", - " return\n", - "\n", - "import numpy as np\n", - "import os\n", - "from os import listdir\n", - "\n", - "directory = 'test_data_after_sorted/'\n", - "\n", - "def test_count_bus_file_1():\n", - " '''\n", - " Test to determine that returned bus folders of test data is 2.\n", - " '''\n", - " bus_count = count_bus_file(directory)\n", - " assert bus_count == 2, \"Number of buses in folder should be 2\"\n", - " return\n", - "\n", - "def test_filter_false_module_1():\n", - " '''\n", - " Test the function should return an array type\n", - " '''\n", - " return_type = filter_false_module(directory)\n", - " assert isinstance(return_type,np.ndarray),'The return type is not an array'\n", - " return\n", - "\n", - "def test_filter_false_module_2():\n", - " '''\n", - " Test the function should return a list type and each element should be string\n", - " '''\n", - " return_list = filter_false_module(directory)\n", - " for element in return_list:\n", - " assert isinstance(element,str),'Each element in this list should be string.'\n", - " return\n", - "\n", - "def test_move_false_bus_1():\n", - " '''\n", - " Test each folder that get relocated should have at least two csv files.\n", - " '''\n", - " for file in listdir(directory):\n", - " if file.startswith('bus'):\n", - " directory_path = os.path.join(directory, file)\n", - " each_bus = listdir(directory_path)\n", - " assert len(each_bus) >= 2, 'Each bus file should contain at least two csv files.'\n", - " else:\n", - " pass\n", - " return\n", - "\n", - "def test_move_false_bus_2():\n", - " '''\n", - " Test each folder that get relocated should have be all csv type files.\n", - " '''\n", - " for file in listdir(directory):\n", - " directory_path = os.path.join(directory, file)\n", - " each_bus = listdir(directory_path)\n", - " for element in each_bus:\n", - " assert element.endswith('.csv'), 'There is a non csv file.'\n", - " return\n", - "\n", - "\n", - "def test_compare_file_mods_1():\n", - " '''\n", - " Tests that function returns a dictionary\n", - " '''\n", - " return_type = compare_file_mods(directory)\n", - " assert isinstance(return_type,dict), 'The returned type is not a dictionary'\n", - " return\n", - "\n", - "\n", - "def test_compare_file_mods_2():\n", - " '''\n", - " Tests that dictionary returns a pandas dataframe when type in attribute name, such as \"bus_1\"\n", - " '''\n", - " test_folder = ['bus_1','bus_2']\n", - " dic = compare_file_mods(directory)\n", - " for folder_name in test_folder:\n", - " dic_key = dic[folder_name]\n", - " assert isinstance(dic_key,pd.DataFrame), 'The returned type is not a pandas dataframe'\n", - " return\n", - "\n", - "\n", - "def test_compare_file_mods_3():\n", - " '''\n", - " Tests that returned dataframe has 16 index, from \"Module 1\" to \"Module 16\"\n", - " '''\n", - " test_folder = ['bus_1', 'bus_2']\n", - " dic = compare_file_mods(directory)\n", - " for folder_name in test_folder:\n", - " dic_key = dic[folder_name]\n", - " index_len = dic_key.index\n", - " assert len(index_len) == 16, 'The dataframe is not in complete form, module number is not 16'\n", - " return" - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "id": "4d347b70-0ac1-41d4-af81-aee72df2005c", - "metadata": { - "collapsed": true, - "jupyter": { - "outputs_hidden": true - } - }, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "\n", - "----------------------------------------------------------------------\n", - "Ran 0 tests in 0.000s\n", - "\n", - "OK\n" - ] - }, - { - "data": { - "text/plain": [ - "" - ] - }, - "execution_count": 13, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "unittest.main(argv=[''], verbosity=2, exit=False)" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "id": "c13730a5-3317-4f35-8abe-e2c8a8580ea9", - "metadata": { - "collapsed": true, - "jupyter": { - "outputs_hidden": true, - "source_hidden": true - } - }, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "F\n", - "======================================================================\n", - "FAIL: test_group_files (__main__.TestGroupFiles.test_group_files)\n", - "----------------------------------------------------------------------\n", - "Traceback (most recent call last):\n", - " File \"/tmp/ipykernel_122037/1270366558.py\", line 44, in test_group_files\n", - " self.assertTrue(os.path.isdir(expected_dir), f\"Expected directory {expected_dir} does not exist.\")\n", - "AssertionError: False is not true : Expected directory /tmp/tmpd0i32ljl/sorted_test_data/all_buses/bus_0039141908140233500710 does not exist.\n", - "\n", - "----------------------------------------------------------------------\n", - "Ran 1 test in 0.005s\n", - "\n", - "FAILED (failures=1)\n" - ] - } - ], - "source": [ - "import os\n", - "import shutil\n", - "import tempfile\n", - "import unittest\n", - "import csv\n", - "from etl import group_files\n", - "\n", - "class TestGroupFiles(unittest.TestCase):\n", - "\n", - " def setUp(self):\n", - " # Create a temporary directory\n", - " self.test_dir = tempfile.mkdtemp()\n", - " self.test_data_dir = os.path.join(self.test_dir, 'sorted_test_data/all_buses')\n", - " os.makedirs(self.test_data_dir)\n", - "\n", - " # Add test CSV file\n", - " self.test_file_name = '14F0154_ProfileData_20171116125343(2).csv'\n", - " self.test_file_path = os.path.join(self.test_data_dir, self.test_file_name)\n", - " \n", - " # Write test data to the CSV file\n", - " serial_number = '0039141908140233500710'\n", - " keyword = 'Mfg Data (ASCII) '\n", - " with open(self.test_file_path, 'w', newline='') as f:\n", - " writer = csv.writer(f)\n", - " # Add a row with the 'Mfg Data (ASCII)' keyword and serial number\n", - " writer.writerow(['Some other data', f'{keyword}{serial_number}'])\n", - " for _ in range(15): # Add enough serial numbers to trigger directory creation\n", - " writer.writerow(['Some other data', f'{keyword}{serial_number}'])\n", - "\n", - " def tearDown(self):\n", - " # Remove the temporary directory after the test\n", - " shutil.rmtree(self.test_dir)\n", - "\n", - " def test_group_files(self):\n", - " # Call the group_files function\n", - " group_files(self.test_data_dir)\n", - "\n", - " # Define the expected directory and file path\n", - " expected_dir_name = 'bus_0039141908140233500710'\n", - " expected_dir = os.path.join(self.test_data_dir, expected_dir_name)\n", - " expected_file = os.path.join(expected_dir, self.test_file_name)\n", - " \n", - " # Check if the expected directory exists\n", - " self.assertTrue(os.path.isdir(expected_dir), f\"Expected directory {expected_dir} does not exist.\")\n", - " \n", - " # Check if the expected file exists in the new directory\n", - " self.assertTrue(os.path.isfile(expected_file), f\"Expected file {expected_file} does not exist in {expected_dir}.\")\n", - "\n", - "if __name__ == '__main__':\n", - " suite = unittest.TestLoader().loadTestsFromTestCase(TestGroupFiles)\n", - " unittest.TextTestRunner().run(suite)\n" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "id": "78c1e87b-ae99-4e52-8dea-52d00e219882", - "metadata": { - "collapsed": true, - "jupyter": { - "outputs_hidden": true, - "source_hidden": true - } - }, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "E\n", - "======================================================================\n", - "ERROR: test_group_files (__main__.TestGroupFiles.test_group_files)\n", - "----------------------------------------------------------------------\n", - "Traceback (most recent call last):\n", - " File \"/home/clgould99/miniconda3/lib/python3.11/site-packages/pkg_resources/__init__.py\", line 402, in get_provider\n", - " module = sys.modules[moduleOrReq]\n", - " ~~~~~~~~~~~^^^^^^^^^^^^^\n", - "KeyError: 'alfred/tests'\n", - "\n", - "During handling of the above exception, another exception occurred:\n", - "\n", - "Traceback (most recent call last):\n", - " File \"/tmp/ipykernel_122037/3865055541.py\", line 18, in setUp\n", - " original_test_data_dir = pkg_resources.resource_filename('alfred/tests', 'unsorted_test_data/all_buses')\n", - " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n", - " File \"/home/clgould99/miniconda3/lib/python3.11/site-packages/pkg_resources/__init__.py\", line 1213, in resource_filename\n", - " return get_provider(package_or_requirement).get_resource_filename(\n", - " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n", - " File \"/home/clgould99/miniconda3/lib/python3.11/site-packages/pkg_resources/__init__.py\", line 404, in get_provider\n", - " __import__(moduleOrReq)\n", - "ModuleNotFoundError: No module named 'alfred/tests'\n", - "\n", - "----------------------------------------------------------------------\n", - "Ran 1 test in 0.003s\n", - "\n", - "FAILED (errors=1)\n" - ] - } - ], - "source": [ - "import os\n", - "import shutil\n", - "import tempfile\n", - "import unittest\n", - "import csv\n", - "import pkg_resources\n", - "from etl import group_files\n", - "\n", - "class TestGroupFiles(unittest.TestCase):\n", - "\n", - " def setUp(self):\n", - " # Create a temporary directory\n", - " self.test_dir = tempfile.mkdtemp()\n", - " self.test_data_dir = os.path.join(self.test_dir, 'all_buses')\n", - " os.makedirs(self.test_data_dir)\n", - "\n", - " # Path to the actual test data in the repository\n", - " original_test_data_dir = pkg_resources.resource_filename('alfred/tests', 'unsorted_test_data/all_buses')\n", - " \n", - " # Copy the test data from the repository to the temporary directory\n", - " for filename in os.listdir(original_test_data_dir):\n", - " shutil.copy(os.path.join(original_test_data_dir, filename), self.test_data_dir)\n", - "\n", - " # Print statements for debugging\n", - " print(f\"Setup: Created test directory at {self.test_data_dir}\")\n", - " print(f\"Setup: Copied test data from {original_test_data_dir}\")\n", - "\n", - " # Print the content of the CSV files in the test directory\n", - " for filename in os.listdir(self.test_data_dir):\n", - " file_path = os.path.join(self.test_data_dir, filename)\n", - " with open(file_path, 'r') as f:\n", - " content = f.readlines()\n", - " print(f\"Setup: Content of the file {filename}:\\n{''.join(content)}\")\n", - "\n", - " def tearDown(self):\n", - " # Print statements for debugging\n", - " print(f\"TearDown: Removing test directory {self.test_dir}\")\n", - "\n", - " # Remove the temporary directory after the test\n", - " shutil.rmtree(self.test_dir)\n", - "\n", - " def test_group_files(self):\n", - " # Call the group_files function\n", - " group_files(self.test_data_dir)\n", - "\n", - " # Define the expected directory and file path\n", - " expected_dir_name = 'bus_0039141908140233500710'\n", - " expected_dir = os.path.join(self.test_data_dir, expected_dir_name)\n", - " expected_file = os.path.join(expected_dir, '14F0154_ProfileData_20171116125343(2).csv')\n", - " \n", - " # Print statements for debugging\n", - " print(f\"Test: Expected directory: {expected_dir}\")\n", - " print(f\"Test: Expected file: {expected_file}\")\n", - "\n", - " # Check if the expected directory exists\n", - " dir_exists = os.path.isdir(expected_dir)\n", - " print(f\"Test: Directory exists: {dir_exists}\")\n", - " self.assertTrue(dir_exists, f\"Expected directory {expected_dir} does not exist.\")\n", - " \n", - " # Check if the expected file exists in the new directory\n", - " file_exists = os.path.isfile(expected_file)\n", - " print(f\"Test: File exists: {file_exists}\")\n", - " self.assertTrue(file_exists, f\"Expected file {expected_file} does not exist in {expected_dir}\")\n", - "\n", - "if __name__ == '__main__':\n", - " suite = unittest.TestLoader().loadTestsFromTestCase(TestGroupFiles)\n", - " unittest.TextTestRunner().run(suite)\n" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "id": "d229361f-d2cd-4d49-83f5-c02d6f94e14f", - "metadata": {}, - "outputs": [], - "source": [ - "sorted_directory = 'alfred/sorted_test_data/'\n", - "unsorted_directory = 'alfred/unsorted_test_data/'\n", - "raw_directory = 'alfred/unzipped_test_data/'" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "id": "ede1fbe7-ba8d-4ff8-bc6e-037eef737192", - "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "E\n", - "======================================================================\n", - "ERROR: test_build_bus_df_1 (__main__.TestVis1.test_build_bus_df_1)\n", - "Tests to determine that the return is a Pandas Dataframe\n", - "----------------------------------------------------------------------\n", - "Traceback (most recent call last):\n", - " File \"/tmp/ipykernel_24734/655553593.py\", line 14, in test_build_bus_df_1\n", - " bus_df = build_bus_df(directory, 'bus_0039141908140233500710/', 'Current')\n", - " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n", - " File \"/home/clgould99/TheBattmobile/Alfred/alfred/vis.py\", line 84, in build_bus_df\n", - " bus_dates = sort_bus_by_date(directory, bus_num)\n", - " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n", - " File \"/home/clgould99/TheBattmobile/Alfred/alfred/etl.py\", line 196, in sort_bus_by_date\n", - " bus_directory = directory + bus_num\n", - " ~~~~~~~~~~^~~~~~~~~\n", - "TypeError: unsupported operand type(s) for +: 'TestVis1' and 'str'\n", - "\n", - "----------------------------------------------------------------------\n", - "Ran 1 test in 0.003s\n", - "\n", - "FAILED (errors=1)\n" - ] - } - ], - "source": [ - "import unittest\n", - "import os\n", - "import tempfile\n", - "import zipfile\n", - "import etl\n", - "import vis\n", - "from vis import build_bus_df\n", - "\n", - "class TestVis1(unittest.TestCase):\n", - " def test_build_bus_df_1(directory):\n", - " '''\n", - " Tests to determine that the return is a Pandas Dataframe\n", - " '''\n", - " bus_df = build_bus_df(directory, 'bus_0039141908140233500710/', 'Current')\n", - " assert isinstance(bus_df, pd.DataFrame), 'Pandas DataFrame not returned'\n", - " return\n", - "if __name__ == '__main__':\n", - " suite = unittest.TestLoader().loadTestsFromTestCase(TestVis1)\n", - " unittest.TextTestRunner().run(suite)" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "id": "d4530564-83e4-4326-b645-58ea3a589832", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'/home/clgould99/TheBattmobile/Alfred/alfred'" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "import os\n", - "\n", - "os. getcwd()\n" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "id": "a0eb43f5-8c7d-4e23-91f6-bfd1834b57c4", - "metadata": {}, - "outputs": [], - "source": [ - "import vis\n", - "import etl" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "id": "09c1c7ba-a578-41f3-a99d-7c3a94f10856", - "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
\n", - "\n", - "\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "
< 450.0450460470480490500510520530...670680690700710720730740>= 750.0TOTAL
00000002033439...301690194716106311181511063400011730296
127000002033440...305173196624107354183991099501711912854
227000002070646...460645297335153481236501385501717233181
\n", - "

3 rows × 33 columns

\n", - "
" - ], - "text/plain": [ - " < 450.0 450 460 470 480 490 500 510 520 530 ... 670 680 \\\n", - "0 0 0 0 0 0 0 2 0 33 439 ... 301690 194716 \n", - "1 27 0 0 0 0 0 2 0 33 440 ... 305173 196624 \n", - "2 27 0 0 0 0 0 2 0 70 646 ... 460645 297335 \n", - "\n", - " 690 700 710 720 730 740 >= 750.0 TOTAL \n", - "0 106311 18151 1063 4 0 0 0 11730296 \n", - "1 107354 18399 1099 5 0 1 7 11912854 \n", - "2 153481 23650 1385 5 0 1 7 17233181 \n", - "\n", - "[3 rows x 33 columns]" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "directory = etl.find_directory() + 'sorted_test_data/vis_buses/'\n", - "\n", - "vis.build_bus_df(directory, 'bus_0039142903b40233500710/', 'Voltage')" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "id": "0af67a2c-1a31-4f17-aa11-1355b6b78322", - "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "...\n", - "----------------------------------------------------------------------\n", - "Ran 3 tests in 0.106s\n", - "\n", - "OK\n" - ] - } - ], - "source": [ - "import unittest\n", - "import pandas as pd\n", - "import os\n", - "import etl\n", - "from vis import build_bus_df\n", - "# Assuming build_bus_df is imported from the module where it is defined\n", - "# from your_module import build_bus_df\n", - "\n", - "class TestBuildBusDF(unittest.TestCase):\n", - " def setUp(self):\n", - " # Setup code here if needed\n", - " self.valid_directory = os.path.join(etl.find_directory(), 'sorted_test_data/vis_buses/')\n", - " self.bus_num = \"bus_0039142903b40233500710/\" \n", - " #self.valid_keywords = [\"Current\", \"Voltage\", \"Power\"]\n", - "\n", - " def test_dataframe_creation_current(self):\n", - " # Test to ensure a DataFrame is created for 'Current'\n", - " keyword = \"Current\"\n", - " result = build_bus_df(self.valid_directory, self.bus_num, keyword)\n", - " self.assertIsInstance(result, pd.DataFrame, \"The result should be a pandas DataFrame for 'Current'\")\n", - "\n", - " def test_dataframe_creation_voltage(self):\n", - " # Test to ensure a DataFrame is created for 'Voltage'\n", - " keyword = \"Voltage\"\n", - " result = build_bus_df(self.valid_directory, self.bus_num, keyword)\n", - " self.assertIsInstance(result, pd.DataFrame, \"The result should be a pandas DataFrame for 'Voltage'\")\n", - "\n", - " def test_dataframe_creation_power(self):\n", - " # Test to ensure a DataFrame is created for 'Power'\n", - " keyword = \"Power\"\n", - " result = build_bus_df(self.valid_directory, self.bus_num, keyword)\n", - " self.assertIsInstance(result, pd.DataFrame, \"The result should be a pandas DataFrame for 'Power'\")\n", - "\n", - "\n", - " def tearDown(self):\n", - " # Clean up code here if needed\n", - " pass\n", - "\n", - "if __name__ == '__main__':\n", - " suite = unittest.TestLoader().loadTestsFromTestCase(TestBuildBusDF)\n", - " unittest.TextTestRunner().run(suite)\n", - "\n" - ] - }, - { - "cell_type": "code", - "execution_count": 24, - "id": "e29fb6a9-20ae-4ab0-bb84-68f32e31cd01", - "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "...\n", - "----------------------------------------------------------------------\n", - "Ran 3 tests in 0.539s\n", - "\n", - "OK\n" - ] - } - ], - "source": [ - "import unittest\n", - "import pandas as pd\n", - "import os\n", - "import etl\n", - "from vis import build_module_df\n", - "#import alfred\n", - "# Assuming build_module_df is imported from the module where it is defined\n", - "# from build_data_vis import build_module_df\n", - "\n", - "class TestBuildModuleDF(unittest.TestCase):\n", - " def setUp(self):\n", - " # Setup code here if needed\n", - " self.valid_directory = os.path.join(etl.find_directory(), 'sorted_test_data/vis_buses/')\n", - " self.invalid_directory = os.path.join('alfred/sorted_test_data/sorted_data/incomplete_test/')\n", - " self.bus_num = \"bus_0039142903b40233500710/\" \n", - " def test_dataframe_creation_for_valid_numbers(self):\n", - " # Test to ensure a DataFrame is created for each valid number from 1 to 16\n", - " for num in range(1, 17):\n", - " with self.subTest(num=num):\n", - " result = build_module_df(self.valid_directory, self.bus_num, num)\n", - " self.assertIsInstance(result, pd.DataFrame, f\"The result should be a pandas DataFrame for module number {num}\")\n", - "\n", - " def test_no_dataframe_creation_for_invalid_data(self):\n", - " # Test to ensure no DataFrame is created for invalid/incomplete data\n", - " num = 1\n", - " try:\n", - " result = build_module_df(self.invalid_directory, self.bus_num, num)\n", - " # Check if the function returns None or handles the case gracefully\n", - " self.assertIsNone(result, \"The result should be None for invalid/incomplete data\")\n", - " except Exception as e:\n", - " # If an exception is raised, ensure it's the expected behavior\n", - " self.assertIsInstance(e, (FileNotFoundError, pd.errors.EmptyDataError), \"Expected an error due to invalid data\")\n", - " def test_invalid_number_input(self):\n", - " # Test to ensure no DataFrame is created for invalid module number\n", - " invalid_numbers = [ 17, -1, 'a']\n", - " for num in invalid_numbers:\n", - " with self.subTest(num=num):\n", - " with self.assertRaises((ValueError, TypeError), msg=f\"Expected a ValueError or TypeError for invalid module number {num}\"):\n", - " build_module_df(self.valid_directory, self.bus_num, num)\n", - "\n", - " def tearDown(self):\n", - " # Clean up code here if needed\n", - " pass\n", - "\n", - "if __name__ == '__main__':\n", - " suite = unittest.TestLoader().loadTestsFromTestCase(TestBuildModuleDF)\n", - " unittest.TextTestRunner().run(suite)\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "195849ce-3695-41d2-9af2-25b1d075cf03", - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.11.5" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -}