Database System 0.1.0
Advanced C++20 Database System with Multi-Backend Support
Loading...
Searching...
No Matches
test_immutable_query_builder.cpp File Reference
#include <gtest/gtest.h>
#include <string>
#include "database/query/immutable_query_builder.h"
Include dependency graph for test_immutable_query_builder.cpp:

Go to the source code of this file.

Classes

class  ImmutableQueryBuilderTest
 

Functions

 TEST_F (ImmutableQueryBuilderTest, DefaultBuildSelectsStar)
 
 TEST_F (ImmutableQueryBuilderTest, SelectSpecificColumns)
 
 TEST_F (ImmutableQueryBuilderTest, WhereWithFieldOpValue)
 
 TEST_F (ImmutableQueryBuilderTest, WhereMultipleConditionsJoinedByAnd)
 
 TEST_F (ImmutableQueryBuilderTest, WhereWithQueryCondition)
 
 TEST_F (ImmutableQueryBuilderTest, OrderByAscending)
 
 TEST_F (ImmutableQueryBuilderTest, OrderByDescending)
 
 TEST_F (ImmutableQueryBuilderTest, OrderByMultipleFields)
 
 TEST_F (ImmutableQueryBuilderTest, LimitClause)
 
 TEST_F (ImmutableQueryBuilderTest, OffsetClause)
 
 TEST_F (ImmutableQueryBuilderTest, LimitAndOffset)
 
 TEST_F (ImmutableQueryBuilderTest, InnerJoin)
 
 TEST_F (ImmutableQueryBuilderTest, LeftJoin)
 
 TEST_F (ImmutableQueryBuilderTest, RightJoin)
 
 TEST_F (ImmutableQueryBuilderTest, FullOuterJoin)
 
 TEST_F (ImmutableQueryBuilderTest, CrossJoin)
 
 TEST_F (ImmutableQueryBuilderTest, GroupBy)
 
 TEST_F (ImmutableQueryBuilderTest, GroupByMultipleFields)
 
 TEST_F (ImmutableQueryBuilderTest, Having)
 
 TEST_F (ImmutableQueryBuilderTest, PostgresUsesDoubleQuotes)
 
 TEST_F (ImmutableQueryBuilderTest, SqliteUsesDoubleQuotes)
 
 TEST_F (ImmutableQueryBuilderTest, DefaultBuildUsesPostgresDialect)
 
 TEST_F (ImmutableQueryBuilderTest, OriginalUnchangedAfterSelect)
 
 TEST_F (ImmutableQueryBuilderTest, OriginalUnchangedAfterWhere)
 
 TEST_F (ImmutableQueryBuilderTest, OriginalUnchangedAfterLimit)
 
 TEST_F (ImmutableQueryBuilderTest, ChainingProducesCorrectQuery)
 
 TEST_F (ImmutableQueryBuilderTest, ComplexQueryComposition)
 

Function Documentation

◆ TEST_F() [1/27]

TEST_F ( ImmutableQueryBuilderTest ,
ChainingProducesCorrectQuery  )

Definition at line 219 of file test_immutable_query_builder.cpp.

219 {
220 auto q = builder_
221 .select({"id", "name"})
222 .where("active", "=", true)
223 .order_by("name", sort_order::asc)
224 .limit(10)
225 .offset(0);
226
227 std::string sql = q.build();
228 EXPECT_NE(sql.find("SELECT"), std::string::npos);
229 EXPECT_NE(sql.find("WHERE"), std::string::npos);
230 EXPECT_NE(sql.find("ORDER BY"), std::string::npos);
231 EXPECT_NE(sql.find("LIMIT 10"), std::string::npos);
232 EXPECT_NE(sql.find("OFFSET 0"), std::string::npos);
233}

◆ TEST_F() [2/27]

TEST_F ( ImmutableQueryBuilderTest ,
ComplexQueryComposition  )

Definition at line 237 of file test_immutable_query_builder.cpp.

