Exemplo n.º 1
0
 def test_as_dict(self):
     student = Student(record_id=PrimaryKey(123),
                       first_name='first',
                       last_name='last',
                       birthday_date='2020-03-03')
     assert student.as_dict() == {
         'record_id': 123,
         'first_name': 'first',
         'last_name': 'last',
         'birthday_date': '2020-03-03',
     }
Exemplo n.º 2
0
    def test_from_dict(self):
        as_dict = {
            'record_id': '123',
            'first_name': 'first',
            'last_name': 'last',
            'birthday_date': '2020-03-03',
        }
        student = Student.from_dict(as_dict)

        assert student == Student(record_id=PrimaryKey(123),
                                  first_name='first',
                                  last_name='last',
                                  birthday_date='2020-03-03')
Exemplo n.º 3
0
 def test_load_entity(self):
     client = FileDataClient('students_test.txt', Student)
     assert client._load_entity('1,first,last,12-02-2000\n') == Student(
         record_id=PrimaryKey(1),
         first_name='first',
         last_name='last',
         birthday_date='12-02-2000',
     )
Exemplo n.º 4
0
 def test_iter_read(self):
     client = FileDataClient('students_test.txt', Student)
     students = [
         (Student(record_id=PrimaryKey(1),
                  first_name='first',
                  last_name='last',
                  birthday_date='12-02-2000'), 45),
         (Student(record_id=PrimaryKey(3),
                  first_name='dsa',
                  last_name='ddd',
                  birthday_date='12-01-2000'), 69),
         (Student(record_id=PrimaryKey(2),
                  first_name='dssss',
                  last_name='ccc',
                  birthday_date='12-03-2000'), 90),
     ]
     assert [student for student in client.iter_read()] == students
Exemplo n.º 5
0
 def test_read_at_position(self):
     client = FileDataClient('students_test.txt', Student)
     assert client.read_at_position(45) == Student(
         record_id=PrimaryKey(1),
         first_name='first',
         last_name='last',
         birthday_date='12-02-2000',
     )
Exemplo n.º 6
0
    def test_add_student(self):
        file_client = MagicMock()
        dao = FileStudentDataAccess(file_client)

        student = Student(record_id=PrimaryKey(1),
                          first_name='first',
                          last_name='last',
                          birthday_date='2020-03-03')
        dao.add_student(student)

        file_client.write.assert_called_once_with(student)
Exemplo n.º 7
0
    def add_student(self):
        record_id = StudentInputCli.input_record_id()
        first_name = StudentInputCli.input_first_name()
        last_name = StudentInputCli.input_last_name()
        birthday_date = StudentInputCli.input_birthday()

        student = Student(
            record_id=record_id,
            first_name=first_name,
            last_name=last_name,
            birthday_date=birthday_date,
        )
        try:
            self.student_data_access.add_student(student)
            print("Добавлено!\n")
        except DuplicateRecordId:
            print("Студент с таким номером зачетной книжки уже существует!\n")
Exemplo n.º 8
0
    def test_add_student(self, record_id, current_index, position,
                         expected_index, expected_exception):
        file_client = MagicMock()
        file_client.iter_read.return_value = ()
        file_client.write.return_value = position

        dao = IndexFileStudentDataAccess(file_client)
        dao._index_array = current_index

        student = Student(record_id=record_id,
                          first_name='123',
                          last_name='123',
                          birthday_date='123')

        with expected_exception:
            dao.add_student(student)

        assert dao._index_array == expected_index
