From deea445d5fbe5917cb1ad9d67cdd8a3e5888b3fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Cie=C5=9Blar?= Date: Tue, 28 May 2024 20:35:22 +0200 Subject: [PATCH] refactor database module data (#601) --- CMakeLists.txt | 1 + include/faker-cxx/Database.h | 10 +++++----- src/modules/database/Database.cpp | 21 +++++++++------------ src/modules/database/DatabaseData.cpp | 22 ++++++++++++++++++++++ src/modules/database/DatabaseData.h | 12 ++++++++++++ src/modules/database/data/Collations.h | 10 ---------- src/modules/database/data/ColumnNames.h | 13 ------------- src/modules/database/data/ColumnTypes.h | 13 ------------- src/modules/database/data/Engines.h | 10 ---------- tests/modules/database/DatabaseTest.cpp | 13 +++++-------- 10 files changed, 54 insertions(+), 71 deletions(-) create mode 100644 src/modules/database/DatabaseData.cpp create mode 100644 src/modules/database/DatabaseData.h delete mode 100644 src/modules/database/data/Collations.h delete mode 100644 src/modules/database/data/ColumnNames.h delete mode 100644 src/modules/database/data/ColumnTypes.h delete mode 100644 src/modules/database/data/Engines.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 9c780fee5..9b6d8a672 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -40,6 +40,7 @@ set(FAKER_SOURCES src/modules/computer/ComputerData.cpp src/modules/crypto/Crypto.cpp src/modules/database/Database.cpp + src/modules/database/DatabaseData.cpp src/modules/datatype/Datatype.cpp src/modules/date/Date.cpp src/modules/finance/Finance.cpp diff --git a/include/faker-cxx/Database.h b/include/faker-cxx/Database.h index c09dc455c..a88587828 100644 --- a/include/faker-cxx/Database.h +++ b/include/faker-cxx/Database.h @@ -1,6 +1,6 @@ #pragma once -#include +#include namespace faker { @@ -16,7 +16,7 @@ class Database * Database::columnName() // "created_at" * @endcode */ - static std::string columnName(); + static std::string_view columnName(); /** * @brief Returns a random database column type. @@ -27,7 +27,7 @@ class Database * Database::columnType() // "timestamp" * @endcode */ - static std::string columnType(); + static std::string_view columnType(); /** * @brief Returns a random database collation. @@ -38,7 +38,7 @@ class Database * Database::collation() // "utf8_unicode_ci" * @endcode */ - static std::string collation(); + static std::string_view collation(); /** * @brief Returns a random database engine. @@ -49,7 +49,7 @@ class Database * Database::engine() // "ARCHIVE" * @endcode */ - static std::string engine(); + static std::string_view engine(); /** * @brief Returns a MongoDB Object Id. diff --git a/src/modules/database/Database.cpp b/src/modules/database/Database.cpp index 71a0b67d7..3d44acc59 100644 --- a/src/modules/database/Database.cpp +++ b/src/modules/database/Database.cpp @@ -1,32 +1,29 @@ #include "faker-cxx/Database.h" -#include "data/Collations.h" -#include "data/ColumnNames.h" -#include "data/ColumnTypes.h" -#include "data/Engines.h" +#include "DatabaseData.h" #include "faker-cxx/Helper.h" #include "faker-cxx/String.h" namespace faker { -std::string Database::columnName() +std::string_view Database::columnName() { - return Helper::arrayElement(columnNames); + return Helper::arrayElement(columnNames); } -std::string Database::columnType() +std::string_view Database::columnType() { - return Helper::arrayElement(columnTypes); + return Helper::arrayElement(columnTypes); } -std::string Database::collation() +std::string_view Database::collation() { - return Helper::arrayElement(collations); + return Helper::arrayElement(collations); } -std::string Database::engine() +std::string_view Database::engine() { - return Helper::arrayElement(engines); + return Helper::arrayElement(engines); } std::string Database::mongoDbObjectId() diff --git a/src/modules/database/DatabaseData.cpp b/src/modules/database/DatabaseData.cpp new file mode 100644 index 000000000..e164b6e2d --- /dev/null +++ b/src/modules/database/DatabaseData.cpp @@ -0,0 +1,22 @@ +#include "DatabaseData.h" + +namespace faker +{ +const std::array collations = {"utf8_unicode_ci", "utf8_general_ci", "utf8_bin", + "ascii_bin", "ascii_general_ci", "cp1250_bin", + "cp1250_general_ci"}; + +const std::array columnNames = { + "id", "title", "name", "email", "username", "first_name", "last_name", "phone", "token", + "group", "category", "password", "comment", "avatar", "status", "created_at", "updated_at", +}; + +const std::array columnTypes = { + "int", "varchar", "text", "date", "datetime", "tinyint", "time", "timestamp", + "smallint", "mediumint", "bigint", "decimal", "float", "double", "real", "bit", + "boolean", "serial", "blob", "binary", "enum", "set", "geometry", "point", +}; + +const std::array engines = {"InnoDB", "MyISAM", "MEMORY", "CSV", "BLACKHOLE", "ARCHIVE"}; + +} diff --git a/src/modules/database/DatabaseData.h b/src/modules/database/DatabaseData.h new file mode 100644 index 000000000..71ef7fce3 --- /dev/null +++ b/src/modules/database/DatabaseData.h @@ -0,0 +1,12 @@ +#pragma once + +#include +#include + +namespace faker +{ +extern const std::array collations; +extern const std::array columnNames; +extern const std::array columnTypes; +extern const std::array engines; +} diff --git a/src/modules/database/data/Collations.h b/src/modules/database/data/Collations.h deleted file mode 100644 index 3a53c6da1..000000000 --- a/src/modules/database/data/Collations.h +++ /dev/null @@ -1,10 +0,0 @@ -#pragma once - -#include -#include - -namespace faker -{ -const std::vector collations = {"utf8_unicode_ci", "utf8_general_ci", "utf8_bin", "ascii_bin", - "ascii_general_ci", "cp1250_bin", "cp1250_general_ci"}; -} diff --git a/src/modules/database/data/ColumnNames.h b/src/modules/database/data/ColumnNames.h deleted file mode 100644 index 68d16b3db..000000000 --- a/src/modules/database/data/ColumnNames.h +++ /dev/null @@ -1,13 +0,0 @@ -#pragma once - -#include -#include - -namespace faker -{ -const std::vector columnNames = { - "id", "title", "name", "email", "username", "first_name", "last_name", "phone", "token", - "group", "category", "password", "comment", "avatar", "status", "created_at", "updated_at", -}; - -} diff --git a/src/modules/database/data/ColumnTypes.h b/src/modules/database/data/ColumnTypes.h deleted file mode 100644 index 1f3f864b2..000000000 --- a/src/modules/database/data/ColumnTypes.h +++ /dev/null @@ -1,13 +0,0 @@ -#pragma once - -#include -#include - -namespace faker -{ -const std::vector columnTypes = { - "int", "varchar", "text", "date", "datetime", "tinyint", "time", "timestamp", - "smallint", "mediumint", "bigint", "decimal", "float", "double", "real", "bit", - "boolean", "serial", "blob", "binary", "enum", "set", "geometry", "point", -}; -} diff --git a/src/modules/database/data/Engines.h b/src/modules/database/data/Engines.h deleted file mode 100644 index 586b80fa8..000000000 --- a/src/modules/database/data/Engines.h +++ /dev/null @@ -1,10 +0,0 @@ -#pragma once - -#include -#include - -namespace faker -{ -const std::vector engines = {"InnoDB", "MyISAM", "MEMORY", "CSV", "BLACKHOLE", "ARCHIVE"}; - -} diff --git a/tests/modules/database/DatabaseTest.cpp b/tests/modules/database/DatabaseTest.cpp index bc440ddad..8f91ffa95 100644 --- a/tests/modules/database/DatabaseTest.cpp +++ b/tests/modules/database/DatabaseTest.cpp @@ -4,11 +4,8 @@ #include "gtest/gtest.h" +#include "database/DatabaseData.h" #include "string/data/Characters.h" -#include "database/data/Collations.h" -#include "database/data/ColumnNames.h" -#include "database/data/ColumnTypes.h" -#include "database/data/Engines.h" using namespace ::testing; using namespace faker; @@ -22,7 +19,7 @@ TEST_F(DatabaseTest, shouldGenerateColumnName) { const auto generatedColumnName = Database::columnName(); - ASSERT_TRUE(std::ranges::any_of(columnNames, [generatedColumnName](const std::string& columnName) + ASSERT_TRUE(std::ranges::any_of(columnNames, [generatedColumnName](const std::string_view& columnName) { return generatedColumnName == columnName; })); } @@ -30,7 +27,7 @@ TEST_F(DatabaseTest, shouldGenerateColumnType) { const auto generatedColumnType = Database::columnType(); - ASSERT_TRUE(std::ranges::any_of(columnTypes, [generatedColumnType](const std::string& columnType) + ASSERT_TRUE(std::ranges::any_of(columnTypes, [generatedColumnType](const std::string_view& columnType) { return generatedColumnType == columnType; })); } @@ -38,7 +35,7 @@ TEST_F(DatabaseTest, shouldGenerateEngine) { const auto generatedEngine = Database::engine(); - ASSERT_TRUE(std::ranges::any_of(engines, [generatedEngine](const std::string& engine) + ASSERT_TRUE(std::ranges::any_of(engines, [generatedEngine](const std::string_view& engine) { return generatedEngine == engine; })); } @@ -46,7 +43,7 @@ TEST_F(DatabaseTest, shouldGenerateCollation) { const auto generatedCollation = Database::collation(); - ASSERT_TRUE(std::ranges::any_of(collations, [generatedCollation](const std::string& collation) + ASSERT_TRUE(std::ranges::any_of(collations, [generatedCollation](const std::string_view& collation) { return generatedCollation == collation; })); }