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

add database module #76

Merged
merged 1 commit into from
Aug 11, 2023
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
8 changes: 6 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ set(FAKER_SOURCES
src/modules/phone/Phone.cpp
src/common/LuhnCheck.cpp
src/common/mappers/PrecisionMapper.cpp
src/modules/system/System.cpp)
src/modules/system/System.cpp
src/modules/database/Database.cpp
)

set(FAKER_UT_SOURCES
src/modules/book/BookTest.cpp
Expand All @@ -58,7 +60,9 @@ set(FAKER_UT_SOURCES
src/modules/helper/HelperTest.cpp
src/common/LuhnCheckTest.cpp
src/common/mappers/PrecisionMapperTest.cpp
src/modules/system/SystemTest.cpp)
src/modules/system/SystemTest.cpp
src/modules/database/DatabaseTest.cpp
)

add_library(${LIBRARY_NAME} ${FAKER_SOURCES})

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ target_link_libraries(main faker-cxx)
- 📖 Book - Generate book title, genre, author, publisher, ISBN.
- 📚 Lorem - Generate lorem words, sentences, paragraphs.
- 🔢 String - Generate uuids, alphanumeric, numeric, hexadecimal.
- 💻 System - Generate file paths, file extensions, file names, directories, semantic version.
- Database - Generate column names, column types, database engines.

### 🔨 [TODO Modules](https://github.com/cieslarmichal/faker-cxx/blob/main/TODO.md)

Expand Down
10 changes: 5 additions & 5 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,11 @@

### Database:

- [ ] collation
- [ ] column
- [ ] engine
- [ ] mongodbObjectId
- [ ] type
- [x] collation
- [x] column
- [x] engine
- [x] type
- [x] mongodbObjectId

### 💻 System:

Expand Down
65 changes: 65 additions & 0 deletions include/faker-cxx/Database.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#pragma once

#include <string>

namespace faker
{
class Database
{
public:
/**
* @brief Returns a random database column name.
*
* @returns Database column name.
*
* @code
* Database::columnName() // "created_at"
* @endcode
*/
static std::string columnName();

/**
* @brief Returns a random database column type.
*
* @returns Database column type.
*
* @code
* Database::columnType() // "timestamp"
* @endcode
*/
static std::string columnType();

/**
* @brief Returns a random database collation.
*
* @returns Database collation.
*
* @code
* Database::collation() // "utf8_unicode_ci"
* @endcode
*/
static std::string collation();

/**
* @brief Returns a random database engine.
*
* @returns Database engine.
*
* @code
* Database::engine() // "ARCHIVE"
* @endcode
*/
static std::string engine();

/**
* @brief Returns a MongoDB Object Id.
*
* @returns MongoDB Object Id.
*
* @code
* Database::mongoDbObjectId() // "e175cac316a79afdd0ad3afb"
* @endcode
*/
static std::string mongoDbObjectId();
};
}
1 change: 1 addition & 0 deletions include/faker-cxx/String.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ class String
* String::hexadecimal() // "0xb"
* String::hexadecimal(10) // "0xae13d044cb"
* String::hexadecimal(6, HexCasing::Upper, HexPrefix::Hash) // "#E3F380"
* String::hexadecimal(6, HexCasing::Lower, HexPrefix::None) // "e3f380"
* @endcode
*/
static std::string hexadecimal(unsigned length = 1, HexCasing casing = HexCasing::Lower,
Expand Down
3 changes: 2 additions & 1 deletion include/faker-cxx/types/Hex.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ enum class HexCasing
enum class HexPrefix
{
ZeroX,
Hash
Hash,
None
};
}
37 changes: 37 additions & 0 deletions src/modules/database/Database.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "faker-cxx/Database.h"

#include "data/Collations.h"
#include "data/ColumnNames.h"
#include "data/ColumnTypes.h"
#include "data/Engines.h"
#include "faker-cxx/Helper.h"
#include "faker-cxx/String.h"

