Skip to content

Commit

Permalink
Merge pull request #11 from dylanjm/heron-unit-tests
Browse files Browse the repository at this point in the history
Add Heron unit tests
  • Loading branch information
PaulTalbot-INL authored May 2, 2024
2 parents 1f705e0 + ec97108 commit 8f5afa0
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/heron.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def create_componentsets_in_HERON(comp_sets_folder, heron_input_xml):
heron_comp_list = [] # The list of compoenents
for comp in component:
heron_comp_list.append(comp.attrib["name"]) # The list of components found in the HERON input XML file
print(f" \n The following components are already in the HERON input XML file:'{heron_comp_list}'")
# print(f" \n The following components are already in the HERON input XML file:'{heron_comp_list}'")

comp_set_files_list = os.listdir(comp_sets_folder)

Expand All @@ -67,11 +67,11 @@ def create_componentsets_in_HERON(comp_sets_folder, heron_input_xml):
scaling_factor = comp_set_dict.get('Scaling Factor')

fit_error = comp_set_dict.get('Fitting Average Error (%)')
print(f" \n The FORCE component set '{comp_set_name}' is found")
# print(f" \n The FORCE component set '{comp_set_name}' is found")

# if the component is already in the HERON file, it gets updated
if comp_set_name in heron_comp_list:
print(f"The component set {comp_set_name} already exists in the HERON XML input file. The {comp_set_name} info will be updated")
#print(f"The component set {comp_set_name} already exists in the HERON XML input file. The {comp_set_name} info will be updated")
for component in components_list:
for comp in component:
if comp.attrib["name"] == comp_set_name:
Expand All @@ -80,7 +80,7 @@ def create_componentsets_in_HERON(comp_sets_folder, heron_input_xml):
if node.tag == "economics":
ECO_NODE_FOUND = "True"
print(f"The 'economics' node is found in the component {comp.attrib['name']} and will be updated.")

for subnode in node:
# If the cashflow node is found
if subnode.tag == "CashFlow":
Expand Down Expand Up @@ -127,7 +127,7 @@ def create_componentsets_in_HERON(comp_sets_folder, heron_input_xml):

else:
# if the component is not already in the HERON file, it is created.
print(f"The component set '{comp_set_name}' is not found in the HERON XMl input file. The componnent node '{comp_set_name}' will be created")
# print(f"The component set '{comp_set_name}' is not found in the HERON XMl input file. The componnent node '{comp_set_name}' will be created")
comp_name_dict = {'name': comp_set_name}
for components in components_list:
new_comp_node = ET.SubElement(components, "Component", comp_name_dict)
Expand Down
Empty file added tests/__init__.py
Empty file.
Empty file added tests/unit_tests/__init__.py
Empty file.
61 changes: 61 additions & 0 deletions tests/unit_tests/test_heron.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import unittest
from unittest.mock import mock_open, patch, MagicMock
import xml.etree.ElementTree as ET

from FORCE.src.heron import create_componentsets_in_HERON

class TestCreateComponentSetsInHERON(unittest.TestCase):

def setUp(self):
# Example of a minimal XML structure
self.heron_xml = """<HERON>
<Components>
<Component name="ExistingComponent">
<economics>
<CashFlow name="ExistingComponent_capex">
<fixed_value>100</fixed_value>
</CashFlow>
</economics>
</Component>
</Components>
</HERON>"""
self.tree = ET.ElementTree(ET.fromstring(self.heron_xml))

@patch('xml.etree.ElementTree.parse')
@patch('os.listdir')
@patch('builtins.open',
new_callable=mock_open,
read_data="""{
"Component Set Name": "NewComponent",
"Reference Driver": 1000,
"Reference Driver Power Units": "kW",
"Reference Price (USD)": 2000,
"Scaling Factor": 0.5
}""")
def test_new_component_creation(self, mock_file, mock_listdir, mock_parse):
# Setup the mock to return an XML tree
mock_parse.return_value = self.tree
mock_listdir.return_value = ['componentSet1.json']

# Call the function
result_tree = create_componentsets_in_HERON("/fake/folder", "/fake/heron_input.xml")

# Verify the XML was updated correctly
components = result_tree.findall('.//Component[@name="NewComponent"]')
self.assertEqual(len(components), 1)
economics = components[0].find('economics')
self.assertIsNotNone(economics)

# Verify the CashFlow node was created
cashflows = economics.findall('CashFlow')
self.assertEqual(len(cashflows), 1)
self.assertEqual(cashflows[0].attrib['name'], 'NewComponent_capex')

# Verify the reference driver and price updates
ref_driver = cashflows[0].find('./reference_driver/fixed_value')
self.assertEqual(ref_driver.text, '1.0') # The driver should have been converted from kW to MW

# Add more tests here to cover other conditions and edge cases

if __name__ == '__main__':
unittest.main()

0 comments on commit 8f5afa0

Please sign in to comment.