<commit_before>import os
import sys

from nxdrive.tests.common_unit_test import UnitTestCase
from nxdrive.client import LocalClient
from nxdrive.client import RemoteDocumentClient
from nxdrive.tests.common import TEST_WORKSPACE_PATH
from nxdrive.client.common import NotFound
from nose.plugins.skip import SkipTest
from time import sleep
from nxdrive.engine.dao.sqlite import EngineDAO

# TODO NXDRIVE-170: refactor
LastKnownState = None


class TestLocalMoveAndRename(UnitTestCase):

    # Sets up the following local hierarchy:
    # Nuxeo Drive Test Workspace
    #    |-- Original File 1.txt
    #    |-- Original File 2.txt
    #    |-- Original Folder 1
    #    |       |-- Sub-Folder 1.1
    #    |       |-- Sub-Folder 1.2
    #    |       |-- Original File 1.1.txt
    #    |-- Original Folder 2
    #    |       |-- Original File 3.txt
    def setUp(self):
        self.engine_1.start()
        self.wait_sync(wait_for_async=True)
        self.local_client_1.make_file('/', u'Original File 1.txt',
            content=u'Some Content 1'.encode('utf-8'))

        self.local_client_1.make_file('/', u'Original File 2.txt',
            content=u'Some Content 2'.encode('utf-8'))

        self.local_client_1.make_folder(u'/', u'Original Folder 1')
        self.local_client_1.make_folder(
            u'/Original Folder 1', u'Sub-Folder 1.1')
        self.local_client_1.make_folder(
            u'/Original Folder 1', u'Sub-Folder 1.2')
        self.local_client_1.make_file(u'/Original Folder 1',
            u'Original File 1.1.txt',
            content=u'Some Content 1'.encode('utf-8'))  # Same content as OF1

        self.local_client_1.make_folder('/', 'Original Folder 2')
        self.local_client_1.make_file('/Original Folder 2',
            u'Original File 3.txt',
            content=u'Some Content 3'.encode('utf-8'))
        # Increase timeout as noticed it was sometimes insufficient in Jenkins build
        self.wait_sync(timeout=30)

    def test_local_rename_folder_while_creating(self):
        global marker
        local_client = self.local_client_1
        root_local_client = self.local_root_client_1
        remote_client = self.remote_document_client_1
        marker = False
        def update_remote_state(row, info, remote_parent_path=None, versionned=True):
            global marker
            EngineDAO.update_remote_state(self.engine_1._dao, row, info, remote_parent_path, versionned)
            if row.local_name == 'New Folder' and not marker:
                root_local_client.rename(row.local_path, 'Renamed Folder')
                sleep(5)
                marker = True
        self.engine_1._dao.update_remote_state = update_remote_state
        local_client.make_folder('/', 'New Folder')
        self.wait_sync(fail_if_timeout=False)

        self.assertTrue(local_client.exists(u'/Renamed Folder'))
        self.assertFalse(local_client.exists(u'/New Folder'))
        # Path dont change on Nuxeo
        info = remote_client.get_info(u'/New Folder')
        self.assertEquals('Renamed Folder', info.name)

    def test_local_rename_file_while_creating(self):
        global marker
        local_client = self.local_client_1
        root_local_client = self.local_root_client_1
        remote_client = self.remote_document_client_1
        marker = False
        def update_remote_state(row, info, remote_parent_path=None, versionned=True):
            global marker
            EngineDAO.update_remote_state(self.engine_1._dao, row, info, remote_parent_path, versionned)
            if row.local_name == 'File.txt' and not marker:
                root_local_client.rename(row.local_path, 'Renamed File.txt')
                sleep(5)
                marker = True
        self.engine_1._dao.update_remote_state = update_remote_state
        self.local_client_1.make_file('/', u'File.txt',
            content=u'Some Content 2'.encode('utf-8'))
        self.wait_sync(fail_if_timeout=False)
        
        self.assertTrue(local_client.exists(u'/Renamed File.txt'))
        self.assertFalse(local_client.exists(u'/File.txt'))
        # Path dont change on Nuxeo
        info = remote_client.get_info(u'/File.txt')
        self.assertEquals('Renamed File.txt', info.name)

    def test_local_rename_file(self):
        local_client = self.local_client_1
        remote_client = self.remote_document_client_1

        # Rename /Original File 1.txt to /Renamed File 1.txt
        original_file_1_uid = remote_client.get_info(
            u'/Original File 1.txt').uid
        local_client.rename(u'/Original File 1.txt', u'Renamed File 1.txt')
        self.assertFalse(local_client.exists(u'/Original File 1.txt'))
        self.assertTrue(local_client.exists(u'/Renamed File 1.txt'))

        self.wait_sync()
        self.assertFalse(local_client.exists(u'/Original File 1.txt'))
        self.assertTrue(local_client.exists(u'/Renamed File 1.txt'))
        original_file_1_remote_info = remote_client.get_info(
            original_file_1_uid)
        self.assertEquals(original_file_1_remote_info.name,
            u'Renamed File 1.txt')

        # Rename 'Renamed File 1.txt' to 'Renamed Again File 1.txt'
        # and 'Original File 1.1.txt' to
        # 'Renamed File 1.1.txt' at the same time as they share
        # the same digest but do not live in the same folder
        original_1_1_uid = remote_client.get_info(
            u'/Original Folder 1/Original File 1.1.txt').uid
        local_client.rename(
            u'/Original Folder 1/Original File 1.1.txt',
            u'Renamed File 1.1 \xe9.txt')
        self.assertFalse(local_client.exists(
             '/Original Folder 1/Original File 1.1.txt'))
        self.assertTrue(local_client.exists(
            u'/Original Folder 1/Renamed File 1.1 \xe9.txt'))
        local_client.rename('/Renamed File 1.txt', 'Renamed Again File 1.txt')
        self.assertFalse(local_client.exists(u'/Renamed File 1.txt'))
        self.assertTrue(local_client.exists(u'/Renamed Again File 1.txt'))

        self.wait_sync()
        self.assertFalse(local_client.exists(u'/Renamed File 1.txt'))
        self.assertTrue(local_client.exists(u'/Renamed Again File 1.txt'))
        self.assertFalse(local_client.exists(
             u'/Original Folder 1/Original File 1.1.txt'))
        self.assertTrue(local_client.exists(
            u'/Original Folder 1/Renamed File 1.1 \xe9.txt'))

        file_1_remote_info = remote_client.get_info(original_file_1_uid)
        self.assertEquals(file_1_remote_info.name,
            u'Renamed Again File 1.txt')

        # User 1 does not have the rights to see the parent container
        # of the test workspace, hence set fetch_parent_uid=False
        parent_of_file_1_remote_info = remote_client.get_info(
            file_1_remote_info.parent_uid, fetch_parent_uid=False)
        self.assertEquals(parent_of_file_1_remote_info.name,
            self.workspace_title)

        file_1_1_remote_info = remote_client.get_info(original_1_1_uid)
        self.assertEquals(file_1_1_remote_info.name,
            u'Renamed File 1.1 \xe9.txt')

        parent_of_file_1_1_remote_info = remote_client.get_info(
            file_1_1_remote_info.parent_uid)
        self.assertEquals(parent_of_file_1_1_remote_info.name,
            u'Original Folder 1')

    def test_local_rename_file_uppercase(self):
        local_client = self.local_client_1
        remote_client = self.remote_document_client_1

        # Rename /Original File 1.txt to /Renamed File 1.txt

        # Rename 'Renamed File 1.txt' to 'Renamed Again File 1.txt'
        # and 'Original File 1.1.txt' to
        # 'Renamed File 1.1.txt' at the same time as they share
        # the same digest but do not live in the same folder
        original_1_1_uid = remote_client.get_info(
            u'/Original Folder 1/Original File 1.1.txt').uid
        local_client.rename(
            u'/Original Folder 1/Original File 1.1.txt',
            u'original File 1.1.txt')

        self.wait_sync()

        file_1_1_remote_info = remote_client.get_info(original_1_1_uid)
        self.assertEquals(file_1_1_remote_info.name,
            u'original File 1.1.txt')

        parent_of_file_1_1_remote_info = remote_client.get_info(
            file_1_1_remote_info.parent_uid)
        self.assertEquals(parent_of_file_1_1_remote_info.name,
            u'Original Folder 1')

    def test_local_move_file(self):
        if sys.platform == 'win32':
            raise SkipTest("NXDRIVE-480: Temporarily skipped, need to investigate")
        local_client = self.local_client_1
        remote_client = self.remote_document_client_1

        # Move /Original File 1.txt to /Original Folder 1/Original File 1.txt
        original_file_1_uid = remote_client.get_info(
            u'/Original File 1.txt').uid
        local_client.move(u'/Original File 1.txt', u'/Original Folder 1')
        self.assertFalse(local_client.exists(u'/Original File 1.txt'))
        self.assertTrue(local_client.exists(
            u'/Original Folder 1/Original File 1.txt'))

        self.wait_sync()
        self.assertFalse(local_client.exists(u'/Original File 1.txt'))
        self.assertTrue(local_client.exists(
            u'/Original Folder 1/Original File 1.txt'))

        file_1_remote_info = remote_client.get_info(original_file_1_uid)
        self.assertEquals(file_1_remote_info.name, u'Original File 1.txt')<commit_msg>NXDRIVE-480: Unskip test_local_move_file, should be fixed by NXDRIVE-483<commit_after>import os
