Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ddomin212 committed Jun 17, 2023
1 parent dd7cd07 commit e3ded3f
Show file tree
Hide file tree
Showing 12 changed files with 391 additions and 26 deletions.
2 changes: 1 addition & 1 deletion app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
""" The main entry point for the Flask application. """
import os
from flask import Flask, render_template
from flask import Flask
from dotenv import load_dotenv
from views import main, authapi, posts, reviews, profile, payments, add, fav
from config import initialize_app
Expand Down
2 changes: 2 additions & 0 deletions config/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import stripe
from .firebase import initialize_firebase
from .redis import initialize_redis
from .papertrail import initialize_papertrail


def initialize_app(app, testing=False):
Expand Down Expand Up @@ -33,3 +34,4 @@ def inject_variables():
if not testing:
initialize_redis(app)
initialize_firebase()
initialize_papertrail()
15 changes: 15 additions & 0 deletions config/papertrail.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import logging
from logging.handlers import SysLogHandler
import os


def initialize_papertrail():
"""Initialize papertrail logging."""
logger = logging.getLogger("first_logger")
logger.setLevel(logging.DEBUG)
syslog = SysLogHandler(
address=(os.getenv("PAPERTRAIL_HOST"), int(os.getenv("PAPERTRAIL_PORT")))
)
logger.addHandler(syslog)

logger.debug("Logger setup complete.")
20 changes: 15 additions & 5 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,25 @@ def mocked_posts():
"price": 10,
"rating": 4,
"user_uid": "12358896",
"images": ["https://via.placeholder.com/150"],
"images": [
"https://via.placeholder.com/150",
"https://via.placeholder.com/300",
"https://via.placeholder.com/450",
],
"description": "This is a test description",
"tags": {
"basics": ["Wifi", "Kitchen"],
"safety": ["Fire extinguisher", "First aid kit"],
"standout": ["Pool", "Gym"],
"views": ["Ocean view", "Mountain view"],
"basics": '["Wifi", "Kitchen"]',
"safety": '["Fire extinguisher", "First aid kit"]',
"standout": '["Pool", "Gym"]',
"views": '["Ocean view", "Mountain view"]',
},
"type": "apartment",
"bedrooms": "1",
"bathrooms": "1",
"guests": "1",
"beds": "1",
"month_disc": "15",
"year_disc": "20",
"from": convert_date("2022-01-01"),
"to": convert_date("2022-05-01"),
},
Expand Down
56 changes: 56 additions & 0 deletions tests/tests_add/test_add4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from unittest.mock import patch, MagicMock


def test_add_4_get(auth_client):
# Make a GET request to the route with parameters
response = auth_client.get("/add-4")

# Assert the response status code
assert response.status_code == 200
assert b"<fieldset multiple>" in response.data


def test_add_4_post(auth_client):
# Make a GET request to the route with parameters
with patch("firebase_admin.firestore.client"):
response = auth_client.post(
"/add-4",
data={
"basics": ["Paid parking", "Free parking", "Kitchen"],
"safety": ["Smoke alarm", "CO detector", "First aid kit"],
},
)

# Assert the response status code
assert response.status_code == 302
assert response.headers["Location"] == "/add-5"


def test_add_4_edit_get(auth_client, mocked_posts):
with patch("firebase_admin.firestore.client") as firestore:
firestore.return_value.collection.return_value.document.return_value.get.return_value = MagicMock(
to_dict=lambda: mocked_posts[0]
)
# Make a GET request to the route with parameters
response = auth_client.get("/add-4/1")

# Assert the response status code
assert response.status_code == 200
print(response.data.decode("utf-8"))
assert b'<input type="checkbox"' in response.data


def test_add_4_edit_post(auth_client):
# Make a GET request to the route with parameters
with patch("firebase_admin.firestore.client"):
response = auth_client.post(
"/add-4/1",
data={
"basics": ["Paid parking", "Free parking", "Kitchen"],
"safety": ["Smoke alarm", "CO detector", "First aid kit"],
},
)

# Assert the response status code
assert response.status_code == 302
assert response.headers["Location"] == "/edit/1"
53 changes: 53 additions & 0 deletions tests/tests_add/test_add5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from unittest.mock import patch, MagicMock


def test_add_5_get(auth_client):
# Make a GET request to the route with parameters
response = auth_client.get("/add-5")

# Assert the response status code
assert response.status_code == 200
assert b"<textarea" in response.data


def test_add_5_post(auth_client):
# Make a GET request to the route with parameters
with patch("firebase_admin.firestore.client"):
response = auth_client.post(
"/add-5",
data={"desc": "This is a test description22"},
)

# Assert the response status code
assert response.status_code == 302
assert response.headers["Location"] == "/add-6"


def test_add_5_edit_get(auth_client, mocked_posts):
with patch("firebase_admin.firestore.client") as firestore:
firestore.return_value.collection.return_value.document.return_value.get.return_value = MagicMock(
to_dict=lambda: mocked_posts[0]
)
# Make a GET request to the route with parameters
response = auth_client.get("/add-5/1")

# Assert the response status code
assert response.status_code == 200
print(response.data.decode("utf-8"))
assert (
b'<textarea name="desc" style="max-width: 1000px;min-width: 400px;" rows="20">This is a test description'
in response.data
)


