forked from AlexStark9/dm_api_tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconftest.py
153 lines (120 loc) · 3.83 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import pytest
import random
import structlog
from vyper import v
from pathlib import Path
from string import ascii_letters, digits
from apis.dm_api_search_async import SearchEngineStub
from generic.assertions.post_v1_account import AssertionsPostV1Account
from generic.helpers.orm_db import OrmDatabase
from generic.helpers.search import Search
from services.dm_api_account import Facade
from collections import namedtuple
from generic.helpers.mailhog import MailhogApi
from grpclib.client import Channel
structlog.configure(
processors=[
structlog.processors.JSONRenderer(indent=4, sort_keys=True, ensure_ascii=False)
]
)
@pytest.fixture
def generating_test_data(begin: int = 6, end: int = 30):
data = []
symbols = ascii_letters + digits
for i in range(3):
string = ''
for _ in range(random.randint(begin, end)):
string += random.choice(symbols)
if i == 1:
data.append(string + '@mail.com')
else:
data.append(string)
user = namedtuple('Data', 'login, email, password')
Data = user(login=data[0], email=data[1], password=data[2])
return Data
@pytest.fixture
def generating_invalid_test_data():
login = ''
email = ''
password = ''
symbols = ascii_letters + digits
for _ in range(random.randint(1, 1)):
login += random.choice(symbols)
for _ in range(random.randint(1, 10)):
email = email + random.choice(symbols) + 'mail.com'
for _ in range(random.randint(1, 5)):
login += random.choice(symbols)
user = namedtuple('Data', 'login, email, password')
Data = user(login=login, email=email, password=password)
return Data
@pytest.fixture
def mailhog():
return MailhogApi(host=v.get('service.mailhog'))
@pytest.fixture
def dm_api_facade(mailhog):
return Facade(
host=v.get('service.dm_api_account'),
mailhog=mailhog
)
@pytest.fixture()
def assertion(dm_db):
return AssertionsPostV1Account(dm_db)
@pytest.fixture
def dm_db():
db = OrmDatabase(
user=v.get('database.dm3_5.user'),
password=v.get('database.dm3_5.password'),
host=v.get('database.dm3_5.host'),
database=v.get('database.dm3_5.database')
)
return db
@pytest.fixture
def prepare_user(dm_api_facade, dm_db, generating_test_data):
user = namedtuple('User', 'login, email, password')
User = user(
login=generating_test_data.login,
email=generating_test_data.email,
password=generating_test_data.password
)
dm_db.delete_user_by_login(login=User.login)
dataset = dm_db.get_users_by_login(login=User.login)
assert len(dataset) == 0
dm_api_facade.mailhog.delete_all_messages()
return User
@pytest.fixture
def random_string(begin, end):
symbol = ascii_letters + digits
string = ''
for _ in range(random.randint(begin, end)):
string += random.choice(symbol)
return string
options = (
'service.dm_api_account',
'service.mailhog',
'service.dm3_5.host'
)
@pytest.fixture(autouse=True)
def set_config(request):
config = Path(__file__).parent.joinpath('config')
print(config)
config_name = request.config.getoption('--env')
v.set_config_name(config_name)
v.add_config_path(config)
v.read_in_config()
for option in options:
v.set(option, request.config.getoption(f'--{option}'))
def pytest_addoption(parser):
parser.addoption('--env', action='store', default='stg')
for option in options:
parser.addoption(f'--{option}', action='store', default=None)
@pytest.fixture
def grpc_search():
client = Search(target=v.get('service.dm_api_search'))
yield client
client.grpc_search.close()
@pytest.fixture
def grpc_search_async():
channel = Channel(host='5.63.153.31', port=5052)
client = SearchEngineStub(channel)
yield client
channel.close()