forked from kobotoolbox/kobocat
-
Notifications
You must be signed in to change notification settings - Fork 4
/
conftest.py
93 lines (75 loc) · 2.85 KB
/
conftest.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
# coding: utf-8
import os
import pytest
import sys
from django.conf import settings
from onadata.libs.utils.storage import rmdir, default_storage
TEST_USERNAMES = [
'alice',
'auser',
'bob',
'carl',
'deno'
'harry',
'jo',
'lilly',
'peter',
]
def stderr_prompt(message):
sys.stderr.write(message)
return input().strip()
def toggle_capturing(capture_manager, stop):
if stop:
capture_manager.suspend_global_capture()
capture_manager.stop_global_capturing()
else:
capture_manager.start_global_capturing()
capture_manager.resume_global_capture()
@pytest.fixture(scope="session", autouse=True)
def setup(request):
# We need to disable global capturing in case `-s` is not passed to `pytest`
# by the users to force print the safeguard messages about data loss.
capture_manager = request.config.pluginmanager.getplugin("capturemanager")
is_global_capturing = capture_manager.is_globally_capturing()
if is_global_capturing:
toggle_capturing(capture_manager, stop=True)
for username in TEST_USERNAMES:
if default_storage.exists(username):
response = stderr_prompt(
'\n\n'
'WARNING - DATA LOSS! A storage directory already exists for '
'user {}, but it will be DELETED if you continue with these '
'tests!\nPlease type "yes" to proceed anyway, or "no" to '
'cancel: '.format(username)
)
if response.lower() != 'yes':
if is_global_capturing:
toggle_capturing(capture_manager, stop=False)
pytest.exit('User interrupted tests', pytest.ExitCode.INTERRUPTED)
if 'instances' in settings.MONGO_DB.collection_names():
response = stderr_prompt(
'\n\n'
'WARNING: the MongoDB collection {}.instances already exists!\n'
"Type 'yes' if you would like to delete it, or 'no' to "
'cancel: '.format(settings.MONGO_DB.name)
)
if response.lower() == 'yes':
settings.MONGO_DB.instances.drop()
else:
if is_global_capturing:
toggle_capturing(capture_manager, stop=False)
pytest.exit('User interrupted tests', pytest.ExitCode.INTERRUPTED)
if is_global_capturing:
toggle_capturing(capture_manager, stop=False)
request.addfinalizer(_tear_down)
def _tear_down():
print("\nCleaning testing environment...")
print('Removing MongoDB...')
settings.MONGO_DB.instances.drop()
root_path = os.path.dirname(os.path.realpath(__file__))
if os.path.exists(os.path.join(root_path, 'db.sqlite3')):
print('Removing SQL DB...')
os.remove(os.path.join(root_path, 'db.sqlite3'))
print("Removing users' storage...")
for username in TEST_USERNAMES:
rmdir(username)