Skip to content
Snippets Groups Projects
Commit 76798f53 authored by rxd's avatar rxd
Browse files

tests added

parent 191efa91
No related branches found
No related tags found
No related merge requests found
"""
Shared test fixtures.
Pytest will automatically run the client_setup_teardown() function before a
test. A test should use "client" as an input, because the name of the fixture
is "client".
EXAMPLE:
>>> def test_simple(client):
>>> response = client.get("/")
>>> assert response.status_code == 200
Pytest docs:
https://docs.pytest.org/en/latest/fixture.html#conftest-py-sharing-fixture-functions
"""
import subprocess
import sqlite3
import pytest
import MemeGenerator
@pytest.fixture(name="client")
def client_setup_teardown():
"""
Start a Flask test server with a clean database.
Flask docs: https://flask.palletsprojects.com/en/1.1.x/testing/#testing
"""
# Configure Flask test server
MemeGenerator.app.config["TESTING"] = True
# Transfer control to test. The code before the "yield" statement is setup
# code, which is executed before the test. Code after the "yield" is
# teardown code, which is executed at the end of the test. Teardown code
# is executed whether the test passed or failed.
with MemeGenerator.app.test_client() as client:
yield client
"""
Check setup page at /generate URL.
MemeGenerator
Ruizhe Deng rxd@umich.edu
"""
import re
import bs4
def test_generate_empty(client):
url = '/generate?top=&url=https%3A%2F%2Flive.staticflickr.com%2F3606%2F3369014165_7441c25d45_q.jpg&bot='
response = client.get(url)
soup = bs4.BeautifulSoup(response.data, "html.parser")
assert response.status_code == 200
src = [x.get("src") for x in soup.find_all('img')]
# check if there is only one image link
assert len(src) == 1
# check if that link to the image is correct
first_part = src[0].split('?')[0]
first_part_reg = 'https:\/\/api\.memegen\.link\/images\/custom\/[a-zA-Z0-9_]*\/[a-zA-Z0-9_]*\.png'
assert re.match(first_part_reg, first_part) is not None
def test_generate_one_empty(client):
bot_url = '/generate?top=&url=https%3A%2F%2Flive.staticflickr.com%2F3606%2F3369014165_7441c25d45_q.jpg&bot=bot'
top_url = '/generate?top=top&url=https%3A%2F%2Flive.staticflickr.com%2F3606%2F3369014165_7441c25d45_q.jpg&bot='
# bot first
response = client.get(bot_url)
soup = bs4.BeautifulSoup(response.data, "html.parser")
assert response.status_code == 200
# check for link correctness
src = [x.get("src") for x in soup.find_all('img')]
first_part = src[0].split('?')[0]
first_part_reg = 'https:\/\/api\.memegen\.link\/images\/custom\/[a-zA-Z0-9_]*\/[a-zA-Z0-9_]*\.png'
assert re.match(first_part_reg, first_part) is not None
# top check
response = client.get(top_url)
soup = bs4.BeautifulSoup(response.data, "html.parser")
assert response.status_code == 200
# check for link correctness
src = [x.get("src") for x in soup.find_all('img')]
first_part = src[0].split('?')[0]
first_part_reg = 'https:\/\/api\.memegen\.link\/images\/custom\/[a-zA-Z0-9_]*\/[a-zA-Z0-9_]*\.png'
assert re.match(first_part_reg, first_part) is not None
def check_generate_full(client):
url = '/generate?top=a&url=https%3A%2F%2Flive.staticflickr.com%2F3606%2F3369014165_7441c25d45_q.jpg&bot=c'
response = client.get(url)
soup = bs4.BeautifulSoup(response.data, "html.parser")
assert response.status_code == 200
# check for link correctness
src = [x.get("src") for x in soup.find_all('img')]
first_part = src[0].split('?')[0]
first_part_reg = 'https:\/\/api\.memegen\.link\/images\/custom\/[a-zA-Z0-9_]*\/[a-zA-Z0-9_]*\.png'
assert re.match(first_part_reg, first_part) is not None
\ No newline at end of file
"""
Check index page at / URL.
MemeGenerator
Ruizhe Deng rxd@umich.edu
"""
import re
import bs4
def test_images(client):
"""Verify all images are present in / URL.
Note: 'client' is a fixture fuction that provides a Flask test server
interface with a clean database. It is implemented in conftest.py and
reused by many tests. Docs: https://docs.pytest.org/en/latest/fixture.html
"""
# Load and parse index page
response = client.get("/")
assert response.status_code == 200
soup = bs4.BeautifulSoup(response.data, "html.parser")
types = [x.get("type") for x in soup.find_all('input')]
assert 'text' in types
assert 'submit' in types
"""
Check search page at /search/<int>?q URL.
MemeGenerator
Ruizhe Deng rxd@umich.edu
"""
import re
import bs4
def test_search(client):
"""Verify expected links present in / URL.
Note: 'client' is a fixture fuction that provides a Flask test server
interface with a clean database. It is implemented in conftest.py and
reused by many tests. Docs: https://docs.pytest.org/en/latest/fixture.html
"""
response = client.get("/search/1?q=umich")
assert response.status_code == 200
soup = bs4.BeautifulSoup(response.data, "html.parser")
srcs = [x.get("src") for x in soup.find_all("img")]
btns = [x.get("value") for x in soup.find_all("input")]
# check if there are 20 images in the page
assert len(srcs) == 20
# check if page has next and prev button
assert 'next' in btns
assert 'prev' in btns
def test_next_and_prev_page(client):
response = client.get("/search/1?q=umich")
assert response.status_code == 200 or response.status_code == 304
soup = bs4.BeautifulSoup(response.data, "html.parser")
links = [x.get("action") for x in soup.find_all("form")]
# check if next page in the links
assert '/search/2' in links
# go to next page
response = client.get("/search/2?q=umich")
assert response.status_code == 200
# go to prev page
response = client.get("/search/1?q=umich")
assert response.status_code == 200 or response.status_code == 304
\ No newline at end of file
"""
Check setup page at /setup URL.
MemeGenerator
Ruizhe Deng rxd@umich.edu
"""
import re
import bs4
def test_layout(client):
response = client.get("http://localhost:8000/setup?url=https%3A%2F%2Flive.staticflickr.com%2F3606%2F3369014165_7441c25d45_q.jpg")
assert response.status_code == 200
soup = bs4.BeautifulSoup(response.data, "html.parser")
inputs_in_form = [x for x in soup.find_all("input")]
types = [x.get("type") for x in soup.find_all("input")]
actions = [x.get("action") for x in soup.find_all("form")]
# check if there are four inputs in the page
assert len(inputs_in_form) == 4
# check if there is a submit button
types = [x.get("type") for x in soup.find_all("input")]
assert 'submit' in types
# check two text input
assert set(['text', 'text']).issubset(types)
# check if there's a link to generate page
assert '/generate' in actions
\ No newline at end of file
"""Test suite utility functions."""
import pathlib
# Directory containing unit tests
TEST_DIR = pathlib.Path(__file__).parent
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment