1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
//! Gap 28 — Q-objects (composable predicates).
//!
//! Coverage:
//!
//! - **`Q::or(a, b)`:** SQL `(a OR b)` generated correctly.
//! - **`Q::and(a, b)`:** SQL `(a AND b)` generated correctly.
//! - **`Q::not(p)`:** SQL `NOT p` generated correctly.
//! - **Nested composition:** `Q::or(Q::and(a, b), c)` nests with the right
//! parentheses shape.
//! - **Live SQLite:** each variant executes correctly against a real pool.
use sqlx::SqlitePool;
use umbral::orm::Q;
use umbral_core::db;
// =========================================================================
// Model declarations
// =========================================================================
#[derive(
Debug, Clone, PartialEq, sqlx::FromRow, serde::Serialize, serde::Deserialize, umbral::orm::Model,
)]
#[umbral(table = "q_post")]
pub struct Post {
pub id: i64,
pub title: String,
pub published: bool,
pub author_id: i64,
}
// =========================================================================
// Pool helper
// =========================================================================
async fn fresh_pool() -> SqlitePool {
let pool = db::connect_sqlite("sqlite::memory:")
.await
.expect("in-memory SQLite should always connect");
sqlx::query(
"CREATE TABLE q_post (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
published INTEGER NOT NULL DEFAULT 0,
author_id INTEGER NOT NULL DEFAULT 0
)",
)
.execute(&pool)
.await
.expect("CREATE TABLE q_post");
// Seed: 5 rows covering published/unpublished and two author IDs.
// id=1: published=true, author=1
// id=2: published=false, author=1
// id=3: published=true, author=2
// id=4: published=false, author=2
// id=5: published=true, author=1
for (title, published, author_id) in &[
("pub-a1-1", true, 1i64),
("draft-a1", false, 1),
("pub-a2", true, 2),
("draft-a2", false, 2),
("pub-a1-2", true, 1),
] {
sqlx::query("INSERT INTO q_post (title, published, author_id) VALUES (?, ?, ?)")
.bind(*title)
.bind(*published)
.bind(*author_id)
.execute(&pool)
.await
.expect("insert seed");
}
pool
}
// =========================================================================
// SQL rendering (pure, no pool)
// =========================================================================
/// `Q::or` renders a predicate whose SQL text contains the two column names.
#[test]
fn q_or_renders_with_both_column_names() {
let pred = Q::or(post::PUBLISHED.eq(true), post::AUTHOR_ID.eq(99));
let sql = Post::objects().filter(pred).to_sql();
let lower = sql.to_ascii_lowercase();
assert!(
lower.contains("published"),
"should mention published; got: {sql}"
);
assert!(
lower.contains("author_id"),
"should mention author_id; got: {sql}"
);
}
/// `Q::and` renders a predicate whose SQL text contains the two column names.
#[test]
fn q_and_renders_with_both_column_names() {
let pred = Q::and(post::PUBLISHED.eq(true), post::AUTHOR_ID.eq(1));
let sql = Post::objects().filter(pred).to_sql();
let lower = sql.to_ascii_lowercase();
assert!(lower.contains("published"), "got: {sql}");
assert!(lower.contains("author_id"), "got: {sql}");
}
/// `Q::not` wraps the predicate in a NOT.
#[test]
fn q_not_renders_not_keyword() {
let pred = Q::not(post::AUTHOR_ID.eq(1));
let sql = Post::objects().filter(pred).to_sql();
let lower = sql.to_ascii_lowercase();
assert!(lower.contains("not"), "should contain NOT; got: {sql}");
assert!(
lower.contains("author_id"),
"should mention author_id; got: {sql}"
);
}
// =========================================================================
// Live SQLite: Q::or
// =========================================================================
/// `Q::or(published=true, author_id=2)` returns all published posts PLUS
/// all posts by author 2 (without duplication — SQL OR semantics).
#[tokio::test]
async fn q_or_returns_union() {
let pool = fresh_pool().await;
// published=true: ids 1, 3, 5
// author_id=2: ids 3, 4
// union: ids 1, 3, 4, 5
let rows = Post::objects()
.on(&pool)
.filter(Q::or(post::PUBLISHED.eq(true), post::AUTHOR_ID.eq(2)))
.fetch()
.await
.expect("fetch Q::or");
assert_eq!(
rows.len(),
4,
"should match 4 rows (union of published and author_id=2)"
);
}
// =========================================================================
// Live SQLite: Q::and
// =========================================================================
/// `Q::and(published=true, author_id=1)` returns only rows where both
/// conditions hold.
#[tokio::test]
async fn q_and_returns_intersection() {
let pool = fresh_pool().await;
// published=true AND author_id=1: ids 1, 5
let rows = Post::objects()
.on(&pool)
.filter(Q::and(post::PUBLISHED.eq(true), post::AUTHOR_ID.eq(1)))
.fetch()
.await
.expect("fetch Q::and");
assert_eq!(
rows.len(),
2,
"should match 2 rows (published AND author_id=1)"
);
assert!(rows.iter().all(|r| r.published && r.author_id == 1));
}
// =========================================================================
// Live SQLite: Q::not
// =========================================================================
/// `Q::not(author_id=1)` excludes all posts by author 1.
#[tokio::test]
async fn q_not_excludes_matching_rows() {
let pool = fresh_pool().await;
// author_id != 1: ids 3, 4
let rows = Post::objects()
.on(&pool)
.filter(Q::not(post::AUTHOR_ID.eq(1)))
.fetch()
.await
.expect("fetch Q::not");
assert_eq!(rows.len(), 2, "should match 2 rows (not author_id=1)");
assert!(rows.iter().all(|r| r.author_id != 1));
}
// =========================================================================
// Live SQLite: nested Q composition
// =========================================================================
/// `Q::or(Q::and(published=true, author_id=1), Q::not(published=true))`
/// returns all published rows by author 1 PLUS all unpublished rows.
#[tokio::test]
async fn q_nested_composition() {
let pool = fresh_pool().await;
// (published AND author=1) OR (NOT published)
// = {1, 5} OR {2, 4} = {1, 2, 4, 5}
let rows = Post::objects()
.on(&pool)
.filter(Q::or(
Q::and(post::PUBLISHED.eq(true), post::AUTHOR_ID.eq(1)),
Q::not(post::PUBLISHED.eq(true)),
))
.fetch()
.await
.expect("fetch nested Q");
assert_eq!(rows.len(), 4, "nested Q should return 4 rows");
let mut ids: Vec<i64> = rows.iter().map(|r| r.id).collect();
ids.sort();
assert_eq!(ids, vec![1, 2, 4, 5]);
}
/// Two `.filter()` calls AND together — existing behaviour preserved.
#[tokio::test]
async fn multiple_filter_calls_and_together() {
let pool = fresh_pool().await;
// published AND author=1 via two filter calls
let rows = Post::objects()
.on(&pool)
.filter(post::PUBLISHED.eq(true))
.filter(post::AUTHOR_ID.eq(1))
.fetch()
.await
.expect("chained filters");
assert_eq!(
rows.len(),
2,
"two filter calls should AND (published AND author=1)"
);
}