From 863f0db3650a7db276fa6f15cbd8760fac35ba9b Mon Sep 17 00:00:00 2001 From: JDierkse Date: Fri, 1 May 2020 11:15:15 +0200 Subject: [PATCH] Add Tests --- .gitignore | 2 + Libraries/Utilities | 1 + Makefile.conf | 13 ++++- Makefile.target | 7 ++- Test/Makefile | 1 + Test/MultipleConnections.cpp | 67 +++++++++++++++++++++++++ Test/MultipleConnections.h | 11 ++++ Test/MultipleThreads.cpp | 97 ++++++++++++++++++++++++++++++++++++ Test/MultipleThreads.h | 11 ++++ Test/SingleConnection.cpp | 54 ++++++++++++++++++++ Test/SingleConnection.h | 11 ++++ Test/Test.cc | 22 ++++++++ Test/UseDatabase.cpp | 45 +++++++++++++++++ Test/UseDatabase.h | 11 ++++ 14 files changed, 351 insertions(+), 2 deletions(-) create mode 120000 Libraries/Utilities create mode 120000 Test/Makefile create mode 100644 Test/MultipleConnections.cpp create mode 100644 Test/MultipleConnections.h create mode 100644 Test/MultipleThreads.cpp create mode 100644 Test/MultipleThreads.h create mode 100644 Test/SingleConnection.cpp create mode 100644 Test/SingleConnection.h create mode 100644 Test/Test.cc create mode 100644 Test/UseDatabase.cpp create mode 100644 Test/UseDatabase.h diff --git a/.gitignore b/.gitignore index 851ef09..0b9ff8b 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,5 @@ .*.swp .AppleDouble lib +test* +!test.cc diff --git a/Libraries/Utilities b/Libraries/Utilities new file mode 120000 index 0000000..4a6b454 --- /dev/null +++ b/Libraries/Utilities @@ -0,0 +1 @@ +../../Utilities \ No newline at end of file diff --git a/Makefile.conf b/Makefile.conf index ded1ce8..53df5d3 100644 --- a/Makefile.conf +++ b/Makefile.conf @@ -2,5 +2,16 @@ # Makefile.conf # -CFLAGS += -I$(ROOTPATH) -I$(ROOTPATH)/include -I$(ROOTPATH)/Libraries/Logging/include +LFLAGS += -lUtilities +LFLAGS += -L$(ROOTPATH)/Libraries/Utilities/lib/$(ARCH) +CFLAGS += -I$(ROOTPATH)/Libraries/Utilities/include + +LFLAGS += -lLogging +LFLAGS += -L$(ROOTPATH)/Libraries/Logging/lib/$(ARCH) +CFLAGS += -I$(ROOTPATH)/Libraries/Logging/include + +LFLAGS += -lMySQL -lmysqlclient -lmysqlcppconn +LFLAGS += -L$(ROOTPATH)/lib/$(ARCH) +CFLAGS += -I$(ROOTPATH) -I$(ROOTPATH)/include + DEBUGDIR := .debug diff --git a/Makefile.target b/Makefile.target index af751b1..bea52d0 100644 --- a/Makefile.target +++ b/Makefile.target @@ -7,6 +7,11 @@ MySQL.a.$(ARCH) : $(OBJECTS) MySQL.a: $(call build_target,$@) +test.$(ARCH): $(OBJECTS) Test/Test.o.$(ARCH) | MySQL.a.$(ARCH) + $(call build_target_arch,$@,$^) +test: + $(call build_target,$@) + .DEFAULT_GOAL := MySQL.a -TARGETS += MySQL.a +TARGETS += MySQL.a test diff --git a/Test/Makefile b/Test/Makefile new file mode 120000 index 0000000..d0b0e8e --- /dev/null +++ b/Test/Makefile @@ -0,0 +1 @@ +../Makefile \ No newline at end of file diff --git a/Test/MultipleConnections.cpp b/Test/MultipleConnections.cpp new file mode 100644 index 0000000..db8c484 --- /dev/null +++ b/Test/MultipleConnections.cpp @@ -0,0 +1,67 @@ +#include "MultipleConnections.h" +#include "MySQLClient.h" +#include +#include + +namespace Test { + +bool MultipleConnections() +{ + Logging::Log(Logging::Severity::Info, "MultipleConnections"); + + try + { + std::string hostname1 = "MySQL"; + std::string username1 = "datalog"; + std::string password1 = "NfhdUwjjdRbslR"; + std::string database1 = "datalog"; + + std::string hostname2 = "Pi"; + std::string username2 = "root"; + std::string password2 = "Wh1sK3y"; + std::string database2 = "domotica"; + + MySQL::MySQLClient MySQLClient1; + MySQL::MySQLClient MySQLClient2; + + MySQLClient1.Connect(hostname1, username1, password1, database1); + MySQLClient2.Connect(hostname2, username2, password2, database2); + + if (!MySQLClient1.Connected() || !MySQLClient2.Connected()) + return false; + + std::stringstream query1; + query1 << "SELECT * FROM `datalog` LIMIT 1;"; + + std::stringstream query2; + query2 << "SELECT * FROM `device` LIMIT 1;"; + + auto result1 = MySQLClient1.ExecuteQuery(query1.str()); + auto result2 = MySQLClient2.ExecuteQuery(query2.str()); + + if (result1.RowsCount() == 0 || result2.RowsCount() == 0) + return false; + + result1.First(); + result2.First(); + + auto id1 = result1.Int("device_id"); + auto id2 = result2.Int("id"); + + if (id1 == 0 || id2 == 0) + return false; + } + catch (const std::exception& e) + { + std::stringstream ss; + ss << "ERROR: " << e.what() << std::endl; + + Logging::Log(Logging::Severity::Error, ss.str()); + + return false; + } + + return true; +} + +} // namespace Test diff --git a/Test/MultipleConnections.h b/Test/MultipleConnections.h new file mode 100644 index 0000000..a640d8b --- /dev/null +++ b/Test/MultipleConnections.h @@ -0,0 +1,11 @@ +#ifndef TEST_MULTIPLECONNECTIONS_H +#define TEST_MULTIPLECONNECTIONS_H + + +namespace Test { + +bool MultipleConnections(); + +} // namespace Test + +#endif // TEST_MULTIPLECONNECTIONS_H diff --git a/Test/MultipleThreads.cpp b/Test/MultipleThreads.cpp new file mode 100644 index 0000000..cc74d6c --- /dev/null +++ b/Test/MultipleThreads.cpp @@ -0,0 +1,97 @@ +#include "MultipleThreads.h" +#include "MySQLClient.h" +#include +#include + +namespace Test { + +bool Thread1() +{ + std::string hostname1 = "MySQL"; + std::string username1 = "datalog"; + std::string password1 = "NfhdUwjjdRbslR"; + std::string database1 = "datalog"; + + MySQL::MySQLClient MySQLClient1; + + MySQLClient1.Connect(hostname1, username1, password1, database1); + + if (!MySQLClient1.Connected()) + return false; + + std::stringstream query1; + query1 << "SELECT * FROM `datalog` LIMIT 1;"; + + auto result1 = MySQLClient1.ExecuteQuery(query1.str()); + + if (result1.RowsCount() == 0) + return false; + + result1.First(); + + auto id1 = result1.Int("device_id"); + + if (id1 == 0) + return false; + + return true; +} + +bool Thread2() +{ + std::string hostname2 = "Pi"; + std::string username2 = "root"; + std::string password2 = "Wh1sK3y"; + std::string database2 = "domotica"; + + MySQL::MySQLClient MySQLClient2; + + MySQLClient2.Connect(hostname2, username2, password2, database2); + + if (!MySQLClient2.Connected()) + return false; + + std::stringstream query2; + query2 << "SELECT * FROM `device` LIMIT 1;"; + + auto result2 = MySQLClient2.ExecuteQuery(query2.str()); + + if (result2.RowsCount() == 0) + return false; + + result2.First(); + + auto id2 = result2.Int("id"); + + if (id2 == 0) + return false; + + return true; +} + +bool MultipleThreads() +{ + Logging::Log(Logging::Severity::Info, "MultipleThreads"); + + try + { + bool thread1 = [] { Thread1(); }; + bool thread2 = [] { Thread2(); }; + + if (!thread1 || !thread2) + return false; + } + catch (const std::exception& e) + { + std::stringstream ss; + ss << "ERROR: " << e.what() << std::endl; + + Logging::Log(Logging::Severity::Error, ss.str()); + + return false; + } + + return true; +} + +} // namespace Test diff --git a/Test/MultipleThreads.h b/Test/MultipleThreads.h new file mode 100644 index 0000000..aa1a897 --- /dev/null +++ b/Test/MultipleThreads.h @@ -0,0 +1,11 @@ +#ifndef TEST_MULTIPLETHREADS_H +#define TEST_MULTIPLETHREADS_H + + +namespace Test { + +bool MultipleThreads(); + +} // namespace Test + +#endif // TEST_MULTIPLETHREADS_H diff --git a/Test/SingleConnection.cpp b/Test/SingleConnection.cpp new file mode 100644 index 0000000..93bc00b --- /dev/null +++ b/Test/SingleConnection.cpp @@ -0,0 +1,54 @@ +#include "SingleConnection.h" +#include "MySQLClient.h" +#include +#include + +namespace Test { + +bool SingleConnection() +{ + Logging::Log(Logging::Severity::Info, "SingleConnection"); + + try + { + std::string hostname1 = "MySQL"; + std::string username1 = "datalog"; + std::string password1 = "NfhdUwjjdRbslR"; + std::string database1 = "datalog"; + + MySQL::MySQLClient MySQLClient1; + + MySQLClient1.Connect(hostname1, username1, password1, database1); + + if (!MySQLClient1.Connected()) + return false; + + std::stringstream query1; + query1 << "SELECT * FROM `datalog` LIMIT 1;"; + + auto result1 = MySQLClient1.ExecuteQuery(query1.str()); + + if (result1.RowsCount() == 0) + return false; + + result1.First(); + + auto id1 = result1.Int("device_id"); + + if (id1 == 0) + return false; + } + catch (const std::exception& e) + { + std::stringstream ss; + ss << "ERROR: " << e.what() << std::endl; + + Logging::Log(Logging::Severity::Error, ss.str()); + + return false; + } + + return true; +} + +} // namespace Test diff --git a/Test/SingleConnection.h b/Test/SingleConnection.h new file mode 100644 index 0000000..1221429 --- /dev/null +++ b/Test/SingleConnection.h @@ -0,0 +1,11 @@ +#ifndef TEST_SINGLECONNECTION_H +#define TEST_SINGLECONNECTION_H + + +namespace Test { + +bool SingleConnection(); + +} // namespace Test + +#endif // TEST_SINGLECONNECTION_H diff --git a/Test/Test.cc b/Test/Test.cc new file mode 100644 index 0000000..becae8e --- /dev/null +++ b/Test/Test.cc @@ -0,0 +1,22 @@ +#include "UseDatabase.h" +#include "SingleConnection.h" +#include "MultipleConnections.h" +#include "MultipleThreads.h" +#include + + +int main(int argc, char *argv[]) +{ + if ( + Test::UseDatabase() && + Test::SingleConnection() && + Test::MultipleConnections() && + Test::MultipleThreads() + ) + { + std::cout << "Pass" << std::endl; + return 0; + } + + std::cout << "Fail" << std::endl; +} diff --git a/Test/UseDatabase.cpp b/Test/UseDatabase.cpp new file mode 100644 index 0000000..00dc8c3 --- /dev/null +++ b/Test/UseDatabase.cpp @@ -0,0 +1,45 @@ +#include "UseDatabase.h" +#include "MySQLClient.h" +#include +#include +#include + +#include + +namespace Test { + +bool UseDatabase() +{ + Logging::Log(Logging::Severity::Info, "UseDatabase"); + + try + { + std::string hostname1 = "MySQL"; + std::string username1 = "datalog"; + std::string password1 = "NfhdUwjjdRbslR"; + std::string database1 = "datalog"; + + MySQL::MySQLClient MySQLClient1; + + MySQLClient1.Connect(hostname1, username1, password1, database1); + + if (!MySQLClient1.Connected()) + return false; + + if (!StringAlgorithm::iequals(MySQLClient1.Database(), database1)) + return false; + } + catch (const std::exception& e) + { + std::stringstream ss; + ss << "ERROR: " << e.what() << std::endl; + + Logging::Log(Logging::Severity::Error, ss.str()); + + return false; + } + + return true; +} + +} // namespace Test diff --git a/Test/UseDatabase.h b/Test/UseDatabase.h new file mode 100644 index 0000000..545aab1 --- /dev/null +++ b/Test/UseDatabase.h @@ -0,0 +1,11 @@ +#ifndef TEST_USEDATABASE_H +#define TEST_USEDATABASE_H + + +namespace Test { + +bool UseDatabase(); + +} // namespace Test + +#endif // TEST_USEDATABASE_H