-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathconftest.py
More file actions
84 lines (60 loc) · 2.16 KB
/
conftest.py
File metadata and controls
84 lines (60 loc) · 2.16 KB
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
import copy
import importlib
import sys
import time
from collections import defaultdict
from datetime import datetime, timedelta, timezone
from typing import Dict
from unittest import mock
import pytest
from modelgauge.secret_values import (
get_all_secrets,
)
@pytest.fixture()
def cwd_tmpdir(monkeypatch, tmp_path):
monkeypatch.chdir(tmp_path)
return tmp_path
@pytest.fixture()
def fake_secrets(value="some-value"):
secrets = get_all_secrets()
raw_secrets: Dict[str, Dict[str, str]] = {}
for secret in secrets:
if secret.scope not in raw_secrets:
raw_secrets[secret.scope] = {}
raw_secrets[secret.scope][secret.key] = value
return raw_secrets
@pytest.fixture
def start_time():
return datetime.now(timezone.utc)
@pytest.fixture
def end_time():
return datetime.now(timezone.utc) + timedelta(minutes=2)
def pytest_addoption(parser):
parser.addoption(
"--expensive-tests",
action="store_true",
dest="expensive-tests",
help="enable expensive tests",
)
# This monkeypatch makes it possible to run the tests without having to have an actual config file and should work
# with any additional secrets going forward. It has to be weird because it has to be done before the import of
# sut_factory as secrets are loaded during the import of the module, when the SUT_FACTORY is instantiated.
@pytest.hookimpl(tryfirst=True)
def pytest_sessionstart(session):
import modelgauge.config as mg_config
mock_secret = defaultdict(lambda: defaultdict(lambda: "fake-secret"))
mock_secret["demo"] = {"api_key": "12345"}
original_func = copy.copy(mg_config.load_secrets_from_config)
def new_func(path=None):
if not path:
return mock_secret
else:
return original_func(path)
mg_config.load_secrets_from_config = new_func
if "modelgauge.sut_factory" in sys.modules:
importlib.reload(sys.modules["modelgauge.sut_factory"])
actual_time_sleep = time.sleep
@pytest.fixture(scope="session", autouse=True)
def sleep_faster():
with mock.patch("time.sleep", lambda x: actual_time_sleep(x / 100000)) as _fixture:
yield _fixture