Skip to content

Commit

Permalink
added a few more tests for limit
Browse files Browse the repository at this point in the history
  • Loading branch information
sh-rp committed Dec 11, 2024
1 parent c7ddf9b commit 95e0e78
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion dlt/extract/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ def __init__(self, max_items: int) -> None:
def bind(self, pipe: SupportsPipe) -> "LimitItem":
self.gen = pipe.gen
self.count = 0
self.exceeded = False
self.exhausted = False
return self

def __call__(self, item: TDataItems, meta: Any = None) -> Optional[TDataItems]:
Expand Down
34 changes: 34 additions & 0 deletions tests/extract/test_sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,40 @@ async def r_async():
raise AssertionError(f"Unexpected limit: {limit}")


def test_various_limit_setups() -> None:
# basic test
r = dlt.resource([1, 2, 3, 4, 5], name="test").add_limit(3)
assert list(r) == [1, 2, 3]

# yield map test
r = (
dlt.resource([1, 2, 3, 4, 5], name="test")
.add_map(lambda i: str(i) * i, 1)
.add_yield_map(lambda i: (yield from i))
.add_limit(3)
)
# limit is applied at the end
assert list(r) == ["1", "2", "2"] # "3" ,"3" ,"3" ,"4" ,"4" ,"4" ,"4", ...]

# nested lists test (limit only applied to yields, not actual items)
r = dlt.resource([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]], name="test").add_limit(3)
assert list(r) == [1, 2, 3, 4, 5, 6, 7, 8, 9]

# transformer test
r = dlt.resource([1, 2, 3, 4, 5], name="test").add_limit(4)
t = dlt.transformer(lambda i: i * 2, name="test")
assert list(r) == [1, 2, 3, 4]
assert list(r | t) == [2, 4, 6, 8]

# adding limit to transformer is disregarded
t = t.add_limit(2)
assert list(r | t) == [2, 4, 6, 8]

# limits are fully replaced (more genereous limit applied later takes precedence)
r = dlt.resource([1, 2, 3, 4, 5], name="test").add_limit(3).add_limit(4)
assert list(r) == [1, 2, 3, 4]


def test_limit_source() -> None:
def mul_c(item):
yield from "A" * (item + 2)
Expand Down

0 comments on commit 95e0e78

Please sign in to comment.