Module: BTAPMeasureTestHelper

Included in:
BTAPModelMeasure_Test
Defined in:
lib/openstudio-standards/btap/costing/btap_measure_helper.rb,
lib/openstudio-standards/utilities/template_measure/resources/BTAPMeasureHelper.rb

Instance Method Summary collapse

Instance Method Details

#argument_type(argument) ⇒ Object

Determines the OS argument type dynamically.



317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/openstudio-standards/btap/costing/btap_measure_helper.rb', line 317

def argument_type(argument)
  case argument.type.value
    when 0
      return "Bool"
    when 1 #Double
      return "Double"
    when 2 #Quantity
      return "Quantity"
    when 3 #Integer
      return "Integer"
    when 4
      return "String"
    when 5 #Choice
      return "Choice"
    when 6 #Path
      return "Path"
    when 7 #Separator
      return "Separator"
    else
      return "Blah"
  end
end

#copy_model(model) ⇒ Object

Method does a deep copy of a model.



346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/openstudio-standards/btap/costing/btap_measure_helper.rb', line 346

def copy_model(model)
  copy_model = OpenStudio::Model::Model.new
  # remove existing objects from model
  handles = OpenStudio::UUIDVector.new
  copy_model.objects.each do |obj|
    handles << obj.handle
  end
  copy_model.removeObjects(handles)
  # put contents of new_model into model_to_replace
  copy_model.addObjects(model.toIdfFile.objects)
  return copy_model
end

#create_necb_protype_model(building_type, climate_zone, epw_file, template) ⇒ Object

helper method to create necb archetype as a starting point for testing.



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/openstudio-standards/btap/costing/btap_measure_helper.rb', line 244

def create_necb_protype_model(building_type, climate_zone, epw_file, template)

  osm_directory = "#{Dir.pwd}/output/#{building_type}-#{template}-#{climate_zone}-#{epw_file}"
  FileUtils.mkdir_p (osm_directory) unless Dir.exist?(osm_directory)
  #Get Weather climate zone from lookup
  weather = BTAP::Environment::WeatherFile.new(epw_file)
  #create model
  building_name = "#{template}_#{building_type}"

  prototype_creator = Standard.build(template)
  model = prototype_creator.model_create_prototype_model(
      epw_file: epw_file,
      sizing_run_dir: osm_directory,
      debug: @debug,
      template: template,
      building_type: building_type)

  #set weather file to epw_file passed to model.
  weather.set_weather_file(model)
  return model
end

#get_measure_objectObject

Fancy way of getting the measure object automatically.



305
306
307
308
309
310
311
312
313
314
# File 'lib/openstudio-standards/btap/costing/btap_measure_helper.rb', line 305

def get_measure_object()
  measure_class_name = self.class.name.to_s.match(/(BTAP.*)(\_Test)/i).captures[0]
  measure = nil
  eval "measure = #{measure_class_name}.new"
  if measure.nil?
    puts "Measure class #{measure_class_name} is invalid. Please ensure the test class name is of the form 'MeasureName_Test' "
    return false
  end
  return measure
end

#run_measure(input_arguments, model) ⇒ Object

Custom way to run the measure in the test.



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/openstudio-standards/btap/costing/btap_measure_helper.rb', line 267

def run_measure(input_arguments, model)

  # This will create a instance of the measure you wish to test. It does this based on the test class name.
  measure = get_measure_object()
  measure.use_json_package = @use_json_package
  measure.use_string_double = @use_string_double
  # Return false if can't
  return false if false == measure
  arguments = measure.arguments(model)
  argument_map = OpenStudio::Measure.convertOSArgumentVectorToMap(arguments)
  runner = OpenStudio::Measure::OSRunner.new(OpenStudio::WorkflowJSON.new)
  #Check if

  # Set the arguements in the argument map use json or real arguments.
  if @use_json_package
    argument = arguments[0].clone
    assert(argument.setValue(input_arguments['json_input']), "Could not set value for 'json_input' to #{input_arguments['json_input']}")
    argument_map['json_input'] = argument
  else
    input_arguments.each_with_index do |(key, value), index|
      argument = arguments[index].clone
      if argument_type(argument) == "Double"
        #forces it to a double if it is a double.
        assert(argument.setValue(value.to_f), "Could not set value for #{key} to #{value}")
      else
        assert(argument.setValue(value.to_s), "Could not set value for #{key} to #{value}")
      end
      argument_map[key] = argument
    end
  end
  #run the measure
  measure.run(model, runner, argument_map)
  runner.result
  return runner
end

#test_argument_rangesObject

Test argument ranges.



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/openstudio-standards/btap/costing/btap_measure_helper.rb', line 190

