~dobey/ubuntuone-client/py-only

« back to all changes in this revision

Viewing changes to tests/syncdaemon/test_fsm.py

  • Committer: Tarmac
  • Author(s): mike.mccracken at canonical
  • Date: 2013-01-28 23:27:04 UTC
  • mfrom: (1375.1.10 fix-udf-search)
  • Revision ID: tarmac-20130128232704-42b7218jmujafwo3
- Fix support for searching local files in UDFs. (LP: #1101271)

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
 
33
33
import errno
34
34
import os
35
 
import re
36
35
import time
37
36
 
38
37
from mocker import MockerTestCase, ANY
4321
4320
        self.fsm = FileSystemManager(self.fsmdir, self.partials_dir,
4322
4321
                                     FakeVolumeManager(self.root_dir), self.db)
4323
4322
 
 
4323
        mdid1 = 'id1'
 
4324
        self.path1 = 'test-root/path/to/file_test'
 
4325
        mdid2 = 'id2'
 
4326
        self.path2 = 'test-root/path/to/my_photos'
 
4327
        self.mdobj1 = {'server_hash': 'asdqwe123', 'share_id': ''}
 
4328
        self.mdobj2 = {'server_hash': 'asdqwe456', 'share_id': ''}
 
4329
 
 
4330
        serv_missing_mdid = 'serv_missing_id'
 
4331
        self.serv_missing_path = 'test-root/path/to/other_photos'
 
4332
        serv_missing_mdobj = {'server_hash': '', 'share_id': ''}
 
4333
 
 
4334
        # create a file in a UDF, no need to add it to the
 
4335
        # FakeVolumeManager
 
4336
        udfid1 = 'udfid'
 
4337
        self.udfpath1 = 'test-udf/path/to/afile'
 
4338
        self.udfmdobj = {'server_hash': 'asdqwe789',
 
4339
                         'share_id': 'udf-share-id'}
 
4340
 
 
4341
        shareid1 = 'shareid'
 
4342
        self.sharepath1 = 'test-shared-to-me/path/to/afile'
 
4343
        self.sharemdobj = {'server_hash': 'sharehash',
 
4344
                           'share_id': 'share-share-id'}
 
4345
        self.fsm.vm.shares['share-share-id'] = {}  # value not used
 
4346
 
 
4347
        self.fsm._idx_path = {self.path1: mdid1, self.path2: mdid2,
 
4348
                              self.serv_missing_path: serv_missing_mdid,
 
4349
                              self.udfpath1: udfid1,
 
4350
                              self.sharepath1: shareid1}
 
4351
        self.fsm.fs = {mdid1: self.mdobj1, mdid2: self.mdobj2,
 
4352
                       serv_missing_mdid: serv_missing_mdobj,
 
4353
                       udfid1: self.udfmdobj,
 
4354
                       shareid1: self.sharemdobj}
 
4355
 
 
4356
    def test_get_nothing_no_matches(self):
 
4357
        """Check that we don't give matches for nonexisting queries."""
 
4358
        self.assertEqual([], self.fsm.get_paths_by_pattern('does not exist'))
 
4359
 
4324
4360
    def test_get_paths_by_pattern(self):
4325
 
        """Check that we obtain the files that correspond to the filter."""
4326
 
        mdid = 'id'
4327
 
        path = '/my/path/to/file_test'
4328
 
        mdobj = {'server_hash': 'asdqwe123'}
4329
 
        mdid2 = 'id2'
4330
 
        path2 = '/my/path/to/my_photos'
4331
 
        mdobj2 = {'server_hash': 'asdqwe456'}
4332
 
        self.fsm._idx_path = {path: mdid, path2: mdid2}
4333
 
        self.fsm.fs = {mdid: mdobj, mdid2: mdobj2}
4334
 
        expected = ['/my/path/to/file_test']
4335
 
        # expectations
4336
 
        paths = "(%s|%s)" % ('/my/path/', '/home/')
4337
 
        pattern = paths + ".*/.*%s.*$"
4338
 
        keywords = '.+'.join(re.escape('file').split('\\ '))
4339
 
        search = pattern % keywords
4340
 
        result = self.fsm.get_paths_by_pattern(search)
 
4361
        expected = [self.path1]
 
4362
        result = self.fsm.get_paths_by_pattern('file_test')
4341
4363
        self.assertEqual(result, expected)
4342
4364
 
4343
4365
    def test_get_paths_by_pattern_case_insensitive(self):
4344
4366
        """Check that we obtain the files that correspond to the filter."""
4345
 
        mdid = 'id'
4346
 
        path = '/my/path/to/file_test'
4347
 
        mdobj = {'server_hash': 'asdqwe123'}
4348
 
        mdid2 = 'id2'
4349
 
        path2 = '/my/path/to/my_photos'
4350
 
        mdobj2 = {'server_hash': 'asdqwe456'}
4351
 
        self.fsm._idx_path = {path: mdid, path2: mdid2}
4352
 
        self.fsm.fs = {mdid: mdobj, mdid2: mdobj2}
4353
 
        expected = ['/my/path/to/file_test']
4354
 
        # expectations
4355
 
        paths = "(%s|%s)" % ('/my/path/', '/home/')
4356
 
        pattern = paths + ".*/.*%s.*$"
4357
 
        keywords = '.+'.join(re.escape('FILE').split('\\ '))
4358
 
        search = pattern % keywords
4359
 
        result = self.fsm.get_paths_by_pattern(search)
4360
 
        self.assertEqual(result, expected)
4361
 
 
4362
 
    def test_get_paths_by_pattern_valid_folders(self):
4363
 
        """Search for the pattern in the valid folders."""
4364
 
        mdid = 'id'
4365
 
        path = '/my/path/to/file_test'
4366
 
        mdobj = {'server_hash': 'asdqwe123'}
4367
 
        mdid2 = 'id2'
4368
 
        path2 = '/home/to/my_files'
4369
 
        mdobj2 = {'server_hash': 'asdqwe456'}
4370
 
        mdid3 = 'id3'
4371
 
        path3 = '/home2/to/my_files'
4372
 
        mdobj3 = {'server_hash': 'asdqwe456'}
4373
 
        self.fsm._idx_path = {path: mdid, path2: mdid2, path3: mdid3}
4374
 
        self.fsm.fs = {mdid: mdobj, mdid2: mdobj2, mdid3: mdobj3}
4375
 
        expected = ['/home/to/my_files', '/my/path/to/file_test']
4376
 
        # expectations
4377
 
        paths = "(%s|%s)" % ('/my/path/', '/home/')
4378
 
        pattern = paths + ".*/.*%s.*$"
4379
 
        keywords = '.+'.join(re.escape('file').split('\\ '))
4380
 
        search = pattern % keywords
4381
 
        result = self.fsm.get_paths_by_pattern(search)
 
4367
        expected = [self.path1]
 
4368
        result = self.fsm.get_paths_by_pattern('FILE_tEsT')
 
4369
        self.assertEqual(result, expected)
 
4370
 
 
4371
    def test_get_paths_by_pattern_avoid_shares_see_udfs(self):
 
4372
        """Do not return paths in shares, but do return those in UDFs."""
 
4373
        expected = [self.udfpath1]
 
4374
        result = self.fsm.get_paths_by_pattern('afile')
 
4375
        self.assertEqual(result, expected)
 
4376
 
 
4377
    def test_get_paths_by_pattern_welcome_shares_see_udfs(self):
 
4378
        """Return paths in shares and UDFs if asked."""
 
4379
        expected = [self.sharepath1, self.udfpath1]
 
4380
        result = self.fsm.get_paths_by_pattern('afile', ignore_shares=False)
4382
4381
        self.assertEqual(result, expected)
4383
4382
 
4384
4383
    def test_get_paths_by_pattern_not_in_server(self):
4385
 
        """Check that we ignore the files that are not still in the server.."""
4386
 
        mdid = 'id'
4387
 
        path = '/my/path/to/file_test'
4388
 
        mdobj = {'server_hash': 'asdqwe123'}
4389
 
        mdid2 = 'id2'
4390
 
        path2 = '/home/to/my_files'
4391
 
        mdobj2 = {'server_hash': ''}
4392
 
        mdid3 = 'id3'
4393
 
        path3 = '/home2/to/my_files'
4394
 
        mdobj3 = {'server_hash': 'asdqwe456'}
4395
 
        self.fsm._idx_path = {path: mdid, path2: mdid2, path3: mdid3}
4396
 
        self.fsm.fs = {mdid: mdobj, mdid2: mdobj2, mdid3: mdobj3}
4397
 
        expected = ['/my/path/to/file_test']
4398
 
        # expectations
4399
 
        paths = "(%s|%s)" % ('/my/path/', '/home/')
4400
 
        pattern = paths + ".*/.*%s.*$"
4401
 
        keywords = '.+'.join(re.escape('file').split('\\ '))
4402
 
        search = pattern % keywords
4403
 
        result = self.fsm.get_paths_by_pattern(search)
 
4384
        """Check that we ignore the files that are not still in the server."""
 
4385
        expected = [self.path2]
 
4386
        result = self.fsm.get_paths_by_pattern('photo')
4404
4387
        self.assertEqual(result, expected)
4405
4388
 
4406
4389
    def test_get_paths_by_pattern_sorted_result(self):
4407
 
        """Search that we obtained the paths sorted.."""
4408
 
        mdid = 'id'
4409
 
        path = '/my/path/to/file_test'
4410
 
        mdobj = {'server_hash': 'asdqwe123'}
4411
 
        mdid2 = 'id2'
4412
 
        path2 = '/home/to/my_files'
4413
 
        mdobj2 = {'server_hash': 'asdqwe456'}
4414
 
        mdid3 = 'id3'
4415
 
        path3 = '/home2/to/my_files'
4416
 
        mdobj3 = {'server_hash': 'asdqwe456'}
4417
 
        self.fsm._idx_path = {path: mdid, path2: mdid2, path3: mdid3}
4418
 
        self.fsm.fs = {mdid: mdobj, mdid2: mdobj2, mdid3: mdobj3}
4419
 
        expected = ['/my/path/to/file_test', '/home/to/my_files']
4420
 
        # expectations
4421
 
        paths = "(%s|%s)" % ('/my/path/', '/home/')
4422
 
        pattern = paths + ".*/.*%s.*$"
4423
 
        keywords = '.+'.join(re.escape('file').split('\\ '))
4424
 
        search = pattern % keywords
4425
 
        result = self.fsm.get_paths_by_pattern(search)
 
4390
        """Results should be sorted."""
 
4391
        expected = [self.udfpath1, self.path1]
 
4392
        result = self.fsm.get_paths_by_pattern('file')
4426
4393
        self.assertNotEqual(result, expected)
4427
4394
        expected = sorted(expected)
4428
4395
        self.assertEqual(result, expected)
 
4396
 
 
4397
    def test_get_paths_from_udf_name(self):
 
4398
        """Searching on a UDF name should return files in that UDF."""
 
4399
        expected = [self.udfpath1]
 
4400
        result = self.fsm.get_paths_by_pattern('udf')
 
4401
        self.assertEqual(expected, result)
 
4402
 
 
4403
    def test_fuzzy_search(self):
 
4404
        """Test that searching for path components without slashes works."""
 
4405
        expected = [self.path1, self.udfpath1]
 
4406
        result = self.fsm.get_paths_by_pattern('path file')
 
4407
        self.assertEqual(expected, result)
 
4408
 
 
4409
    def test_fuzzy_search_in_udf(self):
 
4410
        """Test that searching for path components without slashes works."""
 
4411
        expected = [self.udfpath1]
 
4412
        result = self.fsm.get_paths_by_pattern('udf afile')
 
4413
        self.assertEqual(expected, result)
 
4414