From 2fa684180567e650f06385e6b4fe2063c18a3bdf Mon Sep 17 00:00:00 2001 From: Julien Marrec Date: Tue, 16 Apr 2024 10:37:36 +0200 Subject: [PATCH] Fix #5147 - Adjust templates * make it clear that if you use runner.lastEnergyPlusSQLFile you need to reopen it potentially * Use a chdir context --- .../tests/energyplus_measure_test.rb | 3 +- .../ModelMeasure/tests/model_measure_test.rb | 5 +- .../tests/reporting_measure_test.rb | 57 +++++++++++-------- .../tests/test_reporting_measure.py | 37 +++++++----- 4 files changed, 62 insertions(+), 40 deletions(-) diff --git a/src/utilities/bcl/templates/EnergyPlusMeasure/tests/energyplus_measure_test.rb b/src/utilities/bcl/templates/EnergyPlusMeasure/tests/energyplus_measure_test.rb index b486a8bc82b..92144342d0d 100644 --- a/src/utilities/bcl/templates/EnergyPlusMeasure/tests/energyplus_measure_test.rb +++ b/src/utilities/bcl/templates/EnergyPlusMeasure/tests/energyplus_measure_test.rb @@ -3,9 +3,10 @@ require 'openstudio' require 'openstudio/measure/ShowRunnerOutput' require 'minitest/autorun' -require_relative '../measure.rb' require 'fileutils' +require_relative '../measure' + class EnergyPlusMeasureNameTest < Minitest::Test # def setup # end diff --git a/src/utilities/bcl/templates/ModelMeasure/tests/model_measure_test.rb b/src/utilities/bcl/templates/ModelMeasure/tests/model_measure_test.rb index 101f8dcb5ea..e1713f86c2e 100644 --- a/src/utilities/bcl/templates/ModelMeasure/tests/model_measure_test.rb +++ b/src/utilities/bcl/templates/ModelMeasure/tests/model_measure_test.rb @@ -3,9 +3,10 @@ require 'openstudio' require 'openstudio/measure/ShowRunnerOutput' require 'minitest/autorun' -require_relative '../measure.rb' require 'fileutils' +require_relative '../measure' + class ModelMeasureNameTest < Minitest::Test # def setup # end @@ -118,7 +119,7 @@ def test_good_argument_values assert_equal(1, model.getSpaces.size - num_spaces_seed) # save the model to test output directory - output_file_path = "#{File.dirname(__FILE__)}//output/test_output.osm" + output_file_path = "#{File.dirname(__FILE__)}/output/test_output.osm" model.save(output_file_path, true) end end diff --git a/src/utilities/bcl/templates/ReportingMeasure/tests/reporting_measure_test.rb b/src/utilities/bcl/templates/ReportingMeasure/tests/reporting_measure_test.rb index be337bd414d..63b9f2c1eec 100644 --- a/src/utilities/bcl/templates/ReportingMeasure/tests/reporting_measure_test.rb +++ b/src/utilities/bcl/templates/ReportingMeasure/tests/reporting_measure_test.rb @@ -3,9 +3,10 @@ require 'openstudio' require 'openstudio/measure/ShowRunnerOutput' require 'minitest/autorun' -require_relative '../measure.rb' require 'fileutils' +require_relative '../measure' + class ReportingMeasureNameTest < Minitest::Test def model_in_path_default return "#{File.dirname(__FILE__)}/example_model.osm" @@ -142,20 +143,23 @@ def test_with_drybulb_temp assert(!File.exist?(report_path(test_name))) # temporarily change directory to the run directory and run the measure - start_dir = Dir.pwd - begin - Dir.chdir(run_dir(test_name)) - - # run the measure + Dir.chdir(run_dir(test_name)) do measure.run(runner, argument_map) - result = runner.result - show_output(result) - assert_equal('Success', result.value.valueName) - assert(result.warnings.empty?) - ensure - Dir.chdir(start_dir) end + result = runner.result + show_output(result) + assert_equal('Success', result.value.valueName) + assert(result.warnings.empty?) + + sqlFile = runner.lastEnergyPlusSqlFile.get + if !sqlFile.connectionOpen + sqlFile.reopen + end + hours = sqlFile.hoursSimulated + refute_empty(hours) + assert_equal(8760.0, hours.get) + # make sure the report file exists assert(File.exist?(report_path(test_name))) end @@ -189,7 +193,8 @@ def test_without_drybulb_temp idf_output_requests = measure.energyPlusOutputRequests(runner, argument_map) assert_equal(0, idf_output_requests.size) - # mimic the process of running this measure in OS App or PAT. Optionally set custom model_in_path and custom epw_path. + # mimic the process of running this measure in OS App or PAT. + # Optionally set custom model_in_path and custom epw_path. epw_path = epw_path_default setup_test(test_name, idf_output_requests) @@ -209,20 +214,24 @@ def test_without_drybulb_temp assert(!File.exist?(report_path(test_name))) # temporarily change directory to the run directory and run the measure - start_dir = Dir.pwd - begin - Dir.chdir(run_dir(test_name)) - - # run the measure + Dir.chdir(run_dir(test_name)) do measure.run(runner, argument_map) - result = runner.result - show_output(result) - assert_equal('Success', result.value.valueName) - assert(result.warnings.empty?) - ensure - Dir.chdir(start_dir) end + result = runner.result + show_output(result) + assert_equal('Success', result.value.valueName) + assert(result.warnings.empty?) + + # sqlFile = OpenStudio::SqlFile.new(OpenStudio::Path.new(sql_path(test_name))) + sqlFile = runner.lastEnergyPlusSqlFile.get + if !sqlFile.connectionOpen + sqlFile.reopen + end + hours = sqlFile.hoursSimulated + refute_empty(hours) + assert_equal(8760.0, hours.get) + # make sure the report file exists assert(File.exist?(report_path(test_name))) end diff --git a/src/utilities/bcl/templates/ReportingMeasure/tests/test_reporting_measure.py b/src/utilities/bcl/templates/ReportingMeasure/tests/test_reporting_measure.py index 1cde9d9ed0c..ddea7702251 100644 --- a/src/utilities/bcl/templates/ReportingMeasure/tests/test_reporting_measure.py +++ b/src/utilities/bcl/templates/ReportingMeasure/tests/test_reporting_measure.py @@ -6,7 +6,6 @@ import openstudio import pytest - from measure import ReportingMeasureName CURRENT_DIR_PATH = Path(__file__).parent.absolute() @@ -151,18 +150,24 @@ def test_with_drybulb_temp(self): # temporarily change directory to the run directory and run the measure start_dir = Path.cwd() - # try: os.chdir(TestReportingMeasureName.run_dir(test_name)) + try: + # run the measure + measure.run(runner, argument_map) + finally: + os.chdir(start_dir) - # run the measure - measure.run(runner, argument_map) result = runner.result() print(result) assert result.value().valueName() == "Success" assert len(result.warnings()) == 0 - os.chdir(start_dir) - # except: - # os.chdir(start_dir) + + sqlFile = runner.lastEnergyPlusSqlFile().get() + if not sqlFile.connectionOpen(): + sqlFile.reopen() + hours = sqlFile.hoursSimulated() + assert hours.is_initialized() + assert hours.get() == 8760.0 # make sure the report file exists assert report_path.exists() @@ -220,18 +225,24 @@ def test_without_drybulb_temp(self): # temporarily change directory to the run directory and run the measure start_dir = Path.cwd() - # try: os.chdir(TestReportingMeasureName.run_dir(test_name)) + try: + # run the measure + measure.run(runner, argument_map) + finally: + os.chdir(start_dir) - # run the measure - measure.run(runner, argument_map) result = runner.result() print(result) assert result.value().valueName() == "Success" assert len(result.warnings()) == 0 - os.chdir(start_dir) - # except: - # os.chdir(start_dir) + + sqlFile = runner.lastEnergyPlusSqlFile().get() + if not sqlFile.connectionOpen(): + sqlFile.reopen() + hours = sqlFile.hoursSimulated() + assert hours.is_initialized() + assert hours.get() == 8760.0 # make sure the report file exists assert report_path.exists()