Skip to content

Commit e7ee843

Browse files
testing rest engine param update
Signed-off-by: Lanuti_emanuele <[email protected]>
1 parent 0732820 commit e7ee843

File tree

1 file changed

+134
-1
lines changed

1 file changed

+134
-1
lines changed

tests/unit/test_sqconfig_load.py

Lines changed: 134 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
from suzieq.shared.utils import (get_sq_install_dir, load_sq_config,
77
validate_sq_config)
88
from tests.conftest import create_dummy_config_file
9-
9+
from suzieq.shared.context import SqContext
10+
from suzieq.cli.sqcmds import context_commands
11+
from dataclasses import dataclass
1012

1113
@pytest.mark.sq_config
1214
def test_valid_sq_config():
@@ -90,3 +92,134 @@ def test_config_validation(monkeypatch):
9092
monkeypatch.setenv(env_var, env_key)
9193
error = validate_sq_config(cfg)
9294
assert error is None, error
95+
96+
97+
@ pytest.mark.sq_config
98+
@ pytest.mark.rest
99+
def test_config_rest():
100+
#with pandas engine
101+
cfg = {'rest': {}}
102+
103+
ctxt = SqContext(cfg=cfg)
104+
#check that the default value are the same
105+
assert ctxt.rest_transport == 'https'
106+
assert ctxt.rest_server_ip == '127.0.0.1'
107+
assert ctxt.rest_server_port == 8000
108+
assert ctxt.rest_api_key == ''
109+
assert ctxt.engine == 'pandas'
110+
111+
# defining rest engine params
112+
cfg['rest']['API_KEY'] = '496157e6e869ef7f3d6ecb24a6f6d847b224ee4f'
113+
cfg['rest']['address'] = '0.0.0.0'
114+
cfg['rest']['port'] = 8000
115+
cfg['rest']['no-https'] = True
116+
cfg['ux']= {'engine': 'rest'}
117+
118+
ctxt = SqContext(cfg=cfg)
119+
assert ctxt.rest_transport == 'http'
120+
assert ctxt.rest_server_ip == '0.0.0.0'
121+
assert ctxt.rest_server_port == 8000
122+
assert ctxt.rest_api_key == '496157e6e869ef7f3d6ecb24a6f6d847b224ee4f'
123+
assert ctxt.engine == 'rest'
124+
125+
#https with rest engine
126+
cfg['rest']['no-https'] = False
127+
ctxt = SqContext(cfg=cfg)
128+
assert ctxt.rest_transport == 'https'
129+
assert ctxt.rest_server_ip == '0.0.0.0'
130+
assert ctxt.rest_server_port == 8000
131+
assert ctxt.rest_api_key == '496157e6e869ef7f3d6ecb24a6f6d847b224ee4f'
132+
assert ctxt.engine == 'rest'
133+
134+
135+
136+
137+
138+
@ pytest.mark.sq_config
139+
@ pytest.mark.rest
140+
def test_context_commands_context(monkeypatch):
141+
CTXT_REST_ATTRS = {
142+
'rest_server_ip': 'address',
143+
'rest_server_port': 'port',
144+
'rest_transport': 'no-https',
145+
'rest_api_key': 'API_KEY'}
146+
147+
@dataclass
148+
class fake_ctxt_class():
149+
engine = 'rest'
150+
cfg = {'rest':
151+
{
152+
'address': '0.0.0.0',
153+
'port': '8000',
154+
'no-https': True,
155+
'API_KEY': '496157e6e869ef7f3d6ecb24a6f6d847b224ee4f'
156+
}}
157+
rest_server_ip: str = 'test_rest_server_ip'
158+
rest_server_port: int = 8080
159+
rest_api_key: str = 'test_rest_api_key'
160+
rest_transport: str = 'test_rest_transport'
161+
162+
@dataclass
163+
class fake_context_class():
164+
ctxt = fake_ctxt_class()
165+
166+
def change_engine(self, engine):
167+
self.ctxt.engine = engine
168+
169+
def fake_get_context():
170+
return fake_context_class()
171+
172+
173+
#sending set engine: rest when is already selected, nothing should change
174+
monkeypatch.setattr(context_commands.context,
175+
'get_context',
176+
fake_get_context)
177+
context_commands.set_ctxt(engine='rest')
178+
assert context_commands.context.get_context().ctxt.engine == 'rest'
179+
assert getattr(context_commands.context.get_context().ctxt,
180+
'rest_server_ip') == 'test_rest_server_ip'
181+
assert getattr(context_commands.context.get_context().ctxt,
182+
'rest_server_port') == 8080
183+
assert getattr(context_commands.context.get_context().ctxt,
184+
'rest_api_key') == 'test_rest_api_key'
185+
assert getattr(context_commands.context.get_context().ctxt,
186+
'rest_transport') == 'test_rest_transport'
187+
188+
189+
#sending set engine: rest when is selected engine: pandas with http
190+
fake_context_class.ctxt.engine = 'pandas'
191+
monkeypatch.setattr(context_commands.context,
192+
'get_context',
193+
fake_get_context)
194+
context_commands.set_ctxt(engine='rest')
195+
assert context_commands.context.get_context().ctxt.engine == 'rest'
196+
for attr in CTXT_REST_ATTRS:
197+
#get the expexted value of the rest param
198+
expected_value = fake_ctxt_class.cfg['rest'][CTXT_REST_ATTRS[attr]]
199+
if CTXT_REST_ATTRS[attr] == 'no-https':
200+
expected_value = 'http' if expected_value == True else 'https'
201+
#check if all the rest attr match
202+
assert getattr(
203+
context_commands.context.get_context().ctxt,
204+
attr) == expected_value, f'{attr} value shold be {expected_value},\
205+
not {getattr(context_commands.context.get_context().ctxt, attr)}'
206+
207+
208+
#sending set engine: rest when is selected engine: pandas with https
209+
fake_context_class.ctxt.engine = 'pandas'
210+
fake_ctxt_class.cfg['rest']['no-https'] = False
211+
monkeypatch.setattr(context_commands.context,
212+
'get_context',
213+
fake_get_context)
214+
context_commands.set_ctxt(engine='rest')
215+
assert context_commands.context.get_context().ctxt.engine == 'rest'
216+
for attr in CTXT_REST_ATTRS:
217+
#get the expexted value of the rest param
218+
expected_value = fake_ctxt_class.cfg['rest'][CTXT_REST_ATTRS[attr]]
219+
if CTXT_REST_ATTRS[attr] == 'no-https':
220+
expected_value = 'http' if expected_value == True else 'https'
221+
#check if all the rest attr match
222+
assert getattr(
223+
context_commands.context.get_context().ctxt,
224+
attr) == expected_value, f'{attr} value shold be {expected_value},\
225+
not {getattr(context_commands.context.get_context().ctxt, attr)}'

0 commit comments

Comments
 (0)