def test_add_5_edit_post(auth_client):
# Make a GET request to the route with parameters
with patch("firebase_admin.firestore.client"):
response = auth_client.post(
"/add-5/1",
data={"desc": "This is a test description22"},
)

# Assert the response status code
assert response.status_code == 302
assert response.headers["Location"] == "/edit/1"
51 changes: 51 additions & 0 deletions tests/tests_add/test_add6.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from unittest.mock import patch, MagicMock


def test_add_6_get(auth_client):
# Make a GET request to the route with parameters
response = auth_client.get("/add-6")

# Assert the response status code
assert response.status_code == 200
assert b'<i class="fa fa-upload d-block p-4"' in response.data


def test_add_6_post(auth_client):
# Make a GET request to the route with parameters
with patch("firebase_admin.firestore.client"):
response = auth_client.post(
"/add-6",
data={"images": ["https://via.placeholder.com/550"]},
)

# Assert the response status code
assert response.status_code == 302
assert response.headers["Location"] == "/add-7"


def test_add_6_edit_get(auth_client, mocked_posts):
with patch("firebase_admin.firestore.client") as firestore:
firestore.return_value.collection.return_value.document.return_value.get.return_value = MagicMock(
to_dict=lambda: mocked_posts[0]
)
# Make a GET request to the route with parameters
response = auth_client.get("/add-6/1")

# Assert the response status code
assert response.status_code == 200
assert b'<img src="https://via.placeholder.com/150"' in response.data
assert b'<img src="https://via.placeholder.com/300"' in response.data
assert b'<img src="https://via.placeholder.com/450"' in response.data


def test_add_6_edit_post(auth_client):
# Make a GET request to the route with parameters
with patch("firebase_admin.firestore.client"):
response = auth_client.post(
"/add-6/1",
data={"images": ["https://via.placeholder.com/550"]},
)

# Assert the response status code
assert response.status_code == 302
assert response.headers["Location"] == "/edit/1"
66 changes: 66 additions & 0 deletions tests/tests_add/test_add7.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from unittest.mock import patch, MagicMock


def test_add_7_get(auth_client):
# Make a GET request to the route with parameters
response = auth_client.get("/add-7")

# Assert the response status code
assert response.status_code == 200
assert (
b'<input class="form-control-sm form-control w-50" name="bedrooms"'
in response.data
)


def test_add_7_post(auth_client):
# Make a GET request to the route with parameters
with patch("firebase_admin.firestore.client"):
response = auth_client.post(
"/add-7",
data={
"bedrooms": "1",
"baths": "1",
"guests": "1",
"beds": "1",
},
)

# Assert the response status code
assert response.status_code == 302
assert response.headers["Location"] == "/add-8"


def test_add_7_edit_get(auth_client, mocked_posts):
with patch("firebase_admin.firestore.client") as firestore:
firestore.return_value.collection.return_value.document.return_value.get.return_value = MagicMock(
to_dict=lambda: mocked_posts[0]
)
# Make a GET request to the route with parameters
response = auth_client.get("/add-7/1")

# Assert the response status code
assert response.status_code == 200
print(response.data.decode("utf-8"))
assert (
b'<input class="form-control-sm form-control w-50" name="bedrooms" type="number" value="1"'
in response.data
)


def test_add_7_edit_post(auth_client):
# Make a GET request to the route with parameters
with patch("firebase_admin.firestore.client"):
response = auth_client.post(
"/add-7/1",
data={
"bedrooms": "1",
"baths": "1",
"guests": "1",
"beds": "1",
},
)

# Assert the response status code
assert response.status_code == 302
assert response.headers["Location"] == "/edit/1"
69 changes: 69 additions & 0 deletions tests/tests_add/test_add8.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
from unittest.mock import patch, MagicMock


def test_add_8_get(auth_client):
# Make a GET request to the route with parameters
response = auth_client.get("/add-8")

# Assert the response status code
assert response.status_code == 200
assert b'name="price" type="number"' in response.data


def test_add_8_post(auth_client):
# Make a GET request to the route with parameters
with patch("firebase_admin.firestore.client"):
response = auth_client.post(
"/add-8",
data={
"price": "100",
"month-disc": "15",
"year-disc": "20",
},
)

# Assert the response status code
assert response.status_code == 302
assert response.headers["Location"] == "/edit/9999"


def test_add_8_edit_get(auth_client, mocked_posts):
with patch("firebase_admin.firestore.client") as firestore:
firestore.return_value.collection.return_value.document.return_value.get.return_value = MagicMock(
to_dict=lambda: mocked_posts[0]
)
# Make a GET request to the route with parameters
response = auth_client.get("/add-8/1")

# Assert the response status code
assert response.status_code == 200
print(response.data.decode("utf-8"))
assert (
b'<input class="form-control-sm form-control w-75" value="15" name="month-disc"'
in response.data
)
assert (
b'<input class="form-control-sm form-control w-75" value="20" name="year-disc"'
in response.data
)
assert (
b'<input class="form-control-sm form-control w-75" value="10" name="price"'
in response.data
)


def test_add_8_edit_post(auth_client):
# Make a GET request to the route with parameters
with patch("firebase_admin.firestore.client"):
response = auth_client.post(
"/add-8/1",
data={
"price": "100",
"month-disc": "15",
"year-disc": "20",
},
)

# Assert the response status code
assert response.status_code == 302
assert response.headers["Location"] == "/edit/9999"
Loading

0 comments on commit e3ded3f

Please sign in to comment.