From 2e04b147b4ad10c676ce996748af5d548d8425a6 Mon Sep 17 00:00:00 2001 From: Guru Mehar Rachaputi Date: Mon, 24 Jun 2024 00:52:02 +0200 Subject: [PATCH] Database module migrate Database module migration from class to functions within database namespace Signed-off-by: Guru Mehar Rachaputi --- include/faker-cxx/Database.h | 16 ++++++---------- src/modules/database/Database.cpp | 12 ++++++------ src/modules/database/DatabaseData.h | 2 +- tests/modules/database/DatabaseTest.cpp | 15 ++++++++------- 4 files changed, 21 insertions(+), 24 deletions(-) diff --git a/include/faker-cxx/Database.h b/include/faker-cxx/Database.h index a88587828..c985753ac 100644 --- a/include/faker-cxx/Database.h +++ b/include/faker-cxx/Database.h @@ -2,11 +2,8 @@ #include -namespace faker +namespace faker::database { -class Database -{ -public: /** * @brief Returns a random database column name. * @@ -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. @@ -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. @@ -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. @@ -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. @@ -60,6 +57,5 @@ class Database * Database::mongoDbObjectId() // "e175cac316a79afdd0ad3afb" * @endcode */ - static std::string mongoDbObjectId(); -}; + std::string mongoDbObjectId(); } diff --git a/src/modules/database/Database.cpp b/src/modules/database/Database.cpp index 25b1fa67f..7429fd715 100644 --- a/src/modules/database/Database.cpp +++ b/src/modules/database/Database.cpp @@ -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); } diff --git a/src/modules/database/DatabaseData.h b/src/modules/database/DatabaseData.h index a19527dca..0b7dc9b97 100644 --- a/src/modules/database/DatabaseData.h +++ b/src/modules/database/DatabaseData.h @@ -3,7 +3,7 @@ #include #include -namespace faker +namespace faker::database { const auto collations = std::to_array({ "utf8_unicode_ci", "utf8_general_ci", "utf8_bin", diff --git a/tests/modules/database/DatabaseTest.cpp b/tests/modules/database/DatabaseTest.cpp index 1aec7ca7c..a3fd24b88 100644 --- a/tests/modules/database/DatabaseTest.cpp +++ b/tests/modules/database/DatabaseTest.cpp @@ -10,6 +10,7 @@ using namespace ::testing; using namespace faker; +using namespace faker::database; class DatabaseTest : public Test { @@ -18,7 +19,7 @@ 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; })); @@ -26,7 +27,7 @@ TEST_F(DatabaseTest, shouldGenerateColumnName) 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; })); @@ -34,7 +35,7 @@ TEST_F(DatabaseTest, shouldGenerateColumnType) 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; })); @@ -42,7 +43,7 @@ TEST_F(DatabaseTest, shouldGenerateEngine) 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; })); @@ -50,9 +51,9 @@ TEST_F(DatabaseTest, shouldGenerateCollation) 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; })); }