diff --git a/CMakeLists.txt b/CMakeLists.txt index e3ff4be..3df4917 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,20 +3,36 @@ set(PROJECT_NAME "to-dos-api") project(${PROJECT_NAME} CXX) file(GLOB_RECURSE sources CONFIGURE_DEPENDS - ${CMAKE_SOURCE_DIR}/src/*.cpp + ${CMAKE_SOURCE_DIR}/src/*/*.cpp ${CMAKE_SOURCE_DIR}/src/data/models/odb-gen/*.cxx ) -add_executable(${PROJECT_NAME} ${sources}) -target_include_directories(${PROJECT_NAME} PRIVATE -${CMAKE_SOURCE_DIR}/src -/usr/include -src/data/models/ -) +set(PROJECT_OBJECTS ${PROJECT_NAME}_lib) + +add_library(${PROJECT_OBJECTS} OBJECT ${sources}) -target_link_libraries(${PROJECT_NAME} PRIVATE odb odb-pgsql) +target_include_directories(${PROJECT_OBJECTS} PUBLIC + ${CMAKE_SOURCE_DIR}/src + /usr/include + src/data/models/ +) find_package(Drogon REQUIRED) -target_link_libraries(${PROJECT_NAME} PRIVATE Drogon::Drogon) find_package(jsoncpp REQUIRED) -target_link_libraries(${PROJECT_NAME} PRIVATE JsonCpp::JsonCpp) \ No newline at end of file + +target_link_libraries(${PROJECT_OBJECTS} PUBLIC + Drogon::Drogon + JsonCpp::JsonCpp + odb odb-pgsql +) + +add_executable(${PROJECT_NAME} + ${CMAKE_SOURCE_DIR}/src/main.cpp + $ +) + +target_link_libraries(${PROJECT_NAME} PUBLIC + ${PROJECT_OBJECTS} +) + +add_subdirectory(test) diff --git a/README.md b/README.md index d88f327..5a4f0e4 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,17 @@ To run clang-tidy, run the following command: find ./src -name "*.cpp" -not -path "*/build/*" -exec echo "Checking {}..." \; -exec clang-tidy --config-file=.clang-tidy {} -- -I./include -std=c++20 \; ``` +## Tests run + +The project presents an example of test implementation using GTest tools. Test files are located in the `test` directory at the root of the project. Inside the `test` directory there is a `CMakeLists.txt` file created specifically for building a separate executable file for tests. + +### How to run tests + +To run tests, go to the `build/Debug` directory, and then run the `ctest` command in the terminal (`Ctrl + Shift + ~`). + +Alternatively, use the CMake Tools Extension in VS Code. To do this, open the CMake Tools Extension and click `Test`, after configuring and building the project. +

cmakeTests

+ ## Working with ORM+Migration. ### ORM: diff --git a/conanfile.py b/conanfile.py index 99bfcea..0307e98 100644 --- a/conanfile.py +++ b/conanfile.py @@ -44,3 +44,4 @@ def package(self): def requirements(self): self.requires("drogon/1.9.10") + self.requires("gtest/1.14.0") diff --git a/docs/images/cmakeTests.png b/docs/images/cmakeTests.png new file mode 100644 index 0000000..1b9a10c Binary files /dev/null and b/docs/images/cmakeTests.png differ diff --git a/docs/technologies.md b/docs/technologies.md index 3268860..faabec3 100644 --- a/docs/technologies.md +++ b/docs/technologies.md @@ -113,7 +113,6 @@ At the time of research, the following versions were used: | Availability of a suitable ORM | - | (not studied due to exclusion) | - | | Presence of migrations | - | (not studied due to exclusion) | - | - #### Why Drogon - Chosen solution **Advantages:** @@ -190,5 +189,4 @@ Final decision: manage migrations with SQLAlchemy/Alembic and use ODB for C++ da - Implement and maintain C++ data access with ODB on PostgreSQL. - Coordination workflow: apply Alembic migrations first, then synchronize ODB mappings/models and the C++ data access code to reflect schema updates. - ## E2E Tests \ No newline at end of file diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..f8c1bfd --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,22 @@ +enable_testing() + +include(GoogleTest) + +set(TEST_PROJECT ${PROJECT_NAME}_unit_tests) + +file(GLOB_RECURSE test_sources CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp) + +add_executable(${TEST_PROJECT} ${test_sources}) + +find_package(GTest REQUIRED) + +target_link_libraries(${TEST_PROJECT} PRIVATE + ${PROJECT_OBJECTS} + GTest::gtest +) + +target_include_directories(${TEST_PROJECT} PRIVATE + ${CMAKE_SOURCE_DIR}/src +) + +gtest_discover_tests(${TEST_PROJECT}) \ No newline at end of file diff --git a/test/addition-operation.cpp b/test/addition-operation.cpp new file mode 100644 index 0000000..50489ed --- /dev/null +++ b/test/addition-operation.cpp @@ -0,0 +1,6 @@ +#include "addition-operation.h" + +int AdditionOperation(int a, int b) +{ + return a + b; +} \ No newline at end of file diff --git a/test/addition-operation.h b/test/addition-operation.h new file mode 100644 index 0000000..844dd78 --- /dev/null +++ b/test/addition-operation.h @@ -0,0 +1,3 @@ +#pragma once + +int AdditionOperation(int a, int b); \ No newline at end of file diff --git a/test/test_main.cpp b/test/test_main.cpp new file mode 100644 index 0000000..341c9ed --- /dev/null +++ b/test/test_main.cpp @@ -0,0 +1,19 @@ +#include "addition-operation.h" +#include "gtest/gtest.h" + +// Testing reference section in GTest documentation +// https://google.github.io/googletest/reference/testing.html +TEST(MathTest, AdditionOperation) +{ + // Assertions section in GTest documentation + // https://google.github.io/googletest/reference/assertions.html#EXPECT_EQ + EXPECT_EQ(AdditionOperation(2, 3), 5); + EXPECT_EQ(AdditionOperation(-1, 1), 0); +} + +int main(int argc, char** argv) +{ + ::testing::InitGoogleTest(&argc, argv); + + return RUN_ALL_TESTS(); +} \ No newline at end of file