コード例 #1
0
ファイル: test_json.py プロジェクト: toonarmycaptain/dionysus
    def test_update_class(self, empty_json_database, test_full_class):
        test_json_database = empty_json_database
        test_class = Class(test_full_class.name, test_full_class.students)
        # create_class takes a NewClass object due to avatar moving machinery.
        test_class_new_class = NewClass(test_full_class.name,
                                        test_full_class.students)
        assert test_class.json_dict() == test_class_new_class.json_dict(
        )  # Ensure classes are the same.

        # Create class in database.
        test_json_database.create_class(
            NewClass(test_full_class.name, test_full_class.students))
        # Ensure test_class in database
        test_class.id = test_class.name

        # Loaded class will have ids:
        test_loaded_class = test_full_class.json_dict()
        for student in test_loaded_class['students']:
            student['id'] = student['name']
        assert test_json_database.load_class(
            test_class.name).json_dict() == test_loaded_class

        # Change class by adding student, update database:
        new_student = Student(name='new student')
        assert new_student not in test_class and new_student.name not in test_class  # Confirm student not in class.
        test_class.add_student(new_student)

        assert test_json_database.update_class(test_class) is None
        # Look up name because new_student object itself is not in the loaded class object.
        assert new_student.name in test_json_database.load_class(
            test_class.name)
コード例 #2
0
    def test_compose_classlist_dialogue_full_class(self, monkeypatch,
                                                   test_full_class):
        def mocked_take_class_data_input(class_name):
            return test_full_class

        def mocked_blank_class_dialogue():
            raise ValueError  # Should not be called.

        monkeypatch.setattr(class_functions, 'take_class_data_input',
                            mocked_take_class_data_input)
        monkeypatch.setattr(class_functions, 'blank_class_dialogue',
                            mocked_blank_class_dialogue)

        assert compose_classlist_dialogue(
            test_full_class.name).json_dict() == test_full_class.json_dict()
コード例 #3
0
ファイル: test_json.py プロジェクト: toonarmycaptain/dionysus
    def test_load_class(self, monkeypatch, empty_json_database,
                        test_full_class):
        test_json_database = empty_json_database
        test_class_name = 'my_test_class'

        def mock_from_file(class_path):
            assert class_path == Path(
                test_json_database.class_data_path, test_class_name,
                f'{test_class_name}{test_json_database.class_data_file_type}')
            return Class.from_dict(test_full_class.json_dict())

        monkeypatch.setattr(json.Class, 'from_file', mock_from_file)

        # Loaded class will have ids:
        test_loaded_class = test_full_class.json_dict()
        for student in test_loaded_class['students']:
            student['id'] = student['name']
        assert test_json_database.load_class(
            test_class_name).json_dict() == test_loaded_class
コード例 #4
0
    def test_save_chart_image(self, request, database_backend, test_full_class,
                              tmpdir):
        """
        Verify API works.

        NB No db verification in API test, as API for verifying does not exist.
        TODO: Verify db saved chart contents when load/edit features added.
        """
        test_database = request.getfixturevalue(database_backend)

        test_existing_class = NewClass.from_dict(test_full_class.json_dict())
        for student in test_existing_class:
            if student.avatar_id:
                Path(test_existing_class.temp_avatars_dir,
                     student.avatar_id).write_text(student.avatar_id)
        # Create class in db:
        test_database.create_class(test_existing_class)

        # Find class id to load:
        classes = test_database.get_classes()
        test_class_id = classes[0].id

        # test_class = Class.from_dict(test_full_class_data_set['json_dict_rep'])
        test_class = test_database.load_class(test_class_id)

        test_data_dict = {
            'class_id': test_class_id,
            'class_name': "test_class_name",
            'chart_name': "test_chart_name",
            'chart_default_filename': "test_chart_default_filename",
            'chart_params': {
                "some": "chart",
                "default": "params"
            },
            'score-students_dict': {
                0: [test_class.students[0]],  # Cali
                1: [
                    test_class.students[1],  # Monty
                    test_class.students[7]
                ],  # Regina
                3: [
                    test_class.students[2],  # Abby
                    test_class.students[9]
                ],  # Alex
                # No score, not returned: None: [test_class.students[3],  # Zach
                #                                test_class.students[11]],  # Edgar
                50: [test_class.students[4]],  # Janell
                99: [test_class.students[5]],  # Matthew
                100: [test_class.students[6]],  # Olivia
                2: [test_class.students[8]],  # Ashley
                4: [test_class.students[10]],  # Melissa
                6: [test_class.students[12]],  # Danielle
                7: [test_class.students[13]],  # Kayla
                8: [test_class.students[14]],  # Jaleigh
            },
        }
        # Create chart in db:
        test_database.create_chart(test_data_dict)

        mock_plt = plt.figure(figsize=(19.20, 10.80))

        test_image = io.BytesIO()
        # Images must both be saved as '.png' for comparison.
        test_image_path = Path(tmpdir, 'test image.png')
        mock_plt.savefig(test_image_path, format='png', dpi=300)
        test_image.seek(0)  # Return pointer to start of binary stream.

        save_chart_path = test_database.save_chart_image(
            test_data_dict, mock_plt)
        # Return pointer:
        test_image.seek(0)  # Return pointer to start of binary stream.
        # Path exists ad image at path is expected data:
        assert save_chart_path.exists()

        compare_images(save_chart_path, test_image_path, 0.0001)