Exemplo n.º 9
0
class TestIndexFileStudentDataAccess:
    @pytest.mark.parametrize(
        'students, expected_index',
        ((
            [
                (Student(record_id=PrimaryKey(1),
                         first_name='first',
                         last_name='last',
                         birthday_date='2020-03-03'), 11),
                (Student(record_id=PrimaryKey(3),
                         first_name='das',
                         last_name='das',
                         birthday_date='2020-03-03'), 22),
                (Student(record_id=PrimaryKey(8),
                         first_name='cccc',
                         last_name='aaa',
                         birthday_date='2020-03-03'), 33),
            ],
            [
                None, 11, None, 22, None, None, None, None, 33, None, None,
                None, None, None
            ],
        ), ),
    )
    def test_create_index(self, students, expected_index):
        file_client = MagicMock()
        file_client.iter_read.return_value = students

        dao = IndexFileStudentDataAccess(file_client)
        assert dao._index_array == expected_index

    @pytest.mark.parametrize(
        'current_index, record_id, expected_index, expected_exception',
        (
            ([], PrimaryKey(3), [
                None, None, None, None, None, None, None, None
            ], does_not_raise()),
            ([None, 123], PrimaryKey(1), [None, 123
                                          ], pytest.raises(DuplicateRecordId)),
        ),
    )
    def test_validate_record_id(self, current_index, record_id, expected_index,
                                expected_exception):
        file_client = MagicMock()
        file_client.iter_read.return_value = ()

        dao = IndexFileStudentDataAccess(file_client)
        dao._index_array = current_index

        with expected_exception:
            dao._validate_record_id(record_id)
            assert dao._index_array == expected_index

    @pytest.mark.parametrize(
        'key, current_index, expected_result, expected_exception',
        ((PrimaryKey(2), [None, None, 12], 12, does_not_raise()),
         (PrimaryKey(1), [None, None, 12
                          ], None, pytest.raises(RecordNotFound)),
         (PrimaryKey(10), [None, None, 12
                           ], None, pytest.raises(RecordNotFound))),
    )
    def test_get_position_by_key(self, key, current_index, expected_result,
                                 expected_exception):
        file_client = MagicMock()
        file_client.iter_read.return_value = ()

        dao = IndexFileStudentDataAccess(file_client)
        dao._index_array = current_index

        with expected_exception:
            result = dao._get_position_by_key(key)
            assert result == expected_result

    @pytest.mark.parametrize(
        'record_id, current_index, position, expected_index, expected_exception',
        (
            (PrimaryKey(2), [], 123, [None, None, 123, None, None, None
                                      ], does_not_raise()),
            (PrimaryKey(1), [None, 222], 123, [None, 222],
             pytest.raises(DuplicateRecordId)),
        ),
    )
    def test_add_student(self, record_id, current_index, position,
                         expected_index, expected_exception):
        file_client = MagicMock()
        file_client.iter_read.return_value = ()
        file_client.write.return_value = position

        dao = IndexFileStudentDataAccess(file_client)
        dao._index_array = current_index

        student = Student(record_id=record_id,
                          first_name='123',
                          last_name='123',
                          birthday_date='123')

        with expected_exception:
            dao.add_student(student)

        assert dao._index_array == expected_index
Exemplo n.º 10
0
class TestFileStudentDataAccess:
    def test_add_student(self):
        file_client = MagicMock()
        dao = FileStudentDataAccess(file_client)

        student = Student(record_id=PrimaryKey(1),
                          first_name='first',
                          last_name='last',
                          birthday_date='2020-03-03')
        dao.add_student(student)

        file_client.write.assert_called_once_with(student)

    @pytest.mark.parametrize(
        'students, record_id_to_find, expected_result, expected_exception',
        (
            (
                [
                    (Student(record_id=PrimaryKey(1),
                             first_name='first',
                             last_name='last',
                             birthday_date='2020-03-03'), 0),
                    (Student(record_id=PrimaryKey(2),
                             first_name='das',
                             last_name='das',
                             birthday_date='2020-03-03'), 1),
                    (Student(record_id=PrimaryKey(3),
                             first_name='cccc',
                             last_name='aaa',
                             birthday_date='2020-03-03'), 2),
                ],
                PrimaryKey(2),
                Student(record_id=PrimaryKey(2),
                        first_name='das',
                        last_name='das',
                        birthday_date='2020-03-03'),
                does_not_raise(),
            ),
            (
                [
                    (Student(record_id=PrimaryKey(1),
                             first_name='first',
                             last_name='last',
                             birthday_date='2020-03-03'), 0),
                    (Student(record_id=PrimaryKey(2),
                             first_name='das',
                             last_name='das',
                             birthday_date='2020-03-03'), 1),
                ],
                PrimaryKey(3),
                None,
                pytest.raises(RecordNotFound),
            ),
        ),
    )
    def test_get_student(self, students, record_id_to_find, expected_result,
                         expected_exception):
        file_client = MagicMock()
        file_client.iter_read.return_value = students

        dao = FileStudentDataAccess(file_client)

        with expected_exception:
            result = dao.get_student(record_id_to_find)
            assert result == expected_result