Skip to content
Snippets Groups Projects
Commit 31cfd500 authored by Riccardo Maria Bianchi's avatar Riccardo Maria Bianchi :sunny:
Browse files

Add GoogleTest support and an example GTest test

parent d5d6b194
No related branches found
No related tags found
1 merge request!416Add GoogleTest support and an example GTest test
Pipeline #11693541 failed
# 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:
......
......@@ -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 && \
......
# 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 )
......
# 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)
......@@ -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.
......
// 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");
// }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment