-
Notifications
You must be signed in to change notification settings - Fork 229
Expand file tree
/
Copy pathPythonEngine_GTest.cpp
More file actions
195 lines (160 loc) · 7.26 KB
/
PythonEngine_GTest.cpp
File metadata and controls
195 lines (160 loc) · 7.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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
188
189
190
191
192
193
194
195
/***********************************************************************************************************************
* OpenStudio(R), Copyright (c) Alliance for Sustainable Energy, LLC.
* See also https://openstudio.net/license
***********************************************************************************************************************/
#include <gtest/gtest.h>
#include "../../../src/measure/ModelMeasure.hpp"
#include "../../../src/measure/OSArgument.hpp"
#include "../../../src/measure/OSMeasure.hpp"
#include "../../../src/measure/OSRunner.hpp"
#include "../../../src/model/Model.hpp"
#include "../../../src/scriptengine/ScriptEngine.hpp"
#include <fmt/format.h>
class PythonEngineFixture : public testing::Test
{
public:
static openstudio::path getScriptPath(const std::string& classAndDirName) {
openstudio::path scriptPath = openstudio::getApplicationSourceDirectory() / fmt::format("python/engine/test/{}/measure.py", classAndDirName);
OS_ASSERT(openstudio::filesystem::is_regular_file(scriptPath));
return scriptPath;
}
protected:
// initialize for each test
virtual void SetUp() override {
std::vector<std::string> args;
thisEngine = std::make_unique<openstudio::ScriptEngineInstance>("pythonengine", args);
(*thisEngine)->registerType<openstudio::measure::OSMeasure*>("openstudio::measure::OSMeasure *");
(*thisEngine)->registerType<openstudio::measure::ModelMeasure*>("openstudio::measure::ModelMeasure *");
}
// tear down after each test
virtual void TearDown() override {
// Want to ensure the engine is reset regardless of the test outcome, or it may throw a segfault
thisEngine->reset();
}
std::unique_ptr<openstudio::ScriptEngineInstance> thisEngine;
};
TEST_F(PythonEngineFixture, BadMeasure) {
const std::string classAndDirName = "BadMeasure";
const auto scriptPath = getScriptPath(classAndDirName);
auto measureScriptObject = (*thisEngine)->loadMeasure(scriptPath, classAndDirName);
auto* measurePtr = (*thisEngine)->getAs<openstudio::measure::ModelMeasure*>(measureScriptObject);
ASSERT_EQ(measurePtr->name(), "Bad Measure");
std::string expected_exception = fmt::format(R"(SWIG director method error. In method 'arguments': `ValueError('oops')`
Traceback (most recent call last):
File "{0}", line 22, in arguments
self.another_method()
File "{0}", line 19, in another_method
raise ValueError("oops")
ValueError: oops)",
scriptPath.generic_string());
openstudio::model::Model model;
try {
measurePtr->arguments(model);
ASSERT_FALSE(true) << "Expected measure arguments(model) to throw";
} catch (std::exception& e) {
std::string error = e.what();
EXPECT_EQ(expected_exception, error);
}
}
TEST_F(PythonEngineFixture, WrongMethodMeasure) {
const std::string classAndDirName = "WrongMethodMeasure";
const auto scriptPath = getScriptPath(classAndDirName);
auto measureScriptObject = (*thisEngine)->loadMeasure(scriptPath, classAndDirName);
auto* measurePtr = (*thisEngine)->getAs<openstudio::measure::ModelMeasure*>(measureScriptObject);
ASSERT_EQ(measurePtr->name(), "Wrong Method Measure");
std::string expected_exception =
fmt::format(R"(SWIG director method error. In method 'arguments': `AttributeError("'Model' object has no attribute 'nonExistingMethod'")`
Traceback (most recent call last):
File "{}", line 19, in arguments
model.nonExistingMethod()
^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'Model' object has no attribute 'nonExistingMethod')",
scriptPath.generic_string());
openstudio::model::Model model;
try {
measurePtr->arguments(model);
ASSERT_FALSE(true) << "Expected measure arguments(model) to throw";
} catch (std::exception& e) {
std::string error = e.what();
EXPECT_EQ(expected_exception, error);
}
}
TEST_F(PythonEngineFixture, StackLevelTooDeepMeasure) {
const std::string classAndDirName = "StackLevelTooDeepMeasure";
const auto scriptPath = getScriptPath(classAndDirName);
auto measureScriptObject = (*thisEngine)->loadMeasure(scriptPath, classAndDirName);
auto* measurePtr = (*thisEngine)->getAs<openstudio::measure::ModelMeasure*>(measureScriptObject);
ASSERT_EQ(measurePtr->name(), "Stack Level Too Deep Measure");
std::string expected_exception =
fmt::format(R"(SWIG director method error. In method 'arguments': `RecursionError('maximum recursion depth exceeded')`
Traceback (most recent call last):
File "{0}", line 21, in arguments
s(10)
File "{0}", line 11, in s
return s(x)
^^^^
File "{0}", line 11, in s
return s(x)
^^^^
File "{0}", line 11, in s
return s(x)
^^^^
[Previous line repeated 996 more times]
RecursionError: maximum recursion depth exceeded)",
scriptPath.generic_string());
openstudio::model::Model model;
try {
measurePtr->arguments(model);
ASSERT_FALSE(true) << "Expected measure arguments(model) to throw";
} catch (std::exception& e) {
std::string error = e.what();
EXPECT_EQ(expected_exception, error);
}
}
TEST_F(PythonEngineFixture, AlfalfaMeasure) {
const std::string classAndDirName = "AlfalfaMeasure";
const auto scriptPath = getScriptPath(classAndDirName);
auto measureScriptObject = (*thisEngine)->loadMeasure(scriptPath, classAndDirName);
auto* measurePtr = (*thisEngine)->getAs<openstudio::measure::ModelMeasure*>(measureScriptObject);
ASSERT_EQ(measurePtr->name(), "Alfalfa Measure");
std::string workflow_json = R"json(
{
"seed": "../seed.osm",
"weather_file": "../weather.epw",
"steps": [
{
"arguments": {},
"description": "The method attempts to build an alfalfa json in the measure",
"measure_dir_name": "AlfalfaMeasure",
"modeler_description": "The method attempts to build an alfalfa json in the measure",
"name": "AlfalfaMeasure"
}
]
}
)json";
openstudio::model::Model model;
openstudio::WorkflowJSON workflow = *openstudio::WorkflowJSON::load(workflow_json);
openstudio::measure::OSRunner runner(workflow);
EXPECT_TRUE(runner.alfalfa().points().empty());
openstudio::measure::OSArgumentMap arguments;
measurePtr->run(model, runner, arguments);
EXPECT_EQ(5, runner.alfalfa().points().size());
}
TEST_F(PythonEngineFixture, hasMethod) {
{
const std::string classAndDirName = "ReportingMeasureWithoutModelOutputs";
const auto scriptPath = getScriptPath(classAndDirName);
auto measureScriptObject = (*thisEngine)->loadMeasure(scriptPath, classAndDirName);
EXPECT_FALSE((*thisEngine)->hasMethod(measureScriptObject, "doesNotExists"));
EXPECT_TRUE((*thisEngine)->hasMethod(measureScriptObject, "modelOutputRequests", false)); // overriden_only = false
EXPECT_FALSE((*thisEngine)->hasMethod(measureScriptObject, "modelOutputRequests"));
}
{
const std::string classAndDirName = "ReportingMeasureWithModelOutputs";
const auto scriptPath = getScriptPath(classAndDirName);
auto measureScriptObject = (*thisEngine)->loadMeasure(scriptPath, classAndDirName);
EXPECT_FALSE((*thisEngine)->hasMethod(measureScriptObject, "doesNotExists"));
EXPECT_TRUE((*thisEngine)->hasMethod(measureScriptObject, "modelOutputRequests", false)); // overriden_only = false
EXPECT_TRUE((*thisEngine)->hasMethod(measureScriptObject, "modelOutputRequests"));
}
}