Add a database test

This commit is contained in:
antao 2018-11-27 17:37:41 +08:00
parent d356d38319
commit 81540ca19b
9 changed files with 852 additions and 15 deletions

View File

@ -134,6 +134,9 @@ if (MAKETEST STREQUAL YES)
if(PostgreSQL_FOUND)
add_subdirectory(${PROJECT_SOURCE_DIR}/orm_lib/src/postgresql_impl/test)
endif()
if(USE_ORM)
add_subdirectory(${PROJECT_SOURCE_DIR}/orm_lib/src/tests)
endif()
endif ()
SET(CMAKE_INSTALL_PREFIX /usr/local)
@ -144,8 +147,8 @@ install(TARGETS drogon DESTINATION lib)
file(GLOB drogon_headers "${CMAKE_CURRENT_SOURCE_DIR}/lib/inc/drogon/*.h")
install(FILES ${drogon_headers} DESTINATION include/drogon)
if(USE_ORM)
file(GLOB orm_headers "${CMAKE_CURRENT_SOURCE_DIR}/orm_lib/inc/drogon/orm/*.h")
install(FILES ${orm_headers} DESTINATION include/drogon/orm)
file(GLOB orm_headers "${CMAKE_CURRENT_SOURCE_DIR}/orm_lib/inc/drogon/orm/*.h")
install(FILES ${orm_headers} DESTINATION include/drogon/orm)
endif()
file(GLOB drogon_util_headers "${CMAKE_CURRENT_SOURCE_DIR}/lib/inc/drogon/utils/*.h")
install(FILES ${drogon_util_headers}

View File

@ -1,12 +1,12 @@
/**
*
* {{className}}.cc
* created by drogon_ctl
*
*/
<%inc#include "create_model.h"
using namespace drogon_ctl;
%>
/**
*
* {{className}}.cc
* DO NOT EDIT. This file is generated by drogon_ctl
*
*/
#include "{{className}}.h"
#include <drogon/utils/Utilities.h>

View File

@ -1,12 +1,13 @@
/**
*
* {{className}}.h
* Created by drogon_ctl
*
*/
<%inc#include "create_model.h"
using namespace drogon_ctl;
%>
/**
*
* {{className}}.h
* DO NOT EDIT. This file is generated by drogon_ctl
*
*/
#pragma once
#include <drogon/orm/Result.h>
#include <drogon/orm/Row.h>

View File

@ -0,0 +1,3 @@
link_libraries(drogon trantor pthread dl)
add_executable(db_test db_test.cc users.cc)

534
orm_lib/src/tests/Users.cc Normal file
View File

@ -0,0 +1,534 @@
/**
*
* Users.cc
* DO NOT EDIT,This file is generated by drogon_ctl
*
*/
#include "Users.h"
#include <drogon/utils/Utilities.h>
#include <string>
using namespace drogon_model::postgres;
const std::string Users::Cols::user_id = "user_id";
const std::string Users::Cols::user_name = "user_name";
const std::string Users::Cols::password = "password";
const std::string Users::Cols::org_name = "org_name";
const std::string Users::Cols::signature = "signature";
const std::string Users::Cols::avatar_id = "avatar_id";
const std::string Users::Cols::id = "id";
const std::string Users::Cols::salt = "salt";
const std::string Users::Cols::admin = "admin";
const std::string Users::primaryKeyName = "id";
const bool Users::hasPrimaryKey = true;
const std::string Users::tableName = "users";
const std::vector<typename Users::MetaData> Users::_metaData={
{"user_id","std::string","character varying",32,0,0,0},
{"user_name","std::string","character varying",64,0,0,0},
{"password","std::string","character varying",64,0,0,0},
{"org_name","std::string","character varying",20,0,0,0},
{"signature","std::string","character varying",50,0,0,0},
{"avatar_id","std::string","character varying",32,0,0,0},
{"id","int32_t","integer",4,1,1,1},
{"salt","std::string","character varying",20,0,0,0},
{"admin","bool","boolean",1,0,0,0}
};
const std::string &Users::getColumnName(size_t index) noexcept(false)
{
assert(index < _metaData.size());
return _metaData[index]._colName;
}
Users::Users(const Row &r) noexcept
{
if(!r["user_id"].isNull())
{
_userId=std::make_shared<std::string>(r["user_id"].as<std::string>());
}
if(!r["user_name"].isNull())
{
_userName=std::make_shared<std::string>(r["user_name"].as<std::string>());
}
if(!r["password"].isNull())
{
_password=std::make_shared<std::string>(r["password"].as<std::string>());
}
if(!r["org_name"].isNull())
{
_orgName=std::make_shared<std::string>(r["org_name"].as<std::string>());
}
if(!r["signature"].isNull())
{
_signature=std::make_shared<std::string>(r["signature"].as<std::string>());
}
if(!r["avatar_id"].isNull())
{
_avatarId=std::make_shared<std::string>(r["avatar_id"].as<std::string>());
}
if(!r["id"].isNull())
{
_id=std::make_shared<int32_t>(r["id"].as<int32_t>());
}
if(!r["salt"].isNull())
{
_salt=std::make_shared<std::string>(r["salt"].as<std::string>());
}
if(!r["admin"].isNull())
{
_admin=std::make_shared<bool>(r["admin"].as<bool>());
}
}
const std::string & Users::getValueOfUserId(const std::string &defaultValue) const noexcept
{
if(_userId)
return *_userId;
return defaultValue;
}
std::shared_ptr<const std::string> Users::getUserId() const noexcept
{
return _userId;
}
void Users::setUserId(const std::string &userId) noexcept
{
_userId = std::make_shared<std::string>(userId);
_dirtyFlag[0] = true;
}
void Users::setUserId(std::string &&userId) noexcept
{
_userId = std::make_shared<std::string>(std::move(userId));
_dirtyFlag[0] = true;
}
const std::string & Users::getValueOfUserName(const std::string &defaultValue) const noexcept
{
if(_userName)
return *_userName;
return defaultValue;
}
std::shared_ptr<const std::string> Users::getUserName() const noexcept
{
return _userName;
}
void Users::setUserName(const std::string &userName) noexcept
{
_userName = std::make_shared<std::string>(userName);
_dirtyFlag[1] = true;
}
void Users::setUserName(std::string &&userName) noexcept
{
_userName = std::make_shared<std::string>(std::move(userName));
_dirtyFlag[1] = true;
}
const std::string & Users::getValueOfPassword(const std::string &defaultValue) const noexcept
{
if(_password)
return *_password;
return defaultValue;
}
std::shared_ptr<const std::string> Users::getPassword() const noexcept
{
return _password;
}
void Users::setPassword(const std::string &password) noexcept
{
_password = std::make_shared<std::string>(password);
_dirtyFlag[2] = true;
}
void Users::setPassword(std::string &&password) noexcept
{
_password = std::make_shared<std::string>(std::move(password));
_dirtyFlag[2] = true;
}
const std::string & Users::getValueOfOrgName(const std::string &defaultValue) const noexcept
{
if(_orgName)
return *_orgName;
return defaultValue;
}
std::shared_ptr<const std::string> Users::getOrgName() const noexcept
{
return _orgName;
}
void Users::setOrgName(const std::string &orgName) noexcept
{
_orgName = std::make_shared<std::string>(orgName);
_dirtyFlag[3] = true;
}
void Users::setOrgName(std::string &&orgName) noexcept
{
_orgName = std::make_shared<std::string>(std::move(orgName));
_dirtyFlag[3] = true;
}
const std::string & Users::getValueOfSignature(const std::string &defaultValue) const noexcept
{
if(_signature)
return *_signature;
return defaultValue;
}
std::shared_ptr<const std::string> Users::getSignature() const noexcept
{
return _signature;
}
void Users::setSignature(const std::string &signature) noexcept
{
_signature = std::make_shared<std::string>(signature);
_dirtyFlag[4] = true;
}
void Users::setSignature(std::string &&signature) noexcept
{
_signature = std::make_shared<std::string>(std::move(signature));
_dirtyFlag[4] = true;
}
const std::string & Users::getValueOfAvatarId(const std::string &defaultValue) const noexcept
{
if(_avatarId)
return *_avatarId;
return defaultValue;
}
std::shared_ptr<const std::string> Users::getAvatarId() const noexcept
{
return _avatarId;
}
void Users::setAvatarId(const std::string &avatarId) noexcept
{
_avatarId = std::make_shared<std::string>(avatarId);
_dirtyFlag[5] = true;
}
void Users::setAvatarId(std::string &&avatarId) noexcept
{
_avatarId = std::make_shared<std::string>(std::move(avatarId));
_dirtyFlag[5] = true;
}
const int32_t & Users::getValueOfId(const int32_t &defaultValue) const noexcept
{
if(_id)
return *_id;
return defaultValue;
}
std::shared_ptr<const int32_t> Users::getId() const noexcept
{
return _id;
}
const typename Users::PrimaryKeyType & Users::getPrimaryKey() const
{
assert(_id);
return *_id;
}
const std::string & Users::getValueOfSalt(const std::string &defaultValue) const noexcept
{
if(_salt)
return *_salt;
return defaultValue;
}
std::shared_ptr<const std::string> Users::getSalt() const noexcept
{
return _salt;
}
void Users::setSalt(const std::string &salt) noexcept
{
_salt = std::make_shared<std::string>(salt);
_dirtyFlag[7] = true;
}
void Users::setSalt(std::string &&salt) noexcept
{
_salt = std::make_shared<std::string>(std::move(salt));
_dirtyFlag[7] = true;
}
const bool & Users::getValueOfAdmin(const bool &defaultValue) const noexcept
{
if(_admin)
return *_admin;
return defaultValue;
}
std::shared_ptr<const bool> Users::getAdmin() const noexcept
{
return _admin;
}
void Users::setAdmin(const bool &admin) noexcept
{
_admin = std::make_shared<bool>(admin);
_dirtyFlag[8] = true;
}
const std::vector<std::string> &Users::insertColumns() noexcept
{
static const std::vector<std::string> _inCols={
"user_id",
"user_name",
"password",
"org_name",
"signature",
"avatar_id",
"salt",
"admin"
};
return _inCols;
}
void Users::outputArgs(drogon::orm::internal::SqlBinder &binder) const
{
if(getUserId())
{
binder << getValueOfUserId();
}
else
{
binder << nullptr;
}
if(getUserName())
{
binder << getValueOfUserName();
}
else
{
binder << nullptr;
}
if(getPassword())
{
binder << getValueOfPassword();
}
else
{
binder << nullptr;
}
if(getOrgName())
{
binder << getValueOfOrgName();
}
else
{
binder << nullptr;
}
if(getSignature())
{
binder << getValueOfSignature();
}
else
{
binder << nullptr;
}
if(getAvatarId())
{
binder << getValueOfAvatarId();
}
else
{
binder << nullptr;
}
if(getSalt())
{
binder << getValueOfSalt();
}
else
{
binder << nullptr;
}
if(getAdmin())
{
binder << getValueOfAdmin();
}
else
{
binder << nullptr;
}
}
const std::vector<std::string> Users::updateColumns() const
{
std::vector<std::string> ret;
for(size_t i=0;i<sizeof(_dirtyFlag);i++)
{
if(_dirtyFlag[i])
{
ret.push_back(getColumnName(i));
}
}
return ret;
}
void Users::updateArgs(drogon::orm::internal::SqlBinder &binder) const
{
if(_dirtyFlag[0])
{
if(getUserId())
{
binder << getValueOfUserId();
}
else
{
binder << nullptr;
}
}
if(_dirtyFlag[1])
{
if(getUserName())
{
binder << getValueOfUserName();
}
else
{
binder << nullptr;
}
}
if(_dirtyFlag[2])
{
if(getPassword())
{
binder << getValueOfPassword();
}
else
{
binder << nullptr;
}
}
if(_dirtyFlag[3])
{
if(getOrgName())
{
binder << getValueOfOrgName();
}
else
{
binder << nullptr;
}
}
if(_dirtyFlag[4])
{
if(getSignature())
{
binder << getValueOfSignature();
}
else
{
binder << nullptr;
}
}
if(_dirtyFlag[5])
{
if(getAvatarId())
{
binder << getValueOfAvatarId();
}
else
{
binder << nullptr;
}
}
if(_dirtyFlag[7])
{
if(getSalt())
{
binder << getValueOfSalt();
}
else
{
binder << nullptr;
}
}
if(_dirtyFlag[8])
{
if(getAdmin())
{
binder << getValueOfAdmin();
}
else
{
binder << nullptr;
}
}
}
Json::Value Users::toJson() const
{
Json::Value ret;
if(getUserId())
{
ret["user_id"]=getValueOfUserId();
}
else
{
ret["user_id"]=Json::Value();
}
if(getUserName())
{
ret["user_name"]=getValueOfUserName();
}
else
{
ret["user_name"]=Json::Value();
}
if(getPassword())
{
ret["password"]=getValueOfPassword();
}
else
{
ret["password"]=Json::Value();
}
if(getOrgName())
{
ret["org_name"]=getValueOfOrgName();
}
else
{
ret["org_name"]=Json::Value();
}
if(getSignature())
{
ret["signature"]=getValueOfSignature();
}
else
{
ret["signature"]=Json::Value();
}
if(getAvatarId())
{
ret["avatar_id"]=getValueOfAvatarId();
}
else
{
ret["avatar_id"]=Json::Value();
}
if(getId())
{
ret["id"]=getValueOfId();
}
else
{
ret["id"]=Json::Value();
}
if(getSalt())
{
ret["salt"]=getValueOfSalt();
}
else
{
ret["salt"]=Json::Value();
}
if(getAdmin())
{
ret["admin"]=getValueOfAdmin();
}
else
{
ret["admin"]=Json::Value();
}
return ret;
}

167
orm_lib/src/tests/Users.h Normal file
View File

@ -0,0 +1,167 @@
/**
*
* Users.h
* DO NOT EDIT,This file is generated by drogon_ctl
*
*/
#pragma once
#include <drogon/orm/Result.h>
#include <drogon/orm/Row.h>
#include <drogon/orm/Field.h>
#include <drogon/orm/SqlBinder.h>
#include <drogon/orm/Mapper.h>
#include <trantor/utils/Date.h>
#include <json/json.h>
#include <string>
#include <memory>
#include <vector>
#include <tuple>
#include <stdint.h>
#include <iostream>
using namespace drogon::orm;
namespace drogon_model
{
namespace postgres
{
class Users
{
public:
struct Cols
{
static const std::string user_id;
static const std::string user_name;
static const std::string password;
static const std::string org_name;
static const std::string signature;
static const std::string avatar_id;
static const std::string id;
static const std::string salt;
static const std::string admin;
};
const static int primaryKeyNumber;
const static std::string tableName;
const static bool hasPrimaryKey;
const static std::string primaryKeyName;
typedef int32_t PrimaryKeyType;
const PrimaryKeyType & getPrimaryKey() const;
Users(const Row &r) noexcept;
Users() = default;
/** For column user_id */
///Get the value of the column user_id, returns the default value if the column is null
const std::string &getValueOfUserId(const std::string &defaultValue=std::string()) const noexcept;
///Returns a shared_ptr object pointing to the column const value, or an empty shared_ptr object if the column is null
std::shared_ptr<const std::string> getUserId() const noexcept;
///Set the value of the column user_id
void setUserId(const std::string &userId) noexcept;
void setUserId(std::string &&userId) noexcept;
/** For column user_name */
///Get the value of the column user_name, returns the default value if the column is null
const std::string &getValueOfUserName(const std::string &defaultValue=std::string()) const noexcept;
///Returns a shared_ptr object pointing to the column const value, or an empty shared_ptr object if the column is null
std::shared_ptr<const std::string> getUserName() const noexcept;
///Set the value of the column user_name
void setUserName(const std::string &userName) noexcept;
void setUserName(std::string &&userName) noexcept;
/** For column password */
///Get the value of the column password, returns the default value if the column is null
const std::string &getValueOfPassword(const std::string &defaultValue=std::string()) const noexcept;
///Returns a shared_ptr object pointing to the column const value, or an empty shared_ptr object if the column is null
std::shared_ptr<const std::string> getPassword() const noexcept;
///Set the value of the column password
void setPassword(const std::string &password) noexcept;
void setPassword(std::string &&password) noexcept;
/** For column org_name */
///Get the value of the column org_name, returns the default value if the column is null
const std::string &getValueOfOrgName(const std::string &defaultValue=std::string()) const noexcept;
///Returns a shared_ptr object pointing to the column const value, or an empty shared_ptr object if the column is null
std::shared_ptr<const std::string> getOrgName() const noexcept;
///Set the value of the column org_name
void setOrgName(const std::string &orgName) noexcept;
void setOrgName(std::string &&orgName) noexcept;
/** For column signature */
///Get the value of the column signature, returns the default value if the column is null
const std::string &getValueOfSignature(const std::string &defaultValue=std::string()) const noexcept;
///Returns a shared_ptr object pointing to the column const value, or an empty shared_ptr object if the column is null
std::shared_ptr<const std::string> getSignature() const noexcept;
///Set the value of the column signature
void setSignature(const std::string &signature) noexcept;
void setSignature(std::string &&signature) noexcept;
/** For column avatar_id */
///Get the value of the column avatar_id, returns the default value if the column is null
const std::string &getValueOfAvatarId(const std::string &defaultValue=std::string()) const noexcept;
///Returns a shared_ptr object pointing to the column const value, or an empty shared_ptr object if the column is null
std::shared_ptr<const std::string> getAvatarId() const noexcept;
///Set the value of the column avatar_id
void setAvatarId(const std::string &avatarId) noexcept;
void setAvatarId(std::string &&avatarId) noexcept;
/** For column id */
///Get the value of the column id, returns the default value if the column is null
const int32_t &getValueOfId(const int32_t &defaultValue=int32_t()) const noexcept;
///Returns a shared_ptr object pointing to the column const value, or an empty shared_ptr object if the column is null
std::shared_ptr<const int32_t> getId() const noexcept;
/** For column salt */
///Get the value of the column salt, returns the default value if the column is null
const std::string &getValueOfSalt(const std::string &defaultValue=std::string()) const noexcept;
///Returns a shared_ptr object pointing to the column const value, or an empty shared_ptr object if the column is null
std::shared_ptr<const std::string> getSalt() const noexcept;
///Set the value of the column salt
void setSalt(const std::string &salt) noexcept;
void setSalt(std::string &&salt) noexcept;
/** For column admin */
///Get the value of the column admin, returns the default value if the column is null
const bool &getValueOfAdmin(const bool &defaultValue=bool()) const noexcept;
///Returns a shared_ptr object pointing to the column const value, or an empty shared_ptr object if the column is null
std::shared_ptr<const bool> getAdmin() const noexcept;
///Set the value of the column admin
void setAdmin(const bool &admin) noexcept;
static size_t getColumnNumber() noexcept { return 9; }
static const std::string &getColumnName(size_t index) noexcept(false);
Json::Value toJson() const;
private:
friend Mapper<Users>;
static const std::vector<std::string> &insertColumns() noexcept;
void outputArgs(drogon::orm::internal::SqlBinder &binder) const;
const std::vector<std::string> updateColumns() const;
void updateArgs(drogon::orm::internal::SqlBinder &binder) const;
std::shared_ptr<std::string> _userId;
std::shared_ptr<std::string> _userName;
std::shared_ptr<std::string> _password;
std::shared_ptr<std::string> _orgName;
std::shared_ptr<std::string> _signature;
std::shared_ptr<std::string> _avatarId;
std::shared_ptr<int32_t> _id;
std::shared_ptr<std::string> _salt;
std::shared_ptr<bool> _admin;
struct MetaData
{
const std::string _colName;
const std::string _colType;
const std::string _colDatabaseType;
const ssize_t _colLength;
const bool _isAutoVal;
const bool _isPrimaryKey;
const bool _notNull;
};
static const std::vector<MetaData> _metaData;
bool _dirtyFlag[9]={ false };
};
} // namespace postgres
} // namespace drogon_model

View File

@ -0,0 +1,115 @@
/**
*
* db_test.cc
* An Tao
*
* Copyright 2018, An Tao. All rights reserved.
* Use of this source code is governed by a MIT license
* that can be found in the License file.
*
* Drogon
*
* Drogon database test program
*
*/
#include "Users.h"
#include <drogon/orm/DbClient.h>
#include <trantor/utils/Logger.h>
#include <iostream>
#include <unistd.h>
using namespace drogon::orm;
#define RESET "\033[0m"
#define RED "\033[31m" /* Red */
#define GREEN "\033[32m" /* Green */
void testOutput(bool isGood, const std::string &testMessage)
{
if (isGood)
{
std::cout << GREEN << testMessage << "\t\tOK\n";
std::cout << RESET;
}
else
{
std::cout << RED << testMessage << "\t\tBAD\n";
std::cout << RESET;
exit(-1);
}
}
int main()
{
trantor::Logger::setLogLevel(trantor::Logger::DEBUG);
auto clientPtr = DbClient::newPgClient("host=127.0.0.1 port=5432 dbname=postgres user=antao", 1);
LOG_DEBUG << "start!";
sleep(1);
//Prepare the test environment
*clientPtr << "DROP TABLE IF EXISTS USERS" >>
[](const Result &r) {
testOutput(true, "Prepare the test environment(0)");
} >>
[](const DrogonDbException &e) {
std::cerr << e.base().what() << std::endl;
testOutput(false, "Prepare the test environment(0)");
};
*clientPtr << "CREATE TABLE users \
(\
user_id character varying(32),\
user_name character varying(64),\
password character varying(64),\
org_name character varying(20),\
signature character varying(50),\
avatar_id character varying(32),\
id serial PRIMARY KEY,\
salt character varying(20),\
admin boolean DEFAULT false,\
CONSTRAINT user_id_org UNIQUE(user_id, org_name)\
)" >>
[](const Result &r) {
testOutput(true, "Prepare the test environment(1)");
} >>
[](const DrogonDbException &e) {
std::cerr << e.base().what() << std::endl;
testOutput(false, "Prepare the test environment(1)");
};
/// Test1:DbClient streaming-type interface
/// 1.1 insert,non-blocking
*clientPtr << "insert into users \
(user_id,user_name,password,org_name) \
values($1,$2,$3,$4) returning *"
<< "pg"
<< "postgresql"
<< "123"
<< "default" >>
[](const Result &r) {
std::cout << "id=" << r[0]["id"].as<int64_t>() << std::endl;
testOutput(r[0]["id"].as<int64_t>() == 1, "Prepare the test environment(1)");
} >>
[](const DrogonDbException &e) {
std::cerr << e.base().what() << std::endl;
testOutput(false, "Prepare the test environment(1)");
};
///1.2 insert,blocking
*clientPtr << "insert into users \
(user_id,user_name,password,org_name) \
values($1,$2,$3,$4) returning *"
<< "pg1"
<< "postgresql1"
<< "123"
<< "default"
<< Mode::Blocking >>
[](const Result &r) {
std::cout << "id=" << r[0]["id"].as<int64_t>() << std::endl;
testOutput(r[0]["id"].as<int64_t>() == 2, "Prepare the test environment(1)");
} >>
[](const DrogonDbException &e) {
std::cerr << e.base().what() << std::endl;
testOutput(false, "Prepare the test environment(1)");
};
/// Add more testing here
/// 2 DbClient execSqlAsync()...
///
/// Model and Mapper....
getchar();
}

View File

@ -0,0 +1,14 @@
{
//rdbms:server type, postgreSQL
"rdbms":"postgreSQL",
//host:server address,localhost by default;
"host":"127.0.0.1",
//port:server port, 5432 by default;
"port":5432,
//dbname:Database name;
"dbname":"postgres",
"user":"antao",
"passwd":"",
//"tables":["group_users"]
"tables":["users"]
}

@ -1 +1 @@
Subproject commit 90a3ba84b157923ea8b5858a4d16733f02d88c10
Subproject commit fce171a7ad73b61efe77b847318aca0dcd22ac8a