Skip to content

Commit

Permalink
refactor database module data (cieslarmichal#601)
Browse files Browse the repository at this point in the history
  • Loading branch information
cieslarmichal authored and 00thirdeye00 committed Jun 20, 2024
1 parent 8383a58 commit deea445
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 71 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions include/faker-cxx/Database.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include <string>
#include <string_view>

namespace faker
{
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand Down
21 changes: 9 additions & 12 deletions src/modules/database/Database.cpp
Original file line number Diff line number Diff line change
@@ -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<std::string>(columnNames);
return Helper::arrayElement(columnNames);
}

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

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

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

std::string Database::mongoDbObjectId()
Expand Down
22 changes: 22 additions & 0 deletions src/modules/database/DatabaseData.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "DatabaseData.h"

namespace faker
{
const std::array<std::string_view, 7> collations = {"utf8_unicode_ci", "utf8_general_ci", "utf8_bin",
"ascii_bin", "ascii_general_ci", "cp1250_bin",
"cp1250_general_ci"};

const std::array<std::string_view, 17> 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<std::string_view, 24> 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<std::string_view, 6> engines = {"InnoDB", "MyISAM", "MEMORY", "CSV", "BLACKHOLE", "ARCHIVE"};

}
12 changes: 12 additions & 0 deletions src/modules/database/DatabaseData.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include <array>
#include <string_view>

namespace faker
{
extern const std::array<std::string_view, 7> collations;
extern const std::array<std::string_view, 17> columnNames;
extern const std::array<std::string_view, 24> columnTypes;
extern const std::array<std::string_view, 6> engines;
}
10 changes: 0 additions & 10 deletions src/modules/database/data/Collations.h

This file was deleted.

13 changes: 0 additions & 13 deletions src/modules/database/data/ColumnNames.h

This file was deleted.

13 changes: 0 additions & 13 deletions src/modules/database/data/ColumnTypes.h

This file was deleted.

10 changes: 0 additions & 10 deletions src/modules/database/data/Engines.h

This file was deleted.

13 changes: 5 additions & 8 deletions tests/modules/database/DatabaseTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,31 +19,31 @@ 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; }));
}

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; }));
}

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; }));
}

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; }));
}

Expand Down

0 comments on commit deea445

Please sign in to comment.