237 {
238 auto q = builder_
239 .select({"u.id", "u.name", "COUNT(o.id)"})
240 .join("orders", "u.id = o.user_id", join_type::left)
241 .where("u.active", "=", true)
242 .group_by({"u.id", "u.name"})
243 .having("COUNT(o.id) > 0")
244 .order_by("u.name", sort_order::asc)
245 .limit(50);
246
247 std::string sql = q.build();
248 EXPECT_NE(sql.find("SELECT"), std::string::npos);
249 EXPECT_NE(sql.find("LEFT JOIN"), std::string::npos);
250 EXPECT_NE(sql.find("WHERE"), std::string::npos);
251 EXPECT_NE(sql.find("GROUP BY"), std::string::npos);
252 EXPECT_NE(sql.find("HAVING"), std::string::npos);
253 EXPECT_NE(sql.find("ORDER BY"), std::string::npos);
254 EXPECT_NE(sql.find("LIMIT 50"), std::string::npos);
255}

◆ TEST_F() [3/27]

TEST_F ( ImmutableQueryBuilderTest ,
CrossJoin  )

Definition at line 143 of file test_immutable_query_builder.cpp.

143 {
144 auto q = builder_.join("roles", "1=1", join_type::cross);
145 std::string sql = q.build();
146 EXPECT_NE(sql.find("CROSS JOIN"), std::string::npos);
147}

◆ TEST_F() [4/27]

TEST_F ( ImmutableQueryBuilderTest ,
DefaultBuildSelectsStar  )

Definition at line 30 of file test_immutable_query_builder.cpp.

30 {
31 std::string sql = builder_.build();
32 EXPECT_NE(sql.find("SELECT *"), std::string::npos);
33 EXPECT_NE(sql.find("\"users\""), std::string::npos);
34}

References database::immutable_query_builder::build().

Here is the call graph for this function:

◆ TEST_F() [5/27]

TEST_F ( ImmutableQueryBuilderTest ,
DefaultBuildUsesPostgresDialect  )

Definition at line 189 of file test_immutable_query_builder.cpp.

189 {
190 auto q = builder_.select({"id"});
191 std::string default_sql = q.build();
192 std::string pg_sql = q.build_for_database(database_types::postgres);
193 EXPECT_EQ(default_sql, pg_sql);
194}

◆ TEST_F() [6/27]

TEST_F ( ImmutableQueryBuilderTest ,
FullOuterJoin  )

Definition at line 137 of file test_immutable_query_builder.cpp.

137 {
138 auto q = builder_.join("orders", "users.id = orders.user_id", join_type::full_outer);
139 std::string sql = q.build();
140 EXPECT_NE(sql.find("FULL OUTER JOIN"), std::string::npos);
141}

◆ TEST_F() [7/27]

TEST_F ( ImmutableQueryBuilderTest ,
GroupBy  )

Definition at line 151 of file test_immutable_query_builder.cpp.

151 {
152 auto q = builder_.group_by({"department"});
153 std::string sql = q.build();
154 EXPECT_NE(sql.find("GROUP BY"), std::string::npos);
155 EXPECT_NE(sql.find("\"department\""), std::string::npos);
156}

◆ TEST_F() [8/27]

TEST_F ( ImmutableQueryBuilderTest ,
GroupByMultipleFields  )

Definition at line 158 of file test_immutable_query_builder.cpp.

158 {
159 auto q = builder_.group_by({"department", "status"});
160 std::string sql = q.build();
161 EXPECT_NE(sql.find("GROUP BY"), std::string::npos);
162 EXPECT_NE(sql.find("\"department\""), std::string::npos);
163 EXPECT_NE(sql.find("\"status\""), std::string::npos);
164}

◆ TEST_F() [9/27]

TEST_F ( ImmutableQueryBuilderTest ,
Having  )

Definition at line 166 of file test_immutable_query_builder.cpp.

166 {
167 auto q = builder_.group_by({"department"}).having("COUNT(*) > 5");
168 std::string sql = q.build();
169 EXPECT_NE(sql.find("HAVING COUNT(*) > 5"), std::string::npos);
170}

◆ TEST_F() [10/27]

TEST_F ( ImmutableQueryBuilderTest ,
InnerJoin  )

