-
Notifications
You must be signed in to change notification settings - Fork 45
/
GeneralPurposeMeasurementLoggerDaoIntegrationTest.cpp
122 lines (86 loc) · 3.22 KB
/
GeneralPurposeMeasurementLoggerDaoIntegrationTest.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
/*
Copyright (C) AC SOFTWARE SP. Z O.O.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "integration/datalogger/GeneralPurposeMeasurementLoggerDaoIntegrationTest.h"
#include <math.h>
#include <string>
#include "device/value/channel_general_purpose_measurement_value.h"
#include "jsonconfig/channel/general_purpose_measurement_config.h"
using std::string;
namespace testing {
GeneralPurposeMeasurementLoggerDaoIntegrationTest::
GeneralPurposeMeasurementLoggerDaoIntegrationTest()
: IntegrationTest(), Test() {
dba = nullptr;
dao = nullptr;
}
GeneralPurposeMeasurementLoggerDaoIntegrationTest::
~GeneralPurposeMeasurementLoggerDaoIntegrationTest() {}
void GeneralPurposeMeasurementLoggerDaoIntegrationTest::SetUp() {
dba = new supla_db_access_provider();
ASSERT_TRUE(dba != nullptr);
dao = new supla_general_purpose_measurement_logger_dao(dba);
ASSERT_TRUE(dao != nullptr);
initTestDatabase();
Test::SetUp();
}
void GeneralPurposeMeasurementLoggerDaoIntegrationTest::TearDown() {
if (dao) {
delete dao;
dao = nullptr;
}
if (dba) {
delete dba;
dba = nullptr;
}
Test::TearDown();
}
TEST_F(GeneralPurposeMeasurementLoggerDaoIntegrationTest, add) {
ASSERT_TRUE(dba->connect());
string result;
sqlQuery("SELECT count(*) as count FROM supla_gp_measurement_log", &result);
EXPECT_EQ(result, "count\n0\n");
supla_general_purpose_measurement_analyzer analyzer(234);
supla_channel_general_purpose_measurement_value value;
general_purpose_measurement_config cfg;
TChannelConfig_GeneralPurposeMeasurement raw_config = {};
value.set_value(23.34);
analyzer.add_sample(&value, &cfg);
raw_config.KeepHistory = 1;
cfg.set_config(&raw_config);
value.set_value(NAN);
analyzer.add_sample(&value, &cfg);
usleep(1000001);
value.set_value(123.34);
analyzer.add_sample(&value, &cfg);
usleep(1000001);
value.set_value(4);
analyzer.add_sample(&value, &cfg);
usleep(1000001);
value.set_value(15);
analyzer.add_sample(&value, &cfg);
usleep(1000001); // For avg to be available, you must wait at least a second.
dao->add(&analyzer);
result = "";
sqlQuery(
"SELECT channel_id, open_value, close_value, avg_value, max_value, "
"min_value FROM supla_gp_measurement_log WHERE date >= "
"DATE_ADD(UTC_TIMESTAMP(), INTERVAL -2 SECOND) AND date <= "
"DATE_ADD(UTC_TIMESTAMP(), INTERVAL 2 SECOND)",
&result);
EXPECT_EQ(result,
"channel_id\topen_value\tclose_value\tavg_value\tmax_value\tmin_"
"value\n234\t123.34\t15\t47.446666666666665\t123.34\t4\n");
}
} /* namespace testing */