Skip to content

Commit 5ea7a25

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents 4f332ed + 71f5b39 commit 5ea7a25

File tree

3 files changed

+173
-0
lines changed

3 files changed

+173
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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: Week05 Homework
5+
6+
on:
7+
push:
8+
branches: ["master"]
9+
paths: ["Week05/**"]
10+
pull_request:
11+
branches: ["master"]
12+
paths: ["Week05/**"]
13+
14+
permissions:
15+
contents: read
16+
17+
jobs:
18+
build:
19+
runs-on: ubuntu-latest
20+
21+
steps:
22+
- uses: actions/checkout@v3
23+
- name: Set up Python 3.12
24+
uses: actions/setup-python@v3
25+
with:
26+
python-version: "3.12"
27+
- name: Install dependencies
28+
run: |
29+
python -m pip install --upgrade pip
30+
pip install flake8 pytest
31+
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
32+
- name: Lint with flake8
33+
run: |
34+
# stop the build if there are Python syntax errors or undefined names
35+
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
36+
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
37+
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
38+
- name: Test with pytest
39+
run: |
40+
pytest -q --tb=no 'Week05/test_emails.py'

LectureNotes.pdf

408 KB
Binary file not shown.

Week05/test_emails.py

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
import os
2+
import pytest
3+
4+
5+
files = [f for f in os.listdir(os.path.dirname(__file__)) if f.startswith("emails")]
6+
for f in files:
7+
exec("import " + f[:-3] + " as " + f[:-3])
8+
9+
10+
# test the file if it has the Emails class
11+
def test_names():
12+
for f in files:
13+
assert "Emails" in dir(eval(f[:-3])), "Emails is not defined in " + f[:-3]
14+
15+
16+
# test the Emails class if it is a subclass of list
17+
def test_subclass():
18+
for f in files:
19+
assert issubclass(eval(f[:-3]).Emails, list), (
20+
"Emails is not a subclass of list in " + f[:-3]
21+
)
22+
23+
24+
# check if the Emails class is an instance of the list class
25+
def test_instance():
26+
for f in files:
27+
assert isinstance(eval(f[:-3]).Emails([]), list), (
28+
"Emails is not an instance of list in " + f[:-3]
29+
)
30+
31+
32+
# test the Emails class if it has the validate method
33+
def test_validate():
34+
for f in files:
35+
assert "validate" in dir(eval(f[:-3]).Emails), (
36+
"validate is not defined in " + f[:-3]
37+
)
38+
assert callable(eval(f[:-3]).Emails.validate), (
39+
"validate is not callable in " + f[:-3]
40+
)
41+
42+
43+
# test the Emails class if it has the __init__ method
44+
def test_init():
45+
for f in files:
46+
assert "__init__" in dir(eval(f[:-3]).Emails), (
47+
"__init__ is not defined in " + f[:-3]
48+
)
49+
assert callable(eval(f[:-3]).Emails.__init__), (
50+
"__init__ is not callable in " + f[:-3]
51+
)
52+
53+
54+
# test the Emails class if it has the __repr__ method
55+
def test_repr():
56+
for f in files:
57+
assert "__repr__" in dir(eval(f[:-3]).Emails), (
58+
"__repr__ is not defined in " + f[:-3]
59+
)
60+
assert callable(eval(f[:-3]).Emails.__repr__), (
61+
"__repr__ is not callable in " + f[:-3]
62+
)
63+
64+
65+
# check Emails class can reproduce itself with the __repr__ method
66+
def test_repr_output():
67+
for f in files:
68+
assert (
69+
repr(eval(f[:-3]).Emails(["bora.canbula@cbu.edu.tr", "bora@canbula.com"]))
70+
== (
71+
eval(f[:-3]).Emails(["bora.canbula@cbu.edu.tr", "bora@canbula.com"])
72+
).__repr__()
73+
), (
74+
"The Emails class does not reproduce itself with the __repr__ method in "
75+
+ f[:-3]
76+
)
77+
78+
79+
# test the Emails class if it has the __str__ method
80+
def test_str():
81+
for f in files:
82+
assert "__str__" in dir(eval(f[:-3]).Emails), (
83+
"__str__ is not defined in " + f[:-3]
84+
)
85+
assert callable(eval(f[:-3]).Emails.__str__), (
86+
"__str__ is not callable in " + f[:-3]
87+
)
88+
89+
90+
# check if the validate method allows only strings
91+
def test_validate_strings():
92+
for f in files:
93+
with pytest.raises(ValueError) as e:
94+
eval(f[:-3]).Emails([1, 2, 3])
95+
assert str(e.type) == "<class 'ValueError'>", (
96+
"The validate method does not allow only strings in " + f[:-3]
97+
)
98+
99+
100+
# check if the validate method allows only valid email addresses, and raises ValueError otherwise
101+
def test_validate_emails():
102+
for f in files:
103+
with pytest.raises(ValueError) as e:
104+
eval(f[:-3]).Emails(["bora.canbula@cbu.edu.tr", "canbula.com"])
105+
assert str(e.type) == "<class 'ValueError'>", (
106+
"The validate method does not allow only valid email addresses in "
107+
+ f[:-3]
108+
)
109+
with pytest.raises(ValueError) as e:
110+
eval(f[:-3]).Emails(["bora@canbulacom"])
111+
assert str(e.type) == "<class 'ValueError'>", (
112+
"The validate method does not allow only valid email addresses in "
113+
+ f[:-3]
114+
)
115+
116+
117+
# check Emails class does not allow duplicate email addresses, the order is not important
118+
def test_validate_duplicates():
119+
for f in files:
120+
assert (
121+
eval(f[:-3])
122+
.Emails(
123+
[
124+
"bora.canbula@cbu.edu.tr",
125+
"bora@canbula.com",
126+
"bora.canbula@cbu.edu.tr",
127+
]
128+
)
129+
.data.count("bora.canbula@cbu.edu.tr")
130+
== 1
131+
), (
132+
"The Emails class does not allow duplicate email addresses in " + f[:-3]
133+
)

0 commit comments

Comments
 (0)