zhongrj
2025-11-24 276323dce9613867abb3f58a4cc2abbfb2fd0dea
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
import os
 
from django import db
from django.contrib.auth.models import User
from django.test import TestCase
from django.test import TransactionTestCase
from shutil import rmtree
 
from app.boot import boot
from app.models import Project
from webodm import settings
 
 
def setupUsers():
    User.objects.create_superuser(username='testsuperuser',
                                  email='superuser@test.com',
                                  password='test1234')
    User.objects.create_user(username='testuser',
                             email='user@test.com',
                             password='test1234')
    User.objects.create_user(username='testuser2',
                             email='user2@test.com',
                             password='test1234')
 
 
def setupProjects():
    Project.objects.create(
        owner=User.objects.get(username="testsuperuser"),
        name="Super User Test Project",
        description="This is a test project"
    )
    Project.objects.create(
        owner=User.objects.get(username="testuser"),
        name="User Test Project",
        description="This is a test project"
    )
    Project.objects.create(
        owner=User.objects.get(username="testuser2"),
        name="User 2 Test Project",
        description="This is a test project"
    )
 
 
def cleanup():
    if settings.TESTING and \
            os.path.exists(settings.MEDIA_ROOT) and \
                    "_test" in settings.MEDIA_ROOT[-6:]:
        rmtree(settings.MEDIA_ROOT)
        print("Cleaned " + settings.MEDIA_ROOT)
 
 
class BootTestCase(TestCase):
    '''
    This class provides optional default mock data as well as 
    proper boot initialization code. All tests for the app
    module should derive from this class instead of TestCase.
 
    We don't use fixtures because we have signal initialization login
    for some models, which doesn't play well with them.
    '''
    @classmethod
    def setUpTestData(cls):
        super(BootTestCase, cls).setUpTestData()
        cleanup()
        boot()
        setupUsers()
        setupProjects()
 
    @classmethod
    def tearDownClass(cls):
        super(BootTestCase, cls).tearDownClass()
 
 
class BootTransactionTestCase(TransactionTestCase):
    '''
    Same as above, but inherits from TransactionTestCase
    '''
    def setUp(self):
        super().setUp()
        cleanup()
        boot()
        setupUsers()
        setupProjects()
 
    @classmethod
    def tearDownClass(cls):
        super(BootTransactionTestCase, cls).tearDownClass()