class TestWatch(unittest.TestCase): @classmethod def setUpClass(cls): create_config() @classmethod def tearDownClass(cls): cleanup() def setUp(self): self.action = WatchAction(os.getcwd()) self.files = ['sample.txt'] for fn in self.files: create_txt_file(fn) self.downloaded = [] 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) # todo current problem: watchdog does not seem to detect changes in daemon # but not daemonizing watch causes tests to hang.. watch_thread = Thread(target=self.action.watch_action, args=(None, (), None, 5,)) watch_thread.daemon = True watch_thread.start() def tearDown(self): for fn in self.files: self.action.rm_action(fn, True) self.action.clean_action(False, False, None) for fn in self.downloaded: os.remove(fn) def test_watch_new_file(self): file_name = "new_file.txt" added_file = create_txt_file(file_name) # check if watch detected file and added it to db assert self.action.doc_manager.get_doc_by_prop('name', file_name) def test_watch_update(self): append_file(self.files[0]) time.sleep(20) 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() assert "Appended text. " in downloaded
class TestWatch(unittest.TestCase): @classmethod def setUpClass(cls): create_config() @classmethod def tearDownClass(cls): cleanup() def setUp(self): self.action = WatchAction(os.getcwd()) self.action.clean_action(False, False, None) self.downloaded = [] 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) # todo current problem: watchdog does not seem to detect changes in daemon # but not daemonizing watch causes tests to hang.. watch_thread = Thread(target=self.action.watch_action, args=(None, (), None, 5,)) watch_thread.daemon = True watch_thread.start() def tearDown(self): for fn in self.files: self.action.rm_action(fn, force=True) self.action.clean_action(False, False, None) for fn in self.downloaded: os.remove(fn) self.action.close() def test_watch_new_file(self): file_name = "new_file.txt" added_file = create_txt_file(file_name) # self.files.append(file_name) # check if watch detected file and added it to db doc = None time_passed = 0 while doc is None and time_passed < 60: doc = self.action.doc_manager.get_doc_by_prop('name', file_name) time.sleep(1) time_passed += 1 assert doc assert poll_doc()
class TestWatch(unittest.TestCase): @classmethod def setUpClass(cls): create_config() @classmethod def tearDownClass(cls): cleanup() def setUp(self): self.action = WatchAction(os.getcwd(), 1) self.action.clean_action(False, False, None) # self.action.open() self.downloaded = [] self.files = [] self.dir_name = "dir1" create_directory(self.dir_name) self.action.add_action([self.dir_name], force=True) # todo current problem: watchdog does not seem to detect changes in daemon # but not daemonizing watch causes tests to hang.. watch_thread = Thread(target=self.action.watch_action, args=('.', (), None)) watch_thread.daemon = True watch_thread.start() def tearDown(self): #delete files for fn in self.files: self.action.rm_action(fn, force=True) self.action.clean_action(False, False, None) #delete downloads for fn in self.downloaded: os.remove(fn) #delete directory delete_directory(self.dir_name) def test_watch_new_file(self): file_name = "test_watch_sample_0.txt" self.files.append(self.dir_name+"/"+file_name) if os.path.exists(self.dir_name+file_name): delete_file(file_name) create_txt_file(file_name, self.dir_name) # check if watch detected file and added it to db doc = None time_passed = 0 while doc is None and time_passed < 10: doc = self.action.doc_manager.get_doc_by_prop('name', file_name) time.sleep(1) time_passed += 1 assert doc assert poll_doc(self.action, doc['id']) def test_watch_update(self): file_name = "test_watch_sample_1.txt" self.files.append(self.dir_name+'/'+file_name) if os.path.exists(self.dir_name+file_name): delete_file(file_name) create_txt_file(file_name, self.dir_name) doc = None time_passed = 0 while doc is None and time_passed < 10: doc = self.action.doc_manager.get_doc_by_prop('name', file_name) time.sleep(1) time_passed += 1 assert doc assert poll_doc(self.action, doc['id']) append_file(file_name, self.dir_name) #assert check_updated_ids(self.action, [doc['id']]) with open(self.dir_name+'/'+file_name, 'r') as f: downloaded = f.read() assert "Appended text." in downloaded