|
| 1 | +import unittest |
| 2 | +from unittest.mock import mock_open, patch, MagicMock |
| 3 | +import xml.etree.ElementTree as ET |
| 4 | + |
| 5 | +from FORCE.src.heron import create_componentsets_in_HERON |
| 6 | + |
| 7 | +class TestCreateComponentSetsInHERON(unittest.TestCase): |
| 8 | + |
| 9 | + def setUp(self): |
| 10 | + # Example of a minimal XML structure |
| 11 | + self.heron_xml = """<HERON> |
| 12 | + <Components> |
| 13 | + <Component name="ExistingComponent"> |
| 14 | + <economics> |
| 15 | + <CashFlow name="ExistingComponent_capex"> |
| 16 | + <fixed_value>100</fixed_value> |
| 17 | + </CashFlow> |
| 18 | + </economics> |
| 19 | + </Component> |
| 20 | + </Components> |
| 21 | + </HERON>""" |
| 22 | + self.tree = ET.ElementTree(ET.fromstring(self.heron_xml)) |
| 23 | + |
| 24 | + @patch('xml.etree.ElementTree.parse') |
| 25 | + @patch('os.listdir') |
| 26 | + @patch('builtins.open', |
| 27 | + new_callable=mock_open, |
| 28 | + read_data="""{ |
| 29 | + "Component Set Name": "NewComponent", |
| 30 | + "Reference Driver": 1000, |
| 31 | + "Reference Driver Power Units": "kW", |
| 32 | + "Reference Price (USD)": 2000, |
| 33 | + "Scaling Factor": 0.5 |
| 34 | + }""") |
| 35 | + def test_new_component_creation(self, mock_file, mock_listdir, mock_parse): |
| 36 | + # Setup the mock to return an XML tree |
| 37 | + mock_parse.return_value = self.tree |
| 38 | + mock_listdir.return_value = ['componentSet1.json'] |
| 39 | + |
| 40 | + # Call the function |
| 41 | + result_tree = create_componentsets_in_HERON("/fake/folder", "/fake/heron_input.xml") |
| 42 | + |
| 43 | + # Verify the XML was updated correctly |
| 44 | + components = result_tree.findall('.//Component[@name="NewComponent"]') |
| 45 | + self.assertEqual(len(components), 1) |
| 46 | + economics = components[0].find('economics') |
| 47 | + self.assertIsNotNone(economics) |
| 48 | + |
| 49 | + # Verify the CashFlow node was created |
| 50 | + cashflows = economics.findall('CashFlow') |
| 51 | + self.assertEqual(len(cashflows), 1) |
| 52 | + self.assertEqual(cashflows[0].attrib['name'], 'NewComponent_capex') |
| 53 | + |
| 54 | + # Verify the reference driver and price updates |
| 55 | + ref_driver = cashflows[0].find('./reference_driver/fixed_value') |
| 56 | + self.assertEqual(ref_driver.text, '1.0') # The driver should have been converted from kW to MW |
| 57 | + |
| 58 | + # Add more tests here to cover other conditions and edge cases |
| 59 | + |
| 60 | +if __name__ == '__main__': |
| 61 | + unittest.main() |
0 commit comments