Finalse pytest methods

This commit is contained in:
Leigh Morresi
2021-02-21 13:41:00 +01:00
parent b46a7fc4b1
commit 1718e2e86f
7 changed files with 161 additions and 73 deletions

View File

@@ -1,39 +1,98 @@
#!/usr/bin/python3
import pytest
import backend
from backend import store
import os
import time
import requests
# https://github.com/pallets/flask/blob/1.1.2/examples/tutorial/tests/test_auth.py
import pytest
from flask import url_for
from urllib.request import urlopen
# Much better boilerplate than the docs
# https://www.python-boilerplate.com/py3+flask+pytest/
def set_original_response():
test_return_data = """<html>
<body>
Some initial text</br>
<p>Which is across multiple lines</p>
</br>
So let's see what happens. </br>
</body>
</html>
"""
with open("test-datastore/output.txt", "w") as f:
f.write(test_return_data)
def test_import(client):
res = client.get("/")
assert b"IMPORT" in res.data
assert res.status_code == 200
def set_modified_response():
test_return_data = """<html>
<body>
Some initial text</br>
<p>which has this one new line</p>
</br>
So let's see what happens. </br>
</body>
</html>
test_url_list = ["https://slashdot.org"]
res = client.post('/import', data={'urls': "\n".join(test_url_list)}, follow_redirects=True)
s = "{} Imported".format(len(test_url_list))
"""
#p= url_for('test_endpoint', _external=True
assert bytes(s.encode('utf-8')) in res.data
for url in test_url_list:
assert bytes(url.encode('utf-8')) in res.data
#response = requests.get('http://localhost:5000/random_string')
#assert response.status_code == 200
#assert response.json() == [{'id': 1}]
with open("test-datastore/output.txt", "w") as f:
f.write(test_return_data)
def test_import_a(client):
res = client.get("/")
assert b"IMPORT" in res.data
assert res.status_code == 200
def test_add_endpoint_to_live_server(client, live_server):
sleep_time_for_fetch_thread = 3
@live_server.app.route('/test-endpoint')
def test_endpoint():
# Tried using a global var here but didn't seem to work, so reading from a file instead.
with open("test-datastore/output.txt", "r") as f:
return f.read()
set_original_response()
live_server.start()
# Add our URL to the import page
res = client.post(
url_for("import_page"),
data={"urls": url_for('test_endpoint', _external=True)},
follow_redirects=True
)
assert b"1 Imported" in res.data
client.get(url_for("api_watch_checknow"), follow_redirects=True)
# Give the thread time to pick it up
time.sleep(sleep_time_for_fetch_thread)
# It should report nothing found (no new 'unviewed' class)
res = client.get(url_for("index"))
assert b'unviewed' not in res.data
assert b'test-endpoint' in res.data
# Give the thread time to pick it up
time.sleep(sleep_time_for_fetch_thread)
res = client.get(url_for("index"))
assert b'unviewed' not in res.data
#####################
# Make a change
set_modified_response()
res = urlopen(url_for('test_endpoint', _external=True))
assert b'which has this one new line' in res.read()
# Force recheck
res = client.get(url_for("api_watch_checknow"), follow_redirects=True)
assert b'1 watches are rechecking.' in res.data
time.sleep(sleep_time_for_fetch_thread)
# Now something should be ready, indicated by having a 'unviewed' class
res = client.get(url_for("index"))
assert b'unviewed' in res.data