Running Measure Test via OpenStudio CLI Returns Error

asked 2021-10-19 05:14:03 -0500

macabilo's avatar

updated 2021-10-19 09:00:54 -0500

I was modifying a copy of a reporting measure. To save test time, I am using OpenStudio CLI. My modified OS measure was from the OpenStudio Result IP measure and I was testing how to create my own sections using the example template within the same measure. I did this by modifying a copy of os_lib_reporting.rb. See my modified file below:

# Custom OS reporting

require 'json'

module OsLib_Reporting_Custom

#setup - get model, sql, and setup web assets path
def self.setup(runner)
    results = {}

    #get the last model
    model = runner.lastOpenStudioModel

    if model.empty?
        runner.registerError('Cannot find last model.')
        return false
    end
    model = model.get

    #get the last idf
    workspace = runner.lastEnergyPlusWorkspace
    if workspace.empty?
        runner.registerError('Cannot find last idf file.')
        return false
    end
    workspace = workspace.get

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

    #populate hash to pass to measure
    results[:model] = model
    #results[:workspace] = workspace
    results[:sqlFile] = sqlFile
    results[:web_asset_path] = OpenStudio.getSharedResourcesPath / OpenStudio::Path.new('web_assets')

    return results
end

#create template section
def self.template_section(model, sqlFile, runner, name_only = false)
    #array to hold tables
    template_tables = []

    #gather data for section
    @template_section = {}
    @template_section[:title] = 'Template Section'
    @template_section[:tables] = template_tables

    #stop here if only name is requested - this is used to populate display name for arguments
    if name_only == true
        return @template_section
    end

    #create table
    template_table_01 = {}
    template_table_01[:title] = 'Sample Table 1'
    template_table_01[:header] = ['Bldg', 'Energy']
    template_table_01[:units] = ['', 'kWh']
    template_table_01[:data] = []

    #add rows to table
    template_table_01[:data] << ['Bldg 1', 1000]
    template_table_01[:data] << ['Bldg 2', 9870]
    template_table_01[:data] << ['Bldg 3', 431]

    #create chart
    template_table_01[:chart_type] = 'simple_pie' #search for value options
    template_table_01[:chart] = []
    template_table_01[:data].each do |row|
        template_table_01[:chart] << JSON.generate(label: row[0], value: row[1])
    end

    #add table to array of tables
    template_tables << template_table_01

    #use helper method that generates additional table for section
    #template_tables << OsLib_Reporting_Custom.template_table(model, sqlFile, runner)


    return @template_section
end
end

I added this file into my measure.rb as shown below:

require 'erb'
require 'json'

require "#{File.dirname(__FILE__)}/resources/os_lib_reporting"
require "#{File.dirname(__FILE__)}/resources/os_lib_reporting_custom"
require "#{File.dirname(__FILE__)}/resources/os_lib_schedules"
require "#{File.dirname(__FILE__)}/resources/os_lib_helper_methods"
require_relative 'resources/Siz.Model'

# start the measure
class ReportFormatV1 < OpenStudio::Measure::ReportingMeasure
  # human readable name
  def name
    # Measure name should be the title case of the class name.
    return "Report Format v1"
  end

Besides renaming all instances of OsLib_Reporting to OsLib_Reporting_Custom, I haven't edited measure.rb yet as I was excited to see my simple section template.

So what I did next was to proceed to my test file. Once again, I modified a copy of the existing test file (now named report_format_v_1_test.rb). Within it, I simply updated the expected number of arguments; e.g.,

def test_number_of_arguments_and_argument_names
# create an instance of the measure
measure = ReportFormatV1.new

# get arguments and test that they are what we are expecting
arguments = measure.arguments()
assert_equal(1, arguments.size) #originally this was 0
end

Finally, I ... (more)

edit retag flag offensive close merge delete

Comments

The measure you based things on is a modified version of OpenStudio Results measures and I don’t know the state of the measure or tests, but it seems like it ran for you. It is unclear if the error you have. Is based on a measure only with the few lines of code above or is based on the other measure so it is hard to diagnose the error. I would suggest you make a fresh measure that comes with its own tests as a starting point. You can do this in the OpenStuido Application or Parametric Analysis Tool (PAT). We want add make new measure to CLI but have not yet.

David Goldwasser's avatar David Goldwasser  ( 2021-10-19 09:37:41 -0500 )edit
1

I also wanted to point you to an example reporting measure that may be a useful starting point. It pulls in some model data but also some external data that can be replaced with what you want to report. https://github.com/NREL/openstudio-co...

David Goldwasser's avatar David Goldwasser  ( 2021-10-19 09:38:32 -0500 )edit