2021-02-27 15:01:25 +01:00
|
|
|
import os
|
2021-03-06 16:21:28 +01:00
|
|
|
import pytest
|
2021-02-27 15:01:25 +01:00
|
|
|
import tempfile
|
|
|
|
import time
|
|
|
|
|
|
|
|
from platypush.message.event.custom import CustomEvent
|
|
|
|
|
2021-03-06 16:21:28 +01:00
|
|
|
from .utils import register_user, send_request
|
2021-02-27 15:01:25 +01:00
|
|
|
|
2021-03-06 17:03:50 +01:00
|
|
|
tmp_files = []
|
|
|
|
|
2021-02-27 15:01:25 +01:00
|
|
|
|
2021-03-06 16:21:28 +01:00
|
|
|
@pytest.fixture(scope='module', autouse=True)
|
|
|
|
def user(*_):
|
|
|
|
register_user()
|
2021-02-27 15:01:25 +01:00
|
|
|
|
|
|
|
|
2021-03-06 17:03:50 +01:00
|
|
|
@pytest.fixture(scope='module', autouse=True)
|
2021-03-06 16:21:28 +01:00
|
|
|
def tmp_file(*_):
|
2021-03-06 17:03:50 +01:00
|
|
|
n_files = 2
|
|
|
|
for _ in range(n_files):
|
|
|
|
tmp_file = tempfile.NamedTemporaryFile(prefix='platypush-test-procedure-', suffix='.txt', delete=False)
|
|
|
|
tmp_files.append(tmp_file.name)
|
|
|
|
|
|
|
|
yield
|
2021-02-27 15:01:25 +01:00
|
|
|
|
2021-03-06 17:03:50 +01:00
|
|
|
for f in tmp_files:
|
|
|
|
if os.path.isfile(f):
|
|
|
|
os.unlink(f)
|
2021-02-27 15:01:25 +01:00
|
|
|
|
2021-03-06 17:03:50 +01:00
|
|
|
|
|
|
|
def check_file_content(expected_content: str, tmp_file: str):
|
|
|
|
assert os.path.isfile(tmp_file), 'The expected output file was not created'
|
|
|
|
with open(tmp_file, 'r') as f:
|
2021-03-06 16:21:28 +01:00
|
|
|
content = f.read()
|
2021-02-27 15:01:25 +01:00
|
|
|
|
2021-03-06 16:21:28 +01:00
|
|
|
assert content == expected_content, 'The output file did not contain the expected text'
|
2021-02-27 15:01:25 +01:00
|
|
|
|
|
|
|
|
2021-03-06 17:03:50 +01:00
|
|
|
def test_procedure_call(base_url):
|
2021-03-06 16:21:28 +01:00
|
|
|
"""
|
|
|
|
Test the result of a procedure invoked directly over HTTP.
|
|
|
|
"""
|
|
|
|
output_text = 'Procedure test'
|
|
|
|
send_request(
|
|
|
|
action='procedure.write_file',
|
|
|
|
args={
|
2021-03-06 17:03:50 +01:00
|
|
|
'file': tmp_files[0],
|
2021-03-06 16:21:28 +01:00
|
|
|
'content': output_text,
|
|
|
|
})
|
2021-02-27 15:01:25 +01:00
|
|
|
|
2021-03-06 17:03:50 +01:00
|
|
|
check_file_content(expected_content=output_text, tmp_file=tmp_files[0])
|
2021-02-27 15:01:25 +01:00
|
|
|
|
2021-03-06 16:21:28 +01:00
|
|
|
|
2021-03-06 17:03:50 +01:00
|
|
|
def test_procedure_from_event(app, base_url):
|
2021-03-06 16:21:28 +01:00
|
|
|
"""
|
|
|
|
Test the result of a procedure triggered by an event.
|
|
|
|
"""
|
|
|
|
output_text = 'Procedure from event test'
|
2021-03-06 17:03:50 +01:00
|
|
|
event_type = 'test_procedure'
|
2021-03-06 16:21:28 +01:00
|
|
|
# noinspection PyUnresolvedReferences
|
2021-03-06 17:03:50 +01:00
|
|
|
app.bus.post(CustomEvent(subtype=event_type, file=tmp_files[1], content=output_text))
|
2021-03-06 16:25:37 +01:00
|
|
|
time.sleep(3)
|
2021-03-06 17:03:50 +01:00
|
|
|
check_file_content(expected_content=output_text, tmp_file=tmp_files[1])
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
pytest.main()
|
2021-02-27 15:01:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
# vim:sw=4:ts=4:et:
|