-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathInfluxDBTest.cxx
199 lines (164 loc) · 7.49 KB
/
InfluxDBTest.cxx
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
// MIT License
//
// Copyright (c) 2020-2025 offa
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "InfluxDB/InfluxDB.h"
#include "InfluxDB/InfluxDBException.h"
#include "mock/TransportMock.h"
#include <catch2/catch_test_macros.hpp>
#include <catch2/trompeloeil.hpp>
namespace influxdb::test
{
namespace
{
constexpr std::chrono::time_point<std::chrono::system_clock> ignoreTimestamp(std::chrono::milliseconds(4567));
}
TEST_CASE("Ctor throws on nullptr transport", "[InfluxDBTest]")
{
CHECK_THROWS_AS(InfluxDB{nullptr}, InfluxDBException);
}
TEST_CASE("Write transmits point", "[InfluxDBTest]")
{
auto mock = std::make_shared<TransportMock>();
REQUIRE_CALL(*mock, send("p f0=71i 4567000000"));
InfluxDB db{std::make_unique<TransportAdapter>(mock)};
db.write(Point{"p"}.addField("f0", 71).setTimestamp(ignoreTimestamp));
}
TEST_CASE("Write transmits points", "[InfluxDBTest]")
{
auto mock = std::make_shared<TransportMock>();
REQUIRE_CALL(*mock, send("p0 f0=0i 4567000000\np1 f1=1i 4567000000\np2 f2=2i 4567000000"));
InfluxDB db{std::make_unique<TransportAdapter>(mock)};
db.write({Point{"p0"}.addField("f0", 0).setTimestamp(ignoreTimestamp),
Point{"p1"}.addField("f1", 1).setTimestamp(ignoreTimestamp),
Point{"p2"}.addField("f2", 2).setTimestamp(ignoreTimestamp)});
}
TEST_CASE("Write adds global tags", "[InfluxDBTest]")
{
auto mock = std::make_shared<TransportMock>();
REQUIRE_CALL(*mock, send(R"(p0,x=1,t\,\=\ =v\,\=\ f0=11i 4567000000)"
"\n"
R"(p1,x=1,t\,\=\ =v\,\=\ ,existing=yes f1=22i 4567000000)"
"\n"
R"(p2,x=1,t\,\=\ =v\,\=\ f2=33i 4567000000)"));
REQUIRE_CALL(*mock, send(R"(p4,x=1,t\,\=\ =v\,\=\ f3=44i 4567000000)"));
REQUIRE_CALL(*mock, send(R"(p5,x=1,t\,\=\ =v\,\=\ f4=55i 4567000000)"));
InfluxDB db{std::make_unique<TransportAdapter>(mock)};
db.addGlobalTag("x", "1");
// Special characters in global tags and values should be escaped
db.addGlobalTag("t,= ", "v,= ");
db.write({Point{"p0"}.addField("f0", 11).setTimestamp(ignoreTimestamp),
Point{"p1"}.addField("f1", 22).addTag("existing", "yes").setTimestamp(ignoreTimestamp),
Point{"p2"}.addField("f2", 33).setTimestamp(ignoreTimestamp)});
db.write(Point{"p4"}.addField("f3", 44).setTimestamp(ignoreTimestamp));
db.batchOf(1);
db.write(Point{"p5"}.addField("f4", 55).setTimestamp(ignoreTimestamp));
}
TEST_CASE("Write with batch enabled adds point to batch if size not reached", "[InfluxDBTest]")
{
using trompeloeil::_;
auto mock = std::make_shared<TransportMock>();
InfluxDB db{std::make_unique<TransportAdapter>(mock)};
db.batchOf(3);
db.write(Point{"x"});
ALLOW_CALL(*mock, send(_));
db.flushBatch();
}
TEST_CASE("Write with batch enabled writes points if size reached", "[InfluxDBTest]")
{
using trompeloeil::_;
auto mock = std::make_shared<TransportMock>();
REQUIRE_CALL(*mock, send("x 4567000000\ny 4567000000\nz 4567000000"));
InfluxDB db{std::make_unique<TransportAdapter>(mock)};
db.batchOf(3);
db.write(Point{"x"}.setTimestamp(ignoreTimestamp));
db.write({Point{"y"}.setTimestamp(ignoreTimestamp),
Point{"z"}.setTimestamp(ignoreTimestamp),
Point{"not-transmitted"}.setTimestamp(ignoreTimestamp)});
ALLOW_CALL(*mock, send(_));
db.flushBatch();
}
TEST_CASE("Flush batch transmits pending points", "[InfluxDBTest]")
{
using trompeloeil::_;
auto mock = std::make_shared<TransportMock>();
InfluxDB db{std::make_unique<TransportAdapter>(mock)};
db.batchOf(300);
db.write(Point{"x"}.setTimestamp(ignoreTimestamp));
db.write({Point{"y"}.setTimestamp(ignoreTimestamp),
Point{"z"}.setTimestamp(ignoreTimestamp)});
REQUIRE_CALL(*mock, send("x 4567000000\ny 4567000000\nz 4567000000"));
db.flushBatch();
}
TEST_CASE("Destructs cleanly with pending batches", "[InfluxDBTest]")
{
using trompeloeil::_;
auto mock = std::make_shared<TransportMock>();
{
ALLOW_CALL(*mock, send(_)).THROW(std::runtime_error{"Intentional"});
InfluxDB db{std::make_unique<TransportAdapter>(mock)};
db.batchOf(100);
db.write(Point{"x"}.setTimestamp(ignoreTimestamp));
}
}
TEST_CASE("Flush batch does nothing if batch disabled", "[InfluxDBTest]")
{
auto mock = std::make_shared<TransportMock>();
InfluxDB db{std::make_unique<TransportAdapter>(mock)};
{
REQUIRE_CALL(*mock, send("x 4567000000"));
db.write(Point{"x"}.setTimestamp(ignoreTimestamp));
}
db.flushBatch();
}
TEST_CASE("Clear Batch clears batch", "[InfluxDBTest]")
{
auto mock = std::make_shared<TransportMock>();
InfluxDB db{std::make_unique<TransportAdapter>(mock)};
db.batchOf(10);
db.write(Point{"x"}.setTimestamp(ignoreTimestamp));
CHECK(db.batchSize() == 1);
db.clearBatch();
CHECK(db.batchSize() == 0);
}
TEST_CASE("Create database throws if unsupported by transport", "[InfluxDBTest]")
{
auto mock = std::make_shared<TransportMock>();
REQUIRE_CALL(*mock, createDatabase()).THROW(InfluxDBException{"Intentional"});
InfluxDB db{std::make_unique<TransportAdapter>(mock)};
CHECK_THROWS_AS(db.createDatabaseIfNotExists(), InfluxDBException);
}
TEST_CASE("Create database creates database through transport", "[InfluxDBTest]")
{
auto mock = std::make_shared<TransportMock>();
REQUIRE_CALL(*mock, createDatabase());
InfluxDB db{std::make_unique<TransportAdapter>(mock)};
db.createDatabaseIfNotExists();
}
TEST_CASE("Execute executes query", "[InfluxDBTest]")
{
const std::string response = "name: databases\nname\n----\n_internal\n";
auto mock = std::make_shared<TransportMock>();
REQUIRE_CALL(*mock, execute("show databases")).RETURN(response);
InfluxDB db{std::make_unique<TransportAdapter>(mock)};
const auto result = db.execute("show databases");
CHECK(result == response);
}
}