def test_ConfigLoader_init_whole_state(self): answer = ConfigLoader().state home = os.path.join(get_documents_folder(), APP_NAME) expected = { 'home_directory': home, 'save_directory': os.path.join(home, DEFAULT_SAVE_DIR), 'countable_nouns': os.path.join(home, COUNTABLE_NOUNS_CSV), 'uncountable_nouns': os.path.join(home, UNCOUNTABLE_NOUNS_CSV), 'proper_nouns': os.path.join(home, PROPER_NOUNS_CSV), 'verbs': os.path.join(home, VERBS_CSV), 'error_probability': 0.2, 'noun_errors': True, 'pronoun_errors': False, 'verb_errors': True, 'punctuation_errors': True, 'is_do_errors': False, 'preposition_transpose_errors': False, 'tense': 'simple_present', 'probability_plural_noun': 0.3, 'probability_negative_verb': 0.3, 'probability_pronoun': 0.2, 'paragraph_type': 'chain', 'subject_pool': 5, 'num_paragraphs': 4, 'paragraph_size': 15, 'font_size': 13, 'file_prefix': '' } self.assertEqual(answer, expected)
def test_ConfigLoader_revert_to_default_creates_files_and_directories( self): home_to_delete = os.path.abspath('delete_me') save_config({'home_directory': home_to_delete}) new = ConfigLoader() home = os.path.join(get_documents_folder(), APP_NAME) self.assertFalse(os.path.exists(home)) for filename in [ VERBS_CSV, COUNTABLE_NOUNS_CSV, UNCOUNTABLE_NOUNS_CSV ]: self.assertTrue( os.path.exists(os.path.join(home_to_delete, filename))) new.revert_to_default() for filename in [ VERBS_CSV, COUNTABLE_NOUNS_CSV, UNCOUNTABLE_NOUNS_CSV ]: self.assertTrue( os.path.exists(os.path.join(home_to_delete, filename))) self.assert_default_ConfigLoader_state(new) rmtree(home_to_delete)
def test_ConfigLoader_reload_home_directory_change_NEEDS_TO_UPDATE_FULL_CONFIG( self): new = ConfigLoader() new_home = os.path.abspath('delete_me') old_home = os.path.join(get_documents_folder(), APP_NAME) save_config({'home_directory': new_home}) new.reload() to_check = { 'countable_nouns': COUNTABLE_NOUNS_CSV, 'uncountable_nouns': UNCOUNTABLE_NOUNS_CSV, 'verbs': VERBS_CSV } for key, filename in to_check.items(): self.assertEqual(new.state[key], os.path.join(new_home, filename)) with open(os.path.join(DATA_PATH, filename), 'r') as default: default_text = default.read() with open(os.path.join(old_home, filename), 'r') as old_file: self.assertEqual(old_file.read(), default_text) with open(os.path.join(new_home, filename), 'r') as new_file: self.assertEqual(new_file.read(), default_text) rmtree(new_home)
def test_get_documents_folder(self): user_dir = os.path.expanduser('~') docs = os.path.join(user_dir, 'Documents') my_docs = os.path.join(user_dir, 'My Documents') answer = get_documents_folder() self.assertIn(answer, [user_dir, docs, my_docs]) self.assertTrue(os.path.exists(answer))
def assert_default_ConfigLoader_state(self, config_loader): with open(DEFAULT_CONFIG, 'r') as default: with open(CONFIG_FILE, 'r') as target: self.assertEqual(default.read(), target.read()) home_dir = os.path.join(get_documents_folder(), APP_NAME) save_dir = os.path.join(home_dir, DEFAULT_SAVE_DIR) self.assert_ConfigLoader_state(config_loader, home_dir, save_dir)
def test_ConfigLoader_set_up_frame_FileManagement(self): fm = FileManagement() loader = ConfigLoader() loader.set_up_frame(fm) home = os.path.join(get_documents_folder(), APP_NAME) answer = { 'home_directory': home, 'save_directory': os.path.join(home, DEFAULT_SAVE_DIR), 'countable_nouns': os.path.join(home, COUNTABLE_NOUNS_CSV), 'uncountable_nouns': os.path.join(home, UNCOUNTABLE_NOUNS_CSV), 'proper_nouns': os.path.join(home, PROPER_NOUNS_CSV), 'verbs': os.path.join(home, VERBS_CSV) } self.assertEqual(fm.get_values(), answer)
def test_ConfigLoader_init_no_config_file_has_home_dir_and_some_files( self): app_folder = os.path.join(get_documents_folder(), APP_NAME) os.mkdir(app_folder) with open(os.path.join(app_folder, VERBS_CSV), 'w') as f: f.write('hi there') new = ConfigLoader() save = os.path.join(app_folder, DEFAULT_SAVE_DIR) self.assert_ConfigLoader_state(new, app_folder, save, VERBS_CSV) with open(os.path.join(app_folder, VERBS_CSV), 'r') as f: self.assertEqual(f.read(), 'hi there') self.assertEqual(new.state['verbs'], os.path.join(app_folder, VERBS_CSV))
def test_ConfigLoader_reload_home_directory_change(self): new = ConfigLoader() new_home = os.path.abspath('delete_me') old_home = os.path.join(get_documents_folder(), APP_NAME) full_config = new.state full_config.update({'home_directory': new_home}) self.assertNotEqual(full_config, new.state) save_config(full_config) new.reload() self.assertEqual(full_config, new.state) for filename in [ COUNTABLE_NOUNS_CSV, UNCOUNTABLE_NOUNS_CSV, VERBS_CSV ]: with open(os.path.join(DATA_PATH, filename), 'r') as default: with open(os.path.join(old_home, filename), 'r') as target: self.assertEqual(default.read(), target.read()) # new_home should be empty. If this raises an error, there's something very wrong. os.rmdir(new_home)
def test_ConfigLoader_revert_to_default_resets_csvs_but_leaves_other_files( self): new = ConfigLoader() home = os.path.join(get_documents_folder(), APP_NAME) save = os.path.join(home, DEFAULT_SAVE_DIR) home_files = [VERBS_CSV, COUNTABLE_NOUNS_CSV, 'foo.txt', 'bar.txt'] save_files = ['foo.txt', 'bar.txt'] write_files = [os.path.join(home, filename) for filename in home_files] write_files += [ os.path.join(save, filename) for filename in save_files ] files_not_reset = write_files[2:] for filename in write_files: with open(filename, 'w') as f: f.write('foobar') new.revert_to_default() self.assert_default_ConfigLoader_state(new) for filename in files_not_reset: with open(filename, 'r') as f: self.assertEqual(f.read(), 'foobar')
VERBS_CSV, DEFAULT_SAVE_DIR, ConfigLoader, get_documents_folder, save_config, save_config_to_filename) from sentences.gui.filemanagement import FileManagement from sentences.gui.actions import Actions from sentences.gui.gui_tools import all_children from sentences.guimain import MainFrame, catch_errors from sentences.words.verb import Verb from sentences.words.noun import Noun SAVE_CONFIG = os.path.join(TESTS_FILES, 'save.cfg') SAVE_APP_FOLDER = os.path.join(TESTS_FILES, 'saved_app') APP_FOLDER = os.path.join(get_documents_folder(), APP_NAME) def rm_config(): if os.path.exists(CONFIG_FILE): os.remove(CONFIG_FILE) def rm_app_folder(): if os.path.exists(APP_FOLDER): shutil.rmtree(APP_FOLDER) def mv_app_folder(): src_target = APP_FOLDER if os.path.exists(src_target):
def restore_app_folder(): dst_target = os.path.join(get_documents_folder(), APP_NAME) if os.path.exists(SAVE_APP_FOLDER): copytree(SAVE_APP_FOLDER, dst_target) rmtree(SAVE_APP_FOLDER)
def test_ConfigLoader_init_creates_empty_csv_in_home_dir(self): ConfigLoader() with open(os.path.join(get_documents_folder(), APP_NAME, 'EMPTY.csv'), 'r') as f: self.assertEqual(f.read(), '')
def mv_app_folder(): src_target = os.path.join(get_documents_folder(), APP_NAME) if os.path.exists(src_target): copytree(src_target, SAVE_APP_FOLDER)
def rm_app_folder(): target = os.path.join(get_documents_folder(), APP_NAME) if os.path.exists(target): rmtree(target)