musicguru / core / sqlfs / sql_test.py

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
# Created By: Virgil Dupras
# Created On: 2006/10/03
# Copyright 2010 Hardcoded Software (http://www.hardcoded.net)
# 
# This software is licensed under the "BSD" License as described in the "LICENSE" file, 
# which should be included with this package. The terms are also available at 
# http://www.hardcoded.net/licenses/bsd_license

import sqlite3 as sqlite
import os.path as op
import time
import weakref

from hsutil.testutil import eq_

import hsfs as fs
from .. import manualfs
from hscommon.job import Job, JobCancelled

from ..testcase import TestCase
from ._sql import *

INFO_SAMPLE = {
    'foo': 'bar',
    'baz': 42,
    'int_in_str': '42',
    'long': 42
}

class FakeFile(manualfs.File):
    def _read_all_info(self, attrnames=None):
        super(FakeFile, self)._read_all_info(attrnames)
        self.root.callcount += 1        
    
    def _read_info(self, field):
        if field == 'mtime':
            self.mtime = 42
        elif field == 'md5':
            self.md5 = self.name
    

def getref():
    ref = manualfs.Directory()
    ref.cls_file_class = FakeFile
    ref.callcount = 0
    d1 = ref.new_directory('sub1')
    d2 = ref.new_directory('sub2')
    d1.cls_file_class = FakeFile
    d2.cls_file_class = FakeFile
    f1 = ref.new_file('file1')
    f2 = d1.new_file('file2')
    f3 = d2.new_file('file3')
    return ref

class TCDirectory(TestCase):
    def test_new_directory(self):
        root = Root(threaded=False)
        d = root.new_directory('bar')
        self.assertEqual('bar',d.name)
        self.assert_(root is d.parent)
        self.assert_(d in root)
        self.assert_(root[0] is d)
        self.assert_(root.dirs[0] is d)
    
    def test_persistence(self):
        p = self.tmpdir()
        dbpath = op.join(p,'fs.db')
        root = Root(dbpath, 'root', threaded=False)
        root.new_directory('bar')
        root.con.close()
        root = Root(dbpath, 'root', threaded=False)
        self.assertEqual('bar',root[0].name)
    
    def test_two_subdir_levels(self):
        root = Root(threaded=False)
        d1 = root.new_directory('foo')
        d2 = d1.new_directory('bar')
        self.assert_(root[0][0] is d2)
        self.assertEqual('bar',d2.name)
    
    def test_escaping(self):
        root = Root(threaded=False)
        try:
            d1 = root.new_directory('foo\'bar')
            self.assertEqual('foo\'bar',d1.name)
        except sqlite.OperationalError:
            self.fail()
    
    def test_new_file(self):
        root = Root(threaded=False)
        d = root.new_directory('subdir')
        root.new_file('foo')
        file = d.new_file('bar')
        self.assertEqual('foo',root.files[0].name)
        self.assertEqual('bar',root.dirs[0].files[0].name)
        self.assert_(isinstance(file,File))
    
    def test_name_collisions(self):
        root = Root(threaded=False)
        root.new_directory('foo')
        self.assertRaises(fs.AlreadyExistsError,root.new_directory,'foo')
        self.assertRaises(fs.AlreadyExistsError,root.new_file,'foo')
    
    def test_unicode(self):
        root = Root(threaded=False)
        d = root.new_directory('foo\u00e9')
        self.assertEqual('foo\u00e9',d.name)
    

class TCFile(TestCase):
    def test_escaping(self):
        root = Root(threaded=False)
        try:
            f = root.new_file('foo\'bar')
            self.assertEqual('foo\'bar',f.name)
        except sqlite.OperationalError:
            self.fail()
    

