-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
151 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from __future__ import annotations | ||
|
||
import typing as t | ||
|
||
from .comparisons import Condition | ||
|
||
|
||
class Where: | ||
def __init__(self, query): | ||
self.query = query | ||
self._conditions: list[Condition] = [] | ||
|
||
def add_condition(self, condition: Condition): | ||
self._conditions.append(condition) | ||
|
||
def clear(self): | ||
self._conditions.clear() | ||
|
||
def __call__(self, *conditions: t.Union[Condition, str]) -> Where: | ||
for c in conditions: | ||
if isinstance(c, str): | ||
c = Condition(c) | ||
self.add_condition(c) | ||
return self | ||
|
||
@property | ||
def sql(self): | ||
return " AND ".join(str(c) for c in self._conditions) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
"""Testing of Where statement functionality.""" | ||
|
||
import pytest | ||
|
||
import everstone | ||
from everstone.sql import constraints, types | ||
|
||
everstone.db.disable_execution() | ||
|
||
|
||
@pytest.fixture | ||
def example_table(): | ||
t = everstone.db.Table("sample_table") | ||
t.Column("col_a", types.Text, constraints.PrimaryKey) | ||
t.Column("col_b", types.Integer) | ||
return t | ||
|
||
|
||
def test_where(example_table): | ||
s = example_table.select("col_a") | ||
a = example_table.columns.col_a | ||
b = example_table.columns.col_b | ||
s.where(example_table.columns.col_a == example_table.columns.col_b) | ||
assert s.where.sql == "public.sample_table.col_a = public.sample_table.col_b" | ||
s.where.clear() | ||
assert s.where.sql == "" | ||
s.where(a=="john", b >= 100) | ||
assert s.where.sql == "public.sample_table.col_a = 'john' AND public.sample_table.col_b >= 100" | ||
s.where.clear() | ||
s.where(a.is_(True) | (a == 100) & b.ilike("dan")) | ||
assert s.where.sql == ( | ||
"(public.sample_table.col_a IS TRUE" | ||
" OR (public.sample_table.col_a = 100" | ||
" AND public.sample_table.col_b ILIKE 'dan'))" | ||
) | ||
s.where.clear() | ||
s.where("string_example IS NOT NULL") | ||
assert s.where.sql == "string_example IS NOT NULL" |