/
tryDbHelper.py
592 lines (457 loc) · 17.5 KB
/
tryDbHelper.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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
import DbHelper
import DbInterface
import os.path
import SHA1HashUtilities
if os.path.isfile("trythis.sqlite"):
os.remove("trythis.sqlite")
DbHelper.DbHelper.CreateDb("trythis.sqlite")
db = DbHelper.DbHelper("trythis.sqlite")
db.OpenConnection()
db.AddFile("0a4d55a8d778e5022fab701977c5d840bbc486d0", 1045)
db.AddFile("0000000000001111111111111111111111111110", -1)
dbInterface = DbInterface.DbInterface("trythis.sqlite")
query = "select * from FilesV2;"
print dbInterface.ExecuteSqlQueryReturningMultipleRows(query)
print ""
print "check if file 0a4d55a8d778e5022fab701977c5d840bbc486d0 found: should get True"
print db.FileAlreadyInDatabase("0a4d55a8d778e5022fab701977c5d840bbc486d0", 1045)
print ""
print "check if file 00001111 found: should get False"
print db.FileAlreadyInDatabase("00001111", 1045)
print ""
print "check if file 0a4d55a8d778e5022fab701977c5d840bbc486d0 with wrong filesize found: should get exception"
try:
print db.FileAlreadyInDatabase("0a4d55a8d778e5022fab701977c5d840bbc486d0", 1046)
except:
print "correctly caught exception"
print ""
print "check if file 0000000000001111111111111111111111111110 found and size updated, should get true, the contents should have updated size"
print db.FileAlreadyInDatabase("0000000000001111111111111111111111111110", 1001000)
query = "select * from FilesV2;"
print dbInterface.ExecuteSqlQueryReturningMultipleRows(query)
print ""
print "check if file 0a4d55a8d778e5022fab701977c5d840bbc486d0 with unknown filesize found: should get True"
print db.FileAlreadyInDatabase("0a4d55a8d778e5022fab701977c5d840bbc486d0", -1)
print ""
print "check if file 0a4d55a8d778e5022fab701977c5d840bbc486d0 with unknown filesize found (method 2): should get True"
print db.FileAlreadyInDatabaseUnknownSize("0a4d55a8d778e5022fab701977c5d840bbc486d0")
print ""
print "check if file 00001111 with unknown size found: should get False"
print db.FileAlreadyInDatabaseUnknownSize("00001111")
print ""
print "insert new directories"
dirpath = "C:\\trythisone"
dirHash = SHA1HashUtilities.SHA1HashUtilities.HashString(dirpath)
db.addDirectory(dirHash,dirpath)
dirpath = "C:\\fakepath\\trythistwo"
dirHash = SHA1HashUtilities.SHA1HashUtilities.HashString(dirpath)
db.addDirectory(dirHash,dirpath)
query = "select * from originalDirectoriesV2;"
print dbInterface.ExecuteSqlQueryReturningMultipleRows(query)
print ""
print "check if directory hash already in database, should return true"
print db.DirectoryAlreadyInDatabase(dirHash)
print "check if directory hash 555555 already in database, should return false"
print db.DirectoryAlreadyInDatabase("555555")
print ""
print "insert file locations"
filepath = "C:\\trythisone\\filename1.jpg"
filehash = "111111"
db.AddFile("111111", 10400)
db.AddOriginalFileLocation(filehash, filepath)
filepath = "C:\\anotherfakepath\\trythisthree\\filename2.jpg"
filehash = "111111"
db.AddOriginalFileLocation(filehash, filepath)
query = "select * from OriginalDirectoriesForFileV5;"
print dbInterface.ExecuteSqlQueryReturningMultipleRows(query)
print ""
print "check if file location already in database, should return true"
parentDir, filename = os.path.split(filepath)
dirHash = SHA1HashUtilities.SHA1HashUtilities.HashString(parentDir)
print db.FileOriginalLocationAlreadyInDatabase(filehash, filename, dirHash)
print "check if incorrect file location in database, should return false"
print db.FileOriginalLocationAlreadyInDatabase(filehash, "differentName", dirHash)
print "check if incorrect file location in database, should return false"
print db.FileOriginalLocationAlreadyInDatabase(filehash, filename, "4444")
print "check if incorrect file location in database, should return false"
print db.FileOriginalLocationAlreadyInDatabase("454545", filename, dirHash)
print ""
print "testing link stuff"
print "adding files with hash 001 and 002 for test"
db.AddFile("001", 1000)
db.AddFile("002", -1)
print "replace 001 with link 003"
db.AddFile("003", 200)
db.SetLink("001", "003")
db.SetFileToReplacedByLink("001")
print "list contents of link table"
query = "select * from FileLink;"
print dbInterface.ExecuteSqlQueryReturningMultipleRows(query)
print ""
print "list values for 001"
query = "select * from FilesV2 where filehash = \"%s\";" % "001"
print dbInterface.ExecuteSqlQueryReturningMultipleRows(query)
print ""
print "testing RemoveFileCompletely"
print "removing 111111"
db.RemoveFileCompletely("111111")
print "OriginalDirectoriesForFileV5 table:"
query = "select * from OriginalDirectoriesForFileV5;"
print dbInterface.ExecuteSqlQueryReturningMultipleRows(query)
print ""
print "FilesV2 table:"
query = "select * from FilesV2;"
print dbInterface.ExecuteSqlQueryReturningMultipleRows(query)
print ""
print "removing 001"
db.RemoveFileCompletely("001")
print "OriginalDirectoriesForFileV5 table:"
query = "select * from OriginalDirectoriesForFileV5;"
print dbInterface.ExecuteSqlQueryReturningMultipleRows(query)
print ""
print "FilesV2 table:"
query = "select * from FilesV2;"
print dbInterface.ExecuteSqlQueryReturningMultipleRows(query)
print ""
print "FileLink table:"
query = "select * from FileLink;"
print dbInterface.ExecuteSqlQueryReturningMultipleRows(query)
print ""
print "testing AddDirSubdirMapping"
db.AddDirSubdirMapping("parentDirHash", "subdirHash")
db.AddDirSubdirMapping("parentDirHash2", "subdirHash2")
print "originalDirToSubdir table:"
query = "select * from originalDirToSubdir;"
print dbInterface.ExecuteSqlQueryReturningMultipleRows(query)
print ""
print "testing AddDirSubdirMapping"
print "check if subdirHash is subdir of parentDirHash - should return true"
print db.DirSubdirMappingExists("parentDirHash", "subdirHash")
print "check if subdirHash2 is subdir of parentDirHash - should return false"
print db.DirSubdirMappingExists("parentDirHash", "subdirHash2")
print ""
print "test GetListOfSubdirectoriesInOriginalDirectory"
dirpath = "C:\\anotherfakepath\\part2"
db.addDirectory("subdirHash",dirpath)
print db.GetListOfSubdirectoriesInOriginalDirectory("parentDirHash")
print ""
print "testing GetLargestFilesTodo"
print "current two largest files:"
print db.GetLargestFilesTodo(2)
print "adding new largest with hash 111"
db.AddFile("111", 1000000000000)
print "new two largest files"
print db.GetLargestFilesTodo(2)
print "testing setDirectoryStatus"
print "directory table before:"
query = "select * from originalDirectoriesV2;"
print dbInterface.ExecuteSqlQueryReturningMultipleRows(query)
print ""
print "setting status of %s to deleted" % dirHash
db.setDirectoryStatus(dirHash, "Deleted")
print "directory table after:"
query = "select * from originalDirectoriesV2;"
print dbInterface.ExecuteSqlQueryReturningMultipleRows(query)
print ""
print "testing setFileStatus"
print "FilesV2 table before:"
query = "select * from FilesV2;"
print dbInterface.ExecuteSqlQueryReturningMultipleRows(query)
print ""
print "setting status of %s to deleted" % "111"
db.setFileStatus("111", "Deleted")
print "FilesV2 table after:"
query = "select * from FilesV2;"
print dbInterface.ExecuteSqlQueryReturningMultipleRows(query)
print ""
# TODO: add negative implementation and test (what if hash not in db, we do not handle that at all)
print "testing getFileStatus"
print "getting status of 111, should be Deleted"
print db.getFileStatus("111")
print "getting status of 002, should be todo"
print db.getFileStatus("002")
print ""
print "buildCommandStringPart1"
print "calling with everything set to true"
print db.buildCommandStringPart1(2, True, True, True, True)
print "calling with includeTodo set to true"
print db.buildCommandStringPart1(2, True, False, False, False)
print "calling with includeTodoLater set to true"
print db.buildCommandStringPart1(2, False, True, False, False)
print "calling with includeToDelete set to true"
print db.buildCommandStringPart1(2, False, False, True, False)
print "calling with includeDeleted set to true"
print db.buildCommandStringPart1(2, False, False, False, True)
print "calling with includeTodo and includeToDelete set to true"
print db.buildCommandStringPart1(2, True, False, True, False)
print ""
print "testing GetLargestFiles"
print "putting 40 files in, hash values from 11 to 50, sizes from 1000011 to 1000050"
print "set 45 to todelete"
for i in range(11, 50):
hash = "%d" % i
size = 1000000 + i
db.AddFile(hash, size)
# add this for later tests
filepath = "C:\\trythisone\\%s.mov" % hash
db.AddOriginalFileLocation(hash, filepath)
db.setFileStatus("45", "todelete")
print "get largest, not including todelete:"
print db.Get30LargestFiles(10, True, True, False, True)
print ""
print "test JoinStringIfNeeded"
print "should return partial query string"
print db.JoinStringIfNeeded("a", None)
print "should return empty string"
print db.JoinStringIfNeeded(None, None)
print ""
print "test BuildExtensionSubQuery"
print "should be (1)"
print db.BuildExtensionSubQuery(None)
print "should be: extension COLLATE NOCASE IN (a, b, c)"
print db.BuildExtensionSubQuery("a, b, c")
print ""
print "test BuildSearchSubQuery"
print "should be (1)"
print db.BuildSearchSubQuery(None)
print "should be: filename like \'%.jpeg%\'"
print db.BuildSearchSubQuery(".jpeg")
print ""
print "test BuildStatusSubQuery"
print "should be (1)"
print db.BuildStatusSubQuery(None)
print "should be: status COLLATE NOCASE IN (deleted, todo)"
print db.BuildStatusSubQuery("deleted, todo")
print ""
print "test BuildFileLimitSubString"
print db.BuildFileLimitSubString(5)
print ""
#GetLargestFiles(self, numOfFiles, statusList, extensionList, searchTerm):
print "test GetLargestFiles"
print "using (4, None, None, None)"
print db.GetLargestFiles(4, None, None, None)
print ""
print "using (2, \"\"todo\"\", None, None)"
print db.GetLargestFiles(2, "\"todo\"", None, None)
print ""
print "using (5, None, \"\".mov\",\".jpg\"\", None)"
print db.GetLargestFiles(5, None, "\".mov\",\".jpg\"", None)
print ""
print "using (5, \"\"todo\"\", \"\".mov\",\".jpg\"\", \"\"5\"\")"
print db.GetLargestFiles(5, "\"todo\"", "\".mov\",\".jpg\"", "5")
print ""
print "test GetObjectStoreId"
print "negative test, should return none"
print db.GetObjectStoreId("\trythis\doesNotExst")
print "inserting c:\\test1\\test2"
newObjectStoreId = db.CheckObjectStoreExistsAndInsertIfNot("c:\\test1\\test2")
print "got newId %d, now get id and check is same" % newObjectStoreId
print db.GetObjectStoreId("c:\\test1\\test2")
print ""
print "test GetUndeletedFilesWithOnlyOneLocation and doInsertForAddFreshLocation"
print "first give some of our test files (21 to 45) location of objectstore just created"
for i in range(21, 46):
filehash = "%d" % i
db.doInsertForAddFreshLocation(filehash, newObjectStoreId)
print "now look for 5 undeleted files with one location, should get 21 to 25 "
print db.GetUndeletedFilesWithOnlyOneLocation(5)
print ""
print "test GetListOfFilesToDelete"
print db.GetListOfFilesToDelete()
print ""
print "test GetListOfFilesToDeleteWithLocation"
print db.GetListOfFilesToDeleteWithLocation(1)
print ""
print "test GetListOfFilesToTryUnDelete"
db.setFileStatus("45", "tryToUndelete")
print db.GetListOfFilesToTryUnDelete()
print ""
print "test GetFilesFromObjectStore"
print db.GetFilesFromObjectStore(1)
print ""
print "test GetObjectStores"
db.CheckObjectStoreExistsAndInsertIfNot("c:\\test3\\test4")
print db.GetObjectStores()
print ""
print "test GetRootDirectories and AddOriginalRootDirectoryIfNotInDb"
print "no root dirs initially: should get empty list here"
print db.GetRootDirectories()
print "adding root dirs \"C:\\root1\" and \"C:\\root2\" the querying again"
db.AddOriginalRootDirectoryIfNotInDb("C:\\root1")
db.AddOriginalRootDirectoryIfNotInDb("C:\\root2")
db.AddOriginalRootDirectoryIfNotInDb("C:\\root1")
print db.GetRootDirectories()
print ""
print "test numOfFilesWithOnlyOneLocation"
print db.numOfFilesWithOnlyOneLocation(1, "objectStore2", "objectStore1", "objectStore3")
print db.numOfFilesWithOnlyOneLocation(1, "objectStore1", "objectStore2", "objectStore3")
print ""
print "test DeleteObjectStore"
print "try deleting from 1 - cannot as has files, will return false"
print db.DeleteObjectStore(1)
print "delete 2, will return true"
print db.DeleteObjectStore(2)
print "listing object stores"
command = "select * from objectStores"
print dbInterface.ExecuteSqlQueryReturningMultipleRows(command)
print ""
print "test UpdateObjectStore"
db.UpdateObjectStore(1, "C:\\newpath\\newsubdir")
command = "select * from objectStores"
print dbInterface.ExecuteSqlQueryReturningMultipleRows(command)
print ""
print "test getFirstFilenameForFile"
print db.getFirstFilenameForFile("12")
print ""
print "test GetOriginalDirectoriesForFile"
print db.GetOriginalDirectoriesForFile("12")
print ""
print "test GetOriginalDirectoryWithPath"
print db.GetOriginalDirectoryWithPath("C:\\trythisone")
print ""
dirHash = db.GetOriginalDirectoryWithPath("C:\\trythisone")[0][1]
print "test GetDirectoryPathForDirHash"
print db.GetDirectoryPathForDirHash(dirHash)
print ""
print "test UpdateStatusForDirectoryAndContents"
print "set to delete"
db.UpdateStatusForDirectoryAndContents(dirHash, "todelete")
print db.GetOriginalDirectoryWithPath("C:\\trythisone")
print ""
print "test GetListOfFilesInOriginalDirectory"
print db.GetListOfFilesInOriginalDirectory(dirHash)
print ""
print "test UpdateFileAndSubDirListForDir and GetFileListForDirectory"
db.UpdateFileAndSubDirListForDir(dirHash)
print db.GetFileListForDirectory(dirHash)
print ""
print "test GetStatusOfDirectory"
print db.GetStatusOfDirectory(dirHash)
print ""
print "test GetStatusOfFile"
print db.GetStatusOfFile("41")
print ""
print "test GetSubdirectories (use UpdateFileAndSubDirListForDir first)"
db.AddDirSubdirMapping(dirHash, "subdirHash")
db.AddDirSubdirMapping(dirHash, "subdirHash2")
db.UpdateFileAndSubDirListForDir(dirHash)
print db.GetSubdirectories(dirHash)
print ""
print "test doInsertForAddFreshLocation"
command = "select * from fileLocations"
print dbInterface.ExecuteSqlQueryReturningMultipleRows(command)
print ""
print db.doInsertForAddFreshLocation("555", 1)
print ""
print "listing fileLocations"
command = "select * from fileLocations"
print dbInterface.ExecuteSqlQueryReturningMultipleRows(command)
print ""
print "test InsertAdditionalFileLocation"
command = "select * from fileLocations"
print dbInterface.ExecuteSqlQueryReturningMultipleRows(command)
print ""
print db.InsertAdditionalFileLocation("555", 7)
print ""
print "listing fileLocations"
command = "select * from fileLocations"
print dbInterface.ExecuteSqlQueryReturningMultipleRows(command)
print ""
print "test ReplaceFileLocation"
print db.ReplaceFileLocation("555", 1, 8)
print ""
print "listing fileLocations"
command = "select * from fileLocations"
print dbInterface.ExecuteSqlQueryReturningMultipleRows(command)
print ""
print "test GetFileLocations"
print db.GetFileLocations("555")
print db.GetFileLocations("afdadfdfa")
print ""
print "test AddFileLocation"
print "first try to add existing location"
db.AddFileLocation("555", 7)
print db.GetFileLocations("555")
print ""
print "add new location"
db.AddFileLocation("555", 9)
print db.GetFileLocations("555")
print "try to add too many"
try:
db.AddFileLocation("555", 15)
except:
print "got an exception"
print "add location for file without location"
db.AddFileLocation("444", 2)
print db.GetFileLocations("444")
print ""
print "test addFileLocation2"
print "first try to add to existing location"
db.addFileLocation2("1111", "C:\\newpath\\newsubdir")
print db.GetFileLocations("1111")
print ""
print "add new location"
db.addFileLocation2("1111", "C:\\anotherpath\\newsubdir")
print db.GetFileLocations("1111")
print ""
print "test GetObjectStorePathsForFile"
print db.GetObjectStorePathsForFile("adfag")
print db.GetObjectStorePathsForFile("1111")
print ""
print "test SetToDelete"
print db.GetStatusOfFile("41")
db.SetToDelete("41")
print db.GetStatusOfFile("41")
print ""
print "test SetToRemoveCompletely"
print db.GetStatusOfFile("41")
db.SetToRemoveCompletely("41")
print db.GetStatusOfFile("41")
print ""
print "test SetToLater"
db.SetToLater("41")
print db.GetStatusOfFile("41")
print ""
print "test SetNewStatusIfNotDeleted"
print db.GetStatusOfFile("41")
db.SetNewStatusIfNotDeleted("41", "deleted")
print db.GetStatusOfFile("41")
db.SetNewStatusIfNotDeleted("41", "badStatus")
print db.GetStatusOfFile("41")
print ""
print "test GetListOfFilesWithExtensionInOneObjectStore"
print db.GetListOfFilesWithExtensionInOneObjectStore(".txt", "C:\\newpath\\newsubdir")
db.AddFileLocation("11", newObjectStoreId)
db.AddFileLocation("12", newObjectStoreId)
db.AddFileLocation("13", newObjectStoreId)
db.AddOriginalFileLocation("13", "C:\\tt.jpg")
print db.GetListOfFilesWithExtensionInOneObjectStore(".jpg", "C:\\newpath\\newsubdir")
print db.GetListOfFilesWithExtensionInOneObjectStore(".mov", "C:\\newpath\\newsubdir")
print ""
print "test MoveFileLocation"
command = "select * from fileLocations where filehash = 11"
print dbInterface.ExecuteSqlQueryReturningMultipleRows(command)
db.MoveFileLocation("11", "C:\\newpath\\newsubdir", "C:\\newObjectStore")
command = "select * from fileLocations where filehash = 11"
print dbInterface.ExecuteSqlQueryReturningMultipleRows(command)
print ""
print "test GetNumberOfFiles"
print db.GetNumberOfFiles()
print ""
print "test GetNumberOfOriginalVersionsOfFiles"
print db.GetNumberOfOriginalVersionsOfFiles()
print ""
print "test GetNumberOfDirectories"
print db.GetNumberOfDirectories()
print ""
print "test GetListOfExtensions"
print db.GetListOfExtensions(True)
print ""
print db.GetListOfExtensions(False)
print ""
print "test GetDirPathHashListForDirectoriesWithStatus"
print db.GetDirPathHashListForDirectoriesWithStatus("todelete")
print ""
print db.GetDirPathHashListForDirectoriesWithStatus("Deleted")
print ""
db.CloseConnection()