-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconftest.py
68 lines (51 loc) · 1.6 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
import sys
from pathlib import Path
import pytest
from dotenv import load_dotenv
from mixer.backend.flask import mixer as _mixer
load_dotenv()
BASE_DIR = Path(__file__).resolve(strict=True).parent.parent
sys.path.append(str(BASE_DIR))
try:
from yacut import app, db
from yacut.models import URLMap
except NameError:
raise AssertionError(
'Не обнаружен объект приложения. Создайте экземпляр класса Flask и назовите его app.',
)
except ImportError as exc:
if any(obj in exc.name for obj in ['models', 'URLMap']):
raise AssertionError('В файле models не найдена модель URLMap')
raise AssertionError('Не обнаружен объект класса SQLAlchemy. Создайте его и назовите db.')
@pytest.fixture
def default_app():
with app.app_context():
yield app
@pytest.fixture
def _app(tmp_path):
db_path = tmp_path / 'test_db.sqlite3'
db_uri = 'sqlite:///' + str(db_path)
app.config.update({
'TESTING': True,
'SQLALCHEMY_DATABASE_URI': db_uri,
'WTF_CSRF_ENABLED': False,
})
with app.app_context():
db.create_all()
yield app
db.drop_all()
db.session.close()
db_path.unlink()
@pytest.fixture
def client(_app):
return _app.test_client()
@pytest.fixture
def cli_runner():
return app.test_cli_runner()
@pytest.fixture
def mixer():
_mixer.init_app(app)
return _mixer
@pytest.fixture
def short_python_url(mixer):
return mixer.blend(URLMap, original='https://www.python.org', short='py')