Skip to content

Commit 25a3be3

Browse files
committed
added greater than
1 parent f780d23 commit 25a3be3

File tree

8 files changed

+43
-26
lines changed

8 files changed

+43
-26
lines changed

aragorm/fields.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import typing
22
import uuid
33

4-
from .operators import Operator, In, NotIn, Equal, NotEqual, Like
4+
from .operators import Operator, In, NotIn, Equal, NotEqual, Like, GreaterThan
55

66

77
Iterable = typing.Iterable[typing.Any]
@@ -27,18 +27,13 @@ def like(self, value: str) -> 'Where':
2727
raise ValueError('% is required for like operators')
2828
return Where(field=self, value=value, operator=Like)
2929

30-
def __eq__(self, value) -> 'Where':
31-
"""
32-
The challenge here is ... need the name of the field ...
30+
def __gt__(self, value) -> 'Where':
31+
return Where(field=self, value=value, operator=GreaterThan)
3332

34-
I'll have some way on the model to match fields instances to names ...
35-
will be fine.
36-
"""
33+
def __eq__(self, value) -> 'Where':
3734
return Where(field=self, value=value, operator=Equal)
3835

3936
def __ne__(self, value) -> 'Where':
40-
# Can either return it ... or just store the where clauses on the
41-
# field ...
4237
return Where(field=self, value=value, operator=NotEqual)
4338

4439

@@ -50,7 +45,7 @@ def __init__(self, length: int = 255, default: str = None, **kwargs):
5045

5146

5247
class Integer(Field):
53-
def __init__(self, default: int = None):
48+
def __init__(self, default: int = None, **kwargs):
5449
self.default = default
5550
super().__init__(**kwargs)
5651

aragorm/model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def select(cls, *field_names: [str]) -> Query:
6565
type='SELECT',
6666
model=cls,
6767
base=f'SELECT {fields_str} from {cls.tablename}'
68-
)
68+
)
6969

7070
@classmethod
7171
async def insert(cls, *row: ["Model"]):

aragorm/operators.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,8 @@ class NotIn(Operator):
2020

2121
class Like(Operator):
2222
template = "{name} LIKE '{value}'"
23+
24+
25+
class GreaterThan(Operator):
26+
# Add permitted types???
27+
template = "{name} > {value}"

aragorm/query.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,16 @@ class Query(object):
2626

2727
valid_types = ['INSERT', 'UPDATE', 'SELECT']
2828

29-
where_clauses: [Where] = []
30-
3129
def __init__(self, type: str, model: 'Model', base: str):
3230
"""
3331
A query has to be a certain type.
3432
"""
3533
# For example select * from my_table
3634
self.base = base
3735
self.model = model
36+
self.where_clauses: [Where] = []
3837

3938
def generate_query(self):
40-
"""
41-
For each where clause ... need to get the name ...
42-
43-
The query needs a link to the table calling it ...
44-
"""
4539
query = self.base
4640
if self.where_clauses:
4741
query += ' WHERE '
@@ -51,20 +45,24 @@ def generate_query(self):
5145
print(query)
5246
return query
5347

54-
async def execute(self, as_dict=True) -> str:
48+
def first(self):
49+
"""
50+
LIMIT 1 ... and only return the first row ...
51+
52+
I want to think about using .limit() too ...
53+
"""
54+
55+
async def execute(self, as_dict=True):
5556
"""
5657
Now ... just execute it from within here for now ...
5758
"""
5859
conn = await asyncpg.connect(**TEST_CREDENTIALS)
5960
results = await conn.fetch(self.generate_query())
6061
await conn.close()
6162
# TODO Be able to output it in different formats.
62-
return dict(results[0].items())
63+
return [dict(i.items()) for i in results]
6364

6465
def where(self, where: Where):
65-
"""
66-
Just appends where clauses ...
67-
"""
6866
self.where_clauses.append(where)
6967
return self
7068

tests/__init__.py

Whitespace-only changes.

tests/example_project/__init__.py

Whitespace-only changes.

tests/example_project/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44

55
class Pokemon(model.Model):
66
name = fields.Varchar(length=50)
7+
power = fields.Integer()

tests/test_model.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import asyncpg
55

6-
from example_project.models import Pokemon
6+
from .example_project.models import Pokemon
77

88

99
TEST_CREDENTIALS = {
@@ -79,7 +79,7 @@ async def get_pokemon():
7979
print(f'response = {response}')
8080

8181
self.assertDictEqual(
82-
response,
82+
response[0],
8383
{'name': 'pikachu', 'power': 1000}
8484
)
8585

@@ -93,7 +93,7 @@ async def get_pokemon():
9393
print(f'response = {response}')
9494

9595
self.assertDictEqual(
96-
response,
96+
response[0],
9797
{'name': 'pikachu'}
9898
)
9999

@@ -115,6 +115,24 @@ async def get_pokemon():
115115
[{'name': 'pikachu'}, {'name': 'raichu'}]
116116
)
117117

118+
def test_where_greater_than(self):
119+
self.insert_rows()
120+
121+
async def get_pokemon():
122+
return await Pokemon.select(
123+
'name'
124+
).where(
125+
Pokemon.power > 1000
126+
).execute()
127+
128+
response = asyncio.run(get_pokemon())
129+
print(f'response = {response}')
130+
131+
self.assertEqual(
132+
response,
133+
[{'name': 'raichu'}]
134+
)
135+
118136
def tearDown(self):
119137
self.drop_table()
120138

0 commit comments

Comments
 (0)