class TestPush(unittest.TestCase): def setUp(self): create_config() self.downloaded = [] self.action = Action(os.getcwd()) self.action.clean_action(True, False, None) self.files = ['sample.txt', 'sample1.txt', 'sample2.txt'] for fn in self.files: create_txt_file(fn) self.action.add_action(None, ['sample*.txt'], force=True) self.doc_ids = self.action.doc_manager.get_doc_ids() for doc_id in self.doc_ids: assert poll_doc(self.action, doc_id) def tearDown(self): for curr_file in self.files: self.action.rm_action(curr_file, force=True) for df in self.downloaded: os.remove(df) self.action.clean_action(True, False, None) self.action.close() cleanup() def test_push_1(self): append_file(self.files[0]) locales = ['ja_JP'] test_doc_id = self.action.doc_manager.get_doc_by_prop('file_name',self.files[0])['id'] self.action.target_action(None, locales, False, None, None, test_doc_id) with open(self.files[0]) as f: downloaded = f.read() self.action.push_action() assert check_updated_ids(self.action, [test_doc_id]) # Poll and wait until the modification has taken effect on the cloud downloaded_path = self.action.download_action(test_doc_id, None, False) self.downloaded.append(downloaded_path) with open(downloaded_path, 'r') as f: downloaded = f.read() print (downloaded) assert "Appended text. " in downloaded assert "This is a sample text file. " in downloaded def test_push_mult(self): append_file(self.files[0]) append_file(self.files[1]) doc_id0 = self.action.doc_manager.get_doc_by_prop('file_name',self.files[0])['id'] doc_id1 = self.action.doc_manager.get_doc_by_prop('file_name',self.files[1])['id'] locales = ['ja_JP'] self.action.target_action(None, locales, False, None, None, doc_id0) self.action.target_action(None, locales, False, None, None, doc_id1) self.action.push_action() assert check_updated_ids(self.action, [doc_id0, doc_id1]) # Poll and wait until the modification has taken effect on the cloud dl_path = self.action.download_action(doc_id0, None, False) dl_path1 = self.action.download_action(doc_id1, None, False) self.downloaded = [dl_path, dl_path1] for path in self.downloaded: with open(path, 'r') as f: downloaded = f.read() print (downloaded) assert "Appended text. " in downloaded assert "This is a sample text file. " in downloaded def test_push_none(self): try: out = StringIO() sys.stdout = out self.action.push_action() info = out.getvalue() assert 'All documents up-to-date with Lingotek Cloud.' in info finally: sys.stdout = sys.__stdout__
class TestDownload(unittest.TestCase): @classmethod def setUpClass(cls): create_config() @classmethod def tearDownClass(cls): cleanup() def setUp(self): self.downloaded_files = [] self.action = Action(os.getcwd()) self.files = ['sample.txt', 'sample1.txt'] self.locales = ['ja_JP', 'zh_CN'] self.first_doc = 'sample.txt' for fn in self.files: create_txt_file(fn) self.action.add_action(None, ['sample*.txt'], force=True) self.doc_ids = self.action.doc_manager.get_doc_ids() for doc_id in self.doc_ids: assert poll_doc(self.action, doc_id) self.action.target_action(None, self.locales, False, None, None) def tearDown(self): for curr_file in self.files: self.action.rm_action(curr_file, True) self.action.clean_action(False, False, None) for dl_file in self.downloaded_files: os.remove(dl_file) def get_dl_path(self, locale, document): name_parts = document.split('.') if len(name_parts) > 1: name_parts.insert(-1, locale) downloaded_name = '.'.join(part for part in name_parts) else: downloaded_name = name_parts[0] + '.' + locale dl_path = os.path.join(self.action.path, downloaded_name) return dl_path def test_download_name(self): self.action.download_by_name(self.first_doc, self.locales[0], False) dl_file = self.get_dl_path(self.locales[0], self.first_doc) assert self.locales[0] in dl_file assert os.path.isfile(dl_file) self.downloaded_files.append(dl_file) def test_pull_all(self): for document in self.files: for locale in self.locales: dl_file = self.get_dl_path(locale, document) self.downloaded_files.append(dl_file) self.action.pull_action(None, False) for path in self.downloaded_files: assert os.path.isfile(path) def test_pull_locale(self): for document in self.files: dl_file = self.get_dl_path(self.locales[0], document) self.downloaded_files.append(dl_file) self.action.pull_action(self.locales[0], False) for path in self.downloaded_files: assert os.path.isfile(path)
class TestAdd(unittest.TestCase): @classmethod def setUpClass(cls): create_config() @classmethod def tearDownClass(cls): cleanup() def setUp(self): self.action = Action(os.getcwd()) self.action.clean_action(False, False, None) self.added_files = [] self.added_directories = [] def tearDown(self): for fn in self.added_files: self.action.rm_action(fn, force=True) for d in self.added_directories: self.action.rm_action(d, force=True) delete_directory(d) self.action.clean_action(False, False, None) self.action.close() def test_add_db(self): # check that document is added to db file_name = 'sample.txt' self.added_files.append(file_name) create_txt_file(file_name) self.action.add_action([file_name], force=True) doc_id = self.action.doc_manager.get_doc_ids()[0] poll_doc(self.action, doc_id) assert self.action.doc_manager.get_doc_by_prop('name', file_name) delete_file(file_name) def test_add_remote(self): # check that document is added to Lingotek file_name = 'sample.txt' self.added_files.append(file_name) create_txt_file(file_name) self.action.add_action([file_name], force=True) doc_id = self.action.doc_manager.get_doc_ids()[0] assert poll_doc(self.action, doc_id) def test_add_pattern_db(self): # test that adding with a pattern gets all expected matches in local db files = ['sample.txt', 'sample1.txt', 'sample2.txt'] self.added_files = files for fn in files: create_txt_file(fn) # self.action.add_action(['sample*.txt']) os.system('ltk add sample*.txt') # Let the command line handle parsing the file pattern for fn in files: doc = self.action.doc_manager.get_doc_by_prop('name', fn) assert doc assert poll_doc(self.action, doc['id']) def test_add_pattern_remote(self): # test that adding with a pattern gets all expected matches in Lingotek files = ['sample.txt', 'sample1.txt', 'sample2.txt'] self.added_files = files for fn in files: create_txt_file(fn) # self.action.add_action(['sample*.txt']) os.system('ltk add sample*.txt') # Let the command line handle parsing the file pattern doc_ids = self.action.doc_manager.get_doc_ids() for doc_id in doc_ids: assert poll_doc(self.action, doc_id) ''' Test that a directory, and documents inside directory, are added to the db ''' def test_add_directory(self): #test add an empty directory directory = 'test_add_empty_directory' dir_path = os.path.join(os.getcwd(), directory) self.added_directories.append(dir_path) create_directory(dir_path) self.action.add_action([dir_path], force=True) assert self.action._is_folder_added(dir_path) delete_directory(dir_path) #test add a directory with documents inside directory = 'test_add_full_directory' dir_path = os.path.join(os.getcwd(), directory) self.added_directories.append(dir_path) create_directory(dir_path) files = ['sample.txt', 'sample1.txt', 'sample2.txt'] self.added_files = files for fn in files: create_txt_file(fn, dir_path) self.action.add_action([dir_path], force=True) assert self.action._is_folder_added(dir_path) for fn in files: assert self.action.doc_manager.get_doc_by_prop('name', fn) #delete the files and directories created for fn in files: delete_file(fn, dir_path) delete_directory(dir_path) ''' Test adding a directory with a document inside gets added to Lingotek ''' def test_add_remote_directory(self): directory = 'test_add_full_directory' dir_path = os.path.join(os.getcwd(), directory) file_name = 'file_inside_directory.txt' self.added_directories.append(dir_path) create_directory(dir_path) self.added_files.append(file_name) create_txt_file(file_name, dir_path) self.action.add_action([dir_path], force=True) doc_id = self.action.doc_manager.get_doc_ids()[0] assert poll_doc(self.action, doc_id)
class TestRequest(unittest.TestCase): @classmethod def setUpClass(cls): create_config() @classmethod def tearDownClass(cls): cleanup() def setUp(self): self.action = Action(os.getcwd()) self.action.clean_action(True, False, None) self.files = ['sample.txt', 'sample1.txt', 'sample2.txt'] self.first_doc = 'sample.txt' for fn in self.files: create_txt_file(fn) self.action.add_action(None, ['sample*.txt'], force=True) self.doc_ids = self.action.doc_manager.get_doc_ids() for doc_id in self.doc_ids: assert poll_doc(self.action, doc_id) def tearDown(self): for curr_file in self.files: self.action.rm_action(curr_file, force=True) self.action.clean_action(True, False, None) self.action.close() def check_locales_exist(self, documents, locales): for document in documents: curr_doc = self.action.doc_manager.get_doc_by_prop('name', document) return all(locale in curr_doc['locales'] for locale in locales) def test_request_one_locale_doc(self): locales = ['ja_JP'] self.action.target_action(self.first_doc, locales, False, None, None) assert self.check_locales_exist([self.first_doc], locales) def test_request_mult_locale_doc(self): locales = ['ja_JP', 'zh_CN', 'es_MX'] self.action.target_action(self.first_doc, locales, False, None, None) assert self.check_locales_exist([self.first_doc], locales) def test_request_one_locale_proj(self): locales = ['ja_JP'] self.action.target_action(None, locales, False, None, None) assert self.check_locales_exist(self.files, locales) def test_request_mult_locale_proj(self): locales = ['ja_JP', 'zh_CN', 'es_MX'] self.action.target_action(None, locales, False, None, None) assert self.check_locales_exist(self.files, locales) def test_delete_locale_doc(self): locales = ['ja_JP'] self.action.target_action(self.first_doc, locales, False, None, None) assert self.check_locales_exist([self.first_doc], locales) self.action.target_action(self.first_doc, locales, True, None, None) assert not self.check_locales_exist([self.first_doc], locales) def test_delete_locale_proj(self): locales = ['ja_JP'] self.action.target_action(None, locales, False, None, None) assert self.check_locales_exist([self.first_doc], locales) self.action.target_action(None, locales, True, None, None) assert not self.check_locales_exist(self.files, locales)
class TestList(unittest.TestCase): @classmethod def setUpClass(cls): create_config() @classmethod def tearDownClass(cls): cleanup() def setUp(self): self.action = Action(os.getcwd()) self.action.clean_action(True, False, None) def tearDown(self): self.action.clean_action(True, False, None) self.action.close() def test_list_doc(self): files = ["sample.txt", "sample1.txt", "sample2.txt"] file_paths = [] for fn in files: file_paths.append(create_txt_file(fn)) self.action.add_action(["sample*.txt"], overwrite=True) doc_ids = self.action.doc_manager.get_doc_ids() for doc_id in doc_ids: assert poll_doc(self.action, doc_id) try: out = StringIO() sys.stdout = out self.action.list_ids_action(False) info = out.getvalue() for doc_id in doc_ids: assert doc_id in info finally: sys.stdout = sys.__stdout__ for fn in files: self.action.rm_action(fn, force=True) self.action.clean_action(False, False, None) def test_list_no_docs(self): try: out = StringIO() sys.stdout = out self.action.list_ids_action(False) info = out.getvalue() assert "No local documents" in info finally: sys.stdout = sys.__stdout__ def test_list_workflow(self): try: out = StringIO() sys.stdout = out self.action.list_workflow_action() info = out.getvalue() assert "Workflows" in info assert "c675bd20-0688-11e2-892e-0800200c9a66" in info assert "Machine Translation" in info finally: sys.stdout = sys.__stdout__ def test_list_locale(self): try: out = StringIO() sys.stdout = out self.action.list_locale_action() info = out.getvalue() assert "ar_AE (Arabic, United Arab Emirates)" in info assert "zh_TW (Chinese, Taiwan)" in info finally: sys.stdout = sys.__stdout__ def test_list_format(self): try: out = StringIO() sys.stdout = out self.action.list_format_action() info = out.getvalue() assert info.startswith("Lingotek Cloud accepts content") assert "CSV" in info assert "XML_OKAPI" in info finally: sys.stdout = sys.__stdout__ def test_list_filters_default(self): try: out = StringIO() sys.stdout = out self.action.list_filter_action() info = out.getvalue() decoded_info = info assert "Filters:" in info assert "*****@*****.**" in info assert "0e79f34d-f27b-4a0c-880e-cd9181a5d265" in info finally: sys.stdout = sys.__stdout__ def test_list_filters_custom(self): # create custom filters # list # check pass
class TestPush(unittest.TestCase): def setUp(self): create_config() self.downloaded = [] self.action = Action(os.getcwd()) self.files = ['sample.txt', 'sample1.txt', 'sample2.txt'] for fn in self.files: create_txt_file(fn) self.action.add_action(None, ['sample*.txt'], force=True) self.doc_ids = self.action.doc_manager.get_doc_ids() for doc_id in self.doc_ids: assert poll_doc(self.action, doc_id) def tearDown(self): for curr_file in self.files: self.action.rm_action(curr_file, True) for df in self.downloaded: os.remove(df) self.action.clean_action(False, False, None) cleanup() def test_push_1(self): append_file(self.files[0]) locales = ['ja_JP'] self.action.target_action(self.doc_ids[0], locales, False, None, None) with open('sample.txt') as f: downloaded = f.read() print (downloaded) self.action.push_action() # currently sleep for some arbitrary time while document updates in Lingotek # replace when api call or some way to check if update is finished is available print ('pushed') time.sleep(20) # print ('now') downloaded_path = self.action.download_action(self.doc_ids[0], None, False) self.downloaded.append(downloaded_path) with open(downloaded_path, 'r') as f: downloaded = f.read() print (downloaded) assert "Appended text. " in downloaded assert "This is a sample text file. " in downloaded def test_push_mult(self): append_file(self.files[0]) append_file(self.files[1]) locales = ['ja_JP'] self.action.target_action(self.doc_ids[0], locales, False, None, None) self.action.target_action(self.doc_ids[1], locales, False, None, None) self.action.push_action() time.sleep(20) # see test_push_1 comment dl_path = self.action.download_action(self.doc_ids[0], None, False) dl_path1 = self.action.download_action(self.doc_ids[1], None, False) self.downloaded = [dl_path, dl_path1] for path in self.downloaded: with open(path, 'r') as f: downloaded = f.read() print (downloaded) assert "Appended text. " in downloaded assert "This is a sample text file. " in downloaded def test_push_none(self): from io import BytesIO import sys try: out = StringIO() sys.stdout = out self.action.push_action() info = out.getvalue() assert 'All documents up-to-date with Lingotek Cloud.' in info finally: sys.stdout = sys.__stdout__
class TestList(unittest.TestCase): @classmethod def setUpClass(cls): create_config() @classmethod def tearDownClass(cls): cleanup() def setUp(self): self.action = Action(os.getcwd()) def test_list_doc(self): files = ['sample.txt', 'sample1.txt', 'sample2.txt'] file_paths = [] for fn in files: file_paths.append(create_txt_file(fn)) self.action.add_action(None, ['sample*.txt'], force=True) doc_ids = self.action.doc_manager.get_doc_ids() for doc_id in doc_ids: assert poll_doc(self.action, doc_id) try: out = StringIO() sys.stdout = out self.action.list_ids_action() info = out.getvalue() for doc_id in doc_ids: assert doc_id in info finally: sys.stdout = sys.__stdout__ for fn in files: self.action.rm_action(fn, True) self.action.clean_action(False, False, None) def test_list_no_docs(self): try: out = StringIO() sys.stdout = out self.action.list_ids_action() info = out.getvalue() assert 'no documents' in info finally: sys.stdout = sys.__stdout__ def test_list_workflow(self): try: out = StringIO() sys.stdout = out self.action.list_workflow_action() info = out.getvalue() assert 'workflows' in info assert 'c675bd20-0688-11e2-892e-0800200c9a66' in info assert 'Machine Translation' in info finally: sys.stdout = sys.__stdout__ def test_list_locale(self): try: out = StringIO() sys.stdout = out self.action.list_locale_action() info = out.getvalue() assert 'ar_AE (Arabic, United Arab Emirates)' in info assert 'zh_TW (Chinese, Taiwan)' in info finally: sys.stdout = sys.__stdout__ def test_list_format(self): try: out = StringIO() sys.stdout = out self.action.list_format_action() info = out.getvalue() assert info.startswith('Formats Lingotek supports') assert 'CSV' in info assert 'XML_OKAPI' in info finally: sys.stdout = sys.__stdout__ def test_list_filters_default(self): try: out = StringIO() sys.stdout = out self.action.list_filter_action() info = out.getvalue() decoded_info = info assert 'filters:' in info assert '*****@*****.**' in info assert '0adc9a9d-ca67-4217-9525-d5a6af7ba91f' in info finally: sys.stdout = sys.__stdout__ def test_list_filters_custom(self): # create custom filters # list # check pass
class TestAdd(unittest.TestCase): @classmethod def setUpClass(cls): create_config() @classmethod def tearDownClass(cls): cleanup() def setUp(self): self.action = Action(os.getcwd()) self.action.clean_action(False, False, None) self.added_files = [] def tearDown(self): for fn in self.added_files: self.action.rm_action(fn, force=True) self.action.clean_action(False, False, None) self.action.close() def test_add_db(self): # check that document is added to db file_name = 'sample.txt' self.added_files.append(file_name) create_txt_file(file_name) self.action.add_action(None, [file_name], force=True) doc_id = self.action.doc_manager.get_doc_ids()[0] poll_doc(self.action, doc_id) # check that document is added in db assert self.action.doc_manager.get_doc_by_prop('name', file_name) def test_add_remote(self): # check that document is added to Lingotek file_name = 'sample.txt' self.added_files.append(file_name) create_txt_file(file_name) self.action.add_action(None, [file_name], force=True) doc_id = self.action.doc_manager.get_doc_ids()[0] assert poll_doc(self.action, doc_id) def test_add_pattern_db(self): # test that adding with a pattern gets all expected matches in local db files = ['sample.txt', 'sample1.txt', 'sample2.txt'] self.added_files = files for fn in files: create_txt_file(fn) self.action.add_action(None, ['sample*.txt']) for fn in files: doc = self.action.doc_manager.get_doc_by_prop('name', fn) assert doc assert poll_doc(self.action, doc['id']) def test_add_pattern_remote(self): # test that adding with a pattern gets all expected matches in Lingotek files = ['sample.txt', 'sample1.txt', 'sample2.txt'] self.added_files = files for fn in files: create_txt_file(fn) self.action.add_action(None, ['sample*.txt']) doc_ids = self.action.doc_manager.get_doc_ids() for doc_id in doc_ids: assert poll_doc(self.action, doc_id)
class TestClean(unittest.TestCase): @classmethod def setUpClass(cls): create_config() @classmethod def tearDownClass(cls): cleanup() def setUp(self): self.action = Action(os.getcwd()) self.action.clean_action(False, False, None) self.files = ['sample.txt', 'sample1.txt', 'sample2.txt'] self.forced = [] for fn in self.files: create_txt_file(fn) self.action.add_action(None, ['sample*.txt'], force=True) self.entries = self.action.doc_manager.get_all_entries() for entry in self.entries: assert poll_doc(self.action, entry['id']) def tearDown(self): for curr_file in self.files: if curr_file in self.forced: continue self.action.rm_action(curr_file, force=True) self.action.clean_action(True, False, None) self.action.close() def test_clean(self): delete_id = self.entries[0]['id'] r = self.action.api.document_delete(delete_id) self.forced.append(self.entries[0]['file_name']) assert r.status_code == 204 assert self.action.doc_manager.get_doc_by_prop('id', delete_id) self.action.clean_action(False, False, None) assert not self.action.doc_manager.get_doc_by_prop('id', delete_id) def test_clean_force(self): delete_id = self.entries[0]['id'] doc_name = self.action.doc_manager.get_doc_by_prop('id', delete_id)['file_name'] r = self.action.api.document_delete(delete_id) assert r.status_code == 204 assert self.action.doc_manager.get_doc_by_prop('id', delete_id) self.action.clean_action(True, False, None) self.forced.append(self.entries[0]['file_name']) assert not self.action.doc_manager.get_doc_by_prop('id', delete_id) assert not os.path.isfile(os.path.join(self.action.path, doc_name)) def test_disassociate(self): self.action.clean_action(False, True, None) for entry in self.entries: assert not self.action.doc_manager.get_doc_by_prop('id', entry['id']) delete_file(entry['file_name']) self.action.api.document_delete(entry['id']) self.forced.append(entry['file_name']) def test_clean_single(self): delete_id = self.entries[0]['id'] doc_name = self.entries[0]['file_name'] self.action.clean_action(False, False, doc_name) delete_file(doc_name) self.action.api.document_delete(delete_id) self.forced.append(doc_name) assert not self.action.doc_manager.get_doc_by_prop('id', delete_id)