Skip to content

Commit 21ab838

Browse files
committed
Tests 4 W04
1 parent 09ab0a6 commit 21ab838

File tree

3 files changed

+220
-0
lines changed

3 files changed

+220
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# This workflow will install Python dependencies, run tests and lint with a single version of Python
2+
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python
3+
4+
name: Week04 Homework
5+
6+
on:
7+
push:
8+
branches: [ "master" ]
9+
paths: ['Week04/**']
10+
pull_request:
11+
branches: [ "master" ]
12+
paths: ['Week04/**']
13+
14+
permissions:
15+
contents: read
16+
17+
jobs:
18+
build:
19+
20+
runs-on: ubuntu-latest
21+
22+
steps:
23+
- uses: actions/checkout@v3
24+
- name: Set up Python 3.12
25+
uses: actions/setup-python@v3
26+
with:
27+
python-version: "3.12"
28+
- name: Install dependencies
29+
run: |
30+
python -m pip install --upgrade pip
31+
pip install flake8 pytest
32+
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
33+
- name: Lint with flake8
34+
run: |
35+
# stop the build if there are Python syntax errors or undefined names
36+
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
37+
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
38+
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
39+
- name: Test with pytest
40+
run: |
41+
pytest -q --tb=no 'Week04/test_functions.py'
42+
pytest -q --tb=no 'Week04/test_decorators.py'

Week04/test_decorators.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import os
2+
import inspect
3+
import time
4+
import random
5+
6+
7+
files = [f for f in os.listdir(os.path.dirname(__file__)) if f.startswith("decorators")]
8+
for f in files:
9+
exec("import " + f[:-3] + " as " + f[:-3])
10+
11+
12+
def test_names():
13+
for f in files:
14+
assert "performance" in dir(eval(f[:-3])), "timer is not defined in " + f[:-3]
15+
16+
def test_callables():
17+
for f in files:
18+
assert callable(eval(f[:-3]).performance), "timer is not callable in " + f[:-3]
19+
20+
def test_performance():
21+
for f in files:
22+
@eval(f[:-3]).performance
23+
def dummy_timer(x):
24+
time.sleep(x)
25+
@eval(f[:-3]).performance
26+
def dummy_memory(x):
27+
return [random.randint(0, 100) for _ in range(x)]
28+
dummy_timer(1)
29+
assert eval(f[:-3]).performance.counter == 1, \
30+
"performance is not working in " + f[:-3] + " (counter)"
31+
assert eval(f[:-3]).performance.total_time > 1, \
32+
"performance is not working in " + f[:-3] + " (total_time)"
33+
dummy_timer(1)
34+
dummy_timer(2)
35+
dummy_timer(3)
36+
assert eval(f[:-3]).performance.counter == 4, \
37+
"performance is not working in " + f[:-3] + " (counter)"
38+
assert eval(f[:-3]).performance.total_time > 7, \
39+
"performance is not working in " + f[:-3] + " (total_time)"
40+
dummy_memory(1000000)
41+
assert eval(f[:-3]).performance.counter == 5, \
42+
"performance is not working in " + f[:-3] + " (counter)"
43+
assert eval(f[:-3]).performance.total_mem > 8.e6, \
44+
"performance is not working in " + f[:-3] + " (total_mem)"

