Skip to content

Commit

Permalink
added test_collection.py
Browse files Browse the repository at this point in the history
added post method in delete_place in collection.py
modified data type in models.py
  • Loading branch information
taka-rl committed Oct 17, 2024
1 parent 61583b6 commit e8616a1
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 8 deletions.
2 changes: 1 addition & 1 deletion flask_app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class Place(db.Model):
location: Mapped[str] = mapped_column(String(50), nullable=False)
open_time: Mapped[str] = mapped_column(String(10), nullable=True)
close_time: Mapped[str] = mapped_column(String(10), nullable=True)
pricing: Mapped[float] = mapped_column(String(15), nullable=True)
pricing: Mapped[str] = mapped_column(String(15), nullable=True)
rating: Mapped[float] = mapped_column(Float, nullable=True)
category: Mapped[str] = mapped_column(String(15), nullable=True)
location_url: Mapped[str] = mapped_column(String(250), nullable=True)
Expand Down
2 changes: 1 addition & 1 deletion flask_app/routes/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def edit_place(place_id):
return render_template('add-place.html', place=place, form=edit_form, is_edit=True)


@collection_bp.route('/delete-place/<int:place_id>')
@collection_bp.route('/delete-place/<int:place_id>', methods=['POST'])
@admin_only
def delete_place(place_id):
place_to_delete = db.get_or_404(Place, place_id)
Expand Down
23 changes: 17 additions & 6 deletions tests/parameters.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
SUPER_ADMIN_EMAIL = '[email protected]'
SUPER_ADMIN_PASSWORD = 'admin'
SUPER_ADMIN_NAME = 'Super Admin'
SUPER_ADMIN_EMAIL: str = '[email protected]'
SUPER_ADMIN_PASSWORD: str = 'admin'
SUPER_ADMIN_NAME: str = 'Super Admin'

TEST_EMAIL: str = '[email protected]'
TEST_PASSWORD: str = 'test'
TEST_NAME: str = 'Test User'


TEST_PLACE_NAME: str = 'Édes Mackó'
TEST_PLACE_LOCATION: str = 'Budapest, Állatkerti krt. 14-16, 1146'
TEST_PLACE_LOCATION_URL: str = 'https://maps.app.goo.gl/FtzKoF8qaQGuhRqA8'
TEST_PLACE_OPEN_TIME: str = '9:30AM'
TEST_PLACE_CLOSE_TIME: str = '6PM'
TEST_PLACE_PRICING: str = '2,000-4,000Ft'
TEST_PLACE_RATING: float = 9.3
TEST_PLACE_CATEGORY: str = 'Cafe'

TEST_EMAIL = '[email protected]'
TEST_PASSWORD = 'test'
TEST_NAME = 'Test User'
113 changes: 113 additions & 0 deletions tests/test_collection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
from flask_login import current_user
from flask_app.models import Place, db
from tests.parameters import TEST_PLACE_NAME, TEST_PLACE_LOCATION, TEST_PLACE_LOCATION_URL, TEST_PLACE_OPEN_TIME
from tests.parameters import TEST_PLACE_CLOSE_TIME, TEST_PLACE_PRICING, TEST_PLACE_RATING, TEST_PLACE_CATEGORY


# access to collection page
def test_access_collection_page(client):
response = client.get('/collection')
assert response.status_code == 200
assert b'Collection' in response.data
assert b'These are what I love' in response.data


# add a place
def test_add_place(super_admin_client):
# Add a place
place_response = super_admin_client.post('/add-place', data={
'name': TEST_PLACE_NAME,
'location': TEST_PLACE_LOCATION,
'location_url': TEST_PLACE_LOCATION_URL,
'open_time': TEST_PLACE_OPEN_TIME,
'close_time': TEST_PLACE_CLOSE_TIME,
'pricing': TEST_PLACE_PRICING,
'rating': TEST_PLACE_RATING,
'category': TEST_PLACE_CATEGORY,
'place_author': current_user
}, follow_redirects=True)

# Verify the place is added
assert place_response.status_code == 200

place = Place.query.filter_by(name=TEST_PLACE_NAME).first()
assert place is not None
assert place.name == TEST_PLACE_NAME
assert place.location == TEST_PLACE_LOCATION
assert place.location_url == TEST_PLACE_LOCATION_URL
assert place.open_time == TEST_PLACE_OPEN_TIME
assert place.close_time == TEST_PLACE_CLOSE_TIME
assert place.pricing == TEST_PLACE_PRICING
assert place.rating == TEST_PLACE_RATING
assert place.category == TEST_PLACE_CATEGORY
assert place.place_author == current_user


# edit a place
def test_edit_place(super_admin_client):
# Add a place manually for testing
new_place = Place(name=TEST_PLACE_NAME,
location=TEST_PLACE_LOCATION,
location_url=TEST_PLACE_LOCATION_URL,
open_time=TEST_PLACE_OPEN_TIME,
close_time=TEST_PLACE_CLOSE_TIME,
pricing=TEST_PLACE_PRICING,
rating=TEST_PLACE_RATING,
category=TEST_PLACE_CATEGORY,
author_id=current_user.id
)

with super_admin_client.application.app_context():
db.session.add(new_place)
db.session.commit()

# Verify the place is stored in the database
place = Place.query.filter_by(name=TEST_PLACE_NAME).first()
assert place is not None

# Edit the place
edit_response = super_admin_client.post(f'/edit-place/{place.id}', data={
'name': TEST_PLACE_NAME + '_TEST',
'location': 'Test_Location',
'category': 'Restaurant'
}, follow_redirects=True)

assert edit_response.status_code == 200

# Verify the place is edited
edit_place = Place.query.filter_by(name=TEST_PLACE_NAME + '_TEST').first()
assert edit_place is not None
assert edit_place.name == TEST_PLACE_NAME + '_TEST'
assert edit_place.location == 'Test_Location'
assert edit_place.category == 'Restaurant'


# delete a place
def test_delete_place(super_admin_client):
# Add a place manually for testing
new_place = Place(name=TEST_PLACE_NAME,
location=TEST_PLACE_LOCATION,
location_url=TEST_PLACE_LOCATION_URL,
open_time=TEST_PLACE_OPEN_TIME,
close_time=TEST_PLACE_CLOSE_TIME,
pricing=TEST_PLACE_PRICING,
rating=TEST_PLACE_RATING,
category=TEST_PLACE_CATEGORY,
author_id=current_user.id
)

with super_admin_client.application.app_context():
db.session.add(new_place)
db.session.commit()

# Verify the place is stored in the database
place = Place.query.filter_by(name=TEST_PLACE_NAME).first()
assert place is not None

# Delete the place
delete_response = super_admin_client.post(f'delete-place/{place.id}')
assert delete_response.status_code == 302

# Verify the place is deleted
delete_place = Place.query.filter_by(name=TEST_PLACE_NAME).first()
assert delete_place is None

0 comments on commit e8616a1

Please sign in to comment.