import sys

from nxdrive.tests.common_unit_test import UnitTestCase
from nxdrive.client import LocalClient
from nxdrive.client import RemoteDocumentClient
from nxdrive.tests.common import TEST_WORKSPACE_PATH
from nxdrive.client.common import NotFound
from nose.plugins.skip import SkipTest
from time import sleep
from nxdrive.engine.dao.sqlite import EngineDAO

# TODO NXDRIVE-170: refactor
LastKnownState = None


class TestLocalMoveAndRename(UnitTestCase):

    # Sets up the following local hierarchy:
    # Nuxeo Drive Test Workspace
    #    |-- Original File 1.txt
    #    |-- Original File 2.txt
    #    |-- Original Folder 1
    #    |       |-- Sub-Folder 1.1
    #    |       |-- Sub-Folder 1.2
    #    |       |-- Original File 1.1.txt
    #    |-- Original Folder 2
    #    |       |-- Original File 3.txt
    def setUp(self):
        self.engine_1.start()
        self.wait_sync(wait_for_async=True)
        self.local_client_1.make_file('/', u'Original File 1.txt',
            content=u'Some Content 1'.encode('utf-8'))

        self.local_client_1.make_file('/', u'Original File 2.txt',
            content=u'Some Content 2'.encode('utf-8'))

        self.local_client_1.make_folder(u'/', u'Original Folder 1')
        self.local_client_1.make_folder(
            u'/Original Folder 1', u'Sub-Folder 1.1')
        self.local_client_1.make_folder(
            u'/Original Folder 1', u'Sub-Folder 1.2')
        self.local_client_1.make_file(u'/Original Folder 1',
            u'Original File 1.1.txt',
            content=u'Some Content 1'.encode('utf-8'))  # Same content as OF1

        self.local_client_1.make_folder('/', 'Original Folder 2')
        self.local_client_1.make_file('/Original Folder 2',
            u'Original File 3.txt',
            content=u'Some Content 3'.encode('utf-8'))
        # Increase timeout as noticed it was sometimes insufficient in Jenkins build
        self.wait_sync(timeout=30)

    def test_local_rename_folder_while_creating(self):
        global marker
        local_client = self.local_client_1
        root_local_client = self.local_root_client_1
        remote_client = self.remote_document_client_1
        marker = False
        def update_remote_state(row, info, remote_parent_path=None, versionned=True):
            global marker
            EngineDAO.update_remote_state(self.engine_1._dao, row, info, remote_parent_path, versionned)
            if row.local_name == 'New Folder' and not marker:
                root_local_client.rename(row.local_path, 'Renamed Folder')
                sleep(5)
                marker = True
        self.engine_1._dao.update_remote_state = update_remote_state
        local_client.make_folder('/', 'New Folder')
        self.wait_sync(fail_if_timeout=False)

        self.assertTrue(local_client.exists(u'/Renamed Folder'))
        self.assertFalse(local_client.exists(u'/New Folder'))
        # Path dont change on Nuxeo
        info = remote_client.get_info(u'/New Folder')
        self.assertEquals('Renamed Folder', info.name)

    def test_local_rename_file_while_creating(self):
        global marker
        local_client = self.local_client_1
        root_local_client = self.local_root_client_1
        remote_client = self.remote_document_client_1
        marker = False
        def update_remote_state(row, info, remote_parent_path=None, versionned=True):
            global marker
            EngineDAO.update_remote_state(self.engine_1._dao, row, info, remote_parent_path, versionned)
            if row.local_name == 'File.txt' and not marker:
                root_local_client.rename(row.local_path, 'Renamed File.txt')
                sleep(5)
                marker = True
        self.engine_1._dao.update_remote_state = update_remote_state
        self.local_client_1.make_file('/', u'File.txt',
            content=u'Some Content 2'.encode('utf-8'))
        self.wait_sync(fail_if_timeout=False)
        
        self.assertTrue(local_client.exists(u'/Renamed File.txt'))
        self.assertFalse(local_client.exists(u'/File.txt'))
        # Path dont change on Nuxeo
        info = remote_client.get_info(u'/File.txt')
        self.assertEquals('Renamed File.txt', info.name)

    def test_local_rename_file(self):
        local_client = self.local_client_1
        remote_client = self.remote_document_client_1

        # Rename /Original File 1.txt to /Renamed File 1.txt
        original_file_1_uid = remote_client.get_info(
            u'/Original File 1.txt').uid
        local_client.rename(u'/Original File 1.txt', u'Renamed File 1.txt')
        self.assertFalse(local_client.exists(u'/Original File 1.txt'))
        self.assertTrue(local_client.exists(u'/Renamed File 1.txt'))

        self.wait_sync()
        self.assertFalse(local_client.exists(u'/Original File 1.txt'))
        self.assertTrue(local_client.exists(u'/Renamed File 1.txt'))
        original_file_1_remote_info = remote_client.get_info(
            original_file_1_uid)
        self.assertEquals(original_file_1_remote_info.name,
            u'Renamed File 1.txt')

        # Rename 'Renamed File 1.txt' to 'Renamed Again File 1.txt'
        # and 'Original File 1.1.txt' to
        # 'Renamed File 1.1.txt' at the same time as they share
        # the same digest but do not live in the same folder
        original_1_1_uid = remote_client.get_info(
            u'/Original Folder 1/Original File 1.1.txt').uid
        local_client.rename(
            u'/Original Folder 1/Original File 1.1.txt',
            u'Renamed File 1.1 \xe9.txt')
        self.assertFalse(local_client.exists(
             '/Original Folder 1/Original File 1.1.txt'))
        self.assertTrue(local_client.exists(
            u'/Original Folder 1/Renamed File 1.1 \xe9.txt'))
        local_client.rename('/Renamed File 1.txt', 'Renamed Again File 1.txt')
        self.assertFalse(local_client.exists(u'/Renamed File 1.txt'))
        self.assertTrue(local_client.exists(u'/Renamed Again File 1.txt'))

        self.wait_sync()
        self.assertFalse(local_client.exists(u'/Renamed File 1.txt'))
        self.assertTrue(local_client.exists(u'/Renamed Again File 1.txt'))
        self.assertFalse(local_client.exists(
             u'/Original Folder 1/Original File 1.1.txt'))
        self.assertTrue(local_client.exists(
            u'/Original Folder 1/Renamed File 1.1 \xe9.txt'))

        file_1_remote_info = remote_client.get_info(original_file_1_uid)
        self.assertEquals(file_1_remote_info.name,
            u'Renamed Again File 1.txt')

        # User 1 does not have the rights to see the parent container
        # of the test workspace, hence set fetch_parent_uid=False
        parent_of_file_1_remote_info = remote_client.get_info(
            file_1_remote_info.parent_uid, fetch_parent_uid=False)
        self.assertEquals(parent_of_file_1_remote_info.name,
            self.workspace_title)

        file_1_1_remote_info = remote_client.get_info(original_1_1_uid)
        self.assertEquals(file_1_1_remote_info.name,
            u'Renamed File 1.1 \xe9.txt')

        parent_of_file_1_1_remote_info = remote_client.get_info(
            file_1_1_remote_info.parent_uid)
        self.assertEquals(parent_of_file_1_1_remote_info.name,
            u'Original Folder 1')

    def test_local_rename_file_uppercase(self):
        local_client = self.local_client_1
        remote_client = self.remote_document_client_1

        # Rename /Original File 1.txt to /Renamed File 1.txt

        # Rename 'Renamed File 1.txt' to 'Renamed Again File 1.txt'
        # and 'Original File 1.1.txt' to
        # 'Renamed File 1.1.txt' at the same time as they share
        # the same digest but do not live in the same folder
        original_1_1_uid = remote_client.get_info(
            u'/Original Folder 1/Original File 1.1.txt').uid
        local_client.rename(
            u'/Original Folder 1/Original File 1.1.txt',
            u'original File 1.1.txt')

        self.wait_sync()

        file_1_1_remote_info = remote_client.get_info(original_1_1_uid)
        self.assertEquals(file_1_1_remote_info.name,
            u'original File 1.1.txt')

        parent_of_file_1_1_remote_info = remote_client.get_info(
            file_1_1_remote_info.parent_uid)
        self.assertEquals(parent_of_file_1_1_remote_info.name,
            u'Original Folder 1')

    def test_local_move_file(self):
        local_client = self.local_client_1
        remote_client = self.remote_document_client_1

        # Move /Original File 1.txt to /Original Folder 1/Original File 1.txt
        original_file_1_uid = remote_client.get_info(
            u'/Original File 1.txt').uid
        local_client.move(u'/Original File 1.txt', u'/Original Folder 1')
        self.assertFalse(local_client.exists(u'/Original File 1.txt'))
        self.assertTrue(local_client.exists(
            u'/Original Folder 1/Original File 1.txt'))

        self.wait_sync()
        self.assertFalse(local_client.exists(u'/Original File 1.txt'))
        self.assertTrue(local_client.exists(
            u'/Original Folder 1/Original File 1.txt'))

        file_1_remote_info = remote_client.get_info(original_file_1_uid)
        self.assertEquals(file_1_remote_info.name, u'Original File 1.txt')
{'fraction_non_alphanumeric': 0.09528639299631667, 'fraction_numerical': 0.021406074841189345, 'mean_word_length': 2.6313238999806163, 'pattern_counts': {'":': 0, '<': 3, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5106142', 'n_tokens_mistral': 6130, 'n_tokens_neox': 5785, 'n_words': 1184}