Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: database module migrate #715

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions include/faker-cxx/Database.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@

#include <string_view>

namespace faker
namespace faker::database
{
class Database
{
public:
/**
* @brief Returns a random database column name.
*
Expand All @@ -16,7 +13,7 @@ class Database
* Database::columnName() // "created_at"
* @endcode
*/
static std::string_view columnName();
std::string_view columnName();

/**
* @brief Returns a random database column type.
Expand All @@ -27,7 +24,7 @@ class Database
* Database::columnType() // "timestamp"
* @endcode
*/
static std::string_view columnType();
std::string_view columnType();

/**
* @brief Returns a random database collation.
Expand All @@ -38,7 +35,7 @@ class Database
* Database::collation() // "utf8_unicode_ci"
* @endcode
*/
static std::string_view collation();
std::string_view collation();

/**
* @brief Returns a random database engine.
Expand All @@ -49,7 +46,7 @@ class Database
* Database::engine() // "ARCHIVE"
* @endcode
*/
static std::string_view engine();
std::string_view engine();

/**
* @brief Returns a MongoDB Object Id.
Expand All @@ -60,6 +57,5 @@ class Database
* Database::mongoDbObjectId() // "e175cac316a79afdd0ad3afb"
* @endcode
*/
static std::string mongoDbObjectId();
};
std::string mongoDbObjectId();
}
12 changes: 6 additions & 6 deletions src/modules/database/Database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,29 @@
#include "faker-cxx/String.h"
#include "faker-cxx/types/Hex.h"

namespace faker
namespace faker::database
{
std::string_view Database::columnName()
std::string_view columnName()
{
return Helper::arrayElement(columnNames);
}

std::string_view Database::columnType()
std::string_view columnType()
{
return Helper::arrayElement(columnTypes);
}

std::string_view Database::collation()
std::string_view collation()
{
return Helper::arrayElement(collations);
}

std::string_view Database::engine()
std::string_view engine()
{
return Helper::arrayElement(engines);
}

std::string Database::mongoDbObjectId()
std::string mongoDbObjectId()
{
return String::hexadecimal(24, HexCasing::Lower, HexPrefix::None);
}
Expand Down
2 changes: 1 addition & 1 deletion src/modules/database/DatabaseData.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <array>
#include <string_view>

namespace faker
namespace faker::database
{
const auto collations = std::to_array<std::string_view>({
"utf8_unicode_ci", "utf8_general_ci", "utf8_bin",
Expand Down
15 changes: 8 additions & 7 deletions tests/modules/database/DatabaseTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

using namespace ::testing;
using namespace faker;
using namespace faker::database;

class DatabaseTest : public Test
{
Expand All @@ -18,41 +19,41 @@ class DatabaseTest : public Test

TEST_F(DatabaseTest, shouldGenerateColumnName)
{
const auto generatedColumnName = Database::columnName();
const auto generatedColumnName = columnName();

ASSERT_TRUE(std::ranges::any_of(columnNames, [generatedColumnName](const std::string_view& columnName)
{ return generatedColumnName == columnName; }));
}

TEST_F(DatabaseTest, shouldGenerateColumnType)
{
const auto generatedColumnType = Database::columnType();
const auto generatedColumnType = columnType();

ASSERT_TRUE(std::ranges::any_of(columnTypes, [generatedColumnType](const std::string_view& columnType)
{ return generatedColumnType == columnType; }));
}

TEST_F(DatabaseTest, shouldGenerateEngine)
{
const auto generatedEngine = Database::engine();
const auto generatedEngine = engine();

ASSERT_TRUE(std::ranges::any_of(engines, [generatedEngine](const std::string_view& engine)
{ return generatedEngine == engine; }));
}

TEST_F(DatabaseTest, shouldGenerateCollation)
{
const auto generatedCollation = Database::collation();
const auto generatedCollation = collation();

ASSERT_TRUE(std::ranges::any_of(collations, [generatedCollation](const std::string_view& collation)
{ return generatedCollation == collation; }));
}

TEST_F(DatabaseTest, shouldGenerateMongoDbObjectId)
{
const auto mongoDbObjectId = Database::mongoDbObjectId();
const auto generatedMongoDbObjectId = mongoDbObjectId();

ASSERT_EQ(mongoDbObjectId.size(), 24);
ASSERT_TRUE(std::ranges::any_of(mongoDbObjectId, [](char hexNumberCharacter)
ASSERT_EQ(generatedMongoDbObjectId.size(), 24);
ASSERT_TRUE(std::ranges::any_of(generatedMongoDbObjectId, [](char hexNumberCharacter)
{ return hexLowerCharacters.find(hexNumberCharacter) != std::string::npos; }));
}
Loading