diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 3374def33af870a1e2a60e58b875183130c1984c..f96afb305c1f84eb94055bb3bf7f5b8b7922f1c1 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,3 +1,5 @@ +# Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration + # List of the CI build stages. stages: - step-A @@ -25,7 +27,7 @@ workflow: # TEMPLATES FOR BUILDING ON DIFFERENT PLATFORMS -# general macOS job template +# Base template for all macOS jobs .macos-template-job: &macos-job tags: - macos @@ -41,10 +43,7 @@ workflow: - export CPPFLAGS="-I/usr/local/opt/expat/include" - export PATH="/usr/local/opt/qt5/bin:$PATH" # to make Qt5 discoverable by CMake -# NOTE -# The Ubuntu package 'nlohmann-json-dev' is version 2.x, too old for us. -# Thus, for GeoModelVisualization (GMEX), we now build nlohmann_json as part -# of the build, by using the 'standalone' job only +# Base template for all Ubuntu jobs .ubuntu-template-job-default: &ubuntu-job image: gitlab-registry.cern.ch/geomodeldev/ubuntu-geant4-image:main-base rules: diff --git a/CI/DockerfileFull b/CI/DockerfileFull index 79cb16a899b034e392d829f6e9a38e3d2c05fc04..bf50b397d014269c6b899fa75a27143a703c6d5d 100755 --- a/CI/DockerfileFull +++ b/CI/DockerfileFull @@ -2,8 +2,12 @@ ARG BASEIMAGE=gitlab-registry.cern.ch/geomodeldev/ubuntu-geant4-image:main-base FROM ${BASEIMAGE} +# Install GoogleTest +# FIXME: we want to move this to the main image later +RUN apt-get update && apt-get install -y libgtest-dev + ### Compile then geo model -COPY . /workdir/GeoModel_src/ +COPY . /workdir/GeoModel_src/ RUN mkdir -p /workdir/build_geomodel/ && \ cd /workdir/build_geomodel/ && \ . ~/.bashrc && \ diff --git a/CMakeLists.txt b/CMakeLists.txt index e747085ca71b79d5a625767de7e31a2eccd5dc6b..11529c9b3f55021f8192adc7290fed422b8e5940 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -# Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration +# Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration # === Preamble === cmake_minimum_required(VERSION 3.16...3.26) @@ -34,6 +34,9 @@ endif() include( SetupEigen3 ) include( SetupXercesC ) include( SetupJSON ) +# Add the GoogleTest framework, for tests +find_package(GTest REQUIRED) +include_directories(${GTEST_INCLUDE_DIRS}) # Find the dependencies that the project always picks up from its environment. find_package( SQLite3 3.7.17 ) diff --git a/GeoModelIO/GeoModelDBManager/CMakeLists.txt b/GeoModelIO/GeoModelDBManager/CMakeLists.txt index 513943cf47bcc58dc6f8647fbe8b233c7244f0c6..37dabe13b3c1153fa62724784d4a6e1765236e19 100644 --- a/GeoModelIO/GeoModelDBManager/CMakeLists.txt +++ b/GeoModelIO/GeoModelDBManager/CMakeLists.txt @@ -1,11 +1,12 @@ -# Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration +# Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration ################################################################################ # Package: GeoModelDBManager # author: Riccardo Maria BIANCHI @ CERN - 2017 # major updates: # - R.M.Bianchi, 2018 -# - R.M.Bianvhi, Nov 2020 -# - R.M.Bianvhi, Apr 2024 +# - R.M.Bianchi, Nov 2020 +# - R.M.Bianchi, Apr 2024 -- Add CTest tests +# - R.M.Bianchi, Feb 2025 -- Add GoogleTest support ################################################################################ # Find the header and source files. @@ -39,7 +40,32 @@ install( FILES ${HEADERS} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/GeoModelDBManager COMPONENT Development ) + +### --- Add Tests --- + +#++ Create a 'simple' unit test add_executable(test_create_db_file tests/test_create_db_file.cpp) target_link_libraries( test_create_db_file GeoModelIO::GeoModelDBManager) add_test(NAME testCreateDBFile COMMAND test_create_db_file) + +#++ Create a GoogleTest test, +# and let CTest run all the sub-tests defined in it separately. +# NOTE: You can still use the standard CMake `add_test` function +# to declare the test; however, in that case, CTest will +# run it as a whole and you will only get one line of output +# for all the tests in it togetehr. +# While with the GTest method here below, you will get +# one line of output for each actual test defined in the +# 'container' test program. +# Therefore, for GTest tests it is advisable to use the +# approach below. +# +# Create test executable +add_executable(test_db_functions tests/test_db_functions.cpp) +target_link_libraries(test_db_functions GeoModelDBManager GTest::GTest GTest::Main) +# Register the test and let CTest discover +# all tests contained in it and run them separately +include(GoogleTest) +gtest_discover_tests(test_db_functions) + diff --git a/GeoModelIO/GeoModelDBManager/tests/test_create_db_file.cpp b/GeoModelIO/GeoModelDBManager/tests/test_create_db_file.cpp index 970e18de7a8f9a009549d12bfb04cd3b692517be..6fb9c5fa5388f0550e7d03dfb894181b164094e3 100644 --- a/GeoModelIO/GeoModelDBManager/tests/test_create_db_file.cpp +++ b/GeoModelIO/GeoModelDBManager/tests/test_create_db_file.cpp @@ -22,7 +22,7 @@ int main(int argc, char *argv[]) //------------------------------------------------------------------------------------// // Open a geometry file //------------------------------------------------------------------------------------// - std::string path = "geometry.db"; + std::string path = "test_geometry.db"; // check if DB file exists. If not, return. // FIXME: TODO: this check should go in the 'GMDBManager' constructor. diff --git a/GeoModelIO/GeoModelDBManager/tests/test_db_functions.cpp b/GeoModelIO/GeoModelDBManager/tests/test_db_functions.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6bba7f476203b34e6f0deb7e09630d5ef19fdd04 --- /dev/null +++ b/GeoModelIO/GeoModelDBManager/tests/test_db_functions.cpp @@ -0,0 +1,64 @@ +// GeoModel includes +#include "GeoModelDBManager/GMDBManager.h" + +#include <gtest/gtest.h> +#include <filesystem> + +class DatabaseTest : public ::testing::Test { +protected: + std::string dbFile = "test_database.db"; + std::unique_ptr<GMDBManager> dbManager{}; + + void SetUp() override { + // Remove any existing test database + if (std::filesystem::exists(dbFile)) { + std::filesystem::remove(dbFile); + } + + // open the DB connection + dbManager = std::make_unique<GMDBManager>(dbFile); + + // check the DB connection + if (dbManager->checkIsDBOpen()) + { + std::cout << "OK! Database is open!" << std::endl; + } + else + { + std::cout << "Database ERROR!! Exiting..." << std::endl; + exit(EXIT_FAILURE); + } + } + + void TearDown() override { + if (std::filesystem::exists(dbFile)) { + std::filesystem::remove(dbFile); + } + } +}; + +TEST_F(DatabaseTest, CreateDatabaseTest) { + // EXPECT_TRUE(dbManager->createDatabase()); + EXPECT_TRUE(std::filesystem::exists(dbFile)); +} + +TEST_F(DatabaseTest, PopulateDatabaseTest) { + // ASSERT_TRUE(dbManager->createDatabase()); + EXPECT_TRUE(dbManager->initDB()); +} + +// TEST_F(DatabaseTest, InsertDataTest) { +// ASSERT_TRUE(dbManager->createDatabase()); +// ASSERT_TRUE(dbManager->initDB()); + +// EXPECT_TRUE(dbManager->insertData("key1", "value1")); +// } + +// TEST_F(DatabaseTest, ReadDataTest) { +// ASSERT_TRUE(dbManager->createDatabase()); +// ASSERT_TRUE(dbManager->populateDatabase()); + +// dbManager->insertData("key1", "value1"); +// EXPECT_EQ(dbManager->readData("key1"), "value1"); +// } +