-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests_data_access.cpp
274 lines (214 loc) · 9.04 KB
/
tests_data_access.cpp
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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include "stdafx.h"
#include <3FD\runtime.h>
#include <3FD\configuration.h>
#include <3FD\callstacktracer.h>
#include "Authenticator.h"
#include "MSDStorageWriter.h"
#include <codecvt>
#include <array>
namespace unit_tests
{
using namespace _3fd;
using namespace _3fd::core;
void HandleException();
/// <summary>
/// Tests the <see cref="application::Authenticator"/> class.
/// </summary>
TEST(TestCase_DataAccess, TestAuthenticator)
{
FrameworkInstance _framework;
CALL_STACK_TRACE;
try
{
application::Authenticator::GetInstance().LoadCredentials();
// this should have been inserted on database setup:
EXPECT_TRUE(
application::Authenticator::GetInstance().IsAuthentic(L"dummyMachine", L"dummyIdKey")
);
std::wstring xMachine(L"dryCatDoesNot");
std::wstring xIdKey(L"digInTheDesert");
// whereas this has not:
EXPECT_FALSE(
application::Authenticator::GetInstance().IsAuthentic(xMachine.c_str(), xIdKey.c_str())
);
using namespace Poco::Data;
using namespace Poco::Data::Keywords;
Session dbSession("ODBC",
AppConfig::GetSettings().application.GetString("dbConnString", "NOT SET")
);
dbSession << "insert into SvcAccessCredential (machine, idKey) values (?, ?);"
, use(xMachine)
, use(xIdKey)
, now;
// cache is not update, so this should still fail:
EXPECT_FALSE(
application::Authenticator::GetInstance().IsAuthentic(xMachine.c_str(), xIdKey.c_str())
);
application::Authenticator::GetInstance().LoadCredentials();
// but now it must succeed:
EXPECT_TRUE(
application::Authenticator::GetInstance().IsAuthentic(xMachine.c_str(), xIdKey.c_str())
);
EXPECT_TRUE(
application::Authenticator::GetInstance().IsAuthentic(L"dummyMachine", L"dummyIdKey")
);
dbSession << R"(
delete from SvcAccessCredential
where machine = cast(? as nvarchar);
)"
, use(xMachine)
, now;
application::Authenticator::Finalize();
}
catch (...)
{
HandleException();
}
}
/// <summary>
/// Tests the <see cref="application::MSDStorageWriter"/> class.
/// </summary>
TEST(TestCase_DataAccess, TestMSDStorageWriter)
{
FrameworkInstance _framework;
CALL_STACK_TRACE;
try
{
using namespace application;
// Generate some dummy stats data:
auto theTime = time(nullptr) * 1000;
std::wstring macNamePrefix(L"joeTheCrazyFrog");
// must keep ASC order here!
std::array<std::wstring, 2> macNames = { macNamePrefix + L'1', macNamePrefix + L'2' };
std::wstring statFloatNamePrefix(L"dummy_stat_float_");
// must keep ASC order here!
std::array<std::wstring, 3> statFloatNames =
{
statFloatNamePrefix + L'0',
statFloatNamePrefix + L'1',
statFloatNamePrefix + L'2'
};
std::wstring statIntNamePrefix(L"dummy_stat_int_");
// must keep ASC order here!
std::array<std::wstring, 3> statIntNames =
{
statIntNamePrefix + L'0',
statIntNamePrefix + L'1',
statIntNamePrefix + L'2'
};
decltype(StorageWriteTask::statSamplesFloat32) expStatSamplesFloat;
expStatSamplesFloat.reserve(3);
expStatSamplesFloat.emplace_back(statFloatNames[0], 60.6F, Quality::Good);
expStatSamplesFloat.emplace_back(statFloatNames[1], 9.0F, Quality::Invalid);
expStatSamplesFloat.emplace_back(statFloatNames[2], 69.6F, Quality::Good);
std::wstring statNameIntPrefix(L"dummy_stat_int_");
decltype(StorageWriteTask::statSamplesInt32) expStatSamplesInt;
expStatSamplesInt.reserve(3);
expStatSamplesInt.emplace_back(statIntNames[0], 606, Quality::Good);
expStatSamplesInt.emplace_back(statIntNames[1], 9, Quality::Unknown);
expStatSamplesInt.emplace_back(statIntNames[2], 696, Quality::Good);
std::vector<std::unique_ptr<StorageWriteTask>> tasks(2);
tasks[0].reset(new StorageWriteTask());
tasks[0]->timeSinceEpochInMillisecs = theTime;
tasks[0]->machine = macNames[0];
tasks[0]->statSamplesFloat32 = expStatSamplesFloat;
tasks[0]->statSamplesInt32 = expStatSamplesInt;
tasks[1].reset(new StorageWriteTask());
tasks[1]->timeSinceEpochInMillisecs = theTime;
tasks[1]->machine = macNames[1];
tasks[1]->statSamplesFloat32 = expStatSamplesFloat;
tasks[1]->statSamplesInt32 = expStatSamplesInt;
// Write it to database:
MSDStorageWriter dbWriter(
AppConfig::GetSettings().application.GetString("dbConnString", "NOT SET")
);
dbWriter.WriteStats(tasks);
// And check what was written:
using namespace Poco::Data;
using namespace Poco::Data::Keywords;
std::wstring_convert<std::codecvt_utf8<wchar_t>> transcoder;
Session dbSession("ODBC",
AppConfig::GetSettings().application.GetString("dbConnString", "NOT SET")
);
std::vector<int16_t> macIds;
dbSession << "select macId from Machine where macName like N'%s%%' order by macName asc"
, transcoder.to_bytes(macNamePrefix) // does not support std::wstring here
, into(macIds)
, now;
ASSERT_EQ(macNames.size(), macIds.size());
std::vector<int16_t> statFloatIds;
dbSession << "select statId from Statistic where statName like N'%s%%' order by statName asc"
, transcoder.to_bytes(statFloatNamePrefix) // does not support std::wstring here
, into(statFloatIds)
, now;
ASSERT_EQ(statFloatNames.size(), statFloatIds.size());
std::vector<int16_t> statIntIds;
dbSession << "select statId from Statistic where statName like N'%s%%' order by statName asc"
, transcoder.to_bytes(statIntNamePrefix) // does not support std::wstring here
, into(statIntIds)
, now;
ASSERT_EQ(statIntNames.size(), statIntIds.size());
float statValFloat;
int statValInt;
int16_t macId;
int16_t statId;
int8_t quality;
// prepare the query:
auto queryFloat = (dbSession << R"(
select statVal
from StatsValFloat32
where instant = %Ld
and macId = ?
and statId = ?
and quality = ?;
)"
// bind by reference:
, theTime
, use(macId)
, use(statId)
, use(quality)
, into(statValFloat)
);
// prepare the query:
auto queryInt = (dbSession << R"(
select statVal
from StatsValInt32
where instant = %Ld
and macId = ?
and statId = ?
and quality = ?;
)"
// bind by reference:
, theTime
, use(macId)
, use(statId)
, use(quality)
, into(statValInt)
);
for (int idxMachine = 0; idxMachine < macNames.size(); ++idxMachine)
{
macId = macIds[idxMachine];
// check stats whose value are float 32-bits type:
for (int idxStat = 0; idxStat < statFloatNames.size(); ++idxStat)
{
statId = statFloatIds[idxStat];
quality = static_cast<int8_t> (expStatSamplesFloat[idxStat].quality);
queryFloat.execute();
EXPECT_EQ(expStatSamplesFloat[idxStat].value, statValFloat);
}
// check stats whose value are integer 32-bits type:
for (int idxStat = 0; idxStat < statIntNames.size(); ++idxStat)
{
statId = statIntIds[idxStat];
quality = static_cast<int8_t> (expStatSamplesInt[idxStat].quality);
queryInt.execute();
EXPECT_EQ(expStatSamplesInt[idxStat].value, statValInt);
}
}
}
catch (...)
{
HandleException();
}
}
}// end of namespace unit_tests