Пример #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_init_button_assignment_load_config(self, mock_filename):
        filename = os.path.join(TESTS_FILES, 'tst.cfg')
        mock_filename.return_value = filename
        save_config_to_filename({'file_prefix': 'silly'}, filename)

        main = MainFrame()
        action_frame = get_action_frame(main)

        default = main.get_state()

        main.load_config_from_file()
        self.assertNotEqual(default, main.get_state())

        action_frame.reload_config.invoke()
        self.assertEqual(default, main.get_state())

        os.remove(filename)
Пример #4
0
    def test_set_config(self):
        main = MainFrame()

        with open(os.path.join(DATA_PATH, CONFIG_FILE), 'w') as f:
            f.write('')
        self.assertRaises(KeyError, ConfigLoader)

        main.set_config()
        self.assertEqual(main.get_state(), ConfigLoader().state)
Пример #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_load_config_from_file_loads_from_file(self, mock_filename):
        filename = os.path.join(TESTS_FILES, 'tst.cfg')

        mock_filename.return_value = filename

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

        main = MainFrame()
        default_state = main.get_state()

        main.load_config_from_file()

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

        loader = ConfigLoader()
        loader.set_state_from_file(filename)
        self.assertEqual(loader.state, main.get_state())

        os.remove(filename)
Пример #7
0
    def test_message_popup_create_texts(self, mock_popup):
        main = MainFrame()
        main.create_texts()
        message = 'Your files are located at:\n{}'.format(
            main.get_state()['save_directory'])
        mock_popup.assert_called_with('success', message,
                                      main.do_not_show_popup)

        main.do_not_show_popup.set(1)
        main.create_texts()
        self.assertEqual(mock_popup.call_count, 1)
Пример #8
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)
Пример #9
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)
Пример #10
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)
Пример #11
0
    def test_init_no_config(self):
        main = MainFrame()
        loader = ConfigLoader()
        self.assertEqual(main.get_state(), loader.state)

        self.assertEqual(main.paragraph_generator._options, loader.state)