Definition at line 118 of file test_immutable_query_builder.cpp.

118 {
119 auto q = builder_.join("orders", "users.id = orders.user_id", join_type::inner);
120 std::string sql = q.build();
121 EXPECT_NE(sql.find("INNER JOIN"), std::string::npos);
122 EXPECT_NE(sql.find("users.id = orders.user_id"), std::string::npos);
123}

◆ TEST_F() [11/27]

TEST_F ( ImmutableQueryBuilderTest ,
LeftJoin  )

Definition at line 125 of file test_immutable_query_builder.cpp.

125 {
126 auto q = builder_.join("orders", "users.id = orders.user_id", join_type::left);
127 std::string sql = q.build();
128 EXPECT_NE(sql.find("LEFT JOIN"), std::string::npos);
129}

◆ TEST_F() [12/27]

TEST_F ( ImmutableQueryBuilderTest ,
LimitAndOffset  )

Definition at line 109 of file test_immutable_query_builder.cpp.

109 {
110 auto q = builder_.limit(10).offset(20);
111 std::string sql = q.build();
112 EXPECT_NE(sql.find("LIMIT 10"), std::string::npos);
113 EXPECT_NE(sql.find("OFFSET 20"), std::string::npos);
114}

◆ TEST_F() [13/27]

TEST_F ( ImmutableQueryBuilderTest ,
LimitClause  )

Definition at line 97 of file test_immutable_query_builder.cpp.

97 {
98 auto q = builder_.limit(10);
99 std::string sql = q.build();
100 EXPECT_NE(sql.find("LIMIT 10"), std::string::npos);
101}

◆ TEST_F() [14/27]

TEST_F ( ImmutableQueryBuilderTest ,
OffsetClause  )

Definition at line 103 of file test_immutable_query_builder.cpp.

103 {
104 auto q = builder_.offset(20);
105 std::string sql = q.build();
106 EXPECT_NE(sql.find("OFFSET 20"), std::string::npos);
107}

◆ TEST_F() [15/27]

TEST_F ( ImmutableQueryBuilderTest ,
OrderByAscending  )

Definition at line 73 of file test_immutable_query_builder.cpp.

73 {
74 auto q = builder_.order_by("name", sort_order::asc);
75 std::string sql = q.build();
76 EXPECT_NE(sql.find("ORDER BY"), std::string::npos);
77 EXPECT_NE(sql.find("ASC"), std::string::npos);
78}

◆ TEST_F() [16/27]

TEST_F ( ImmutableQueryBuilderTest ,
OrderByDescending  )

Definition at line 80 of file test_immutable_query_builder.cpp.

80 {
81 auto q = builder_.order_by("created_at", sort_order::desc);
82 std::string sql = q.build();
83 EXPECT_NE(sql.find("DESC"), std::string::npos);
84}

◆ TEST_F() [17/27]

TEST_F ( ImmutableQueryBuilderTest ,
OrderByMultipleFields  )

Definition at line 86 of file test_immutable_query_builder.cpp.

86 {
87 auto q = builder_
88 .order_by("name", sort_order::asc)
89 .order_by("created_at", sort_order::desc);
90 std::string sql = q.build();
91 EXPECT_NE(sql.find("ASC"), std::string::npos);
92 EXPECT_NE(sql.find("DESC"), std::string::npos);
93}

◆ TEST_F() [18/27]

TEST_F ( ImmutableQueryBuilderTest ,
OriginalUnchangedAfterLimit  )

Definition at line 213 of file test_immutable_query_builder.cpp.

213 {
214 auto q = builder_.limit(10);
215 std::string original_sql = builder_.build();
216 EXPECT_EQ(original_sql.find("LIMIT"), std::string::npos);
217}

◆ TEST_F() [19/27]

TEST_F ( ImmutableQueryBuilderTest ,
OriginalUnchangedAfterSelect  )

Definition at line 198 of file test_immutable_query_builder.cpp.

