コード例 #1
0
    def test_load_config(self):
        main = MainFrame()

        save_config({'probability_plural_noun': 0})
        loader = ConfigLoader()

        self.assertNotEqual(loader.state, main.get_state())
        main.load_config()
        self.assertEqual(loader.state, main.get_state())
コード例 #2
0
    def test_load_config_from_file_empty_string_does_nothing(
            self, mock_filename):
        main = MainFrame()
        save_config({'probability_plural_noun': 0.0})
        main.load_config()

        current_state = main.get_state()

        mock_filename.return_value = ''
        main.load_config_from_file()
        self.assertEqual(current_state, main.get_state())
コード例 #3
0
    def test_change_file_bad_file_by_config(self, mock_error):
        bad_file = os.path.join(DATA_PATH, 'go_time.ico')
        main = MainFrame()
        for count, key in enumerate(
            ('countable_nouns', 'uncountable_nouns', 'verbs')):
            self.assertEqual(mock_error.call_count, count)
            main.revert_to_original()
            save_config({key: bad_file})

            main.load_config()
            message = (
                'LoaderError: Could not read CSV file. ' +
                'If you edited it in MSWord or something similar, it got formatted. Use "notepad"'
            )
            mock_error.assert_called_with('Bad file', message)
            self.assertEqual(mock_error.call_count, count + 1)
コード例 #4
0
    def test_create_texts_creates_files(self):
        prefix = 'adjjk409dvc'
        main = MainFrame()

        save_config({'file_prefix': prefix})
        main.load_config()

        main.do_not_show_popup.set(1)
        main.create_texts()
        self.assertTrue(
            os.path.exists(
                os.path.join(APP_FOLDER, DEFAULT_SAVE_DIR,
                             prefix + '01_answer.pdf')))
        self.assertTrue(
            os.path.exists(
                os.path.join(APP_FOLDER, DEFAULT_SAVE_DIR,
                             prefix + '01_error.pdf')))
コード例 #5
0
    def test_load_config_error_reverts(self, mock_error):
        main = MainFrame()
        default_state = main.get_state()

        save_config({'probability_plural_noun': 0.0})
        main.load_config()

        self.assertNotEqual(main.get_state(), default_state)

        save_config({'probability_plural_noun': 'oops'})

        main.load_config()
        mock_error.assert_called_with(
            'bad config',
            "ConfigFileError: Tried to set key: 'probability_plural_noun' to incompatible value: 'oops'."
        )

        self.assertEqual(main.get_state(), default_state)
コード例 #6
0
    def test_export_config_file_saves_correctly(self, mock_filename):
        filename = os.path.join(TESTS_FILES, 'tst.cfg')

        if os.path.exists(filename):
            os.remove(filename)

        main = MainFrame()
        default_state = main.get_state()
        save_config({'font_size': 15})
        main.load_config()
        current_state = main.get_state()

        ConfigLoader().revert_to_default()

        config_state = ConfigLoader().state
        expected_state = config_state.copy()
        expected_state['font_size'] = 15

        # test current state
        self.assertEqual(default_state, config_state)
        self.assertEqual(current_state, expected_state)
        self.assertNotEqual(default_state, current_state)

        mock_filename.return_value = filename
        main.export_config_file()

        saved_config = ConfigLoader()
        saved_config.set_state_from_file(filename)
        config_file_state = ConfigLoader().state

        self.assertEqual(saved_config.state, current_state)
        self.assertEqual(main.get_state(), current_state)
        self.assertEqual(config_file_state, default_state)

        mock_filename.assert_called_with(
            initialdir=current_state['home_directory'],
            title='select .cfg file',
            initialfile='exported_config.cfg',
            defaultextension='.cfg')
        os.remove(filename)
コード例 #7
0
    def test_load_config_from_file_bad_file_without_reset(
            self, mock_filename, mock_error):
        filename = os.path.join(TESTS_FILES, 'tst.cfg')
        bad_path = os.path.join(TESTS_FILES, 'nope', 'really_nope')

        mock_filename.return_value = filename

        save_config_to_filename({'save_directory': bad_path}, filename)

        main = MainFrame()
        save_config({'probability_plural_noun': 0.0})
        main.load_config()
        current_state = main.get_state()

        main.load_config_from_file()

        self.assertEqual(main.get_state(), current_state)
        self.assertEqual(mock_error.call_args[0][0], 'bad config file')
        msg = "ConfigFileError: Config Loader failed to create the following directory:\n"
        self.assertIn(msg, mock_error.call_args[0][1])

        os.remove(filename)
コード例 #8
0
    def test_load_config_from_file_bad_file_with_reset(self, mock_filename,
                                                       mock_error):
        filename = os.path.join(TESTS_FILES, 'tst.cfg')

        mock_filename.return_value = filename

        save_config_to_filename({'probability_plural_noun': 'oops'}, filename)

        main = MainFrame()
        default_state = main.get_state()
        save_config({'probability_plural_noun': 0.0})
        main.load_config()

        main.load_config_from_file()

        self.assertEqual(main.get_state(), default_state)
        mock_error.assert_called_with(
            'bad config file',
            "ConfigFileError: Tried to set key: 'probability_plural_noun' to incompatible value: 'oops'."
        )

        os.remove(filename)