-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMockSQL.h
More file actions
97 lines (84 loc) · 2.95 KB
/
Copy pathMockSQL.h
File metadata and controls
97 lines (84 loc) · 2.95 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
#pragma once
/**
* Concrete mock implementations of the abstract SQL interfaces for use in
* unit tests. Include this header from any test file that needs a database
* connection without a real backend.
*/
#include "r2rml/MapSQLRow.h"
#include "r2rml/SQLConnection.h"
#include "r2rml/SQLResultSet.h"
#include "r2rml/SQLRow.h"
#include "r2rml/SQLValue.h"
#include "r2rml/StringSQLValue.h"
#include <memory>
#include <string>
#include <vector>
namespace r2rml {
namespace testing {
// ---------------------------------------------------------------------------
// MockSQLResultSet
//
// Iterates over a fixed, in-memory vector of MapSQLRows.
// ---------------------------------------------------------------------------
class MockSQLResultSet : public SQLResultSet {
public:
explicit MockSQLResultSet(std::vector<MapSQLRow> rows) : rows_(std::move(rows)) {
}
bool next() override {
++cursor_;
return cursor_ < static_cast<int>(rows_.size());
}
const SQLRow &getCurrentRow() const override {
return rows_[static_cast<size_t>(cursor_)];
}
private:
std::vector<MapSQLRow> rows_;
int cursor_ {-1};
};
// ---------------------------------------------------------------------------
// MockSQLConnection
//
// Returns pre-registered rows for any query whose text contains a registered
// key fragment. When multiple keys match, the longest one wins (so a more
// specific fragment takes priority over a shorter substring).
//
// Usage:
// MockSQLConnection conn;
// conn.addResult("EMP", { makeRow({{"EMPNO", StringSQLValue(42)}}) });
// ---------------------------------------------------------------------------
class MockSQLConnection : public SQLConnection {
public:
void addResult(std::string queryFragment, std::vector<MapSQLRow> rows) {
results_.push_back({std::move(queryFragment), std::move(rows)});
}
std::unique_ptr<SQLResultSet> execute(const std::string &query) override {
const std::vector<MapSQLRow> *best = nullptr;
size_t bestLen = 0;
for (const auto &kv : results_) {
if (query.find(kv.first) != std::string::npos && kv.first.size() > bestLen) {
bestLen = kv.first.size();
best = &kv.second;
}
}
if (best) {
return std::unique_ptr<SQLResultSet>(new MockSQLResultSet(*best));
}
return std::unique_ptr<SQLResultSet>(new MockSQLResultSet(std::vector<MapSQLRow> {}));
}
private:
std::vector<std::pair<std::string, std::vector<MapSQLRow>>> results_;
};
// ---------------------------------------------------------------------------
// makeRow helper
//
// Build a MapSQLRow from an initializer-list of {column, value} pairs.
// ---------------------------------------------------------------------------
inline MapSQLRow makeRow(std::initializer_list<std::pair<const std::string, StringSQLValue>> cols) {
std::map<std::string, std::unique_ptr<SQLValue>> m;
for (const auto &p : cols) {
m[p.first] = std::unique_ptr<SQLValue>(new StringSQLValue(p.second));
}
return MapSQLRow(std::move(m));
}
} // namespace testing
} // namespace r2rml