Skip to content

Commit

Permalink
Add 8th lecture
Browse files Browse the repository at this point in the history
  • Loading branch information
martinhronec committed Apr 16, 2024
1 parent b7aa9df commit e7bf88b
Show file tree
Hide file tree
Showing 14 changed files with 1,928 additions and 0 deletions.
657 changes: 657 additions & 0 deletions 08_packages_docs_tests/08a_pkg_doc.ipynb

Large diffs are not rendered by default.

1,133 changes: 1,133 additions & 0 deletions 08_packages_docs_tests/08b_testing.ipynb

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions 08_packages_docs_tests/pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[pytest]
markers =
database: mark a test needing access to database
13 changes: 13 additions & 0 deletions 08_packages_docs_tests/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from setuptools import setup, find_packages

setup(
name='PackageName',
version='0.1',
author='YoursTruly',
author_email='[email protected]',
packages= ["src"], #find_packages(),
description='Exemplatory package.',
#long_description=open('README.md').read(),
install_requires=[
"pytest",
],)
Empty file.
7 changes: 7 additions & 0 deletions 08_packages_docs_tests/src/example_module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
text = "modularity is the key"

def f(arg):
print(f'This function takes as an argument: {arg}')

class AClass:
pass
11 changes: 11 additions & 0 deletions 08_packages_docs_tests/test_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
def test_sum():
assert sum([1,1]) == 2, "Should be 2"

def test_len_vs__len__():
a_tuple = (1,2,3,5)
assert len(a_tuple) == a_tuple.__len__(), "Function len returned differnt result than method __len__"

if __name__ == "__main__":
test_sum()
test_len_vs__len__()
print('All tests passed.')
7 changes: 7 additions & 0 deletions 08_packages_docs_tests/test_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

def test_sum():
assert sum([1,1]) == 3, "Should be 2"

def test_len_vs__len__():
a_tuple = (1,2,3,5)
assert len(a_tuple) == a_tuple.__len__(), "Function len returned differnt result than method __len__"
11 changes: 11 additions & 0 deletions 08_packages_docs_tests/test_naive.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
def test_sum():
assert sum([1,1]) == 2, "Should be 2"

def test_len_vs__len__():
a_tuple = (1,2,3,5)
assert len(a_tuple) == a_tuple.__len__(), "Function len returned differnt result than method __len__"

if __name__ == "__main__":
test_sum()
test_len_vs__len__()
print('All tests passed.')
7 changes: 7 additions & 0 deletions 08_packages_docs_tests/tests/test_3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

def test_sum():
assert sum([1,1]) == 3, "Should be 2"

def test_len_vs__len__():
a_tuple = (1,2,3,5)
assert len(a_tuple) == a_tuple.__len__(), "Function len returned differnt result than method __len__"
13 changes: 13 additions & 0 deletions 08_packages_docs_tests/tests/test_fixture_smtp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import pytest

@pytest.fixture
def smtp():
"""Initialize and return SMTP client session object"""
import smtplib
return smtplib.SMTP("smtp.gmail.com")

def test_ehlo(smtp):
"""Test response from sending Extended Helo (EHLO) is 250."""
response, msg = smtp.ehlo()
assert response == 250
# assert 0
14 changes: 14 additions & 0 deletions 08_packages_docs_tests/tests/test_fixtures_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import pytest

@pytest.fixture
def data_names():
import pandas as pd
df = pd.read_csv('tests/data/test_data_names.csv')
return df

def test_addressing(data_names):
df = data_names
titles = df['Title']
surnames = df['Surname']
expected = df['Addressing']
assert (titles + ' ' + expected == surnames).all()
9 changes: 9 additions & 0 deletions 08_packages_docs_tests/tests/test_mark_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import pytest

@pytest.mark.database
def test_pg_read():
pass

@pytest.mark.database
def test_pg_write():
pass
43 changes: 43 additions & 0 deletions 08_packages_docs_tests/tests/test_parametrize_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import pytest
import unicodedata

#######
# Function we would like to test should be defined in package code, not here.
########
def drop_diacritics(text: str) -> str:
"""
Strip accents from input String.
:param text: The input string.
:returns: The processed string.
"""
if not isinstance(text, str):
raise TypeError(f'Input text should be a string, not %s', type(text))

# Return the normal form for the Unicode string
# 'NFKD' stands for the normal form KD
text = unicodedata.normalize('NFKD',text)
output = ''

for char in text:
if not unicodedata.combining(char):
output += char

return output
####


@pytest.mark.parametrize("test_input,expected", [("3+5", 8), ("2+4", 6), ("6*9", 42)])
def test_eval(test_input, expected):
assert eval(test_input) == expected

@pytest.mark.parametrize(
'original,output',
[
('řeřicha', 'rericha'),
('čeština', 'cestina')
]
)
def test_drop_diacritics(original:str, output:str) -> None:
assert drop_diacritics(original) == output

0 comments on commit e7bf88b

Please sign in to comment.