Skip to content

Commit

Permalink
Add a test for an outdated reporting measure taking no model arg + re…
Browse files Browse the repository at this point in the history
…organize test folder so that example_model.osm is shared
  • Loading branch information
jmarrec committed Nov 15, 2023
1 parent f255bbb commit c0c5ae7
Show file tree
Hide file tree
Showing 12 changed files with 329 additions and 86 deletions.
File renamed without changes.
89 changes: 89 additions & 0 deletions resources/workflow/measures/OutdatedReportingMeasure/measure.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# start the measure
class OutdatedReportingMeasure < OpenStudio::Ruleset::ReportingUserScript
# human readable name
def name
# Measure name should be the title case of the class name.
return 'Outdated Reporting Measure'
end

# human readable description
def description
return 'ReportingMeasure is an old ReportingUserScript'
end

# human readable description of modeling approach
def modeler_description
return "The ReportingMeasure Measure derives the deprecated ReportingUserScript class, and arguments doesn't take model"
end

# define the arguments that the user will input
def arguments
args = OpenStudio::Measure::OSArgumentVector.new

# bool argument to report report_drybulb_temp
report_drybulb_temp = OpenStudio::Measure::OSArgument.makeBoolArgument('report_drybulb_temp', true)
report_drybulb_temp.setDisplayName('Add output variables for Drybulb Temperature')
report_drybulb_temp.setDescription('Will add drybulb temp and report min/mix value in html.')
report_drybulb_temp.setValue(true)
args << report_drybulb_temp

return args
end

# define the outputs that the measure will create
def outputs
outs = OpenStudio::Measure::OSOutputVector.new

# this measure does not produce machine readable outputs with registerValue, return an empty list

return outs
end

# return a vector of IdfObject's to request EnergyPlus objects needed by the run method
# Warning: Do not change the name of this method to be snake_case. The method must be lowerCamelCase.
def energyPlusOutputRequests(runner, user_arguments)
super(runner, user_arguments) # Do **NOT** remove this line

result = OpenStudio::IdfObjectVector.new

# use the built-in error checking
unless runner.validateUserArguments(arguments, user_arguments)
return result
end

if runner.getBoolArgumentValue('report_drybulb_temp', user_arguments)
request = OpenStudio::IdfObject.load('Output:Variable,,Site Outdoor Air Drybulb Temperature,Hourly;').get
result << request
end

return result
end

# define what happens when the measure is run
def run(runner, user_arguments)
super(runner, user_arguments)

# get the last model and sql file
model = runner.lastOpenStudioModel
if model.empty?
runner.registerError('Cannot find last model.')
return false
end
model = model.get

# use the built-in error checking (need model)
if !runner.validateUserArguments(arguments, user_arguments)
return false
end

# get measure arguments
report_drybulb_temp = runner.getBoolArgumentValue('report_drybulb_temp', user_arguments)

runner.registerValue("report_drybulb_temp", report_drybulb_temp)

return true
end
end

