tangled
alpha
login
or
join now
bernsteinbear.com
/
db.py
0
fork
atom
this repo has no description
0
fork
atom
overview
issues
pulls
pipelines
.
bernsteinbear.com
2 years ago
46a06ce4
536ec8d3
+35
2 changed files
expand all
collapse all
unified
split
db.py
db_tests.py
+2
db.py
···
94
94
def INNER_JOIN(self, a, b, pred):
95
95
return self.CROSS_JOIN(a, b).filter(pred)
96
96
97
97
+
JOIN = INNER_JOIN
98
98
+
97
99
def LEFT_JOIN(self, a, b, pred):
98
100
rows = []
99
101
empty_b_row = {f"{b.name}.{k}": None for k in b.colnames()}
+33
db_tests.py
···
170
170
),
171
171
)
172
172
173
173
+
def test_inner_join_returns_matching_cross_product(self):
174
174
+
user = Table("user", [{"id": 1, "name": "Alice"}])
175
175
+
post = Table(
176
176
+
"post",
177
177
+
[
178
178
+
{"id": 1, "user_id": 1, "title": "Hello"},
179
179
+
{"id": 2, "user_id": 2, "title": "Hello world"},
180
180
+
{"id": 3, "user_id": 1, "title": "Goodbye world"},
181
181
+
{"id": 2, "user_id": 3, "title": "Hello world again"},
182
182
+
],
183
183
+
)
184
184
+
db = Database()
185
185
+
result = db.JOIN(user, post, lambda row: row["user.id"] == row["post.user_id"])
186
186
+
self.assertEqual(
187
187
+
result.rows,
188
188
+
(
189
189
+
{
190
190
+
"post.id": 1,
191
191
+
"post.title": "Hello",
192
192
+
"post.user_id": 1,
193
193
+
"user.id": 1,
194
194
+
"user.name": "Alice",
195
195
+
},
196
196
+
{
197
197
+
"post.id": 3,
198
198
+
"post.title": "Goodbye world",
199
199
+
"post.user_id": 1,
200
200
+
"user.id": 1,
201
201
+
"user.name": "Alice",
202
202
+
},
203
203
+
),
204
204
+
)
205
205
+
173
206
174
207
if __name__ == "__main__":
175
208
unittest.main()