def test_argument_ranges
  [true, false].each do |json_input|
    [true, false].each do |string_double|
      @use_json_package = json_input
      @use_string_double = string_double
      (@measure_interface_detailed).each_with_index do |argument|
        if argument['type'] == 'Double' or argument['type'] == 'StringDouble'
          puts "testing range for #{argument['name']} "
          #Check over max
          if not argument['max_double_value'].nil?
            puts "testing max limit"
            model = OpenStudio::Model::Model.new
            input_arguments = @good_input_arguments.clone
            over_max_value = argument['max_double_value'].to_f + 1.0
            over_max_value = over_max_value.to_s if argument['type'].downcase == "StringDouble".downcase
            input_arguments[argument['name']] = over_max_value
            puts "Testing argument #{argument['name']} max limit of #{argument['max_double_value']}"
            input_arguments = {'json_input' => JSON.pretty_generate(input_arguments)} if @use_json_package
            run_measure(input_arguments, model)
            runner = run_measure(input_arguments, model)
            assert(runner.result.value.valueName != 'Success', "Checks did not stop a lower than limit value of #{over_max_value} for #{argument['name']}")
            puts "Success: Testing argument #{argument['name']} max limit of #{argument['max_double_value']}"
          end
          #Check over max
          if not argument['min_double_value'].nil?
            puts "testing min limit"
            model = OpenStudio::Model::Model.new
            input_arguments = @good_input_arguments.clone
            over_min_value = argument['min_double_value'].to_f - 1.0
            over_min_value = over_max_value.to_s if argument['type'].downcase == "StringDouble".downcase
            input_arguments[argument['name']] = over_min_value
            puts "Testing argument #{argument['name']} min limit of #{argument['min_double_value']}"
            input_arguments = {'json_input' => JSON.pretty_generate(input_arguments)} if @use_json_package
            runner = run_measure(input_arguments, model)
            assert(runner.result.value.valueName != 'Success', "Checks did not stop a lower than limit value of #{over_min_value} for #{argument['name']}")
            puts "Success:Testing argument #{argument['name']} min limit of #{argument['min_double_value']}"
          end

        end
        if (argument['type'] == 'StringDouble') and (not argument["valid_strings"].nil?) and @use_string_double
          model = OpenStudio::Model::Model.new
          input_arguments = @good_input_arguments.clone
          input_arguments[argument['name']] = SecureRandom.uuid.to_s
          puts "Testing argument #{argument['name']} min limit of #{argument['min_double_value']}"
          input_arguments = {'json_input' => JSON.pretty_generate(input_arguments)} if @use_json_package
          runner = run_measure(input_arguments, model)
          assert(runner.result.value.valueName != 'Success', "Checks did not stop a lower than limit value of #{over_min_value} for #{argument['name']}")
        end
      end
    end
  end
end

#test_arguments_and_defaultsObject

Boiler plate to default values and number of arguments against what is in your test’s setup method.



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/openstudio-standards/btap/costing/btap_measure_helper.rb', line 148

def test_arguments_and_defaults
  [true, false].each do |json_input|
    [true, false].each do |string_double|
      @use_json_package = json_input
      @use_string_double = string_double

      # Create an instance of the measure
      measure = get_measure_object()
      measure.use_json_package = @use_json_package
      measure.use_string_double = @use_string_double
      model = OpenStudio::Model::Model.new

      # Create an instance of a runner
      runner = OpenStudio::Measure::OSRunner.new(OpenStudio::WorkflowJSON.new)

      # Test arguments and defaults
      arguments = measure.arguments(model)
      #convert whatever the input was into a hash. Then test.

      #check number of arguments.
      if @use_json_package
        assert_equal(@measure_interface_detailed.size, JSON.parse(arguments[0].defaultValueAsString).size, "The measure should have #{@measure_interface_detailed.size} but actually has #{arguments.size}. Here the the arguement expected #{@measure_interface_detailed} and this is the actual #{arguments}")
      else
        assert_equal(@measure_interface_detailed.size, arguments.size, "The measure should have #{@measure_interface_detailed.size} but actually has #{arguments.size}. Here the the arguement expected #{@measure_interface_detailed} and this is the actual #{arguments}")
        (@measure_interface_detailed).each_with_index do |argument_expected, index|
          assert_equal(argument_expected['name'], arguments[index].name, "Measure argument name of #{argument_expected['name']} was expected, but got #{arguments[index].name} instead.")
          assert_equal(argument_expected['display_name'], arguments[index].displayName, "Display name for argument #{argument_expected['name']} was expected to be #{argument_expected['display_name']}, but got #{arguments[index].displayName} instead.")
          case argument_type(arguments[index])
            when "String", "Choice"
              assert_equal(argument_expected['default_value'].to_s, arguments[index].defaultValueAsString, "The default value for argument #{argument_expected['name']} was #{argument_expected['default_value']}, but actual was #{arguments[index].defaultValueAsString}")
            when "Double", "Integer"
              assert_equal(argument_expected['default_value'].to_f, arguments[index].defaultValueAsDouble.to_f, "The default value for argument #{argument_expected['name']} was #{argument_expected['default_value']}, but actual was #{arguments[index].defaultValueAsString}")
            when "Bool"
              assert_equal(argument_expected['default_value'], arguments[index].defaultValueAsBool, "The default value for argument #{argument_expected['name']} was #{argument_expected['default_value']}, but actual was #{arguments[index].defaultValueAsString}")
          end
        end
      end
    end
  end
end

#valid_float?(str) ⇒ Boolean

Valid float helper.

Returns:

  • (Boolean)


341
342
343
# File 'lib/openstudio-standards/btap/costing/btap_measure_helper.rb', line 341

def valid_float?(str)
  !!Float(str) rescue false
end