Week04/test_functions.py

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import os
2+
import inspect
3+
4+
5+
files = [f for f in os.listdir(os.path.dirname(__file__)) if f.startswith("functions")]
6+
for f in files:
7+
exec("import " + f[:-3] + " as " + f[:-3])
8+
9+
10+
def test_names():
11+
for f in files:
12+
assert "custom_power" in dir(eval(f[:-3])), "custom_power is not defined in " + f[:-3]
13+
assert "custom_equation" in dir(eval(f[:-3])), "custom_equation is not defined in " + f[:-3]
14+
assert "fn_w_counter" in dir(eval(f[:-3])), "fn_w_counter is not defined in " + f[:-3]
15+
16+
def test_callables():
17+
for f in files:
18+
assert callable(eval(f[:-3]).custom_power), "custom_power is not callable in " + f[:-3]
19+
assert callable(eval(f[:-3]).custom_equation), "custom_equation is not callable in " + f[:-3]
20+
assert callable(eval(f[:-3]).fn_w_counter), "fn_w_counter is not callable in " + f[:-3]
21+
22+
def test_custom_power():
23+
for f in files:
24+
sig = inspect.signature(eval(f[:-3]).custom_power)
25+
assert eval(f[:-3]).custom_power.__name__ == "<lambda>", \
26+
"custom_power is not a lambda function in " + f[:-3]
27+
assert 'x' in sig.parameters, \
28+
"custom_power has wrong parameters in " + f[:-3]
29+
assert 'e' in sig.parameters, \
30+
"custom_power has wrong parameters in " + f[:-3]
31+
assert sig.parameters['x'].kind == inspect._POSITIONAL_ONLY, \
32+
"custom_power has wrong kind in " + f[:-3]
33+
assert sig.parameters['e'].kind == inspect._POSITIONAL_OR_KEYWORD, \
34+
"custom_power has wrong kind in " + f[:-3]
35+
assert sig.parameters['x'].default == 0, \
36+
"custom_power has wrong default value in " + f[:-3]
37+
assert sig.parameters['e'].default == 1, \
38+
"custom_power has wrong default value in " + f[:-3]
39+
assert eval(f[:-3]).custom_power(2) == 2, \
40+
"custom_power is not working in " + f[:-3]
41+
assert eval(f[:-3]).custom_power(2, 3) == 8, \
42+
"custom_power is not working in " + f[:-3]
43+
assert eval(f[:-3]).custom_power(2, e=3) == 8, \
44+
"custom_power is not working in " + f[:-3]
45+
assert eval(f[:-3]).custom_power(49, e=0.5) == 7, \
46+
"custom_power is not working in " + f[:-3]
47+
48+
def test_custom_equation():
49+
for f in files:
50+
sig = inspect.signature(eval(f[:-3]).custom_equation)
51+
assert eval(f[:-3]).custom_equation.__doc__ is not None, \
52+
"custom_equation has no docstring in " + f[:-3]
53+
assert eval(f[:-3]).custom_equation.__doc__.count(":param") == 5, \
54+
"custom_equation has wrong docstring in " + f[:-3] + " (param)"
55+
assert eval(f[:-3]).custom_equation.__doc__.count(":return") == 1, \
56+
"custom_equation has wrong docstring in " + f[:-3] + " (return)"
57+
assert sig.return_annotation == float, \
58+
"custom_equation has wrong return annotation in " + f[:-3]
59+
assert 'x' in sig.parameters, \
60+
"custom_equation has wrong parameters in " + f[:-3]
61+
assert 'y' in sig.parameters, \
62+
"custom_equation has wrong parameters in " + f[:-3]
63+
assert 'a' in sig.parameters, \
64+
"custom_equation has wrong parameters in " + f[:-3]
65+
assert 'b' in sig.parameters, \
66+
"custom_equation has wrong parameters in " + f[:-3]
67+
assert 'c' in sig.parameters, \
68+
"custom_equation has wrong parameters in " + f[:-3]
69+
assert sig.parameters['x'].kind == inspect._POSITIONAL_ONLY, \
70+
"custom_equation has wrong kind for x in " + f[:-3]
71+
assert sig.parameters['y'].kind == inspect._POSITIONAL_ONLY, \
72+
"custom_equation has wrong kind for y in " + f[:-3]
73+
assert sig.parameters['a'].kind == inspect._POSITIONAL_OR_KEYWORD, \
74+
"custom_equation has wrong kind for a in " + f[:-3]
75+
assert sig.parameters['b'].kind == inspect._POSITIONAL_OR_KEYWORD, \
76+
"custom_equation has wrong kind for b in " + f[:-3]
77+
assert sig.parameters['c'].kind == inspect._KEYWORD_ONLY, \
78+
"custom_equation has wrong kind for c in " + f[:-3]
79+
assert sig.parameters['x'].annotation == int, \
80+
"custom_equation has wrong annotation for x in " + f[:-3]
81+
assert sig.parameters['y'].annotation == int, \
82+
"custom_equation has wrong annotation for y in " + f[:-3]
83+
assert sig.parameters['a'].annotation == int, \
84+
"custom_equation has wrong annotation for a in " + f[:-3]
85+
assert sig.parameters['b'].annotation == int, \
86+
"custom_equation has wrong annotation for b in " + f[:-3]
87+
assert sig.parameters['c'].annotation == int, \
88+
"custom_equation has wrong annotation for c in " + f[:-3]
89+
assert sig.parameters['x'].default == 0, \
90+
"custom_equation has wrong default value for x in " + f[:-3]
91+
assert sig.parameters['y'].default == 0, \
92+
"custom_equation has wrong default value for y in " + f[:-3]
93+
assert sig.parameters['a'].default == 1, \
94+
"custom_equation has wrong default value for a in " + f[:-3]
95+
assert sig.parameters['b'].default == 1, \
96+
"custom_equation has wrong default value for b in " + f[:-3]
97+
assert sig.parameters['c'].default == 1, \
98+
"custom_equation has wrong default value for c in " + f[:-3]
99+
try:
100+
eval(f[:-3]).custom_equation(1.5, 2.5)
101+
except TypeError as e:
102+
assert str(e).count("must") > 0, \
103+
"custom_equation has wrong raises in " + f[:-3]
104+
try:
105+
eval(f[:-3]).custom_equation(1, 2, c=1.5)
106+
except TypeError as e:
107+
assert str(e).count("must") > 0, \
108+
"custom_equation has wrong raises in " + f[:-3]
109+
assert eval(f[:-3]).custom_equation(1, 2) == 3, \
110+
"custom_equation is not working in " + f[:-3]
111+
assert eval(f[:-3]).custom_equation(1, 2, 3) == 3, \
112+
"custom_equation is not working in " + f[:-3]
113+
assert eval(f[:-3]).custom_equation(1, 2, 3, 4) == 17, \
114+
"custom_equation is not working in " + f[:-3]
115+
assert eval(f[:-3]).custom_equation(1, 2, 3, 4, c=5) == 3.4, \
116+
"custom_equation is not working in " + f[:-3]
117+
assert eval(f[:-3]).custom_equation(1, 2, 3, c=5, b=6) == 13, \
118+
"custom_equation is not working in " + f[:-3]
119+
120+
def test_fn_w_counter():
121+
for f in files:
122+
sig = inspect.signature(eval(f[:-3]).fn_w_counter)
123+
assert sig.return_annotation == (int, dict[str, int]), \
124+
"fn_w_counter has wrong return annotation in " + f[:-3]
125+
# call the function multiple times
126+
for i in range(1, 10):
127+
assert eval(f[:-3]).fn_w_counter() == (i, {f[:-3]: i}), \
128+
"fn_w_counter is not working in " + f[:-3]
129+
for i in range(10, 97):
130+
assert eval(f[:-3]).fn_w_counter() == (i, {f[:-3]: i}), \
131+
"fn_w_counter is not working in " + f[:-3]
132+
for i in range(97, 100):
133+
assert eval(f[:-3]).fn_w_counter() == (i, {f[:-3]: i}), \
134+
"fn_w_counter is not working in " + f[:-3]

0 commit comments

Comments
 (0)