<commit_before>from tests.test_actions import *
from ltk.watch import WatchAction
from threading import Thread
import unittest
import os

# @unittest.skip("skip testing watch for now")
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()
        print("here")

    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)

    def test_watch_new_file(self):
        file_name = "sample.txt"
        self.files.append(file_name)
        if os.path.exists(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 = "sample.txt"
        self.files.append(file_name)
        if os.path.exists(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:<commit_msg>update watch unit test<commit_after>from tests.test_actions import *
from ltk.watch import WatchAction
from threading import Thread
import unittest
import os

# @unittest.skip("skip testing watch for now")
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()
        print("here")

    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)

    def test_watch_new_file(self):
        file_name = "test_watch_sample_0.txt"
        self.files.append(file_name)
        if os.path.exists("dir1"+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(file_name)
        if os.path.exists("dir1"+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:
{'fraction_non_alphanumeric': 0.10107227958697379, 'fraction_numerical': 0.005559968228752979, 'mean_word_length': 2.362483311081442, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2625943', 'n_tokens_mistral': 1575, 'n_tokens_neox': 1524, 'n_words': 350}