class TCAttr_multiple(TestCase):
    def test_attrs(self):
        root = Root(threaded=False)
        f = root.new_file('foobar')
        f._set_attrs(INFO_SAMPLE)
        result = f._get_attrs()
        self.assertEqual(INFO_SAMPLE, dict(result))
    
    def test_attrs_persistence(self):
        p = self.tmpdir()
        dbpath = op.join(p,'fs.db')
        root = Root(dbpath, threaded=False)
        f = root.new_file('foobar')
        f._set_attrs(INFO_SAMPLE)
        root.con.close()
        root = Root(dbpath, threaded=False)
        f = root[0]
        result = f._get_attrs()
        self.assertEqual(INFO_SAMPLE, dict(result))
    
    def test_set_attrs_twice(self):
        root = Root(threaded=False)
        f = root.new_file('foobar')
        f._set_attrs(INFO_SAMPLE)
        f._set_attrs(INFO_SAMPLE)
        sql = "select count(*) from attrs"
        result = f.con.execute(sql)
        self.assertEqual(len(INFO_SAMPLE), result.fetchall()[0][0])
    
    def test_dir_attrs(self):
        #This test is just to verify that the _get_attrs _set_attrs func are at the Node level.
        #Tests in File are assumed.
        root = Root(threaded=False)
        subdir = root.new_directory('foobar')
        subdir._set_attrs(INFO_SAMPLE)
        result = subdir._get_attrs()
        self.assertEqual(INFO_SAMPLE, dict(result))
    
    def test_escaping(self):
        #Hey, this test nver failed because of my use of 'cur.executemany'
        root = Root(threaded=False)
        d = {'foo': 'foo\'bar'}
        f = root.new_file('foobar')
        try:
            f._set_attrs(d)
        except sqlite.OperationalError:
            self.fail()
    

class TCAttr_single(TestCase):
    def test_str(self):
        root = Root(threaded=False)
        f = root.new_file('foobar')
        f._set_attr('foo','bar')
        self.assertEqual('bar',f._get_attr('foo'))
    
    def test_int(self):
        root = Root(threaded=False)
        f = root.new_file('foobar')
        f._set_attr('foo',42)
        self.assertEqual(42,f._get_attr('foo'))
    
    def test_str_int(self):
        root = Root(threaded=False)
        f = root.new_file('foobar')
        f._set_attr('foo','42')
        self.assertEqual('42',f._get_attr('foo'))
    
    def test_set_same_attr_twice(self):
        #When that happens, we don't want two rows in the db, we want an update to happen.
        root = Root(threaded=False)
        f = root.new_file('foobar')
        f._set_attr('foo',1)
        f._set_attr('foo',2)
        self.assertEqual(2,f._get_attr('foo'))
        result = root.con.execute("select * from attrs")
        self.assertEqual(1, len(result.fetchall()))
    
    def test_escaping(self):
        root = Root(threaded=False)
        f = root.new_file('foobar')
        try:
            f._set_attr('foo','foo\'bar')
        except sqlite.OperationalError:
            self.fail()
    
    def test_raise_key_error_if_not_exists(self):
        root = Root(threaded=False)
        f = root.new_file('foobar')
        self.assertRaises(KeyError,f._get_attr,'foo')
    
    def test_unicode(self):
        root = Root(threaded=False)
        f = root.new_file('foobar')
        f._set_attr('foo','foo\u00e9')
        self.assertEqual('foo\u00e9',f._get_attr('foo'))
    
    def test_binary(self):
        root = Root(threaded=False)
        f = root.new_file('foobar')
        f._set_attr('foo','\x0e\xff')
        self.assertEqual('\x0e\xff',f._get_attr('foo'))
    
    def test_replace_newline_and_cr(self):
        root = Root(threaded=False)
        f = root.new_file('foobar')
        f._set_attr('foo','foo\rbar\n')
        self.assertEqual('foobar',f._get_attr('foo'))
    