namespace faker
{

std::string Database::columnName()
{
return Helper::arrayElement<std::string>(columnNames);
}

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

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

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

std::string Database::mongoDbObjectId()
{
return String::hexadecimal(24, HexCasing::Lower, HexPrefix::None);
}
}
64 changes: 64 additions & 0 deletions src/modules/database/DatabaseTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include "faker-cxx/Database.h"

#include <algorithm>

#include "gtest/gtest.h"

#include "../string/data/Characters.h"
#include "data/Collations.h"
#include "data/ColumnNames.h"
#include "data/ColumnTypes.h"
#include "data/Engines.h"

using namespace ::testing;
using namespace faker;

class DatabaseTest : public Test
{
public:
};

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

ASSERT_TRUE(std::any_of(columnNames.begin(), columnNames.end(),
[generatedColumnName](const std::string& columnName)
{ return generatedColumnName == columnName; }));
}

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

ASSERT_TRUE(std::any_of(columnTypes.begin(), columnTypes.end(),
[generatedColumnType](const std::string& columnType)
{ return generatedColumnType == columnType; }));
}

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

ASSERT_TRUE(std::any_of(engines.begin(), engines.end(),
[generatedEngine](const std::string& engine) { return generatedEngine == engine; }));
}

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

ASSERT_TRUE(std::any_of(collations.begin(), collations.end(),
[generatedCollation](const std::string& collation)
{ return generatedCollation == collation; }));
}

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

ASSERT_EQ(mongoDbObjectId.size(), 24);
ASSERT_TRUE(std::any_of(mongoDbObjectId.begin(), mongoDbObjectId.end(),
[](char hexNumberCharacter)
{ return hexLowerCharacters.find(hexNumberCharacter) != std::string::npos; }));
}
10 changes: 10 additions & 0 deletions src/modules/database/data/Collations.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

#include <string>
#include <vector>

namespace faker
{
const std::vector<std::string> collations = {"utf8_unicode_ci", "utf8_general_ci", "utf8_bin", "ascii_bin",
"ascii_general_ci", "cp1250_bin", "cp1250_general_ci"};
}
13 changes: 13 additions & 0 deletions src/modules/database/data/ColumnNames.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include <string>
#include <vector>

namespace faker
{
const std::vector<std::string> columnNames = {
"id", "title", "name", "email", "username", "first_name", "last_name", "phone", "token",
"group", "category", "password", "comment", "avatar", "status", "created_at", "updated_at",
};

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

#include <string>
#include <vector>

namespace faker
{
const std::vector<std::string> 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",
};
}
10 changes: 10 additions & 0 deletions src/modules/database/data/Engines.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

#include <string>
#include <vector>

namespace faker
{
const std::vector<std::string> engines = {"InnoDB", "MyISAM", "MEMORY", "CSV", "BLACKHOLE", "ARCHIVE"};

}
1 change: 1 addition & 0 deletions src/modules/string/String.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const std::map<HexCasing, std::string> hexCasingToCharactersMapping{
const std::map<HexPrefix, std::string> hexPrefixToStringMapping{
{HexPrefix::ZeroX, "0x"},
{HexPrefix::Hash, "#"},
{HexPrefix::None, ""},
};
}

Expand Down
12 changes: 12 additions & 0 deletions src/modules/string/StringTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,18 @@ TEST_F(StringTest, shouldGenerateHexadecimalWithHashPrefix)
{ return hexUpperCharacters.find(hexNumberCharacter) != std::string::npos; }));
}

TEST_F(StringTest, shouldGenerateHexadecimalWithoutPrefix)
{
const auto hexadecimalLength = 8;

const auto hexadecimal = String::hexadecimal(hexadecimalLength, HexCasing::Upper, HexPrefix::None);

ASSERT_EQ(hexadecimal.size(), hexadecimalLength);
ASSERT_TRUE(std::any_of(hexadecimal.begin(), hexadecimal.end(),
[](char hexNumberCharacter)
{ return hexUpperCharacters.find(hexNumberCharacter) != std::string::npos; }));
}

TEST_F(StringTest, shouldGenerateBinary)
{
const auto binaryLength = 8;
Expand Down