Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions 05_testing_and_ci/examples/python_testing/mean_data.csv

This file was deleted.

147 changes: 97 additions & 50 deletions 05_testing_and_ci/examples/python_testing/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,109 @@
A set of mathematical operations.
"""


def find_max(data):
"""
Find maximum of all elements of a given list

Parameters
----------
data : list
List of data. Elements are numbers

Returns
-------
find_max : float
Maximum of list
"""
# Check that the input list has numbers
for n in data:
assert type(n) == int or type(n) == float

max_num = data[0] # Assume the first number is the maximum
for n in data:
if n > max_num:
max_num = n

return max_num


def find_mean(data):
"""
Find mean of all elements of a given list

Parameters
----------
data : list
List of data. Elements are numbers

Returns
-------
float : float
Mean of list
"""
# Check that the input list has numbers
for n in data:
assert type(n) == int or type(n) == float

return sum(data) / len(data)
class MathOperations:

def __init__(self, data):
self._data = data

def reorder_data(self):
"""
Reorder data in ascending order
"""
self._data.sort()

def find_max(self):
"""
Find maximum of all elements of a given list
Parameters
----------
data : list
List of data. Elements are numbers

Returns
-------
find_max : float
Maximum of list
"""
# Check that the input list has numbers
for n in self._data:
assert type(n) == int or type(n) == float

max_num = self._data[0] # Assume the first number is the maximum
for n in self._data:
if n > max_num:
max_num = n

return max_num

def find_median(self):
"""
Find median of all elements of a given list

Parameters
----------
data : list
List of data. Elements are numbers

Returns
-------
float : float
Median of list
"""
# Check that the input list has numbers
for n in self._data:
assert type(n) == int or type(n) == float

# Sort the data to find the median
sorted_data = sorted(self._data)
n = len(sorted_data)

# If odd number of elements, return the middle one
if n % 2 == 1:
return sorted_data[n // 2]
# If even number of elements, return the average of the two middle ones
else:
mid1 = sorted_data[n // 2 - 1]
mid2 = sorted_data[n // 2]
return (mid1 + mid2) / 2

def find_mean(self):
"""
Find mean of all elements of a given list

Parameters
----------
data : list
List of data. Elements are numbers

Returns
-------
float : float
Mean of list
"""
# Check that the input list has numbers
for n in self._data:
assert type(n) == int or type(n) == float

total = sum(self._data)
count = len(self._data)
mean = total / count
return mean


def main():
data = [5, 3, 14, 27, 4, 9]
data = [5, 3, 14, 27, 4, 9, 53]

math_ops = MathOperations(data)

maximum = find_max(data)
maximum = math_ops.find_max()
print("Maximum of {} is {}".format(data, maximum))

mean = find_mean(data)
print("Average of {} is {}".format(data, mean))
median = math_ops.find_median()
print("Median of {} is {}".format(data, median))

mean = math_ops.find_mean()
print("Mean of {} is {}".format(data, mean))


if __name__ == "__main__":
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1,17,18,32,43,167,209
90 changes: 47 additions & 43 deletions 05_testing_and_ci/examples/python_testing/test_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,87 +2,91 @@
Tests for mathematical operations functions.
"""

from operations import find_max, find_mean
from operations import MathOperations
import pytest
import csv


@pytest.fixture
def math_operations():
"""
Fixture for MathOperations class
"""
data = [43, 32, 167, 18, 1, 209, 17]
return MathOperations(data)

# Unit test
def test_find_max():
def test_find_max(math_operations):
"""
Test operations.find_max
"""
# Fixture
data = [43, 32, 167, 18, 1, 209]

# Expected result
expected_max = 209

# Actual result
actual_max = find_max(data)
actual_max = math_operations.find_max()

# Test
assert actual_max == expected_max

# Unit test
def test_reorder_data(math_operations):
"""
Test operations.reorder_data
"""
# Expected result
expected_data = [1, 17, 18, 32, 43, 167, 209]

# Actual result
math_operations.reorder_data()
actual_data = math_operations._data

# Test
assert actual_data == expected_data

# Unit test
def test_find_mean():
def test_find_mean(math_operations):
"""
Test operations.find_mean
"""
# Fixture
data = [43, 32, 167, 18, 1, 209]

# Expected result
expected_mean = 78.33
# expected_mean = pytest.approx(78.33, abs=0.01)

expected_mean = 69.57

# Actual result
actual_mean = find_mean(data)
actual_mean = math_operations.find_mean()

# Test
assert actual_mean == expected_mean


# Integration test
def test_mean_of_max():
"""
Test operations.find_max and operations.find_mean
def test_find_median(math_operations):
"""
# Fixture
data1 = [43, 32, 167, 18, 1, 209]
data2 = [3, 13, 33, 23, 498]

Test operations.find_median
"""
# Expected result
expected_mean_of_max = 353.5

maximum1 = find_max(data1)
maximum2 = find_max(data2)

expected_median = 32

# Actual result
actual_mean_of_max = find_mean([maximum1, maximum2])
actual_median = math_operations.find_median()

# Test
assert actual_mean_of_max == expected_mean_of_max
assert actual_median == expected_median


# Regression test
def test_regression_mean():
def test_reg_reorder_data(math_operations):
"""
Test operations.find_mean on a previously generated dataset
Test operations.reorder_data with data from CSV file
"""
with open("mean_data.csv") as f:
with open("reordered_data.csv") as f:
rows = csv.reader(f, quoting=csv.QUOTE_NONNUMERIC)
# Fixture
data = next(rows)

# Expected result
reference_mean = next(rows)

for row in rows:
expected_reordered_data = row

# Actual result
actual_mean = find_mean(data)

expected_mean = pytest.approx(reference_mean[0], abs=0.01)

math_operations.reorder_data()
actual_reordered_data = math_operations._data

# Test
assert actual_mean == expected_mean
assert actual_reordered_data == expected_reordered_data
Loading