Skip to content

Commit 41f1ef2

Browse files
committed
Tests 4 W03
1 parent bb7c851 commit 41f1ef2

File tree

2 files changed

+233
-0
lines changed

2 files changed

+233
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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: Week03 Homework
5+
6+
on:
7+
push:
8+
branches: [ "master" ]
9+
paths: ['Week03/**']
10+
pull_request:
11+
branches: [ "master" ]
12+
paths: ['Week03/**']
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 'Week03/test_functions.py'

Week03/test_functions.py

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
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])), (
13+
"custom_power is not defined in " + f[:-3]
14+
)
15+
assert "custom_equation" in dir(eval(f[:-3])), (
16+
"custom_equation is not defined in " + f[:-3]
17+
)
18+
assert "fn_w_counter" in dir(eval(f[:-3])), (
19+
"fn_w_counter is not defined in " + f[:-3]
20+
)
21+
22+
23+
def test_callables():
24+
for f in files:
25+
assert callable(eval(f[:-3]).custom_power), (
26+
"custom_power is not callable in " + f[:-3]
27+
)
28+
assert callable(eval(f[:-3]).custom_equation), (
29+
"custom_equation is not callable in " + f[:-3]
30+
)
31+
assert callable(eval(f[:-3]).fn_w_counter), (
32+
"fn_w_counter is not callable in " + f[:-3]
33+
)
34+
35+
36+
def test_custom_power():
37+
for f in files:
38+
sig = inspect.signature(eval(f[:-3]).custom_power)
39+
assert eval(f[:-3]).custom_power.__name__ == "<lambda>", (
40+
"custom_power is not a lambda function in " + f[:-3]
41+
)
42+
assert "x" in sig.parameters, "custom_power has wrong parameters in " + f[:-3]
43+
assert "e" in sig.parameters, "custom_power has wrong parameters in " + f[:-3]
44+
assert sig.parameters["x"].kind == inspect._POSITIONAL_ONLY, (
45+
"custom_power has wrong kind in " + f[:-3]
46+
)
47+
assert sig.parameters["e"].kind == inspect._POSITIONAL_OR_KEYWORD, (
48+
"custom_power has wrong kind in " + f[:-3]
49+
)
50+
assert sig.parameters["x"].default == 0, (
51+
"custom_power has wrong default value in " + f[:-3]
52+
)
53+
assert sig.parameters["e"].default == 1, (
54+
"custom_power has wrong default value in " + f[:-3]
55+
)
56+
assert eval(f[:-3]).custom_power(2) == 2, (
57+
"custom_power is not working in " + f[:-3]
58+
)
59+
assert eval(f[:-3]).custom_power(2, 3) == 8, (
60+
"custom_power is not working in " + f[:-3]
61+
)
62+
assert eval(f[:-3]).custom_power(2, e=3) == 8, (
63+
"custom_power is not working in " + f[:-3]
64+
)
65+
assert eval(f[:-3]).custom_power(49, e=0.5) == 7, (
66+
"custom_power is not working in " + f[:-3]
67+
)
68+
69+
70+
def test_custom_equation():
71+
for f in files:
72+
sig = inspect.signature(eval(f[:-3]).custom_equation)
73+
assert eval(f[:-3]).custom_equation.__doc__ is not None, (
74+
"custom_equation has no docstring in " + f[:-3]
75+
)
76+
assert eval(f[:-3]).custom_equation.__doc__.count(":param") == 5, (
77+
"custom_equation has wrong docstring in " + f[:-3] + " (param)"
78+
)
79+
assert eval(f[:-3]).custom_equation.__doc__.count(":return") == 1, (
80+
"custom_equation has wrong docstring in " + f[:-3] + " (return)"
81+
)
82+
assert sig.return_annotation == float, (
83+
"custom_equation has wrong return annotation in " + f[:-3]
84+
)
85+
assert "x" in sig.parameters, (
86+
"custom_equation has wrong parameters in " + f[:-3]
87+
)
88+
assert "y" in sig.parameters, (
89+
"custom_equation has wrong parameters in " + f[:-3]
90+
)
91+
assert "a" in sig.parameters, (
92+
"custom_equation has wrong parameters in " + f[:-3]
93+
)
94+
assert "b" in sig.parameters, (
95+
"custom_equation has wrong parameters in " + f[:-3]
96+
)
97+
assert "c" in sig.parameters, (
98+
"custom_equation has wrong parameters in " + f[:-3]
99+
)
100+
assert sig.parameters["x"].kind == inspect._POSITIONAL_ONLY, (
101+
"custom_equation has wrong kind for x in " + f[:-3]
102+
)
103+
assert sig.parameters["y"].kind == inspect._POSITIONAL_ONLY, (
104+
"custom_equation has wrong kind for y in " + f[:-3]
105+
)
106+
assert sig.parameters["a"].kind == inspect._POSITIONAL_OR_KEYWORD, (
107+
"custom_equation has wrong kind for a in " + f[:-3]
108+
)
109+
assert sig.parameters["b"].kind == inspect._POSITIONAL_OR_KEYWORD, (
110+
"custom_equation has wrong kind for b in " + f[:-3]
111+
)
112+
assert sig.parameters["c"].kind == inspect._KEYWORD_ONLY, (
113+
"custom_equation has wrong kind for c in " + f[:-3]
114+
)
115+
assert sig.parameters["x"].annotation == int, (
116+
"custom_equation has wrong annotation for x in " + f[:-3]
117+
)
118+
assert sig.parameters["y"].annotation == int, (
119+
"custom_equation has wrong annotation for y in " + f[:-3]
120+
)
121+
assert sig.parameters["a"].annotation == int, (
122+
"custom_equation has wrong annotation for a in " + f[:-3]
123+
)
124+
assert sig.parameters["b"].annotation == int, (
125+
"custom_equation has wrong annotation for b in " + f[:-3]
126+
)
127+
assert sig.parameters["c"].annotation == int, (
128+
"custom_equation has wrong annotation for c in " + f[:-3]
129+
)
130+
assert sig.parameters["x"].default == 0, (
131+
"custom_equation has wrong default value for x in " + f[:-3]
132+
)
133+
assert sig.parameters["y"].default == 0, (
134+
"custom_equation has wrong default value for y in " + f[:-3]
135+
)
136+
assert sig.parameters["a"].default == 1, (
137+
"custom_equation has wrong default value for a in " + f[:-3]
138+
)
139+
assert sig.parameters["b"].default == 1, (
140+
"custom_equation has wrong default value for b in " + f[:-3]
141+
)
142+
assert sig.parameters["c"].default == 1, (
143+
"custom_equation has wrong default value for c in " + f[:-3]
144+
)
145+
try:
146+
eval(f[:-3]).custom_equation(1.5, 2.5)
147+
except TypeError as e:
148+
assert str(e).count("must") > 0, (
149+
"custom_equation has wrong raises in " + f[:-3]
150+
)
151+
try:
152+
eval(f[:-3]).custom_equation(1, 2, c=1.5)
153+
except TypeError as e:
154+
assert str(e).count("must") > 0, (
155+
"custom_equation has wrong raises in " + f[:-3]
156+
)
157+
assert eval(f[:-3]).custom_equation(1, 2) == 3, (
158+
"custom_equation is not working in " + f[:-3]
159+
)
160+
assert eval(f[:-3]).custom_equation(1, 2, 3) == 3, (
161+
"custom_equation is not working in " + f[:-3]
162+
)
163+
assert eval(f[:-3]).custom_equation(1, 2, 3, 4) == 17, (
164+
"custom_equation is not working in " + f[:-3]
165+
)
166+
assert eval(f[:-3]).custom_equation(1, 2, 3, 4, c=5) == 3.4, (
167+
"custom_equation is not working in " + f[:-3]
168+
)
169+
assert eval(f[:-3]).custom_equation(1, 2, 3, c=5, b=6) == 13, (
170+
"custom_equation is not working in " + f[:-3]
171+
)
172+
173+
174+
def test_fn_w_counter():
175+
for f in files:
176+
sig = inspect.signature(eval(f[:-3]).fn_w_counter)
177+
assert sig.return_annotation == (int, dict[str, int]), (
178+
"fn_w_counter has wrong return annotation in " + f[:-3]
179+
)
180+
# call the function multiple times
181+
for i in range(1, 10):
182+
assert eval(f[:-3]).fn_w_counter() == (i, {f[:-3]: i}), (
183+
"fn_w_counter is not working in " + f[:-3]
184+
)
185+
for i in range(10, 97):
186+
assert eval(f[:-3]).fn_w_counter() == (i, {f[:-3]: i}), (
187+
"fn_w_counter is not working in " + f[:-3]
188+
)
189+
for i in range(97, 100):
190+
assert eval(f[:-3]).fn_w_counter() == (i, {f[:-3]: i}), (
191+
"fn_w_counter is not working in " + f[:-3]
192+
)

0 commit comments

Comments
 (0)