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 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 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) os.system('ltk add sample*.txt -o') # Let the command line handle parsing the file pattern 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): #delete files added to lingotek cloud for curr_file in self.files: self.action.rm_action(curr_file, force=True) #delete downloaded translations for df in self.downloaded: os.remove(df) delete_directory("es_AR") self.downloaded = [] self.action.clean_action(True, False, None) self.action.close() cleanup() def test_push_1(self): append_file(self.files[0]) locales = ['es_AR'] test_doc_id = self.action.doc_manager.get_doc_by_prop('file_name',self.files[0])['id'] self.action.target_action(self.files[0], 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 in the cloud downloaded_path = self.action.download_action(test_doc_id, locales[0], False) #print("downloaded_path: "+str(downloaded_path)) self.downloaded.append(downloaded_path) with open(downloaded_path, 'r') as f: downloaded_text = f.read() #print ("Downloaded_text: " + downloaded) assert "Texto agregado." in downloaded_text assert "Este es un ejemplo de archivo de texto." in downloaded_text def test_push_mult(self): append_file(self.files[0]) append_file(self.files[1]) locales = ['es_AR'] test_doc_id_0 = self.action.doc_manager.get_doc_by_prop('file_name',self.files[0])['id'] test_doc_id_1 = self.action.doc_manager.get_doc_by_prop('file_name',self.files[1])['id'] self.action.target_action(self.files[0], None, locales, False, None, None, test_doc_id_0) self.action.target_action(self.files[1], None, locales, False, None, None, test_doc_id_1) self.action.push_action() assert check_updated_ids(self.action, [test_doc_id_0, test_doc_id_1]) # Poll and wait until the modification has taken effect on the cloud dl_path_0 = self.action.download_action(test_doc_id_0, locales[0], False) dl_path_1 = self.action.download_action(test_doc_id_1, locales[0], False) self.downloaded = [dl_path_0, dl_path_1] for path in self.downloaded: with open(path, 'r') as f: downloaded_text = f.read() #print("downloaded_text: "+downloaded_text) assert "Texto agregado." in downloaded_text assert "Este es un ejemplo de archivo de texto." in downloaded_text def test_push_none(self): try: # out = StringIO() # sys.stdout = out assert not self.action.push_action() # info = out.getvalue() # assert 'All documents up-to-date with Lingotek Cloud.' in info finally: sys.stdout = sys.__stdout__
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 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__