Skip to content
This repository was archived by the owner on Oct 22, 2024. It is now read-only.

Commit 81420c1

Browse files
authored
Merge pull request #40 from deta/feat/add-new-fetch-method
Re-Implement fetch method on base
2 parents 506de01 + 9271a7c commit 81420c1

2 files changed

Lines changed: 108 additions & 35 deletions

File tree

deta/base.py

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,32 @@
66
from .service import _Service, JSON_MIME
77

88

9+
class FetchResponse:
10+
def __init__(self, count=0, last=None, items=[]):
11+
self._count = count
12+
self._last = last
13+
self._items = items
14+
15+
@property
16+
def count(self):
17+
return self._count
18+
19+
@property
20+
def last(self):
21+
return self._last
22+
23+
@property
24+
def items(self):
25+
return self._items
26+
27+
def __eq__(self, other):
28+
return (
29+
self.count == other.count
30+
and self.last == other.last
31+
and self.items == other.items
32+
)
33+
34+
935
class Util:
1036
class Trim:
1137
pass
@@ -148,23 +174,18 @@ def fetch(
148174
self,
149175
query: typing.Union[dict, list] = None,
150176
*,
151-
buffer: int = None,
152-
pages: int = 10,
153-
) -> typing.Generator:
177+
limit: int = 1000,
178+
last: str = None,
179+
):
154180
"""
155181
fetch items from the database.
156182
`query` is an optional filter or list of filters. Without filter, it will return the whole db.
157-
Returns a generator with all the result, We will paginate the request based on `buffer`.
158183
"""
159-
last = True
160-
code = 200
161-
counter = 0
162-
while code == 200 and last and pages > counter:
163-
code, res = self._fetch(query, buffer, last)
164-
items = res["items"]
165-
last = res["paging"].get("last")
166-
counter += 1
167-
yield items
184+
_, res = self._fetch(query, limit, last)
185+
186+
paging = res.get("paging")
187+
188+
return FetchResponse(paging.get("size"), paging.get("last"), res.get("items"))
168189

169190
def update(self, updates: dict, key: str):
170191
"""

tests.py

Lines changed: 74 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from deta.drive import UPLOAD_CHUNK_SIZE
2+
from deta.base import FetchResponse
23
import os
34
import io
45
import unittest
@@ -151,7 +152,7 @@ def test_read_close(self):
151152
body = self.drive.get(tc["name"])
152153
body.close()
153154
self.assertEqual(body.closed, True)
154-
155+
155156
def test_read_lines(self):
156157
test_cases = [
157158
{
@@ -164,16 +165,16 @@ def test_read_lines(self):
164165
},
165166
{
166167
"name": "read_lines_test_3.txt",
167-
"content": "different new line\ranother line\r"
168-
}
169-
168+
"content": "different new line\ranother line\r",
169+
},
170170
]
171171
for tc in test_cases:
172172
test_stream = io.StringIO(tc["content"])
173173
self.drive.put(tc["name"], tc["content"])
174174
body = self.drive.get(tc["name"])
175175
for line in body.iter_lines():
176-
self.assertEqual(test_stream.readline(), line.decode())
176+
self.assertEqual(test_stream.readline(), line.decode())
177+
177178

178179
class TestBaseMethods(unittest.TestCase):
179180
def setUp(self):
@@ -191,9 +192,9 @@ def setUp(self):
191192
self.db.put_many([self.item1, self.item2, self.item3, self.item4, self.item5])
192193

193194
def tearDown(self):
194-
for items in self.db.fetch():
195-
for i in items:
196-
self.db.delete(i["key"])
195+
items = self.db.fetch().items
196+
for i in items:
197+
self.db.delete(i["key"])
197198
self.db.client.close()
198199

199200
def test_put(self):
@@ -244,20 +245,71 @@ def test_delete(self):
244245
self.assertIsNone(self.db.delete("key_does_not_exist"))
245246

246247
def test_fetch(self):
247-
res1 = next(self.db.fetch({"value": "test"}))
248-
res2 = next(self.db.fetch({"valuexyz": "test_none_existing_value"}))
249-
res3 = next(self.db.fetch(buffer=3))
250-
res4 = list(self.db.fetch(buffer=1, pages=4))
251-
res5 = next(self.db.fetch({"value.name": self.item4["value"]["name"]}))
252-
res6 = next(self.db.fetch({"value?gte": 7}))
253-
res7 = next(self.db.fetch([{"value?gt": 6}, {"value?lt": 50}]))
254-
self.assertTrue(len(res1) > 0)
255-
self.assertTrue(len(res2) == 0)
256-
self.assertTrue(len(res3) == 3)
257-
self.assertTrue(len(res4) == 4)
258-
self.assertTrue(len(res5) > 0)
259-
self.assertTrue(len(res6) == 2)
260-
self.assertTrue(len(res7) == 3)
248+
res1 = self.db.fetch({"value?gte": 7})
249+
expectedItem = FetchResponse(
250+
2,
251+
None,
252+
[
253+
{"key": "existing2", "value": 7},
254+
{"key": "existing3", "value": 44},
255+
],
256+
)
257+
self.assertEqual(res1, expectedItem)
258+
259+
res2 = self.db.fetch({"value?gte": 7}, limit=1)
260+
expectedItem = FetchResponse(
261+
1,
262+
"existing2",
263+
[
264+
{"key": "existing2", "value": 7},
265+
],
266+
)
267+
self.assertEqual(res2, expectedItem)
268+
269+
res3 = self.db.fetch([{"value?gt": 6}, {"value?lt": 50}], limit=2)
270+
expectedItem = FetchResponse(
271+
2,
272+
"existing2",
273+
[
274+
{"key": "%@#//#!#)#$_", "list": ["a"], "value": 0},
275+
{"key": "existing2", "value": 7},
276+
],
277+
)
278+
self.assertEqual(res3, expectedItem)
279+
280+
res4 = self.db.fetch(
281+
[{"value?gt": 6}, {"value?lt": 50}], limit=2, last="existing2"
282+
)
283+
expectedItem = FetchResponse(
284+
1,
285+
None,
286+
[{"key": "existing3", "value": 44}],
287+
)
288+
self.assertEqual(res4, expectedItem)
289+
290+
res5 = self.db.fetch({"value": "test"})
291+
expectedItem = FetchResponse(
292+
1,
293+
None,
294+
[{"key": "existing1", "value": "test"}],
295+
)
296+
self.assertEqual(res5, expectedItem)
297+
298+
res6 = self.db.fetch({"valuexyz": "test_none_existing_value"})
299+
expectedItem = FetchResponse(
300+
0,
301+
None,
302+
[],
303+
)
304+
self.assertEqual(res6, expectedItem)
305+
306+
res7 = self.db.fetch({"value.name": self.item4["value"]["name"]})
307+
expectedItem = FetchResponse(
308+
1,
309+
None,
310+
[{"key": "existing4", "value": {"name": "patrick"}}],
311+
)
312+
self.assertEqual(res7, expectedItem)
261313

262314
def test_update(self):
263315
self.assertIsNone(self.db.update({"value.name": "spongebob"}, "existing4"))

0 commit comments

Comments
 (0)