コード例 #5
0
ファイル: test_json.py プロジェクト: toonarmycaptain/dionysus
 def mock_from_file(class_path):
     assert class_path == Path(
         test_json_database.class_data_path, test_class_name,
         f'{test_class_name}{test_json_database.class_data_file_type}')
     return Class.from_dict(test_full_class.json_dict())
コード例 #6
0
    def test_save_chart_image(self, empty_sqlite_database, test_full_class):
        test_database = empty_sqlite_database

        test_existing_class = NewClass.from_dict(test_full_class.json_dict())
        for student in test_existing_class:
            if student.avatar_id:
                Path(test_existing_class.temp_avatars_dir,
                     student.avatar_id).write_text(student.avatar_id)
        # Create class in db:
        test_database.create_class(test_existing_class)

        # Find class id to load:
        classes = test_database.get_classes()
        test_class_id = classes[0].id

        # test_class = Class.from_dict(test_full_class_data_set['json_dict_rep'])
        test_class = test_database.load_class(test_class_id)

        test_data_dict = {
            'class_id': test_class_id,
            'class_name': "test_class_name",
            'chart_name': "test_chart_name",
            'chart_default_filename': "test_chart_default_filename",
            'chart_params': {
                "some": "chart",
                "default": "params"
            },
            'score-students_dict': {
                0: [test_class.students[0]],  # Cali
                1: [
                    test_class.students[1],  # Monty
                    test_class.students[7]
                ],  # Regina
                3: [
                    test_class.students[2],  # Abby
                    test_class.students[9]
                ],  # Alex
                # No score, not returned: None: [test_class.students[3],  # Zach
                #                                test_class.students[11]],  # Edgar
                50: [test_class.students[4]],  # Janell
                99: [test_class.students[5]],  # Matthew
                100: [test_class.students[6]],  # Olivia
                2: [test_class.students[8]],  # Ashley
                4: [test_class.students[10]],  # Melissa
                6: [test_class.students[12]],  # Danielle
                7: [test_class.students[13]],  # Kayla
                8: [test_class.students[14]],  # Jaleigh
            },
        }
        # Create chart in db:
        test_database.create_chart(test_data_dict)

        mock_plt = plt.figure(figsize=(19.20, 10.80))

        test_image = io.BytesIO()
        mock_plt.savefig(test_image, format='png', dpi=300)
        test_image.seek(0)  # Return pointer to start of binary stream.

        save_chart_path = test_database.save_chart_image(
            test_data_dict, mock_plt)
        # Return pointer:
        test_image.seek(0)  # Return pointer to start of binary stream.
        # Path exists ad image at path is expected data:
        assert save_chart_path.exists()
        assert save_chart_path.read_bytes() == test_image.read1(
        )  # size arg can be omitted on 3.7+