-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrulesTest.cpp
More file actions
154 lines (138 loc) · 4.86 KB
/
rulesTest.cpp
File metadata and controls
154 lines (138 loc) · 4.86 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
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
///#define BOOST_TEST_MODULE ruleTable
#include "testSetup.h"
#include "ruleTable.h"
#include "ruleset.h"
#include "fileread.h"
class rulesTest : public genericTable
{
public:
rulesTest() {};
~rulesTest() {} ;
void readCusorHandler( const pqxx::result &rows)
{
std::vector<std::string> fnames { "rule_name", "regex","rule_set","severity"};
std::vector<std::string> data {"test_rule","(warning|error)","5","warning"};
long idx =0;
for (pqxx::result::const_iterator rit = rows.begin(); rit != rows.end(); rit++,idx++)
{
pqxx::result::tuple row = *rit;
try
{
///debug
///std::cout << " " << row["rule_name"].c_str()<< std::endl;
BOOST_CHECK_EQUAL(data[0].compare(row["rule_name"].c_str()),0);
BOOST_CHECK_EQUAL(data[1].compare(row["regex"].c_str()),0);
BOOST_CHECK((row["rule_set"].as<long>()) <= 20000);
BOOST_CHECK_EQUAL(data[3].compare(row["severity"].c_str()),0);
}
catch (std::exception &e)
{
std::cout << e.what() << " " << __LINE__ << std::endl;
}
}
}
};
BOOST_AUTO_TEST_SUITE(s_PgSql_RulesTable)
BOOST_AUTO_TEST_CASE(rt_create)
{
testSetup su;
ruleTable rt(su.connectionString());
std::vector<std::string> fnames { "id","rule_name", "regex","rule_set","severity"};
std::vector<std::string> dataTypes {"serial","character varying","character varying","character varying","character varying"};
std::string condition;
std::string table="rule";
try {
rt.createTable(table,fnames,dataTypes);
}catch (const pqxx::sql_error &e)
{
std::string msg = e.query() + e.what();
su.elog()->errorL(critical,msg.c_str());
}
}
BOOST_AUTO_TEST_CASE(rt_readCursor)
{
testSetup su;
ruleTable rt(su.connectionString());
std::string ctable(
"CREATE TABLE \"rule\" \
( \
\"id\" serial, \
\"rule_name\" character varying, \
\"regex\" character varying, \
\"rule_set\" integer, \
\"severity\" character varying, \
CONSTRAINT ridpk PRIMARY KEY (id) \
);" );
std::vector<std::string> fnames { "rule_name", "regex","rule_set","severity"};
std::vector<std::string> data {"test_rule","(warning|error)","5","warning"};
std::string condition;
try {
pqxx::result r = rt.nontransQuery(nullptr,ctable);
/** write 20000 records to see how we handle ths **/
su.elog()->tokenL(notification,"start of 20000 record write");
for (int i =0 ;i < 20000; i++){
data[2] = std::to_string(i);
rt.write(nullptr,fnames,data,condition);
}
///see if record count is at least 20000
std::vector<std::string> cfields ={"count(*)"};
r = rt.readFields(nullptr,cfields,condition);
if (r.empty()){
BOOST_CHECK_MESSAGE(error,"FAILED TO COLLECT NUMBER OF COLUMNS");
} else {
BOOST_CHECK_GE (r.at(0).at(0).as<long>(),20000);
}
rulesTest * rTest = new rulesTest();
rt.setCusorCallBack(std::bind(&rulesTest::readCusorHandler,rTest,std::placeholders::_1));
su.elog()->tokenL(notification,"end of 20000 record write start of record read");
/**
* reading 10000 records
*/
r=rt.readCusor(nullptr,fnames,condition);
su.elog()->tokenL(notification,"end of 20000 record read/process");
/// size is zero since cursor was emptied
BOOST_CHECK_EQUAL(r.size(), 0);
delete rTest;
}catch (const pqxx::sql_error &e)
{
std::string msg = e.query() + e.what();
su.elog()->errorL(critical,msg.c_str());
}
}
/**
*
*/
BOOST_AUTO_TEST_CASE(rt_getRecordCount)
{
testSetup su;
ruleTable rt(su.connectionString());
std::string ctable(
"CREATE TABLE \"rule\" \
( \
\"id\" serial, \
\"rule_name\" character varying, \
\"regex\" character varying, \
\"rule_set\" integer, \
\"severity\" character varying, \
CONSTRAINT ridpk PRIMARY KEY (id) \
);" );
std::vector<std::string> fnames { "rule_name", "regex","rule_set","severity"};
std::vector<std::string> data {"test_rule","(warning|error)","5","warning"};
std::string condition;
try {
pqxx::result r = rt.nontransQuery(nullptr,ctable);
/** write 20000 records to see how we handle ths **/
su.elog()->tokenL(notification,"start of 20000 record write");
for (int i =0 ;i < 20000; i++){
data[2] = std::to_string(i);
rt.write(nullptr,fnames,data,condition);
}
std::string blank;
BOOST_CHECK_EQUAL(rt.getRecordCount(blank),20000);
}catch (const pqxx::sql_error &e)
{
std::string msg = e.query() + e.what();
su.elog()->errorL(critical,msg.c_str());
}
}
BOOST_AUTO_TEST_SUITE_END()