Skip to content

Commit c850fe7

Browse files
committed
feat : unit tests for decorators and generators.
1 parent d8a7a81 commit c850fe7

6 files changed

Lines changed: 76 additions & 0 deletions

File tree

tests/test_cache.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import time
2+
from decorators.cache_decorator import cache_result
3+
4+
5+
@cache_result(ttl_seconds=2)
6+
def slow_square(x):
7+
time.sleep(1)
8+
return x * x
9+
10+
11+
def test_cache_result():
12+
start = time.time()
13+
assert slow_square(3) == 9
14+
first_duration = time.time() - start
15+
16+
start = time.time()
17+
assert slow_square(3) == 9 # From cache
18+
second_duration = time.time() - start
19+
20+
assert second_duration < first_duration # Cached

tests/test_fibonacci.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from generators.fibonacci import fibonacci
2+
3+
def test_fibonacci():
4+
fib = fibonacci()
5+
results = [next(fib) for _ in range(6)]
6+
assert results == [0, 1, 1, 2, 3, 5]

tests/test_file_chunker.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import tempfile
2+
from generators.file_chunker import file_chunker
3+
4+
5+
def test_file_chunker_reads_data():
6+
with tempfile.NamedTemporaryFile(mode='wb', delete=False) as tf:
7+
tf.write(b"abcdefghij" * 100)
8+
9+
chunks = list(file_chunker(tf.name, chunk_size=100))
10+
assert sum(len(c) for c in chunks) == 1000

tests/test_logging.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from decorators.logging_decorator import log_execution
2+
3+
4+
@log_execution
5+
def add(a, b):
6+
return a + b
7+
8+
9+
def test_log_execution():
10+
assert add(2, 3) == 5
11+
print("Success")

tests/test_retry.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import pytest
2+
from decorators.retry_decorator import retry_on_exception
3+
4+
counter = {"calls": 0}
5+
6+
7+
@retry_on_exception(retries=3, delay=0)
8+
def flaky_function():
9+
counter["calls"] += 1
10+
if counter["calls"] < 3:
11+
raise ValueError("Fail")
12+
return "success"
13+
14+
15+
def test_retry_on_exception():
16+
counter["calls"] = 0
17+
result = flaky_function()
18+
assert result == "success"
19+
assert counter["calls"] == 3

tests/test_time.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from decorators.time_decorator import time_execution
2+
3+
4+
@time_execution
5+
def add(x, y):
6+
return x + y
7+
8+
9+
def test_time_execution():
10+
assert add(2, 5) == 7

0 commit comments

Comments
 (0)