Skip to content
Snippets Groups Projects
test_search.py 1.42 KiB
Newer Older
rxd's avatar
rxd committed
"""
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