CYCAMORE
src/deploy_inst_tests.cc
Go to the documentation of this file.
1 #include <gtest/gtest.h>
2 
3 #include "context.h"
4 #include "deploy_inst.h"
5 #include "institution_tests.h"
6 #include "agent_tests.h"
7 
8 // make sure that the deployed agent's prototype name is identical to the
9 // originally specified prototype name - this is important to test because
10 // DeployInst does some mucking around with registering name-modded prototypes
11 // in order to deal with lifetime setting.
12 
13 using cyclus::QueryResult;
14 
15 TEST(DeployInstTests, ProtoNames) {
16  std::string config =
17  "<prototypes> <val>foobar</val> </prototypes>"
18  "<build_times> <val>1</val> </build_times>"
19  "<n_build> <val>3</val> </n_build>"
20  "<lifetimes> <val>2</val> </lifetimes>"
21  ;
22 
23  int simdur = 5;
24  cyclus::MockSim sim(cyclus::AgentSpec(":cycamore:DeployInst"), config, simdur);
25  sim.DummyProto("foobar");
26  int id = sim.Run();
27 
28  cyclus::SqlStatement::Ptr stmt = sim.db().db().Prepare(
29  "SELECT COUNT(*) FROM AgentEntry WHERE Prototype = 'foobar';"
30  );
31  stmt->Step();
32  EXPECT_EQ(3, stmt->GetInt(0));
33 }
34 
35 TEST(DeployInstTests, BuildTimes) {
36  std::string config =
37  "<prototypes> <val>foobar</val> <val>foobar</val> </prototypes>"
38  "<build_times> <val>1</val> <val>3</val> </build_times>"
39  "<n_build> <val>1</val> <val>7</val> </n_build>"
40  ;
41 
42  int simdur = 5;
43  cyclus::MockSim sim(cyclus::AgentSpec(":cycamore:DeployInst"), config, simdur);
44  sim.DummyProto("foobar");
45  int id = sim.Run();
46 
47  cyclus::SqlStatement::Ptr stmt = sim.db().db().Prepare(
48  "SELECT COUNT(*) FROM AgentEntry WHERE Prototype = 'foobar' AND EnterTime = 1;"
49  );
50  stmt->Step();
51  EXPECT_EQ(1, stmt->GetInt(0));
52 
53  stmt = sim.db().db().Prepare(
54  "SELECT COUNT(*) FROM AgentEntry WHERE Prototype = 'foobar' AND EnterTime = 3;"
55  );
56  stmt->Step();
57  EXPECT_EQ(7, stmt->GetInt(0));
58 }
59 
60 // make sure that specified lifetimes are honored both in agent's table record
61 // and in decommissioning.
62 TEST(DeployInstTests, FiniteLifetimes) {
63  std::string config =
64  "<prototypes> <val>foobar</val> <val>foobar</val> <val>foobar</val> </prototypes>"
65  "<build_times> <val>1</val> <val>1</val> <val>2</val> </build_times>"
66  "<n_build> <val>1</val> <val>7</val> <val>3</val> </n_build>"
67  "<lifetimes> <val>1</val> <val>2</val> <val>-1</val> </lifetimes>"
68  ;
69 
70  int simdur = 5;
71  cyclus::MockSim sim(cyclus::AgentSpec(":cycamore:DeployInst"), config, simdur);
72  sim.DummyProto("foobar");
73  int id = sim.Run();
74 
75  // check agent deployment
76  cyclus::SqlStatement::Ptr stmt = sim.db().db().Prepare(
77  "SELECT COUNT(*) FROM AgentEntry WHERE Prototype = 'foobar' AND EnterTime = 1 AND Lifetime = 1;"
78  );
79  stmt->Step();
80  EXPECT_EQ(1, stmt->GetInt(0));
81 
82  stmt = sim.db().db().Prepare(
83  "SELECT COUNT(*) FROM AgentEntry WHERE Prototype = 'foobar' AND EnterTime = 1 AND Lifetime = 2;"
84  );
85  stmt->Step();
86  EXPECT_EQ(7, stmt->GetInt(0));
87 
88  stmt = sim.db().db().Prepare(
89  "SELECT COUNT(*) FROM AgentEntry WHERE Prototype = 'foobar' AND EnterTime = 2 AND Lifetime = -1;"
90  );
91  stmt->Step();
92  EXPECT_EQ(3, stmt->GetInt(0));
93 
94  // check decommissioning
95  stmt = sim.db().db().Prepare(
96  "SELECT COUNT(*) FROM AgentEntry As e JOIN AgentExit AS x ON x.AgentId = e.AgentId WHERE e.Prototype = 'foobar' AND x.ExitTime = 1;"
97  );
98  stmt->Step();
99  EXPECT_EQ(1, stmt->GetInt(0));
100 
101  stmt = sim.db().db().Prepare(
102  "SELECT COUNT(*) FROM AgentEntry As e JOIN AgentExit AS x ON x.AgentId = e.AgentId WHERE e.Prototype = 'foobar' AND x.ExitTime = 2;"
103  );
104  stmt->Step();
105  EXPECT_EQ(7, stmt->GetInt(0));
106 
107  // agent with -1 lifetime should not be in AgentExit table
108  stmt = sim.db().db().Prepare(
109  "SELECT COUNT(*) FROM AgentExit;"
110  );
111  stmt->Step();
112  EXPECT_EQ(8, stmt->GetInt(0));
113 }
114 
115 TEST(DeployInstTests, NoDupProtos) {
116  std::string config =
117  "<prototypes> <val>foobar</val> <val>foobar</val> <val>foobar</val> </prototypes>"
118  "<build_times> <val>1</val> <val>1</val> <val>2</val> </build_times>"
119  "<n_build> <val>1</val> <val>7</val> <val>3</val> </n_build>"
120  "<lifetimes> <val>1</val> <val>1</val> <val>-1</val> </lifetimes>"
121  ;
122 
123  int simdur = 5;
124  cyclus::MockSim sim(cyclus::AgentSpec(":cycamore:DeployInst"), config, simdur);
125  sim.DummyProto("foobar");
126  int id = sim.Run();
127 
128  // don't duplicate same prototypes with same custom lifetime
129  cyclus::SqlStatement::Ptr stmt = sim.db().db().Prepare(
130  "SELECT COUNT(*) FROM Prototypes WHERE Prototype = 'foobar_life_1';"
131  );
132  stmt->Step();
133  EXPECT_EQ(1, stmt->GetInt(0));
134 
135  // don't duplicate custom lifetimes that are identical to original prototype
136  // lifetime.
137  stmt = sim.db().db().Prepare(
138  "SELECT COUNT(*) FROM Prototypes WHERE Prototype = 'foobar';"
139  );
140  stmt->Step();
141  EXPECT_EQ(1, stmt->GetInt(0));
142 }
143 
144 TEST(DeployInstTests, PositionInitialize) {
145  std::string config =
146  "<prototypes> <val>foobar</val> </prototypes>"
147  "<build_times> <val>1</val> </build_times>"
148  "<n_build> <val>3</val> </n_build>"
149  "<lifetimes> <val>2</val> </lifetimes>";
150 
151  int simdur = 5;
152  cyclus::MockSim sim(cyclus::AgentSpec(":cycamore:DeployInst"), config, simdur);
153  sim.DummyProto("foobar");
154  int id = sim.Run();
155 
156  QueryResult qr = sim.db().Query("AgentPosition", NULL);
157  EXPECT_EQ(qr.GetVal<double>("Latitude"), 0.0);
158  EXPECT_EQ(qr.GetVal<double>("Longitude"), 0.0);
159 }
160 
161 TEST(DeployInstTests, PositionInitialize2) {
162  std::string config =
163  "<prototypes> <val>foobar</val> </prototypes>"
164  "<longitude> -20.0 </longitude>"
165  "<latitude> 2.0 </latitude>"
166  "<build_times> <val>1</val> </build_times>"
167  "<n_build> <val>3</val> </n_build>"
168  "<lifetimes> <val>2</val> </lifetimes>";
169 
170  int simdur = 5;
171  cyclus::MockSim sim(cyclus::AgentSpec(":cycamore:DeployInst"), config, simdur);
172  sim.DummyProto("foobar");
173  int id = sim.Run();
174 
175  QueryResult qr = sim.db().Query("AgentPosition", NULL);
176  EXPECT_EQ(qr.GetVal<double>("Latitude"), 2.0);
177  EXPECT_EQ(qr.GetVal<double>("Longitude"), -20.0);
178 }
179 
180 // required to get functionality in cyclus agent unit tests library
181 cyclus::Agent* DeployInstitutionConstructor(cyclus::Context* ctx) {
182  return new cycamore::DeployInst(ctx);
183 }
184 
185 #ifndef CYCLUS_AGENT_TESTS_CONNECTED
186 int ConnectAgentTests();
188 #define CYCLUS_AGENT_TESTS_CONNECTED cyclus_agent_tests_connected
189 #endif // CYCLUS_AGENT_TESTS_CONNECTED
190 
191 INSTANTIATE_TEST_CASE_P(DeployInst, InstitutionTests,
193 INSTANTIATE_TEST_CASE_P(DeployInst, AgentTests,
int ConnectAgentTests()
cyclus::Agent * DeployInstitutionConstructor(cyclus::Context *ctx)
static int cyclus_agent_tests_connected
cycamore::GrowthRegion string
INSTANTIATE_TEST_CASE_P(DeployInst, InstitutionTests, Values(&DeployInstitutionConstructor))
TEST(DeployInstTests, ProtoNames)