Skip to content

Commit a75b7ff

Browse files
committed
feat: added test for main functionality
1 parent 709db8f commit a75b7ff

File tree

4 files changed

+85
-2
lines changed

4 files changed

+85
-2
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
66
set(CMAKE_CXX_EXTENSIONS OFF)
77

88
option(PERPLEXITY_BUILD_EXAMPLES "Build example programs" ON)
9-
option(PERPLEXITY_BUILD_TESTS "Build tests" OFF)
9+
option(PERPLEXITY_BUILD_TESTS "Build tests" ON)
1010

1111
find_package(CURL CONFIG REQUIRED)
1212
find_package(nlohmann_json CONFIG REQUIRED)

tests/CMakeLists.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
enable_testing()
3+
find_package(GTest CONFIG REQUIRED)
4+
5+
set(TESTS
6+
perplexity_tests.cpp
7+
)
8+
9+
foreach(TEST_FILE ${TESTS})
10+
get_filename_component(TEST_NAME ${TEST_FILE} NAME_WE)
11+
add_executable(${TEST_NAME} ${TEST_FILE})
12+
target_link_libraries(${TEST_NAME} PRIVATE GTest::gtest_main perplexity)
13+
target_compile_features(${TEST_NAME} PRIVATE cxx_std_17)
14+
target_include_directories(${TEST_NAME} PRIVATE
15+
${PROJECT_SOURCE_DIR}/include
16+
)
17+
include(GoogleTest)
18+
gtest_discover_tests(${TEST_NAME})
19+
endforeach()

tests/perplexity_tests.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include <gtest/gtest.h>
2+
#include <perplexity.h>
3+
4+
using namespace perplexity;
5+
6+
// Test: Basic Configuration error
7+
TEST(PerplexityExceptions, ConfigurationError) {
8+
EXPECT_THROW(Config(""), ConfigurationError);
9+
Config cfg("api-key");
10+
EXPECT_NO_THROW(cfg.timeout(std::chrono::seconds(10)));
11+
}
12+
13+
// Test: Checking the Message builder
14+
TEST(PerplexityModels, MessageBuilder) {
15+
Message msg = Message::user("Testuser");
16+
ASSERT_EQ(msg.role, MessageRole::User);
17+
ASSERT_EQ(msg.content, "Testuser");
18+
// Checking serialization
19+
auto j = msg.to_json();
20+
EXPECT_EQ(j["role"], "user");
21+
EXPECT_EQ(j["content"], "Testuser");
22+
}
23+
24+
// Test: Request validation
25+
TEST(PerplexityModels, ChatRequestValidation) {
26+
ChatRequest req;
27+
EXPECT_THROW(req.validate(), ValidationError);
28+
req.model("sonar-pro").add_message(Message::user("Hi!"));
29+
EXPECT_NO_THROW(req.validate());
30+
auto j = req.to_json();
31+
EXPECT_EQ(j["model"], "sonar-pro");
32+
}
33+
34+
// Test: RateLimiter
35+
TEST(PerplexityUtils, RateLimiterBasic) {
36+
RateLimiter limiter(5, true);
37+
for (int i = 0; i < 5; ++i) {
38+
EXPECT_TRUE(limiter.can_make_request());
39+
limiter.wait_if_needed();
40+
}
41+
EXPECT_FALSE(limiter.can_make_request());
42+
limiter.reset();
43+
EXPECT_TRUE(limiter.can_make_request());
44+
}
45+
46+
// Test: Client Exclusions
47+
TEST(PerplexityExceptions, AuthAndNetwork) {
48+
EXPECT_THROW(AuthenticationError("fail"), AuthenticationError);
49+
EXPECT_THROW(NetworkError("fail", 400), NetworkError);
50+
EXPECT_THROW(RateLimitError("fail", 5), RateLimitError);
51+
}
52+
53+
TEST(PerplexityIntegration, RealRequest) {
54+
const char* key = std::getenv("PERPLEXITY_API_KEY");
55+
if (!key) GTEST_SKIP() << "No API key in env";
56+
Client client(key);
57+
auto req = ChatRequest("sonar-pro")
58+
.add_message(Message::user("Ping!"))
59+
.max_tokens(10);
60+
ChatResponse resp = client.chat(req);
61+
EXPECT_TRUE(!resp.content.empty());
62+
}
63+

vcpkg.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"builtin-baseline": "a345bbdc68cdfda65603e24413b21afb28f110fb",
55
"dependencies": [
66
"curl",
7-
"nlohmann-json"
7+
"nlohmann-json",
8+
"gtest"
89
]
910
}

0 commit comments

Comments
 (0)