class NotesService:
    def __init__(self):
        self.notesStorage = NotesStorage()

    def add(self, note):
        return self.notesStorage.add(note)

    def averageOf(self, name):
        if not self.notesStorage.getAllNotesOf(name):
            raise Exception("This person has not notes")
        else:
            marks = map(lambda person: person.note,
                        self.notesStorage.getAllNotesOf(name))
            return sum(marks) / len(self.notesStorage.getAllNotesOf(name))

    def clear(self):
        return self.notesStorage.clear()
Esempio n. 2
0
    def test_average_value_error(self):
        objectStorage = NotesStorage()
        objectStorage.getAllNotesOf = MagicMock()
        objectStorage.getAllNotesOf.side_effect = ValueError

        test_object = NotesService()
        test_object.notesStorage = objectStorage

        result = test_object.averageOf
        self.assertRaises(ValueError, result, 13)
Esempio n. 3
0
    def test_average_exception(self):
        objectStorage = NotesStorage()
        objectStorage.getAllNotesOf = MagicMock()
        objectStorage.getAllNotesOf.return_value = []

        test_object = NotesService()
        test_object.notesStorage = objectStorage

        result = test_object.averageOf
        self.assertRaisesRegex(Exception, "This person has not notes", result,
                               "Maciej")
Esempio n. 4
0
    def test_average(self):
        objectStorage = NotesStorage()
        objectStorage.getAllNotesOf = MagicMock()
        objectStorage.getAllNotesOf.return_value = [
            Note("Maciej", 4.0),
            Note("Maciej", 5.0),
            Note("Maciej", 3.0)
        ]

        test_object = NotesService()
        test_object.notesStorage = objectStorage

        result = test_object.averageOf("Maciej")
        self.assertEqual(result, 4)