QNLP  v1.0
test_db.cpp
Go to the documentation of this file.
1 
12 //#define CATCH_CONFIG_RUNNER
13 
14 //For DB file access paths
15 #include <fstream>
16 #include <cstdlib>
17 
18 #include "db_helper.hpp"
19 #include "corpus_utils.hpp"
20 #include "catch2/catch.hpp"
21 
22 using namespace QNLP;
23 
28 TEST_CASE("DBHelper unit tests: Default constructor: DBHelper()","[db]"){
29  DBHelper db;
30 
31  //REQUIRE(db.getFilename() == "qnlp_tagged_corpus.sqlite");
32  REQUIRE(db.getFilename() == "");
33  REQUIRE(db.getDBRef() == nullptr);
34 
35  SECTION("openDB(const std::string filename) with 'test_db.db'"){
36  int ret_code = db.openDB("test_db.db");
37  REQUIRE(ret_code == 0);
38  REQUIRE(db.getDBRef() != nullptr);
39  REQUIRE(db.getFilename().compare("test_db.db") == 0);
40 
41  SECTION("closeDB()"){
42  db.closeDB();
43  REQUIRE(db.getFilename() == "");
44  REQUIRE(db.getDBRef() == nullptr );
45  }
46  }
47  SECTION("openDB(const std::string filename) with 'none.db'"){
48  REQUIRE(db.openDB("none.db") != 0);
49  REQUIRE(db.getDBRef() == nullptr);
50  REQUIRE(db.getFilename().compare("") == 0);
51 
52  SECTION("closeDB()"){
53  db.closeDB();
54  REQUIRE(db.getFilename() == "");
55  REQUIRE(db.getDBRef() == nullptr );
56  }
57  }
58 }
59 
65 TEST_CASE("Parameterised constructor: DBHelper(const std::string filename) with 'test.db'","[db]"){
66  DBHelper db("test_db.db");
67 
68  REQUIRE(db.getFilename() == "test_db.db");
69  REQUIRE(db.getDBRef() != nullptr);
70 
71  SECTION("openDB(const std::string filename) with 'test_db.db'"){
72  REQUIRE(db.openDB("test_db.db") == 0);
73  REQUIRE(db.getDBRef() != nullptr);
74  REQUIRE(db.getFilename().compare("test_db.db") == 0);
75  }
76  SECTION("closeDB()"){
77  db.closeDB();
78  REQUIRE(db.getFilename() == "");
79  REQUIRE(db.getDBRef() == nullptr );
80  }
81 }
82 
88 TEST_CASE("Parameterised constructor: DBHelper(const std::string filename) with 'none.db'","[db]"){
89  DBHelper db("none.db");
90 
91  REQUIRE(db.getFilename() == "");
92  REQUIRE(db.getDBRef() == nullptr);
93 
94  SECTION("openDB(const std::string filename) with 'none.db'"){
95  REQUIRE(db.openDB("none.db") != 0 );
96  REQUIRE(db.getDBRef() == nullptr);
97  REQUIRE(db.getFilename().compare("") == 0);
98  }
99  SECTION("closeDB()"){
100  db.closeDB();
101  REQUIRE(db.getFilename() == "");
102  REQUIRE(db.getDBRef() == nullptr );
103  }
104 }
105 
110 TEST_CASE("CorpusUtils unit tests: Default constructor: CorpusUtils()","[db]"){
111  CorpusUtils cu;
112 
113  REQUIRE(cu.getBinToName().size() == 0);
114  REQUIRE(cu.getNameToBin().size() == 0);
115  REQUIRE(cu.get_database_filename().compare("qnlp_tagged_corpus.sqlite") == 0);
116  REQUIRE(cu.getDBHelper().getFilename().compare("qnlp_tagged_corpus.sqlite") == 0);
117  REQUIRE(cu.getDBHelper().getDBRef() != nullptr);
118 
119  SECTION("Load data: data_type=noun, table is default ('corpus')"){
120  cu.loadData("noun");
121  std::size_t n_size[2] = {cu.getBinToName().size(), cu.getNameToBin().size()};
122  REQUIRE(n_size[0] != 0);
123  REQUIRE(n_size[1] != 0);
124  }
125  SECTION("Load data: data_type=verb, table is default ('corpus')"){
126  cu.loadData("verb");
127  std::size_t v_size[2] = {cu.getBinToName().size(), cu.getNameToBin().size()};
128  REQUIRE(v_size[0] != 0);
129  REQUIRE(v_size[1] != 0);
130  }
131  SECTION("Load data: data_type=noun and verb, table is default ('corpus')"){
132  cu.loadData("verb");
133  std::size_t v_size[2] = {cu.getBinToName().size(), cu.getNameToBin().size()};
134  REQUIRE(v_size[0] != 0);
135  REQUIRE(v_size[1] != 0);
136 
137  cu.loadData("noun");
138  std::size_t vn_size[2] = {cu.getBinToName().size(), cu.getNameToBin().size()};
139  REQUIRE(vn_size[0] != v_size[0]);
140  REQUIRE(vn_size[1] != v_size[1]);
141  }
142  SECTION("Load data: data_type=noun and verb, table='basis'"){
143  cu.loadData("verb", "basis");
144  std::size_t v_size[2] = {cu.getBinToName().size(), cu.getNameToBin().size()};
145  REQUIRE(v_size[0] != 0);
146  REQUIRE(v_size[1] != 0);
147 
148  cu.loadData("noun", "basis");
149  std::size_t vn_size[2] = {cu.getBinToName().size(), cu.getNameToBin().size()};
150  REQUIRE(vn_size[0] != v_size[0]);
151  REQUIRE(vn_size[1] != v_size[1]);
152  }
153 }
154 
159 TEST_CASE("CorpusUtils unit tests: Paramaterised constructor: CorpusUtils(test_db.db)","[db]"){
160  CorpusUtils cu("test_db.db");
161 
162  REQUIRE(cu.getBinToName().size() == 0);
163  REQUIRE(cu.getNameToBin().size() == 0);
164  REQUIRE(cu.get_database_filename() == cu.getDBHelper().getFilename());
165  REQUIRE(cu.get_database_filename().compare("test_db.db") == 0);
166  REQUIRE(cu.getDBHelper().getFilename().compare("test_db.db") == 0);
167  REQUIRE(cu.getDBHelper().getDBRef() != nullptr);
168 
169  SECTION("Load data: data_type=noun, table is default ('corpus')"){
170  cu.loadData("noun");
171  std::size_t n_size[2] = {cu.getBinToName().size(), cu.getNameToBin().size()};
172  REQUIRE(n_size[0] != 0);
173  REQUIRE(n_size[1] != 0);
174  }
175  SECTION("Load data: data_type=verb, table is default ('corpus')"){
176  cu.loadData("verb");
177  std::size_t v_size[2] = {cu.getBinToName().size(), cu.getNameToBin().size()};
178  REQUIRE(v_size[0] != 0);
179  REQUIRE(v_size[1] != 0);
180  }
181  SECTION("Load data: data_type=noun and verb, table is default ('corpus')"){
182  cu.loadData("verb");
183  std::size_t v_size[2] = {cu.getBinToName().size(), cu.getNameToBin().size()};
184  REQUIRE(v_size[0] != 0);
185  REQUIRE(v_size[1] != 0);
186 
187  cu.loadData("noun");
188  std::size_t vn_size[2] = {cu.getBinToName().size(), cu.getNameToBin().size()};
189  REQUIRE(vn_size[0] != v_size[0]);
190  REQUIRE(vn_size[1] != v_size[1]);
191  }
192  SECTION("Load data: data_type=noun and verb, table='basis'"){
193  cu.loadData("verb", "basis");
194  std::size_t v_size[2] = {cu.getBinToName().size(), cu.getNameToBin().size()};
195  REQUIRE(v_size[0] != 0);
196  REQUIRE(v_size[1] != 0);
197 
198  cu.loadData("noun", "basis");
199  std::size_t vn_size[2] = {cu.getBinToName().size(), cu.getNameToBin().size()};
200  REQUIRE(vn_size[0] != v_size[0]);
201  REQUIRE(vn_size[1] != v_size[1]);
202  }
203 }
204 
Database access and manipulation functions.
NTB & getNameToBin()
Get the NTB object. This is a map of maps, wherein the first key represents the grammatical type to l...
void closeDB()
Closes the database if the DB pointer is open.
Definition: db_helper.cpp:23
BTN & getBinToName()
Get the BTN object. This is a map of maps, wherein the first key represents the grammatical type to l...
void loadData(const std::string data_type, const std::string table="corpus")
Loads from the database for a given data type (noun, verb, etc)
sqlite3 * getDBRef()
Returns a pointer to the DB object.
Definition: db_helper.cpp:47
const std::string get_database_filename()
Get the database filename object.
int openDB(const std::string filename)
Opens the requested database, and gives the return code of the operation. If the DB pointer has alrea...
Definition: db_helper.cpp:32
std::string getFilename()
Get the name of the opened database.
Definition: db_helper.cpp:51
DBHelper & getDBHelper()
Return reference to database object.
TEST_CASE("DBHelper unit tests: Default constructor: DBHelper()","[db]")
Testing default constructor and subsequent functions on DBHelper class.
Definition: test_db.cpp:28