Skip to content
Snippets Groups Projects
Commit ffab033a authored by Sergey Yakubov's avatar Sergey Yakubov
Browse files

add Error class

parent ae7d1da0
No related branches found
No related tags found
No related merge requests found
Showing
with 214 additions and 191 deletions
#ifndef HIDRA2_ERROR_H
#define HIDRA2_ERROR_H
#include <string>
namespace hidra2 {
class ErrorInterface {
public:
virtual std::string Explain() const noexcept = 0;
virtual void Set(const std::string& error) noexcept = 0;
};
using Error = std::unique_ptr<ErrorInterface>;
class SimpleError: public ErrorInterface {
private:
std::string error_;
public:
explicit SimpleError(const std::string& error): error_{error} {
}
std::string Explain() const noexcept override {
return error_;
}
void Set(const std::string& error)noexcept override {
error_ = error;
}
};
inline Error TextError(const std::string& error) {
return Error{new SimpleError{error}};
}
}
#endif //HIDRA2_ERROR_H
......@@ -4,34 +4,35 @@
#include <string>
#include "common/data_structs.h"
#include "common/error.h"
namespace hidra2 {
enum class DBError {
KUnknownError,
kConnectionError,
kInsertError,
kDuplicateID,
kAlreadyConnected,
kNotConnected,
kBadAddress,
kNoError,
kMemoryError
};
namespace DBError {
auto const KUnknownError = "Inknown error";
auto const kConnectionError = "Connection error";
auto const kInsertError = "Insert error";
auto const kDuplicateID = "Duplicate ID";
auto const kAlreadyConnected = "Already connected";
auto const kNotConnected = "Not connected";
auto const kBadAddress = "Bad address";
auto const kMemoryError = "Memory error";
}
constexpr char kDBCollectionName[] = "data";
class Database {
public:
virtual DBError Connect(const std::string& address, const std::string& database,
const std::string& collection ) = 0;
virtual DBError Insert(const FileInfo& file, bool ignore_duplicates) const = 0;
virtual Error Connect(const std::string& address, const std::string& database,
const std::string& collection ) = 0;
virtual Error Insert(const FileInfo& file, bool ignore_duplicates) const = 0;
virtual ~Database() = default;
};
class DatabaseFactory {
public:
virtual std::unique_ptr<Database> Create(DBError* err) const noexcept;
virtual std::unique_ptr<Database> Create(Error* err) const noexcept;
virtual ~DatabaseFactory() = default;
};
......
......@@ -8,31 +8,31 @@
#include <chrono>
#include "common/data_structs.h"
#include "common/error.h"
namespace hidra2 {
enum class IOErrors {
kNoError,
kFileNotFound,
kReadError,
kPermissionDenied,
kUnknownError,
kMemoryAllocationError
};
namespace IOErrors {
auto const kFileNotFound = "File not found";
auto const kReadError = "Read error";
auto const kPermissionDenied = "Permission denied";
auto const kUnknownError = "Unknown error";
auto const kMemoryAllocationError = "Memory Allocation Error";
}
IOErrors IOErrorFromErrno();
Error IOErrorFromErrno();
class IO {
public:
virtual FileData GetDataFromFile(const std::string& fname, uint64_t fsize, IOErrors* err) const noexcept = 0;
virtual uint64_t Read(int fd, uint8_t* array, uint64_t fsize, IOErrors* err) const noexcept = 0;
virtual FileData GetDataFromFile(const std::string& fname, uint64_t fsize, Error* err) const noexcept = 0;
virtual uint64_t Read(int fd, uint8_t* array, uint64_t fsize, Error* err) const noexcept = 0;
virtual int open(const char* __file, int __oflag) const noexcept = 0;
virtual int close(int __fd) const noexcept = 0;
// this is not standard function - to be implemented differently in windows and linux
virtual FileInfos FilesInFolder(const std::string& folder, IOErrors* err) const = 0;
virtual FileInfos FilesInFolder(const std::string& folder, Error* err) const = 0;
};
}
......
......@@ -7,14 +7,14 @@ namespace hidra2 {
class SystemIO final : public IO {
public:
FileData GetDataFromFile(const std::string& fname, uint64_t fsize, IOErrors* err) const noexcept override;
FileData GetDataFromFile(const std::string& fname, uint64_t fsize, Error* err) const noexcept override;
int open(const char* __file, int __oflag) const noexcept override;
int close(int __fd) const noexcept override;
uint64_t Read(int fd, uint8_t* array, uint64_t fsize, IOErrors* err) const noexcept override;
std::vector<FileInfo> FilesInFolder(const std::string& folder, IOErrors* err) const override;
uint64_t Read(int fd, uint8_t* array, uint64_t fsize, Error* err) const noexcept override;
std::vector<FileInfo> FilesInFolder(const std::string& folder, Error* err) const override;
private:
void CollectFileInformationRecursivly(const std::string& path,
std::vector<FileInfo>* files, IOErrors* err) const;
std::vector<FileInfo>* files, Error* err) const;
int64_t read(int __fd, void* buf, size_t count) const noexcept;
int64_t write(int __fd, const void* __buf, size_t __n) const noexcept;
};
......
......@@ -10,8 +10,11 @@ namespace hidra2 {
class MockIO : public IO {
public:
FileData GetDataFromFile(const std::string& fname, uint64_t fsize, IOErrors* err) const noexcept override {
return FileData{GetDataFromFile_t(fname, fsize, err)};
FileData GetDataFromFile(const std::string& fname, uint64_t fsize, Error* err) const noexcept override {
SimpleError* error;
auto data = GetDataFromFile_t(fname, fsize, &error);
err->reset(error);
return FileData(data);
}
int open(const char* __file, int __oflag) const noexcept override {
return 0;
......@@ -19,16 +22,22 @@ class MockIO : public IO {
int close(int __fd) const noexcept override {
return 0;
}
uint64_t Read(int fd, uint8_t* array, uint64_t fsize, IOErrors* err) const noexcept override {
uint64_t Read(int fd, uint8_t* array, uint64_t fsize, Error* err) const noexcept override {
return 0;
}
FileInfos FilesInFolder(const std::string& folder, Error* err) const override {
SimpleError* error;
auto data = FilesInFolder_t(folder, &error);
err->reset(error);
return data;
}
MOCK_CONST_METHOD3(GetDataFromFile_t,
uint8_t* (const std::string& fname, uint64_t fsize, IOErrors* err));
MOCK_CONST_METHOD2(FilesInFolder,
uint8_t* (const std::string& fname, uint64_t fsize, SimpleError** err));
MOCK_CONST_METHOD2(FilesInFolder_t,
FileInfos(
const std::string& folder, IOErrors
*err));
const std::string& folder, hidra2::SimpleError
** err));
MOCK_CONST_METHOD3(read_t,
int64_t(int
......
......@@ -3,13 +3,13 @@
namespace hidra2 {
std::unique_ptr<Database> DatabaseFactory::Create(DBError* err) const noexcept {
std::unique_ptr<Database> DatabaseFactory::Create(Error* err) const noexcept {
std::unique_ptr<Database> p = nullptr;
try {
p.reset(new MongoDBClient());
*err = DBError::kNoError;
*err = nullptr;
} catch (...) {
*err = DBError::kMemoryError;
*err = TextError(DBError::kMemoryError);
}
return p;
};
......
......@@ -13,7 +13,7 @@ MongoDbInstance::~MongoDbInstance() {
mongoc_cleanup ();
}
DBError MongoDBClient::Ping() {
Error MongoDBClient::Ping() {
bson_t* command, reply;
bson_error_t error;
bool retval;
......@@ -25,21 +25,21 @@ DBError MongoDBClient::Ping() {
bson_destroy (&reply);
bson_destroy (command);
return !retval ? DBError::kConnectionError : DBError::kNoError;
return !retval ? TextError(DBError::kConnectionError) : nullptr;
}
MongoDBClient::MongoDBClient() {
MongoDbInstance::Instantiate();
}
DBError MongoDBClient::InitializeClient(const string& address) {
Error MongoDBClient::InitializeClient(const string& address) {
auto uri_str = DBAddress(address);
client_ = mongoc_client_new (uri_str.c_str());
if (client_ == nullptr) {
return DBError::kBadAddress;
return TextError(DBError::kBadAddress);
}
return DBError::kNoError;
return nullptr;
}
......@@ -49,29 +49,29 @@ void MongoDBClient::InitializeCollection(const string& database_name,
collection_name.c_str());
}
DBError MongoDBClient::TryConnectDatabase() {
Error MongoDBClient::TryConnectDatabase() {
auto err = Ping();
if (err == DBError::kNoError) {
if (err == nullptr) {
connected_ = true;
}
return err;
}
DBError MongoDBClient::Connect(const string& address, const string& database_name,
const string& collection_name) {
Error MongoDBClient::Connect(const string& address, const string& database_name,
const string& collection_name) {
if (connected_) {
return DBError::kAlreadyConnected;
return TextError(DBError::kAlreadyConnected);
}
auto err = InitializeClient(address);
if (err != DBError::kNoError) {
if (err) {
return err;
}
InitializeCollection(database_name, collection_name);
err = TryConnectDatabase();
if (err != DBError::kNoError) {
if (err) {
CleanUp();
}
return err;
......@@ -86,40 +86,40 @@ void MongoDBClient::CleanUp() {
mongoc_client_destroy (client_);
}
bson_p PrepareBsonDocument(const FileInfo& file, DBError* err) {
bson_p PrepareBsonDocument(const FileInfo& file, Error* err) {
auto s = file.Json();
auto json = reinterpret_cast<const uint8_t*>(s.c_str());
auto bson = bson_new_from_json(json, -1, nullptr);
if (!bson) {
*err = DBError::kInsertError;
*err = TextError(DBError::kInsertError);
return nullptr;
}
*err = DBError::kNoError;
*err = nullptr;
return bson_p{bson};
}
DBError MongoDBClient::InsertBsonDocument(const bson_p& document, bool ignore_duplicates) const {
Error MongoDBClient::InsertBsonDocument(const bson_p& document, bool ignore_duplicates) const {
bson_error_t mongo_err;
if (!mongoc_collection_insert_one(collection_, document.get(), NULL, NULL, &mongo_err)) {
if (mongo_err.code == MONGOC_ERROR_DUPLICATE_KEY) {
return ignore_duplicates ? DBError::kNoError : DBError::kDuplicateID;
return ignore_duplicates ? nullptr : TextError(DBError::kDuplicateID);
}
return DBError::kInsertError;
return TextError(DBError::kInsertError);
}
return DBError::kNoError;
return nullptr;
}
DBError MongoDBClient::Insert(const FileInfo& file, bool ignore_duplicates) const {
Error MongoDBClient::Insert(const FileInfo& file, bool ignore_duplicates) const {
if (!connected_) {
return DBError::kNotConnected;
return TextError(DBError::kNotConnected);
}
DBError err;
Error err;
auto document = PrepareBsonDocument(file, &err);
if (err != DBError::kNoError) {
if (err) {
return err;
}
......
......@@ -36,9 +36,9 @@ using bson_p = std::unique_ptr<_bson_t, BsonDestroyFunctor>;
class MongoDBClient final : public Database {
public:
MongoDBClient();
DBError Connect(const std::string& address, const std::string& database,
const std::string& collection) override;
DBError Insert(const FileInfo& file, bool ignore_duplicates) const override;
Error Connect(const std::string& address, const std::string& database,
const std::string& collection) override;
Error Insert(const FileInfo& file, bool ignore_duplicates) const override;
~MongoDBClient() override;
private:
mongoc_client_t* client_{nullptr};
......@@ -46,12 +46,12 @@ class MongoDBClient final : public Database {
bool connected_{false};
void CleanUp();
std::string DBAddress(const std::string& address) const;
DBError InitializeClient(const std::string& address);
Error InitializeClient(const std::string& address);
void InitializeCollection(const std::string& database_name,
const std::string& collection_name);
DBError Ping();
DBError TryConnectDatabase();
DBError InsertBsonDocument(const bson_p& document, bool ignore_duplicates) const;
Error Ping();
Error TryConnectDatabase();
Error InsertBsonDocument(const bson_p& document, bool ignore_duplicates) const;
};
}
......
......@@ -9,27 +9,26 @@
namespace hidra2 {
IOErrors IOErrorFromErrno() {
IOErrors err;
Error IOErrorFromErrno() {
const char* message;
switch (errno) {
case 0:
err = IOErrors::kNoError;
break;
return nullptr;
case ENOENT:
case ENOTDIR:
err = IOErrors::kFileNotFound;
message = IOErrors::kFileNotFound;
break;
case EACCES:
err = IOErrors::kPermissionDenied;
message = IOErrors::kPermissionDenied;
break;
default:
err = IOErrors::kUnknownError;
message = IOErrors::kUnknownError;
break;
}
return err;
return TextError(message);
}
uint64_t SystemIO::Read(int fd, uint8_t* array, uint64_t fsize, IOErrors* err) const noexcept {
uint64_t SystemIO::Read(int fd, uint8_t* array, uint64_t fsize, Error* err) const noexcept {
uint64_t totalbytes = 0;
int64_t readbytes = 0;
do {
......@@ -38,36 +37,36 @@ uint64_t SystemIO::Read(int fd, uint8_t* array, uint64_t fsize, IOErrors* err) c
} while (readbytes > 0 && totalbytes < fsize);
if (totalbytes != fsize) {
*err = IOErrors::kReadError;
*err = TextError(IOErrors::kReadError);
}
return totalbytes;
}
FileData SystemIO::GetDataFromFile(const std::string& fname, uint64_t fsize, IOErrors* err) const noexcept {
FileData SystemIO::GetDataFromFile(const std::string& fname, uint64_t fsize, Error* err) const noexcept {
errno = 0;
int fd = open(fname.c_str(), O_RDONLY);
*err = IOErrorFromErrno();
if (*err != IOErrors::kNoError) {
if (*err != nullptr) {
return nullptr;
}
uint8_t* data_array = nullptr;
try {
data_array = new uint8_t[fsize];
} catch (...) {
*err = IOErrors::kMemoryAllocationError;
*err = TextError(IOErrors::kMemoryAllocationError);
return nullptr;
}
Read(fd, data_array, fsize, err);
FileData data{data_array};
if (*err != IOErrors::kNoError) {
if (*err != nullptr) {
close(fd);
return nullptr;
}
errno = 0;
close(fd);
*err = IOErrorFromErrno();
if (*err != IOErrors::kNoError) {
if (*err != nullptr) {
return nullptr;
}
......@@ -96,10 +95,10 @@ void AssignIDs(FileInfos* file_list) {
}
FileInfos SystemIO::FilesInFolder(const std::string& folder, IOErrors* err) const {
FileInfos SystemIO::FilesInFolder(const std::string& folder, Error* err) const {
FileInfos files{};
CollectFileInformationRecursivly(folder, &files, err);
if (*err != IOErrors::kNoError) {
if (*err != nullptr) {
return {};
}
StripBasePath(folder, &files);
......
......@@ -46,7 +46,7 @@ void SetFileName(const string& path, const string& name, FileInfo* file_info) {
file_info->base_name = name;
}
struct stat FileStat(const string& fname, IOErrors* err) {
struct stat FileStat(const string& fname, Error* err) {
struct stat t_stat {};
errno = 0;
int res = stat(fname.c_str(), &t_stat);
......@@ -56,13 +56,13 @@ struct stat FileStat(const string& fname, IOErrors* err) {
return t_stat;
}
FileInfo GetFileInfo(const string& path, const string& name, IOErrors* err) {
FileInfo GetFileInfo(const string& path, const string& name, Error* err) {
FileInfo file_info;
SetFileName(path, name, &file_info);
auto t_stat = FileStat(path + "/" + name, err);
if (*err != IOErrors::kNoError) {
if (*err != nullptr) {
return FileInfo{};
}
......@@ -74,22 +74,22 @@ FileInfo GetFileInfo(const string& path, const string& name, IOErrors* err) {
}
void ProcessFileEntity(const struct dirent* entity, const std::string& path,
FileInfos* files, IOErrors* err) {
FileInfos* files, Error* err) {
*err = IOErrors::kNoError;
*err = nullptr;
if (entity->d_type != DT_REG) {
return;
}
FileInfo file_info = GetFileInfo(path, entity->d_name, err);
if (*err != IOErrors::kNoError) {
if (*err != nullptr) {
return;
}
files->push_back(file_info);
}
void SystemIO::CollectFileInformationRecursivly(const std::string& path,
FileInfos* files, IOErrors* err) const {
FileInfos* files, Error* err) const {
errno = 0;
auto dir = opendir((path).c_str());
if (dir == nullptr) {
......@@ -108,7 +108,7 @@ void SystemIO::CollectFileInformationRecursivly(const std::string& path,
} else {
ProcessFileEntity(current_entity, path, files, err);
}
if (*err != IOErrors::kNoError) {
if (*err != nullptr) {
errno = 0;
closedir(dir);
return;
......
......@@ -12,32 +12,32 @@ using std::chrono::system_clock;
namespace hidra2 {
IOErrors IOErrorFromGetLastError() {
IOErrors err;
Error IOErrorFromGetLastError() {
const char* message;
switch (GetLastError()) {
case ERROR_SUCCESS :
err = IOErrors::kNoError;
message = IOErrors::kNoError;
break;
case ERROR_PATH_NOT_FOUND:
case ERROR_FILE_NOT_FOUND:
err = IOErrors::kFileNotFound;
message = IOErrors::kFileNotFound;
break;
case ERROR_ACCESS_DENIED:
err = IOErrors::kPermissionDenied;
message = IOErrors::kPermissionDenied;
break;
default:
err = IOErrors::kUnknownError;
message = IOErrors::kUnknownError;
break;
}
return err;
return TextError(message);
}
IOErrors CheckFileTime(const FILETIME& ft) {
Error CheckFileTime(const FILETIME& ft) {
SYSTEMTIME st = {0};
if (!FileTimeToSystemTime(&ft, &st)) {
return IOErrorFromGetLastError();
}
return IOErrors::kNoError;
return nullptr;
}
constexpr auto kShift = 11644473600ULL;
......@@ -52,10 +52,10 @@ uint64_t GetLinuxNanosecFromWindowsEpoch(ULARGE_INTEGER ull) {
return (uint64_t)(ull.QuadPart % k100nsInSec) * 100;
}
std::chrono::system_clock::time_point FileTime2TimePoint(const FILETIME& ft, IOErrors* err) {
std::chrono::system_clock::time_point FileTime2TimePoint(const FILETIME& ft, Error* err) {
*err = CheckFileTime(ft);
if (*err != IOErrors::kNoError) {
if (*err) {
return std::chrono::system_clock::time_point{};
}
......@@ -73,7 +73,7 @@ std::chrono::system_clock::time_point FileTime2TimePoint(const FILETIME& ft, IOE
auto tp = system_clock::time_point
{std::chrono::duration_cast<std::chrono::system_clock::duration>(d)};
*err = IOErrors::kNoError;
*err = nullptr;
return tp;
}
......@@ -84,16 +84,16 @@ bool IsDirectory(const WIN32_FIND_DATA f) {
}
void ProcessFileEntity(const WIN32_FIND_DATA f, const std::string& path,
FileInfos* files, IOErrors* err) {
FileInfos* files, Errors* err) {
*err = IOErrors::kNoError;
*err = nullptr;
if (f.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
return;
}
FileInfo file_info;
file_info.modify_date = FileTime2TimePoint(f.ftLastWriteTime, err);
if (*err != IOErrors::kNoError) {
if (*err) {
return;
}
......@@ -104,7 +104,7 @@ void ProcessFileEntity(const WIN32_FIND_DATA f, const std::string& path,
void SystemIO::CollectFileInformationRecursivly(const std::string& path,
FileInfos* files, IOErrors* err) const {
FileInfos* files, Error* err) const {
WIN32_FIND_DATA find_data;
HANDLE handle = FindFirstFile((path + "\\*.*").c_str(), &find_data);
if (handle == INVALID_HANDLE_VALUE) {
......@@ -118,14 +118,14 @@ void SystemIO::CollectFileInformationRecursivly(const std::string& path,
} else {
ProcessFileEntity(find_data, path, files, err);
}
if (*err != IOErrors::kNoError) {
if (*err) {
FindClose(handle);
return;
}
} while (FindNextFile(handle, &find_data));
if (FindClose(handle)) {
*err = IOErrors ::kNoError;
*err = nullptr;
} else {
*err = IOErrorFromGetLastError();
}
......
......@@ -9,8 +9,8 @@
#include "hidra2_worker.h"
using hidra2::WorkerErrorCode;
using std::chrono::high_resolution_clock;
using hidra2::Error;
void WaitThreads(std::vector<std::thread>* threads) {
for (auto& thread : *threads) {
......@@ -22,9 +22,9 @@ std::vector<std::thread> StartThreads(const std::string& server, const std::stri
std::vector<int>* nfiles) {
auto exec_next = [server, run_name, nfiles](int i) {
hidra2::FileInfo fi;
hidra2::WorkerErrorCode err;
Error err;
auto broker = hidra2::DataBrokerFactory::CreateServerBroker(server, run_name, &err);
while (broker->GetNext(&fi, nullptr) == WorkerErrorCode::kOK) {
while (broker->GetNext(&fi, nullptr) == nullptr) {
(*nfiles)[i] ++;
}
};
......
......@@ -8,8 +8,8 @@
#include "hidra2_worker.h"
using hidra2::WorkerErrorCode;
using std::chrono::high_resolution_clock;
using hidra2::Error;
struct Statistics {
std::chrono::milliseconds duration_scan;
......@@ -27,9 +27,9 @@ std::string ProcessCommandArguments(int argc, char* argv[]) {
}
std::unique_ptr<hidra2::DataBroker> CreateBroker(const std::string& folder) {
hidra2::WorkerErrorCode err;
Error err;
auto broker = hidra2::DataBrokerFactory::CreateFolderBroker(folder, &err);
if (err != WorkerErrorCode::kOK) {
if (err != nullptr) {
std::cout << "Cannot create broker" << std::endl;
exit(EXIT_FAILURE);
}
......@@ -38,10 +38,9 @@ std::unique_ptr<hidra2::DataBroker> CreateBroker(const std::string& folder) {
}
void ConnectToBrocker(std::unique_ptr<hidra2::DataBroker>* broker, Statistics* statistics) {
hidra2::WorkerErrorCode err;
high_resolution_clock::time_point t1 = high_resolution_clock::now();
err = (*broker)->Connect();
if (err != WorkerErrorCode::kOK) {
Error err = (*broker)->Connect();
if (err != nullptr) {
std::cout << "Cannot connect to broker" << std::endl;
exit(EXIT_FAILURE);
}
......@@ -50,18 +49,18 @@ void ConnectToBrocker(std::unique_ptr<hidra2::DataBroker>* broker, Statistics* s
}
void ReadAllData(std::unique_ptr<hidra2::DataBroker>* broker, Statistics* statistics) {
hidra2::WorkerErrorCode err;
Error err;
hidra2::FileInfo file_info;
hidra2::FileData file_data;
high_resolution_clock::time_point t1 = high_resolution_clock::now();
int nfiles = 0;
uint64_t size = 0;
while ((err = (*broker)->GetNext(&file_info, &file_data)) == WorkerErrorCode::kOK) {
while ((err = (*broker)->GetNext(&file_info, &file_data)) == nullptr) {
nfiles++;
size += file_info.size;
}
if (err != WorkerErrorCode::kNoData) {
if (err->Explain() != hidra2::WorkerErrorMessage::kNoData) {
std::cout << "Read error" << std::endl;
exit(EXIT_FAILURE);
}
......
#include "testing.h"
#include <iostream>
#include <algorithm>
namespace hidra2 {
......@@ -32,8 +33,22 @@ void M_AssertEq(int expected, int got) {
T_AssertEq(expected, got);
}
std::string EraseSpaces(const std::string& str) {
auto tmp = str;
auto end_pos = std::remove(tmp.begin(), tmp.end(), ' ');
tmp.erase(end_pos, tmp.end());
return tmp;
}
void M_AssertContains( const std::string& whole, const std::string& sub) {
if (whole.find(sub) == std::string::npos) {
auto whole_t = EraseSpaces(whole);
auto sub_t = EraseSpaces(sub);
if (whole_t.find(sub_t) == std::string::npos) {
std::cerr << "Assert failed:\n"
<< "Got (spaces erased):\t'" << whole_t << "'\n"
<< "Expected containes (spaces erased):\t'" << sub_t << "'\n";
exit(EXIT_FAILURE);
}
}
......
......@@ -14,7 +14,7 @@ target_include_directories(${TARGET_NAME} PUBLIC ${HIDRA2_CXX_COMMON_INCLUDE_DIR
################################
add_integration_test(${TARGET_NAME} connectOK "127.0.0.1 data test OK")
add_integration_test(${TARGET_NAME} connectFAILS "127.0.0.0 data test ConnectionError")
add_integration_test(${TARGET_NAME} connectBadAddress "#?ß// data test BadAddress")
add_integration_test(${TARGET_NAME} connectFAILS "127.0.0.0 data test Connectionerror")
add_integration_test(${TARGET_NAME} connectBadAddress "#?ß// data test Badaddress")
......@@ -5,28 +5,17 @@
#include "testing.h"
using hidra2::M_AssertEq;
using hidra2::DBError;
using hidra2::M_AssertContains;
using hidra2::Error;
void Assert(DBError error, const std::string& expect) {
void Assert(const Error& error, const std::string& expect) {
std::string result;
switch (error) {
case DBError::kConnectionError:
result = "ConnectionError";
break;
case DBError::kAlreadyConnected:
result = "AlreadyConnected";
break;
case DBError::kBadAddress:
result = "BadAddress";
break;
default:
if (error == nullptr) {
result = "OK";
break;
} else {
result = error->Explain();
}
M_AssertEq(expect, result);
M_AssertContains(result, expect);
}
struct Args {
......@@ -53,9 +42,9 @@ int main(int argc, char* argv[]) {
auto err = db.Connect(args.address, args.database_name, args.collection_name);
Assert(err, args.keyword);
if (err == DBError::kNoError) {
if (err == nullptr) {
err = db.Connect(args.address, args.database_name, args.collection_name);
Assert(err, "AlreadyConnected");
Assert(err, hidra2::DBError::kAlreadyConnected);
}
return 0;
}
......
......@@ -15,8 +15,8 @@ target_include_directories(${TARGET_NAME} PUBLIC ${HIDRA2_CXX_COMMON_INCLUDE_DIR
add_test_cleanup(${TARGET_NAME})
add_integration_test(${TARGET_NAME} insertOK "OK 1" "OK 2")
add_integration_test(${TARGET_NAME} insertFailsWhenNotConnected
"NotConnected 3"
"NotConnected 4")
"Notconnected 3"
"Notconnected 4")
add_integration_test(${TARGET_NAME} insertFailsForDuplicateID
"DuplicateID 6"
"DuplicateID 5")
......
......@@ -5,29 +5,18 @@
#include "testing.h"
using hidra2::M_AssertEq;
using hidra2::DBError;
using hidra2::M_AssertContains;
using hidra2::Error;
void Assert(DBError error, const std::string& expect) {
void Assert(const Error& error, const std::string& expect) {
std::string result;
switch (error) {
case DBError::kInsertError:
result = "InsertError";
break;
case DBError::kNotConnected:
result = "NotConnected";
break;
case DBError::kDuplicateID:
result = "DuplicateID";
break;
default:
if (error == nullptr) {
result = "OK";
break;
} else {
result = error->Explain();
}
M_AssertEq(expect, result);
M_AssertContains(result, expect);
}
struct Args {
......@@ -55,7 +44,7 @@ int main(int argc, char* argv[]) {
fi.relative_path = "relpath";
fi.modify_date = std::chrono::system_clock::now();
if (args.keyword != "NotConnected") {
if (args.keyword != "Notconnected") {
db.Connect("127.0.0.1", "data", "test");
}
......
......@@ -17,5 +17,5 @@ set_target_properties(${TARGET_NAME} PROPERTIES LINKER_LANGUAGE CXX)
add_test_setup_cleanup(${TARGET_NAME})
add_integration_test(${TARGET_NAME} readfile "test/1 123")
add_integration_test(${TARGET_NAME} filenotfound "test_notexist notfound")
add_integration_test(${TARGET_NAME} filenoaccess "file_noaccess noaccess")
add_integration_test(${TARGET_NAME} filenoaccess "file_noaccess Permissiondenied")
......@@ -4,8 +4,6 @@
#include "testing.h"
using hidra2::SystemIO;
using hidra2::IOErrors;
using hidra2::M_AssertEq;
int main(int argc, char* argv[]) {
if (argc != 3) {
......@@ -14,32 +12,19 @@ int main(int argc, char* argv[]) {
}
std::string expect{argv[2]};
IOErrors err;
hidra2::Error err;
auto io = std::unique_ptr<SystemIO> {new SystemIO};
auto data = io->GetDataFromFile(argv[1], expect.size(), &err);
std::string result;
switch (err) {
case IOErrors::kFileNotFound:
result = "notfound";
break;
case IOErrors::kNoError:
if (err == nullptr) {
for(unsigned int i = 0; i < expect.size(); i++)
result += data[i];
break;
case IOErrors::kPermissionDenied:
result = "noaccess";
break;
case IOErrors::kReadError:
result = "readerror";
break;
default:
result = "";
break;
} else {
result = err->Explain();
}
M_AssertEq(expect, result);
hidra2::M_AssertContains(result, expect);
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment