瀏覽代碼

Add Tests

JDierkse 5 年之前
父節點
當前提交
863f0db365

+ 2 - 0
.gitignore

@@ -4,3 +4,5 @@
 .*.swp
 .AppleDouble
 lib
+test*
+!test.cc

+ 1 - 0
Libraries/Utilities

@@ -0,0 +1 @@
+../../Utilities

+ 12 - 1
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

+ 6 - 1
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

+ 1 - 0
Test/Makefile

@@ -0,0 +1 @@
+../Makefile

+ 67 - 0
Test/MultipleConnections.cpp

@@ -0,0 +1,67 @@
+#include "MultipleConnections.h"
+#include "MySQLClient.h"
+#include <Logging.h>
+#include <sstream>
+
+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

+ 11 - 0
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

+ 97 - 0
Test/MultipleThreads.cpp

@@ -0,0 +1,97 @@
+#include "MultipleThreads.h"
+#include "MySQLClient.h"
+#include <Logging.h>
+#include <sstream>
+
+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

+ 11 - 0
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

+ 54 - 0
Test/SingleConnection.cpp

@@ -0,0 +1,54 @@
+#include "SingleConnection.h"
+#include "MySQLClient.h"
+#include <Logging.h>
+#include <sstream>
+
+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

+ 11 - 0
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

+ 22 - 0
Test/Test.cc

@@ -0,0 +1,22 @@
+#include "UseDatabase.h"
+#include "SingleConnection.h"
+#include "MultipleConnections.h"
+#include "MultipleThreads.h"
+#include <iostream>
+
+
+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;
+}

+ 45 - 0
Test/UseDatabase.cpp

@@ -0,0 +1,45 @@
+#include "UseDatabase.h"
+#include "MySQLClient.h"
+#include <Logging.h>
+#include <StringAlgorithm.h>
+#include <sstream>
+
+#include <unistd.h>
+
+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

+ 11 - 0
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