Skip to content
Snippets Groups Projects
stats.py 1.36 KiB
Newer Older
Olivier Levillain's avatar
Olivier Levillain committed
import pytest

class Module:
    def __init__(self):
        self.__grades = {}

    def add_student(self, last_name, first_name, grades):
        self.__grades[(last_name, first_name)] = grades

    def n_students(self):
        return len(self.__grades)

    def is_enrolled(self, last_name, first_name):
        return (last_name, first_name) in self.__grades

Olivier Levillain's avatar
Olivier Levillain committed
    def overall_grade(self, last_name, first_name):
        grades = self.__grades[(last_name, first_name)]
        return (sum(grades) / len(grades))


Olivier Levillain's avatar
Olivier Levillain committed
@pytest.fixture
def sample_module():
    m = Module()
    m.add_student("Durden", "Tyler", [15, 10, 12])
    m.add_student("Singer", "Marla", [12, 14, 5])
    m.add_student("Paulson", "Bob", [5, 6, 10])
    m.add_student("Face", "Angel", [12, 20, 14])
    return m

def test_n_students(sample_module):
    assert sample_module.n_students() == 4

def test_is_enrolled(sample_module):
    assert sample_module.is_enrolled("Durden", "Tyler")
    assert not sample_module.is_enrolled("Simpson", "Homer")
Olivier Levillain's avatar
Olivier Levillain committed

def test_overall_grade(sample_module):
    assert sample_module.overall_grade("Durden", "Tyler") == pytest.approx(12.33, 2)
    assert sample_module.overall_grade("Singer", "Marla") == pytest.approx(10.33, 2)
    assert sample_module.overall_grade("Paulson", "Bob") == pytest.approx(7.00, 2)
Olivier Levillain's avatar
Olivier Levillain committed
    assert sample_module.overall_grade("Face", "Angel") == pytest.approx(15.33, 2)