Database System 0.1.0
Advanced C++20 Database System with Multi-Backend Support
Loading...
Searching...
No Matches
mongodb_backend_test.cpp
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
16#include <gtest/gtest.h>
17#include <memory>
18#include <string>
19
22
23using namespace database;
24using namespace database::backends;
25using namespace database::core;
26
27// =============================================================================
28// Test Fixture
29// =============================================================================
30
31class MongoDBBackendTest : public ::testing::Test {
32protected:
33 std::unique_ptr<mongodb_backend> backend_;
35
36 void SetUp() override
37 {
38 backend_ = std::make_unique<mongodb_backend>();
39 test_config_.host = "localhost";
40 test_config_.port = 27017;
41 test_config_.database = "test_db";
44 }
45
46 void TearDown() override
47 {
48 if (backend_ && backend_->is_initialized()) {
49 backend_->shutdown();
50 }
51 }
52};
53
54// =============================================================================
55// Type Identification Tests
56// =============================================================================
57
58TEST_F(MongoDBBackendTest, TypeReturnsMongoDB)
59{
60 EXPECT_EQ(backend_->type(), database_types::mongodb);
61}
62
63TEST_F(MongoDBBackendTest, BackendNameIsCorrect)
64{
65 EXPECT_STREQ(mongodb_backend::backend_name(), "mongodb_backend");
66}
67
68// =============================================================================
69// Initial State Tests
70// =============================================================================
71
72TEST_F(MongoDBBackendTest, InitiallyNotInitialized)
73{
74 EXPECT_FALSE(backend_->is_initialized());
75}
76
77TEST_F(MongoDBBackendTest, InitiallyNotInTransaction)
78{
79 EXPECT_FALSE(backend_->in_transaction());
80}
81
82// =============================================================================
83// Operations Without Initialization Tests
84// =============================================================================
85
86TEST_F(MongoDBBackendTest, ExecuteQueryFailsWithoutInit)
87{
88 auto result = backend_->execute_query("users:{\"name\":\"John\"}");
89 EXPECT_FALSE(result.is_ok());
90}
91
92TEST_F(MongoDBBackendTest, SelectQueryFailsWithoutInit)
93{
94 auto result = backend_->select_query("users:{\"name\":\"John\"}");
95 EXPECT_FALSE(result.is_ok());
96}
97
98TEST_F(MongoDBBackendTest, BeginTransactionFailsWithoutInit)
99{
100 auto result = backend_->begin_transaction();
101 EXPECT_FALSE(result.is_ok());
102}
103
104TEST_F(MongoDBBackendTest, CommitTransactionFailsWithoutInit)
105{
106 auto result = backend_->commit_transaction();
107 EXPECT_FALSE(result.is_ok());
108}
109
110TEST_F(MongoDBBackendTest, RollbackTransactionFailsWithoutInit)
111{
112 auto result = backend_->rollback_transaction();
113 EXPECT_FALSE(result.is_ok());
114}
115
116// =============================================================================
117// Lifecycle Guard Tests (via backend_base)
118// =============================================================================
119
120TEST_F(MongoDBBackendTest, ShutdownWithoutInitIsNoOp)
121{
122 auto result = backend_->shutdown();
123 EXPECT_TRUE(result.is_ok());
124}
125
126// =============================================================================
127// Factory Method Tests
128// =============================================================================
129
130TEST_F(MongoDBBackendTest, CreateReturnsValidBackend)
131{
132 auto backend = mongodb_backend::create();
133 ASSERT_NE(backend, nullptr);
134 EXPECT_EQ(backend->type(), database_types::mongodb);
135 EXPECT_FALSE(backend->is_initialized());
136}
137
138// =============================================================================
139// Connection Info Tests
140// =============================================================================
141
142TEST_F(MongoDBBackendTest, ConnectionInfoBeforeInit)
143{
144 auto info = backend_->connection_info();
145 SUCCEED();
146}
147
148TEST_F(MongoDBBackendTest, LastErrorBeforeInit)
149{
150 auto error = backend_->last_error();
151 SUCCEED();
152}
153
154// =============================================================================
155// Real Connection Tests (conditional on USE_MONGODB)
156// =============================================================================
157
158#ifdef USE_MONGODB
159
160TEST_F(MongoDBBackendTest, ConnectToLocalMongoDB)
161{
162 connection_config config;
163 config.host = "localhost";
164 config.port = 27017;
165 config.database = "test";
166
167 auto result = backend_->initialize(config);
168 if (!result.is_ok()) {
169 GTEST_SKIP() << "Local MongoDB not available: " << backend_->last_error();
170 }
171
172 EXPECT_TRUE(backend_->is_initialized());
173 auto info = backend_->connection_info();
174 EXPECT_FALSE(info.empty());
175}
176
177TEST_F(MongoDBBackendTest, CRUDOperationsOnMongoDB)
178{
179 connection_config config;
180 config.host = "localhost";
181 config.port = 27017;
182 config.database = "test";
183
184 if (!backend_->initialize(config).is_ok()) {
185 GTEST_SKIP() << "Local MongoDB not available";
186 }
187
188 // Insert document
189 auto insert_result = backend_->execute_query(
190 "mongo_test:{\"name\":\"test_item\",\"value\":42}");
191 EXPECT_TRUE(insert_result.is_ok());
192
193 // Select document
194 auto select_result = backend_->select_query(
195 "mongo_test:{\"name\":\"test_item\"}");
196 ASSERT_TRUE(select_result.is_ok());
197 EXPECT_GE(select_result.value().size(), 1u);
198
199 // Update document
200 auto update_result = backend_->execute_query(
201 "mongo_test:{\"name\":\"test_item\"}:{\"$set\":{\"value\":99}}");
202 EXPECT_TRUE(update_result.is_ok());
203
204 // Delete document
205 auto delete_result = backend_->execute_query(
206 "mongo_test:{\"name\":\"test_item\"}");
207 EXPECT_TRUE(delete_result.is_ok());
208}
209
210#endif // USE_MONGODB
211
212int main(int argc, char** argv)
213{
214 ::testing::InitGoogleTest(&argc, argv);
215 return RUN_ALL_TESTS();
216}
connection_config test_config_
std::unique_ptr< mongodb_backend > backend_
static constexpr const char * backend_name()
Backend name for error messages.
Abstract interface for database backends.
MongoDB database backend plugin implementation.
TEST_F(MongoDBBackendTest, TypeReturnsMongoDB)
@ info
Informational messages (default)
Configuration for database connection.
#define ASSERT_TRUE(condition, message)