class TC_delete(TestCase):
    def test_that_objects_are_gone(self):
        root = Root(threaded=False)
        f = root.new_file('file')
        d = root.new_directory('dir')
        f.delete()
        self.assertEqual(1,len(root))
        d.delete()
        self.assertEqual(0,len(root))
    
    def test_transaction_is_commited(self):
        p = self.tmpdir()
        dbpath = op.join(p,'fs.db')
        root = Root(dbpath, threaded=False)
        f = root.new_file('file')
        f.delete()
        root.con.close()
        root = Root(dbpath, threaded=False)
        result = root.con.execute("select * from nodes")
        self.assertEqual(0, len(result.fetchall()))
    
    def test_also_delete_attrs(self):
        root = Root(threaded=False)
        f = root.new_file('file')
        d = root.new_directory('dir')
        f._set_attrs(INFO_SAMPLE)
        d._set_attrs(INFO_SAMPLE)
        f.delete()
        d.delete()
        result = root.con.execute("select * from attrs")
        self.assertEqual(0, len(result.fetchall()))
    
    def test_delete_dir_then_add_file_with_same_name(self):
        root = Root(threaded=False)
        d = root.new_directory('foo')
        d.delete()
        result = root.new_file('foo')
        self.assert_(isinstance(result,File))
    
    def test_that_delete_is_recursive(self):
        root = Root(threaded=False)
        d1 = root.new_directory('dir1')
        d2 = root.new_directory('dir2')
        sd1 = d1.new_directory('dir')
        sd1.new_file('file1')
        sd1.new_file('file2')
        d1.delete()
        result = root.con.execute("select * from nodes")
        self.assertEqual(1, len(result.fetchall())) #Only d2 should be left
    

class TCUpdate_initial(TestCase):
    #Only test the first call of Update on a Directory
    def test_that_dirs_and_files_are_added(self):
        root = Root(threaded=False)
        root.update(getref())
        self.assert_('sub1' in root)
        self.assert_('sub2' in root)
        self.assert_('file1' in root)
        self.assert_('file2' in root['sub1'])
        self.assert_('file3' in root['sub2'])
    
    def test_that_attrs_are_set(self):
        root = Root(threaded=False)
        ref = getref()
        for f in ref.allfiles:
            f._read_all_info()
            f.md5 = f.name
        root.update(ref)
        self.assertEqual('file1',root['file1'].md5)
        self.assertEqual('file2',root['sub1']['file2'].md5)
        self.assertEqual('file3',root['sub2']['file3'].md5)
    
    def test_that_attrs_are_set_with_persistence(self):
        p = self.tmpdir()
        dbpath = op.join(p,'fs.db')
        root = Root(dbpath, threaded=False)
        ref = getref()
        for f in ref.allfiles:
            f._read_all_info()
            f.md5 = f.name
        root.update(ref)
        root.con.close()
        root = Root(dbpath, threaded=False)
        self.assertEqual('file1',root['file1'].md5)
        self.assertEqual('file2',root['sub1']['file2'].md5)
        self.assertEqual('file3',root['sub2']['file3'].md5)
    
    def test_update_files_with_higher_mtime(self):
        root = Root(threaded=False)
        ref = getref()
        for f in ref.allfiles:
            f._read_all_info()
            f.md5 = f.name
        root.update(ref)
        ref['file1'].md5 = 'changed'
        ref['sub1']['file2'].md5 = 'changed'
        ref['sub1']['file2'].mtime = time.time() + 1
        root.update(ref)
        self.assertEqual('file1',root['file1'].md5)
        self.assertEqual('changed',root['sub1']['file2'].md5)
    
    def test_only_read_selected_attrs(self):
        root = Root(threaded=False)
        assert root._attrs_to_read is None
        root._attrs_to_read = ['mtime']
        root.update(getref())
        f = root.allfiles[0]
        eq_(f.md5, '') # md5 has not been read
    
    def test_cancel(self):
        def callback(progress):
            self.log.append(progress)
            return len(self.log) < 5
        
        self.log = []
        job = Job(1,callback)
        root = Root(threaded=False)
        ref = getref()
        try:
            root.update(ref,job=job)
            self.fail("Should have cancelled")
        except JobCancelled:
            pass
        self.assertEqual(0,len(root))
    

