-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconftest.py
89 lines (71 loc) · 3 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
"""Project conftest"""
import os
from types import SimpleNamespace
import pytest
import yaml
from fixtures.common import * # pylint:disable=wildcard-import,unused-wildcard-import # noqa: F403
from websites.constants import OMNIBUS_STARTER_SLUG
from websites.models import WebsiteStarter
from websites.site_config_api import SiteConfig
@pytest.fixture(autouse=True)
def default_settings(settings): # noqa: PT004
"""Set default settings for all tests"""
settings.DISABLE_WEBPACK_LOADER_STATS = True
@pytest.fixture()
def mocked_celery(mocker):
"""Mock object that patches certain celery functions"""
exception_class = TabError
replace_mock = mocker.patch(
"celery.app.task.Task.replace", autospec=True, side_effect=exception_class
)
group_mock = mocker.patch("celery.group", autospec=True)
chain_mock = mocker.patch("celery.chain", autospec=True)
return SimpleNamespace(
replace=replace_mock,
group=group_mock,
chain=chain_mock,
replace_exception_class=exception_class,
)
@pytest.fixture()
@pytest.mark.django_db()
def course_starter(settings):
"""Returns the 'course'-type WebsiteStarter that is seeded in a data migration""" # noqa: D401
return WebsiteStarter.objects.get(slug=settings.OCW_COURSE_STARTER_SLUG)
@pytest.fixture()
@pytest.mark.django_db()
def omnibus_starter():
"""Returns the omnibus WebsiteStarter that is seeded in a data migration""" # noqa: D401
return WebsiteStarter.objects.get(slug=OMNIBUS_STARTER_SLUG)
@pytest.fixture()
@pytest.mark.django_db()
def omnibus_config(settings):
"""Returns the omnibus site config""" # noqa: D401
with open( # noqa: PTH123
os.path.join( # noqa: PTH118
settings.BASE_DIR, "localdev/configs/omnibus-site-config.yml"
)
) as f:
raw_config = f.read().strip()
parsed_config = yaml.load(raw_config, Loader=yaml.SafeLoader)
return SiteConfig(parsed_config)
def pytest_addoption(parser):
"""Pytest hook that adds command line parameters"""
parser.addoption(
"--simple",
action="store_true",
help="Run tests only (no cov, warning output silenced)",
)
def pytest_configure(config):
"""Pytest hook to perform some initial configuration"""
if getattr(config.option, "simple") is True: # noqa: B009
# NOTE: These plugins are already configured by the time the pytest_cmdline_main
# hook is run, so we can't simply add/alter the command line options in
# that hook. This hook is being used to reconfigure/unregister plugins
# that we can't change via the pytest_cmdline_main hook. Switch off
# coverage plugin
cov = config.pluginmanager.get_plugin("_cov")
cov.options.no_cov = True
# Remove warnings plugin to suppress warnings
if config.pluginmanager.has_plugin("warnings"):
warnings_plugin = config.pluginmanager.get_plugin("warnings")
config.pluginmanager.unregister(warnings_plugin)