# register the measure to be used by the application
OutdatedReportingMeasure.new.registerWithApplication
63 changes: 63 additions & 0 deletions resources/workflow/measures/OutdatedReportingMeasure/measure.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?xml version="1.0"?>
<measure>
<schema_version>3.1</schema_version>
<name>outdated_reporting_measure</name>
<uid>681fa83e-57e1-439b-8bef-d3d234ac6f68</uid>
<version_id>ed691a5a-2114-4635-b462-cde7102e0df1</version_id>
<version_modified>2023-11-15T20:56:44Z</version_modified>
<xml_checksum>C3203FCF</xml_checksum>
<class_name>OutdatedReportingMeasure</class_name>
<display_name>Outdated Reporting Measure</display_name>
<description>ReportingMeasure is an old ReportingUserScript</description>
<modeler_description>The ReportingMeasure Measure derives the deprecated ReportingUserScript class, and arguments doesn't take model</modeler_description>
<arguments>
<argument>
<name>report_drybulb_temp</name>
<display_name>Add output variables for Drybulb Temperature</display_name>
<description>Will add drybulb temp and report min/mix value in html.</description>
<type>Boolean</type>
<required>true</required>
<model_dependent>false</model_dependent>
<choices>
<choice>
<value>true</value>
<display_name>true</display_name>
</choice>
<choice>
<value>false</value>
<display_name>false</display_name>
</choice>
</choices>
</argument>
</arguments>
<outputs />
<provenances />
<tags>
<tag>Envelope.Form</tag>
</tags>
<attributes>
<attribute>
<name>Measure Type</name>
<value>ReportingMeasure</value>
<datatype>string</datatype>
</attribute>
<attribute>
<name>Measure Language</name>
<value>Ruby</value>
<datatype>string</datatype>
</attribute>
</attributes>
<files>
<file>
<version>
<software_program>OpenStudio</software_program>
<identifier>3.7.0</identifier>
<min_compatible>3.7.0</min_compatible>
</version>
<filename>measure.rb</filename>
<filetype>rb</filetype>
<usage_type>script</usage_type>
<checksum>11D2080F</checksum>
</file>
</files>
</measure>
12 changes: 6 additions & 6 deletions resources/workflow/measures/new_measure.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
$os_build/Products/openstudio measure new \
--class-name ModelMeasureRegistersError \
--type ModelMeasure \
--class-name OutdatedReportingMeasure \
--type ReportingMeasure \
--language Ruby \
--name "Model Measure Registers Error" \
--description "Model Measure Registers Error" \
--modeler-description "The Model Measure, in the run method does runner.registerError" \
--taxonomy-tag "Envelope.Form" ./ModelMeasureRegistersError
--name "Outdated Reporting Measure" \
--description "ReportingMeasure is an old ReportingUserScript" \
--modeler-description "The ReportingMeasure Measure derives the deprecated ReportingUserScript class, and arguments doesn't take model" \
--taxonomy-tag "Envelope.Form" ./OutdatedReportingMeasure
11 changes: 11 additions & 0 deletions resources/workflow/outdated_measures/outdatedreportingmeasure.osw
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"weather_file": "../../Examples/compact_osw/files/srrl_2013_amy.epw",
"seed_file": "../example_model.osm",
"measure_paths": ["../measures/"],
"steps": [
{
"measure_dir_name": "OutdatedReportingMeasure",
"arguments": {}
}
]
}
2 changes: 1 addition & 1 deletion resources/workflow/runner_errors/modelmeasure.osw
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"weather_file": "../../Examples/compact_osw/files/srrl_2013_amy.epw",
"seed_file": "example_model.osm",
"seed_file": "../example_model.osm",
"measure_paths": ["../measures/"],
"steps": [
{
Expand Down
2 changes: 1 addition & 1 deletion resources/workflow/runner_errors/reportingmeasure.osw
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"weather_file": "../../Examples/compact_osw/files/srrl_2013_amy.epw",
"seed_file": "example_model.osm",
"seed_file": "../example_model.osm",
"measure_paths": ["../measures/"],
"steps": [
{
Expand Down
6 changes: 6 additions & 0 deletions src/cli/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,12 @@ if(BUILD_TESTING)
COMMAND ${Python_EXECUTABLE} -m pytest --verbose --os-cli-path $<TARGET_FILE:openstudio> "${CMAKE_CURRENT_SOURCE_DIR}/test/test_runner_registers_error.py"
WORKING_DIRECTORY "${PROJECT_BINARY_DIR}/resources/workflow/runner_errors/"
)

add_test(NAME OpenStudioCLI.test_outdated_measure
COMMAND ${Python_EXECUTABLE} -m pytest --verbose --os-cli-path $<TARGET_FILE:openstudio> "${CMAKE_CURRENT_SOURCE_DIR}/test/test_outdated_measure.py"
WORKING_DIRECTORY "${PROJECT_BINARY_DIR}/resources/workflow/outdated_measures/"
)

else()
# TODO: Remove. Fallback on these for now, as I don't know if CI has pytest installed
add_test(NAME OpenStudioCLI.Classic.test_logger_rb
Expand Down
22 changes: 22 additions & 0 deletions src/cli/test/test_outdated_measure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import pytest

from workflow_helpers import run_workflow


@pytest.mark.parametrize(
"is_labs",
[pytest.param(True, id="labs"), pytest.param(False, id="classic")],
)
def test_outdated_reportingmeasure(osclipath, is_labs: bool):
suffix = "labs" if is_labs else "classic"
runDir, r = run_workflow(
osclipath=osclipath,
base_osw_name="outdatedreportingmeasure.osw",
suffix=suffix,
is_labs=is_labs,
verbose=False,
debug=True,
post_process_only=True,
)
r.check_returncode()
assert r.returncode == 0
43 changes: 1 addition & 42 deletions src/cli/test/test_runner_registers_error.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,9 @@
import json
import shutil
import subprocess
from pathlib import Path

import pytest


def run_workflow(
osclipath: Path,
base_osw_name: str,
suffix: str,
is_labs: bool,
verbose: bool = False,
debug: bool = False,
post_process_only: bool = False,
):
base_osw_path = Path(base_osw_name).resolve()
assert base_osw_path.is_file(), f"{base_osw_path=} is not found"

osw = json.loads(base_osw_path.read_text())
osw_path = base_osw_path.parent / f"{base_osw_path.stem}_{suffix}.osw"
runDir = base_osw_path.parent / f"run_{osw_path.stem}"
osw["run_directory"] = str(runDir)
if runDir.is_dir():
shutil.rmtree(runDir)
runDir.mkdir(exist_ok=False)
with open(osw_path, "w") as f:
json.dump(osw, fp=f, indent=2, sort_keys=True)

command = [str(osclipath)]
if not is_labs:
command.append("classic")
if verbose:
command.append("--verbose")
command.append("run")
if post_process_only:
# Fake having an in.idf or it won't run in the "classic" subcommand
# Doing it for labs too so that it's less confusing
with open(runDir / "in.idf", "w") as f:
f.write("Building,;")
command.append("--postprocess_only")
if debug:
command.append("--debug")
command += ["-w", str(osw_path)]
# print(f"Running command: {' '.join(command)}")
return runDir, subprocess.run(command, capture_output=True, encoding="utf-8")
from workflow_helpers import run_workflow


@pytest.mark.parametrize(
Expand Down
69 changes: 33 additions & 36 deletions src/cli/test/test_with_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,24 @@

import pytest

from workflow_helpers import run_workflow


def run_workflow(osclipath: Path, suffix: str, is_labs: bool, verbose: bool = False, debug: bool = False):
base_osw_path = Path("with_analysis.osw").resolve()
assert base_osw_path.is_file(), f"{base_osw_path=} is not found"

osw = json.loads(base_osw_path.read_text())
osw_path = base_osw_path.parent / f"with_analysis_{suffix}.osw"
runDir = base_osw_path.parent / f"run_{suffix}"
osw["run_directory"] = str(runDir)
if runDir.is_dir():
shutil.rmtree(runDir)
runDir.mkdir(exist_ok=False)
with open(osw_path, "w") as f:
json.dump(osw, fp=f, indent=2, sort_keys=True)

# Fake having an in.idf or it won't run in the "classic" subcommand
# Doing it for labs too so that it's less confusing
with open(runDir / "in.idf", "w") as f:
f.write("Building,;")
command = [str(osclipath)]
if not is_labs:
command.append("classic")
if verbose:
command.append("--verbose")
command += ["run", "--postprocess_only"]
if debug:
command.append("--debug")
command += ["-w", str(osw_path)]
# print(f"Running command: {' '.join(command)}")
lines = subprocess.check_output(command, encoding="utf-8").splitlines()
return runDir


# Only want to run the CLI call ONCE
@pytest.fixture(scope="module", params=[True, False], ids=["labs", "classic"])
def runWorkflow(osclipath, request):
is_labs = request.param
suffix = "labs" if is_labs else "classic"
return run_workflow(osclipath=osclipath, suffix=suffix, is_labs=is_labs, verbose=False, debug=False), is_labs
runDir, r = run_workflow(
osclipath=osclipath,
base_osw_name="with_analysis.osw",
suffix=suffix,
is_labs=is_labs,
verbose=False,
debug=False,
post_process_only=True,
)
r.check_returncode()
return runDir, is_labs


def test_run_with_analysis(runWorkflow):
Expand Down Expand Up @@ -159,7 +138,16 @@ def test_run_log(runWorkflow):
def test_run_log_debug(osclipath, is_labs: bool):
suffix = "labs" if is_labs else "classic"
suffix += "_debug"
runDir = run_workflow(osclipath=osclipath, suffix=suffix, is_labs=is_labs, verbose=False, debug=True)
runDir, r = run_workflow(
osclipath=osclipath,
base_osw_name="with_analysis.osw",
suffix=suffix,
is_labs=is_labs,
verbose=False,
debug=True,
post_process_only=True,
)
r.check_returncode()
assert runDir.is_dir()
run_log_path = runDir / "run.log"
assert run_log_path.is_file()
Expand All @@ -183,7 +171,16 @@ def test_run_log_debug(osclipath, is_labs: bool):
def test_run_log_toplevel_verbose(osclipath, is_labs: bool):
suffix = "labs" if is_labs else "classic"
suffix += "_toplevel_verbose"
runDir = run_workflow(osclipath=osclipath, suffix=suffix, is_labs=is_labs, verbose=True, debug=False)
runDir, r = run_workflow(
osclipath=osclipath,
base_osw_name="with_analysis.osw",
suffix=suffix,
is_labs=is_labs,
verbose=True,
debug=False,
post_process_only=True,
)
r.check_returncode()
assert runDir.is_dir()
run_log_path = runDir / "run.log"
assert run_log_path.is_file()
Expand Down
Loading

0 comments on commit c0c5ae7

Please sign in to comment.