Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bloombar committed Apr 11, 2024
1 parent d860e14 commit 576f9be
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 11 deletions.
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ python-dotenv = "==0.16.0"
werkzeug = "==1.0.1"
pytest = "*"
pytest-flask = "*"
coverage = "*"

[dev-packages]

Expand Down
61 changes: 60 additions & 1 deletion Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,20 @@ def delete(post_id):
db.messages.delete_one({"_id": ObjectId(post_id)})
return redirect(url_for("home"))

@app.route("/delete-by-content/<post_name>/<post_message>", methods=["POST"])
def delete_by_content(post_name, post_message):
"""
Route for POST requests to delete all post by their author's name and post message.
Deletes the specified record from the database, and then redirects the browser to the home page.
Args:
post_name (str): The name of the author of the post.
post_message (str): The contents of the message of the post.
Returns:
redirect (Response): A redirect response to the home page.
"""
db.messages.delete_many({"name": post_name, "message": post_message})
return redirect(url_for("home"))

@app.errorhandler(Exception)
def handle_error(e):
"""
Expand Down
48 changes: 38 additions & 10 deletions tests/test_app.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,43 @@
import pytest # testing framework
import datetime
from app import create_app # flask app object

# unique data for any test post
random_text = datetime.datetime.now().time() # current date/time
test_fname = f"Test name {random_text}"
test_fmessage = f"Test message {random_text}"


@pytest.fixture
def client():
"""
Create and yield Flask app
"""
app = create_app()
app.testing = True # necessary for assertions to work correctly
with app.test_client() as client:
yield client


@pytest.fixture(scope="function")
def test_post(client):
"""
Create and remove a test post saved in the database
"""
# create the post
response = client.post(
"/create",
data=dict(fname=test_fname, fmessage=test_fmessage),
follow_redirects=True,
)
yield response
# delete the post
response = client.post(
f"/delete-by-content/{test_fname}/{test_fmessage}",
follow_redirects=True,
)


def test_home_status(client):
response = client.get("/") # make get request for home route
assert response.status_code == 200, "Home route should return status code 200"
Expand All @@ -34,18 +62,18 @@ def test_home_content(client):
assert "<h2>Posts</h2>" in response_data, "HTML should contain <h2>Posts</h2>"


def test_create_post(client):
def test_create_post(client, test_post):
"""
Test post creation
"""
test_fname = "Test name"
test_fmessage = "Test message"
response = client.post(
"/create",
data=dict(fname=test_fname, fmessage=test_fmessage),
follow_redirects=True,
)
response_data = response.data.decode() # the HTML code
assert response.status_code == 200, "Home route should return status code 200"
response_data = test_post.data.decode() # the HTML code
assert test_post.status_code == 200, "Home route should return status code 200"
assert test_fname in response_data, f"HTML should contain '{test_fname}'"
assert test_fmessage in response_data, f"HTML should contain '{test_fmessage}'"


#
# these test are not exhaustive
# they should include tests for the /create, /edit, /delete routes
# this requires refactoring the code to make it more easily testable, which is standard practice
#

0 comments on commit 576f9be

Please sign in to comment.