198 {
199 auto q = builder_.select({"id"});
200 std::string original_sql = builder_.build();
201 std::string new_sql = q.build();
202 EXPECT_NE(original_sql, new_sql);
203 EXPECT_NE(original_sql.find("*"), std::string::npos);
204 EXPECT_EQ(new_sql.find("*"), std::string::npos);
205}

◆ TEST_F() [20/27]

TEST_F ( ImmutableQueryBuilderTest ,
OriginalUnchangedAfterWhere  )

Definition at line 207 of file test_immutable_query_builder.cpp.

207 {
208 auto q = builder_.where("id", "=", int64_t{1});
209 std::string original_sql = builder_.build();
210 EXPECT_EQ(original_sql.find("WHERE"), std::string::npos);
211}

◆ TEST_F() [21/27]

TEST_F ( ImmutableQueryBuilderTest ,
PostgresUsesDoubleQuotes  )

Definition at line 174 of file test_immutable_query_builder.cpp.

174 {
175 auto q = builder_.select({"id", "name"});
176 std::string sql = q.build_for_database(database_types::postgres);
177 EXPECT_NE(sql.find("\"id\""), std::string::npos);
178 EXPECT_NE(sql.find("\"name\""), std::string::npos);
179 EXPECT_NE(sql.find("\"users\""), std::string::npos);
180}

◆ TEST_F() [22/27]

TEST_F ( ImmutableQueryBuilderTest ,
RightJoin  )

Definition at line 131 of file test_immutable_query_builder.cpp.

131 {
132 auto q = builder_.join("orders", "users.id = orders.user_id", join_type::right);
133 std::string sql = q.build();
134 EXPECT_NE(sql.find("RIGHT JOIN"), std::string::npos);
135}

◆ TEST_F() [23/27]

TEST_F ( ImmutableQueryBuilderTest ,
SelectSpecificColumns  )

Definition at line 38 of file test_immutable_query_builder.cpp.

38 {
39 auto q = builder_.select({"id", "name", "email"});
40 std::string sql = q.build();
41 EXPECT_NE(sql.find("\"id\""), std::string::npos);
42 EXPECT_NE(sql.find("\"name\""), std::string::npos);
43 EXPECT_NE(sql.find("\"email\""), std::string::npos);
44 EXPECT_EQ(sql.find("*"), std::string::npos);
45}

◆ TEST_F() [24/27]

TEST_F ( ImmutableQueryBuilderTest ,
SqliteUsesDoubleQuotes  )

Definition at line 182 of file test_immutable_query_builder.cpp.

182 {
183 auto q = builder_.select({"id"});
184 std::string sql = q.build_for_database(database_types::sqlite);
185 EXPECT_NE(sql.find("\"id\""), std::string::npos);
186 EXPECT_NE(sql.find("\"users\""), std::string::npos);
187}

◆ TEST_F() [25/27]

TEST_F ( ImmutableQueryBuilderTest ,
WhereMultipleConditionsJoinedByAnd  )

Definition at line 55 of file test_immutable_query_builder.cpp.

55 {
56 auto q = builder_
57 .where("active", "=", true)
58 .where("age", ">", int64_t{18});
59 std::string sql = q.build();
60 EXPECT_NE(sql.find("AND"), std::string::npos);
61}

◆ TEST_F() [26/27]

TEST_F ( ImmutableQueryBuilderTest ,
WhereWithFieldOpValue  )

Definition at line 49 of file test_immutable_query_builder.cpp.

49 {
50 auto q = builder_.where("active", "=", true);
51 std::string sql = q.build();
52 EXPECT_NE(sql.find("WHERE"), std::string::npos);
53}

◆ TEST_F() [27/27]

TEST_F ( ImmutableQueryBuilderTest ,
WhereWithQueryCondition  )

Definition at line 63 of file test_immutable_query_builder.cpp.

63 {
64 query_condition cond("status", "=", std::string("active"));
65 auto q = builder_.where(cond);
66 std::string sql = q.build();
67 EXPECT_NE(sql.find("WHERE"), std::string::npos);
68 EXPECT_NE(sql.find("status"), std::string::npos);
69}
Represents a WHERE condition in a query.