1
|
|
|
import os |
2
|
|
|
import unittest |
3
|
|
|
|
4
|
|
|
from django.conf import settings |
5
|
|
|
from django.core.management import BaseCommand |
6
|
|
|
|
7
|
|
|
from cryoweb.models import db_has_data as cryoweb_has_data |
8
|
|
|
from uid.models import Submission, db_has_data as image_has_data |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
class Command(BaseCommand): |
12
|
|
|
# This will be displayed when calling: python manage.py mycheck --help |
13
|
|
|
help = """Performs a series of checks on the real Image database""" |
14
|
|
|
|
15
|
|
|
def handle(self, *args, **options): |
16
|
|
|
suite = unittest.TestLoader().loadTestsFromTestCase(TestDB) |
17
|
|
|
unittest.TextTestRunner().run(suite) |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
class TestDB(unittest.TestCase): |
21
|
|
|
"""Testing database status""" |
22
|
|
|
|
23
|
|
|
def test_db1_animal_table_is_not_empty(self): |
24
|
|
|
"""Tests that cryoweb.animal table is not empty""" |
25
|
|
|
|
26
|
|
|
self.assertFalse( |
27
|
|
|
cryoweb_has_data(), |
28
|
|
|
msg="%s database has data!!!" % ( |
29
|
|
|
settings.DATABASES['cryoweb']['NAME'])) |
30
|
|
|
|
31
|
|
|
def test_db2_animal_table_is_not_empty(self): |
32
|
|
|
"""Tests that image animal table is not empty""" |
33
|
|
|
|
34
|
|
|
self.assertTrue( |
35
|
|
|
image_has_data(), |
36
|
|
|
msg="%s database is empty!!!" % ( |
37
|
|
|
settings.DATABASES['default']['NAME'])) |
38
|
|
|
|
39
|
|
|
def test_no_orphaned_backup_files_in_filesys(self): |
40
|
|
|
"""Tests no orphaned backup files in /media/data_source dir""" |
41
|
|
|
|
42
|
|
|
# get the file list from the filesystem |
43
|
|
|
data_source_dir = os.path.join( |
44
|
|
|
settings.PROTECTED_MEDIA_ROOT, |
45
|
|
|
Submission.upload_dir) |
46
|
|
|
|
47
|
|
|
data_source_files = os.listdir(data_source_dir) |
48
|
|
|
|
49
|
|
|
# prepend Submission.upload_dir to file list |
50
|
|
|
data_source_files = [ |
51
|
|
|
os.path.join( |
52
|
|
|
Submission.upload_dir, |
53
|
|
|
uploaded_file) |
54
|
|
|
for uploaded_file in data_source_files] |
55
|
|
|
|
56
|
|
|
# get the file list from the db |
57
|
|
|
queryset = Submission.objects.all() |
58
|
|
|
database_files = [ |
59
|
|
|
str(datasource.uploaded_file) for datasource in queryset] |
60
|
|
|
|
61
|
|
|
# Order the two lists |
62
|
|
|
data_source_files.sort() |
63
|
|
|
database_files.sort() |
64
|
|
|
|
65
|
|
|
self.assertListEqual( |
66
|
|
|
data_source_files, |
67
|
|
|
database_files, |
68
|
|
|
"\n " |
69
|
|
|
"\n Orphaned backup files found " |
70
|
|
|
"\n in {}" |
71
|
|
|
"\n try " |
72
|
|
|
"\n $ ...manage.py clean_backup command " |
73
|
|
|
"".format(settings.PROTECTED_MEDIA_ROOT)) |
74
|
|
|
|