Skip to content

Commit

Permalink
Chore/utils unitests (#1400)
Browse files Browse the repository at this point in the history
* fix/mistral-nemo-chat-template

* chore: add unitest for file manager utils and yaml config utils

* chore: add unitest for cuda toolkit utils and semantic version utils
  • Loading branch information
nguyenhoangthuan99 authored Oct 4, 2024
1 parent 0a3c5da commit 9032501
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 1 deletion.
2 changes: 1 addition & 1 deletion engine/test/components/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ find_package(LibArchive REQUIRED)
find_package(CURL REQUIRED)
find_package(SQLiteCpp REQUIRED)

target_link_libraries(${PROJECT_NAME} PRIVATE Drogon::Drogon GTest::gtest GTest::gtest_main yaml-cpp::yaml-cpp
target_link_libraries(${PROJECT_NAME} PRIVATE Drogon::Drogon GTest::gtest GTest::gtest_main yaml-cpp::yaml-cpp
${CMAKE_THREAD_LIBS_INIT})

target_link_libraries(${PROJECT_NAME} PRIVATE httplib::httplib)
Expand Down
43 changes: 43 additions & 0 deletions engine/test/components/test_cuda_toolkit_utils.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <gtest/gtest.h>
#include "utils/cuda_toolkit_utils.h"

// Test fixture for cuda_toolkit_utils
class CudaToolkitUtilsTest : public ::testing::Test {};

// Tests for cuda_toolkit_utils

TEST_F(CudaToolkitUtilsTest, WindowsCompatibleVersions) {
EXPECT_EQ("12.4", cuda_toolkit_utils::GetCompatibleCudaToolkitVersion(
"527.41", "windows", ""));
EXPECT_EQ("11.7", cuda_toolkit_utils::GetCompatibleCudaToolkitVersion(
"452.39", "windows", ""));
}

TEST_F(CudaToolkitUtilsTest, LinuxCompatibleVersions) {
EXPECT_EQ("12.4", cuda_toolkit_utils::GetCompatibleCudaToolkitVersion(
"525.60.13", "linux", ""));
EXPECT_EQ("11.7", cuda_toolkit_utils::GetCompatibleCudaToolkitVersion(
"450.80.02", "linux", ""));
}

TEST_F(CudaToolkitUtilsTest, TensorRTLLMEngine) {
EXPECT_EQ("12.4", cuda_toolkit_utils::GetCompatibleCudaToolkitVersion(
"527.41", "windows", "cortex.tensorrt-llm"));
EXPECT_EQ("12.4", cuda_toolkit_utils::GetCompatibleCudaToolkitVersion(
"525.60.13", "linux", "cortex.tensorrt-llm"));
}

TEST_F(CudaToolkitUtilsTest, UnsupportedDriverVersion) {
EXPECT_THROW(cuda_toolkit_utils::GetCompatibleCudaToolkitVersion(
"450.00", "windows", ""),
std::runtime_error);
EXPECT_THROW(cuda_toolkit_utils::GetCompatibleCudaToolkitVersion("450.00",
"linux", ""),
std::runtime_error);
}

TEST_F(CudaToolkitUtilsTest, UnsupportedOS) {
EXPECT_THROW(cuda_toolkit_utils::GetCompatibleCudaToolkitVersion("527.41",
"macos", ""),
std::runtime_error);
}
94 changes: 94 additions & 0 deletions engine/test/components/test_file_manager_config_yaml_utils.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <filesystem>
#include "utils/config_yaml_utils.h"
#include "utils/file_manager_utils.h"

// Mock for filesystem operations

// Test fixture
class FileManagerConfigTest : public ::testing::Test {};

// Tests for file_manager_utils

TEST_F(FileManagerConfigTest, GetExecutableFolderContainerPath) {
auto path = file_manager_utils::GetExecutableFolderContainerPath();
EXPECT_FALSE(path.empty());
EXPECT_TRUE(std::filesystem::is_directory(path));
}

TEST_F(FileManagerConfigTest, GetHomeDirectoryPath) {
auto path = file_manager_utils::GetHomeDirectoryPath();
EXPECT_FALSE(path.empty());
EXPECT_TRUE(std::filesystem::is_directory(path));
}

TEST_F(FileManagerConfigTest, GetConfigurationPath) {
auto path = file_manager_utils::GetConfigurationPath();
EXPECT_FALSE(path.empty());
EXPECT_TRUE(path.has_filename());
}

TEST_F(FileManagerConfigTest, GetDefaultDataFolderName) {
auto folder_name = file_manager_utils::GetDefaultDataFolderName();
EXPECT_FALSE(folder_name.empty());
EXPECT_TRUE(folder_name.find("cortexcpp") != std::string::npos);
}

TEST_F(FileManagerConfigTest, CreateConfigFileIfNotExist) {

file_manager_utils::CreateConfigFileIfNotExist();
EXPECT_TRUE(
std::filesystem::exists(file_manager_utils::GetConfigurationPath()));
std::filesystem::remove(file_manager_utils::GetConfigurationPath());
}

TEST_F(FileManagerConfigTest, GetCortexConfig) {
file_manager_utils::CreateConfigFileIfNotExist();
auto config = file_manager_utils::GetCortexConfig();
EXPECT_FALSE(config.dataFolderPath.empty());
EXPECT_FALSE(config.logFolderPath.empty());
EXPECT_GT(config.maxLogLines, 0);
}

// Tests for config_yaml_utils

TEST_F(FileManagerConfigTest, DumpYamlConfig) {
config_yaml_utils::CortexConfig config{.logFolderPath = "/path/to/logs",
.dataFolderPath = "/path/to/data",
.maxLogLines = 1000,
.apiServerHost = "localhost",
.apiServerPort = "8080"};

std::string test_file = "test_config.yaml";
config_yaml_utils::DumpYamlConfig(config, test_file);

EXPECT_TRUE(std::filesystem::exists(test_file));

// Clean up
std::filesystem::remove(test_file);
}

TEST_F(FileManagerConfigTest, FromYaml) {
// Create a test YAML file
std::string test_file = "test_config.yaml";
std::ofstream out_file(test_file);
out_file << "logFolderPath: /path/to/logs\n"
<< "dataFolderPath: /path/to/data\n"
<< "maxLogLines: 1000\n"
<< "apiServerHost: localhost\n"
<< "apiServerPort: '8080'\n";
out_file.close();

config_yaml_utils::CortexConfig default_config{};
auto config = config_yaml_utils::FromYaml(test_file, default_config);

EXPECT_EQ(config.logFolderPath, "/path/to/logs");
EXPECT_EQ(config.dataFolderPath, "/path/to/data");
EXPECT_EQ(config.maxLogLines, 1000);
EXPECT_EQ(config.apiServerHost, "localhost");
EXPECT_EQ(config.apiServerPort, "8080");

// Clean up
std::filesystem::remove(test_file);
}
43 changes: 43 additions & 0 deletions engine/test/components/test_semantic_version.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <gtest/gtest.h>
#include "utils/semantic_version_utils.h"

class SemanticVersionUtilsTest : public ::testing::Test {
protected:
void SetUp() override {
// Setup code if needed
}

void TearDown() override {
// Teardown code if needed
}
};

// Tests for semantic_version_utils

TEST_F(SemanticVersionUtilsTest, SplitVersion) {
auto version = semantic_version_utils::SplitVersion("1.2.3");
EXPECT_EQ(1, version.major);
EXPECT_EQ(2, version.minor);
EXPECT_EQ(3, version.patch);
}

TEST_F(SemanticVersionUtilsTest, SplitVersionPartial) {
auto version = semantic_version_utils::SplitVersion("1.2");
EXPECT_EQ(1, version.major);
EXPECT_EQ(2, version.minor);
EXPECT_EQ(0, version.patch);
}

TEST_F(SemanticVersionUtilsTest, SplitVersionEmpty) {
auto version = semantic_version_utils::SplitVersion("");
EXPECT_EQ(0, version.major);
EXPECT_EQ(0, version.minor);
EXPECT_EQ(0, version.patch);
}

TEST_F(SemanticVersionUtilsTest, CompareSemanticVersion) {
EXPECT_EQ(0, semantic_version_utils::CompareSemanticVersion("1.2.3", "1.2.3"));
EXPECT_EQ(-1, semantic_version_utils::CompareSemanticVersion("1.2.3", "1.2.4"));
EXPECT_EQ(1, semantic_version_utils::CompareSemanticVersion("1.3.0", "1.2.9"));
EXPECT_EQ(-1, semantic_version_utils::CompareSemanticVersion("1.9.9", "2.0.0"));
}

0 comments on commit 9032501

Please sign in to comment.