class TCUpdate_subsequent(TestCase):
    #Test what happens when Update is called a second time.
    def setUp(self):
        def FakeReadAllInfo(instance,sections=None):
            self.callcount += 1
            self.oldread(instance,sections)
        
        self.callcount = 0
        self.oldread = manualfs.File._read_all_info
        manualfs.File._read_all_info = FakeReadAllInfo
        root = Root(threaded=False)
        ref = getref()
        root.update(ref)
        self.root = root
        self.ref = ref
        
    def tearDown(self):
        manualfs.File._read_all_info = self.oldread
    
    def test_files_were_added(self):
        root,ref = self.root,self.ref
        ref.new_file('file4')
        root.update(ref)
        self.assertEqual(2,root.filecount)
    
    def test_always_update_zero_mtime_nodes(self):
        #We always want attrs to be read when a sql file mtime is zero, event if the ref mtime
        # is also zero.
        root,ref = self.root,self.ref
        reffile = ref.new_file('file4')
        reffile.mtime #read info
        reffile.mtime = 0
        reffile.size = 42
        root.update(ref)
        self.assertEqual(2,root.filecount)
        file4 = root['file4']
        self.assertEqual(42,file4.size)
    
    def test_files_were_removed(self):
        root,ref = self.root,self.ref
        ref.files[0].delete()
        ref.dirs[0].files[0].delete()
        root.update(ref)
        self.assertEqual(1,len(root.allfiles))
    
    def test_dirs_were_added(self):
        root,ref = self.root,self.ref
        ref.new_directory('sub3')
        root.update(ref)
        self.assertEqual(3,root.dircount)
    
    def test_dirs_were_removed(self):
        root,ref = self.root,self.ref
        ref.dirs[0].delete()
        root.update(ref)
        self.assertEqual(1,root.dircount)
    
    def test_file_removed_and_added_as_dir(self):
        root,ref = self.root,self.ref
        ref['file1'].delete()
        ref.new_directory('file1')
        root.update(ref)
        self.assertEqual(3,root.dircount)
        self.assertEqual(0,root.filecount)
    
    def test_dir_removed_and_added_as_file(self):
        root,ref = self.root,self.ref
        ref['sub1'].delete()
        ref.new_file('sub1')
        root.update(ref)
        self.assertEqual(1,root.dircount)
        self.assertEqual(2,root.filecount)
    
    def test_file_removed_and_added_as_dir(self):
        root,ref = self.root,self.ref
        ref['file1'].delete()
        ref.new_directory('file1')
        root.update(ref)
        self.assertEqual(3,root.dircount)
        self.assertEqual(0,root.filecount)
    

class TCJobs(TestCase):
    def test_Update(self):
        def callback(progress):
            self.log.append(progress)
            return True
        
        self.log = []
        job = Job(1,callback)
        root = Root(threaded=False)
        ref = getref()
        root.update(ref,job=job)
        self.assertEqual(0,self.log[0])
        self.assertEqual(100,self.log[-1])
    

class TCfind_node_of_id(TestCase):
    def test_id_is_self(self):
        root = Root(threaded=False)
        d = root.new_directory('foo')
        self.assert_(root.find_node_of_id(root.id) is root)
    
    def test_find_id_not_in_self(self):
        root = Root(threaded=False)
        d = root.new_directory('foo')
        self.assert_(root.find_node_of_id(42) is None)
    
    def test_find_id_in_children(self):
        root = Root(threaded=False)
        d = root.new_directory('foo')
        self.assert_(root.find_node_of_id(d.id) is d)
    
    def test_id_cache_is_weakref(self):
        root = Root(threaded=False)
        d = root.new_directory('foo')
        w = weakref.ref(d)
        root.find_node_of_id(d.id)
        d.delete()
        root[:] #Perform the actual delete
        del d
        self.assert_(w() is None)
    
    def test_check_cache_validity(self):
        #Ok, the cache is weakref. But what if something else keeps the object alive?
        #The cache would return an erronous value.
        root = Root(threaded=False)
        d = root.new_directory('foo')
        root.find_node_of_id(d.id)
        d.delete()
        self.assert_(root.find_node_of_id(d.id) is None)
    
    def test_node_was_deleted_and_a_new_node_was_created_with_the_same_id(self):
        root = Root(threaded=False)
        d1 = root.new_directory('foo')
        root.find_node_of_id(d1.id)
        d1.delete()
        root[:] #Remove d1 instance from root
        d2 = root.new_directory('foo') #Should have same id as d1
        self.assert_(root.find_node_of_id(d2.id) is d2)
    
    def test_two_levels_deep(self):
        root = Root(threaded=False)
        d = root.new_directory('foo')
        f = d.new_file('bar')
        self.assert_(root.find_node_of_id(f.id) is f)
    
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.