DevelopmentTests.cpp
Go to the documentation of this file.
1 #include <cppunit/extensions/HelperMacros.h>
2 #include <NovaDatabase/Table.h>
3 #include <boost/shared_ptr.hpp>
4 
5 #include <NovaDAQConventions/DAQConventions.h>
6 
7 // dummy comment
8 
9 using namespace nova::database;
10 
12  "NovaDatabase/cxx/unittest/basicTestTable3.xml";
13 
14 class DevelopmentTests : public CppUnit::TestFixture
15 {
16  CPPUNIT_TEST_SUITE(DevelopmentTests);
17  CPPUNIT_TEST(populateDBTable);
18  CPPUNIT_TEST(testUpdateTwice);
19  // CPPUNIT_TEST(testEmptyStringVsNULL);
20  CPPUNIT_TEST(testColumnLookup);
21  CPPUNIT_TEST(testDistinctAndOrder);
22  // CPPUNIT_TEST(testTableCreationAndDestruction);
23  // CPPUNIT_TEST(testDBCommandCache);
24  CPPUNIT_TEST(testUserName);
25  CPPUNIT_TEST(cleanupDBTable);
26  CPPUNIT_TEST_SUITE_END();
27 
28 public:
29  void setUp();
30  void tearDown();
31 
32  void populateDBTable();
33  void testColumnLookup();
34  void testDistinctAndOrder();
35  void testTableCreationAndDestruction();
36  void testDBCommandCache();
37  void testEmptyStringVsNULL();
38  void testUpdateTwice();
39  void cleanupDBTable();
40  void testUserName();
41 
42 private:
43 
44  static boost::shared_ptr<Table> _dbt3;
45 };
46 
47 boost::shared_ptr<Table> DevelopmentTests::_dbt3;
48 
49 void
51 {
52  // flush stdout to get progress markers to display in a timely way
53  std::cout.flush();
54 }
55 
56 void
58 {
59  // flush stdout to get progress markers to display in a timely way
60  std::cout.flush();
61 }
62 
63 void
65 {
66  std::cout << "Column lookup test:" << std::endl;
67  CPPUNIT_ASSERT(_dbt3->ExistsInDB());
68  _dbt3->Clear();
69  CPPUNIT_ASSERT(_dbt3->LoadFromDB());
70 
71  int nRow = _dbt3->NRow();
72  CPPUNIT_ASSERT(nRow > 0);
73 
74  // test individual columns
75  std::string columnValue;
76  Row* row = _dbt3->GetRow(0);
77  Column* col = row->Col("id");
78  CPPUNIT_ASSERT(col != 0);
79  CPPUNIT_ASSERT(col->Get(columnValue));
80  col = row->Col("channel");
81  CPPUNIT_ASSERT(col != 0);
82  CPPUNIT_ASSERT(col->Get(columnValue));
83  col = row->Col("value1");
84  CPPUNIT_ASSERT(col != 0);
85  CPPUNIT_ASSERT(col->Get(columnValue));
86  col = row->Col("value2");
87  CPPUNIT_ASSERT(col != 0);
88  CPPUNIT_ASSERT(col->Get(columnValue));
89  col = row->Col("inserttime");
90  CPPUNIT_ASSERT(col != 0);
91  CPPUNIT_ASSERT(col->Get(columnValue));
92  col = row->Col("insertuser");
93  CPPUNIT_ASSERT(col != 0);
94  CPPUNIT_ASSERT(col->Get(columnValue));
95 
96  // test the full set of columns
97  bool foundChannelColumn = false;
98  CPPUNIT_ASSERT(_dbt3->NCol() == 11);
99  for (int idx = 0; idx < _dbt3->NCol(); ++idx) {
100  Column& colRef = row->Col(idx);
101  std::string cname = colRef.Name();
102  if (! colRef.IsNull()) {
103  if (cname != "nameid" && cname != "updateuser" &&
104  cname != "updatetime" && cname != "insertuser" &&
105  cname != "inserttime") {
106  CPPUNIT_ASSERT(colRef.Get(columnValue));
107  }
108  if (colRef.Name() == "channel") {
109  foundChannelColumn = true;
110  }
111  }
112  }
113  CPPUNIT_ASSERT(foundChannelColumn);
114 }
115 
116 void
118 {
119  CPPUNIT_ASSERT(_dbt3->ExistsInDB());
120 
121  _dbt3->Clear();
122  CPPUNIT_ASSERT(_dbt3->AddDistinctColumn("string1"));
123  CPPUNIT_ASSERT(_dbt3->LoadFromDB());
124  CPPUNIT_ASSERT(_dbt3->NRow() == 2);
125 
126  _dbt3->Clear();
127  CPPUNIT_ASSERT(_dbt3->AddDistinctColumn("string2"));
128  CPPUNIT_ASSERT(_dbt3->LoadFromDB());
129  CPPUNIT_ASSERT(_dbt3->NRow() == 3);
130 
131  _dbt3->Clear();
132  CPPUNIT_ASSERT(_dbt3->AddDistinctColumn("string1"));
133  CPPUNIT_ASSERT(_dbt3->AddDistinctColumn("string2"));
134  CPPUNIT_ASSERT(_dbt3->LoadFromDB());
135  CPPUNIT_ASSERT(_dbt3->NRow() == 3);
136 
137  _dbt3->Clear();
138  CPPUNIT_ASSERT(_dbt3->AddOrderColumn("channel"));
139  CPPUNIT_ASSERT(_dbt3->AddOrderColumn("value2"));
140  CPPUNIT_ASSERT(_dbt3->LoadFromDB());
141 
142  std::vector<int> id;
143  int tid;
144  for (int i=0; i<_dbt3->NRow(); ++i) {
145  CPPUNIT_ASSERT(_dbt3->GetRow(i)->Col(0).Get(tid));
146  id.push_back(tid);
147  }
148  unsigned int sz = id.size();
149  CPPUNIT_ASSERT(id[sz-3]==2 && id[sz-2]==3 && id[sz-1]==4);
150 
151 }
152 
153 void
155 {
156  boost::shared_ptr<Table> localDBTablePtr;
157 
158  std::cout << "Table creation test:" << std::endl;
159  for (int idx = 0; idx < 200; ++idx) {
160  //std::cout << "Table creation test, version 1, index "
161  // << idx << std::endl;
162  localDBTablePtr.reset(new Table(TABLE3_DEFINITION_FILE.c_str()));
163  localDBTablePtr->SetDetector(novadaq::cnv::kNDOS);
164  CPPUNIT_ASSERT(localDBTablePtr->ExistsInDB());
165  }
166 
167  std::cout << "Table connection test:" << std::endl;
168  for (int idx = 0; idx < 200; ++idx) {
169  //std::cout << "Table creation test, version 1, index "
170  // << idx << std::endl;
171  localDBTablePtr.reset(new Table(TABLE3_DEFINITION_FILE.c_str()));
172  localDBTablePtr->SetDetector(novadaq::cnv::kNDOS);
173  localDBTablePtr->GetConnection();
174  CPPUNIT_ASSERT(localDBTablePtr->ExistsInDB());
175  }
176 }
177 
178 void
180 {
181  std::cout << "Populate dB table test:" << std::endl;
182  // create the table that is used in these tests
183  _dbt3.reset(new Table(TABLE3_DEFINITION_FILE.c_str()));
184  _dbt3->SetDetector(novadaq::cnv::kNDOS);
185  CPPUNIT_ASSERT(_dbt3->ExistsInDB());
186  _dbt3->Clear();
187  CPPUNIT_ASSERT(_dbt3->LoadFromDB());
188 
189  // create some filler rows, if needed
190  int nRow = _dbt3->NRow();
191  if (nRow == 0) {
192  boost::shared_ptr<Row> rowPtr(_dbt3->NewRow());
193  CPPUNIT_ASSERT(rowPtr->Set("channel", 101));
194  CPPUNIT_ASSERT(rowPtr->Set("value1", 5.0));
195  CPPUNIT_ASSERT(rowPtr->Set("value2", 42.6));
196  CPPUNIT_ASSERT(rowPtr->Set("string1", "one"));
197  CPPUNIT_ASSERT(rowPtr->Set("string2", "two"));
198  _dbt3->AddRow(rowPtr.get());
199  CPPUNIT_ASSERT(_dbt3->WriteToDB());
200  rowPtr.reset(_dbt3->NewRow());
201  CPPUNIT_ASSERT(rowPtr->Set("channel", 102));
202  CPPUNIT_ASSERT(rowPtr->Set("value1", 5.0));
203  CPPUNIT_ASSERT(rowPtr->Set("value2", 42.7));
204  CPPUNIT_ASSERT(rowPtr->Set("string1", "one"));
205  CPPUNIT_ASSERT(rowPtr->Set("string2", "four"));
206  _dbt3->AddRow(rowPtr.get());
207  CPPUNIT_ASSERT(_dbt3->WriteToDB());
208  rowPtr.reset(_dbt3->NewRow());
209  CPPUNIT_ASSERT(rowPtr->Set("channel", 103));
210  CPPUNIT_ASSERT(rowPtr->Set("value1", 5.0));
211  CPPUNIT_ASSERT(rowPtr->Set("value2", 42.8));
212  CPPUNIT_ASSERT(rowPtr->Set("string1", "one"));
213  CPPUNIT_ASSERT(rowPtr->Set("string2", "two"));
214  _dbt3->AddRow(rowPtr.get());
215  CPPUNIT_ASSERT(_dbt3->WriteToDB());
216  }
217 
218  // clear the Table object as a nicety
219  _dbt3->Clear();
220 }
221 
222 void
224 {
225  std::cout << "dB command cache test:" << std::endl;
226  // create the table that is used in these tests
227  _dbt3.reset(new Table(TABLE3_DEFINITION_FILE.c_str()));
228  _dbt3->SetDetector(novadaq::cnv::kNDOS);
229  CPPUNIT_ASSERT(_dbt3->ExistsInDB());
230  _dbt3->Clear();
231  CPPUNIT_ASSERT(_dbt3->LoadFromDB());
232 
233  _dbt3->LoadFromDB();
234  int initialRowCount = _dbt3->NRow();
235  int channel = 3 * (initialRowCount + 1);
236  time_t now = time(0);
237  int tmpVal = now % 10000;
238  float value1 = (float) (((double) tmpVal) / 100.0);
239  float value2 = (float) (((double) tmpVal) / 357.0);
240 
241  // create the test row and store it in the database
242  boost::shared_ptr<Row> rowPtr(_dbt3->NewRow());
243  CPPUNIT_ASSERT(rowPtr->Set("channel", channel));
244  CPPUNIT_ASSERT(rowPtr->Set("value1", value1));
245  CPPUNIT_ASSERT(rowPtr->Set("value2", value2));
246  CPPUNIT_ASSERT(rowPtr->Set("string1", "three"));
247  CPPUNIT_ASSERT(rowPtr->Set("string2", "two"));
248  _dbt3->AddRow(rowPtr.get());
249 
250  _dbt3->SetUser("dummy");
251  CPPUNIT_ASSERT(_dbt3->WriteToDB());
252  _dbt3->SetUser(getenv("USER"));
253 
254  // JMP NOTE: need to add some code to check that cache file was created/appended to
255  // for now, I'm doing this by hand...
256 
257  _dbt3->Clear();
258 }
259 
260 void
262 {
263  std::cout << "\ntestUserName\n" << std::endl;
264  sleep(2);
265  // create the table that is used in these tests
266  _dbt3.reset(new Table(TABLE3_DEFINITION_FILE.c_str()));
267  _dbt3->SetDetector(novadaq::cnv::kNDOS);
268  CPPUNIT_ASSERT(_dbt3->ExistsInDB());
269  _dbt3->Clear();
270  CPPUNIT_ASSERT(_dbt3->LoadFromDB());
271 
272  _dbt3->LoadFromDB();
273  int initialRowCount = _dbt3->NRow();
274  int channel = 3 * (initialRowCount + 1);
275  time_t now = time(0);
276  int tmpVal = now % 10000;
277  float value1 = (float) (((double) tmpVal) / 100.0);
278  float value2 = (float) (((double) tmpVal) / 357.0);
279 
280  // create the test row and store it in the database
281  boost::shared_ptr<Row> rowPtr(_dbt3->NewRow());
282  CPPUNIT_ASSERT(rowPtr->Set("channel", channel));
283  CPPUNIT_ASSERT(rowPtr->Set("value1", value1));
284  CPPUNIT_ASSERT(rowPtr->Set("value2", value2));
285  CPPUNIT_ASSERT(rowPtr->Set("string1", "three"));
286  CPPUNIT_ASSERT(rowPtr->Set("string2", "two"));
287  _dbt3->AddRow(rowPtr.get());
288 
289  _dbt3->SetIgnoreEnvVar(true);
290  _dbt3->SetUser("nova_reader");
291  _dbt3->SetPasswordFile("./unittest/nova_reader.dat");
292  bool didIt = (! _dbt3->WriteToDB());
293  if (didIt) std::cout << "[INSERT should have failed]" << std::endl;
294  CPPUNIT_ASSERT(didIt);
295  _dbt3->SetUser(getenv("USER"));
296  _dbt3->DisablePasswordAccess();
297  CPPUNIT_ASSERT(_dbt3->WriteToDB());
298  CPPUNIT_ASSERT(_dbt3->GetConnection());
299  _dbt3->SetUser("nova_reader");
300  rowPtr->Set("channel", channel+1);
301  rowPtr->Set("value1", value1+10.);
302  rowPtr->Set("value2", value2+10.);
303  rowPtr->Set("string1", "three+");
304  rowPtr->Set("string2", "two++");
305  _dbt3->AddRow(rowPtr.get());
306  CPPUNIT_ASSERT(_dbt3->WriteToDB());
307  CPPUNIT_ASSERT(_dbt3->CloseConnection());
308 
309  _dbt3->Clear();
310 }
311 
312 void
314 {
315  _dbt3->Clear();
316  std::cout << "Empty String vs. NULL Test:" << std::endl;
317  Row* rowPtr = _dbt3->NewRow();
318 
319  CPPUNIT_ASSERT(rowPtr->Set("channel", 1024));
320  CPPUNIT_ASSERT(rowPtr->Set("value1", 3.1415));
321  CPPUNIT_ASSERT(rowPtr->Set("value2", 0.01));
322  CPPUNIT_ASSERT(rowPtr->Set("string1", ""));
323  CPPUNIT_ASSERT(rowPtr->Set("string2", "NULL"));
324  _dbt3->AddRow(rowPtr);
325  CPPUNIT_ASSERT(_dbt3->WriteToDB());
326  delete rowPtr;
327 
328  _dbt3->Clear();
329  CPPUNIT_ASSERT(_dbt3->LoadFromDB());
330  int nRows = _dbt3->NRow();
331  rowPtr = _dbt3->GetRow(nRows-1);
332  std::string testStr;
333  CPPUNIT_ASSERT(rowPtr->Col("string1")->Get(testStr));
334  CPPUNIT_ASSERT(testStr == "");
335  std::cout << "\"string1\" is correctly read back as an empty string." << std::endl;
336  CPPUNIT_ASSERT(rowPtr->Col("string2")->IsNull());
337  std::cout << "\"string2\" is correctly read back as NULL." << std::endl;
338  _dbt3->Clear();
339 }
340 
341 void
343 {
344  _dbt3->Clear();
345  _dbt3->SetUser(getenv("USER"));
346  std::cout << "Update-Twice Test:" << std::endl;
347  Row* rowPtr = _dbt3->NewRow();
348 
349  CPPUNIT_ASSERT(rowPtr->Set("channel", 2048));
350  CPPUNIT_ASSERT(rowPtr->Set("value1", 929.929));
351  CPPUNIT_ASSERT(rowPtr->Set("value2", 0.929));
352  CPPUNIT_ASSERT(rowPtr->Set("string1", "929"));
353  CPPUNIT_ASSERT(rowPtr->Set("string2", "92929"));
354  _dbt3->AddRow(rowPtr);
355  CPPUNIT_ASSERT(_dbt3->WriteToDB());
356  delete rowPtr;
357 
358  _dbt3->Clear();
359  CPPUNIT_ASSERT(_dbt3->LoadFromDB());
360  int nRows = _dbt3->NRow();
361  CPPUNIT_ASSERT(_dbt3->GetRow(nRows-1)->Update("channel",2049));
362  CPPUNIT_ASSERT(_dbt3->WriteToDB());
363  _dbt3->Clear();
364  CPPUNIT_ASSERT(_dbt3->LoadFromDB());
365  int myChan;
366  CPPUNIT_ASSERT(_dbt3->GetRow(nRows-1)->Col("channel")->Get(myChan));
367  CPPUNIT_ASSERT(myChan == 2049);
368  std::string updateUser, updateTime;
369  CPPUNIT_ASSERT(_dbt3->GetRow(nRows-1)->Col("updateuser")->Get(updateUser));
370  CPPUNIT_ASSERT(_dbt3->GetRow(nRows-1)->Col("updatetime")->Get(updateTime));
371  sleep(2);
372  CPPUNIT_ASSERT(_dbt3->GetRow(nRows-1)->Update("channel",2050));
373  CPPUNIT_ASSERT(_dbt3->WriteToDB());
374  _dbt3->Clear();
375  CPPUNIT_ASSERT(_dbt3->LoadFromDB());
376  CPPUNIT_ASSERT(_dbt3->GetRow(nRows-1)->Col("channel")->Get(myChan));
377  CPPUNIT_ASSERT(myChan == 2050);
378  std::string updateUser_save = updateUser;
379  std::string updateTime_save = updateTime;
380  CPPUNIT_ASSERT(_dbt3->GetRow(nRows-1)->Col("updateuser")->Get(updateUser));
381  CPPUNIT_ASSERT(_dbt3->GetRow(nRows-1)->Col("updatetime")->Get(updateTime));
382  CPPUNIT_ASSERT(updateUser_save == updateUser);
383  std::cout << "Two updatetimes: " << updateTime_save << ", "
384  << updateTime << std::endl;
385 
386 }
387 
388 void
390 {
391  _dbt3.reset();
392 }
393 
395 CPPUNIT_REGISTRY_ADD_TO_DEFAULT("NovaDatabase");
::xsd::cxx::tree::id< char, ncname > id
Definition: Database.h:165
bool Set(std::string cname, T value)
Definition: Row.h:30
void Clear()
Definition: Row.cpp:61
::xsd::cxx::tree::time< char, simple_type > time
Definition: Database.h:194
const std::string TABLE3_DEFINITION_FILE
Int_t col[ntarg]
Definition: Style.C:29
Prototype Near Detector on the surface at FNAL.
std::string getenv(std::string const &name)
void testTableCreationAndDestruction()
bool Get(T &val) const
Definition: Column.h:53
bool IsNull() const
Definition: Column.h:39
OStream cout
Definition: OStream.cxx:6
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
std::string Name() const
Definition: Column.h:35
struct Table Table
Definition: TexBuilder.h:2
static boost::shared_ptr< Table > _dbt3
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(DevelopmentTests,"NovaDatabase")
CPPUNIT_REGISTRY_ADD_TO_DEFAULT("NovaDatabase")
Column & Col(int i)
Definition: Row.h:51