app factory now outside tests dir, other minor changes
This commit is contained in:
parent
d487e40c1e
commit
970f1fa0c8
41
CHANGELOG.md
41
CHANGELOG.md
@ -5,6 +5,38 @@ All notable changes to this project will be documented in this file.
|
|||||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
||||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||||
|
|
||||||
|
## [Unreleased]
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
1. Models subpackage:
|
||||||
|
- User class:
|
||||||
|
* get_dogs() method
|
||||||
|
2. App factory now in app_factory.py
|
||||||
|
3. Development and testing now have different configurations.
|
||||||
|
- development_config.py
|
||||||
|
- tests/testing_config.py
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
1. Fixed typo in CHANGELOG.md
|
||||||
|
2. Models subpackage:
|
||||||
|
- User class:
|
||||||
|
* get_bookings() method renamed to get_visits()
|
||||||
|
* get_bookings_history() method renamed to get_visit_history()
|
||||||
|
* removed redundent import from get_visit_history() method
|
||||||
|
3. Tests package:
|
||||||
|
- conftest.py:
|
||||||
|
* Updated to use new app_factory.py
|
||||||
|
* client() fixture renamed to http_client()
|
||||||
|
- test_user_model.py:
|
||||||
|
* Updated to use new app_factory.py
|
||||||
|
* Variables updated to reflect methods in User model
|
||||||
|
4. Views package:
|
||||||
|
- Users dashboard route:
|
||||||
|
* Update User model object usage to reflect renamed methods
|
||||||
|
* Renamed variable user_dogs to user_pets
|
||||||
|
|
||||||
## [0.0.6] - 2024-04-28
|
## [0.0.6] - 2024-04-28
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
@ -25,11 +57,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
+ created_at column
|
+ created_at column
|
||||||
+ status column
|
+ status column
|
||||||
+ updated_at column
|
+ updated_at column
|
||||||
- User subpackage:
|
- User class:
|
||||||
* User class:
|
* created_at column
|
||||||
+ created_at column
|
* name column
|
||||||
+ name column
|
* updated_at column
|
||||||
+ updated_at column
|
|
||||||
- Visit subpackage:
|
- Visit subpackage:
|
||||||
* VisitType class
|
* VisitType class
|
||||||
* Visit class:
|
* Visit class:
|
||||||
|
|||||||
@ -20,13 +20,16 @@ class User(db.Model):
|
|||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return f'{self.username}'
|
return f'{self.username}'
|
||||||
|
|
||||||
|
# Get all dogs currently set
|
||||||
|
def get_dogs(self):
|
||||||
|
return [dog for dog in self.dogs]
|
||||||
|
|
||||||
# Retrieve all booked visits
|
# Retrieve all booked visits
|
||||||
def get_bookings(self):
|
def get_visits(self):
|
||||||
return [visit for visit in self.visits]
|
return [visit for visit in self.visits]
|
||||||
|
|
||||||
# Retrieve past booked visits
|
# Retrieve past booked visits
|
||||||
def get_bookings_history(self):
|
def get_visit_history(self):
|
||||||
from datetime import datetime
|
|
||||||
current_time = datetime.now()
|
current_time = datetime.now()
|
||||||
|
|
||||||
return [visit for visit in self.visits if visit.date_time < current_time]
|
return [visit for visit in self.visits if visit.date_time < current_time]
|
||||||
|
|||||||
@ -18,12 +18,7 @@ def user_dashboard(username):
|
|||||||
# Retrieve user data
|
# Retrieve user data
|
||||||
user = db.session.execute(db.select(User).filter_by(username=username)).scalar_one()
|
user = db.session.execute(db.select(User).filter_by(username=username)).scalar_one()
|
||||||
|
|
||||||
# Gather names of dogs for this client
|
return render_template('users/dashboard/base.html', user=user, user_pets=user.get_dogs(), user_book_history=user.get_visits())
|
||||||
dogs = list()
|
|
||||||
for dog in user.dogs:
|
|
||||||
dogs.append(dog.name)
|
|
||||||
|
|
||||||
return render_template('users/dashboard/base.html', user=user, user_dogs=dogs, user_book_history=user.get_bookings())
|
|
||||||
|
|
||||||
@accounts.route('/users/<username>', methods=['POST'])
|
@accounts.route('/users/<username>', methods=['POST'])
|
||||||
def user(username):
|
def user(username):
|
||||||
|
|||||||
@ -9,7 +9,7 @@ from flask_sqlalchemy import SQLAlchemy
|
|||||||
from accounts import accounts
|
from accounts import accounts
|
||||||
|
|
||||||
# Import db
|
# Import db
|
||||||
from accounts.models import db
|
from accounts.models import db as _db
|
||||||
|
|
||||||
# Import db models
|
# Import db models
|
||||||
from accounts.models.dog import Dog
|
from accounts.models.dog import Dog
|
||||||
@ -18,29 +18,19 @@ from accounts.models.user import User
|
|||||||
from accounts.models.visit import Visit
|
from accounts.models.visit import Visit
|
||||||
from accounts.models.visit.visit_type import VisitType
|
from accounts.models.visit.visit_type import VisitType
|
||||||
|
|
||||||
def create_app():
|
def create_app(config_class='development_config.py'):
|
||||||
# Initialize flask app
|
accounts_app = Flask(__name__, template_folder='tests/templates')
|
||||||
accounts_app = Flask(__name__, template_folder='templates')
|
|
||||||
accounts_app.register_blueprint(accounts)
|
accounts_app.register_blueprint(accounts)
|
||||||
accounts_app.config.from_pyfile('config.py')
|
accounts_app.config.from_pyfile(config_class)
|
||||||
|
|
||||||
# Initialize db with flask app
|
|
||||||
db.init_app(accounts_app)
|
|
||||||
|
|
||||||
with accounts_app.app_context():
|
|
||||||
# Create db tables
|
|
||||||
db.create_all()
|
|
||||||
|
|
||||||
# Add test data to db session
|
|
||||||
add_test_data(db)
|
|
||||||
|
|
||||||
# Commit test data to db
|
|
||||||
db.session.commit()
|
|
||||||
|
|
||||||
return accounts_app
|
return accounts_app
|
||||||
|
|
||||||
def add_test_data(db):
|
def add_test_data(db):
|
||||||
# Create test users
|
add_test_users(db)
|
||||||
|
add_test_dogs(db)
|
||||||
|
add_test_visits(db)
|
||||||
|
|
||||||
|
def add_test_users(db):
|
||||||
man_man = User(name='Man Man',
|
man_man = User(name='Man Man',
|
||||||
username='mamanman',
|
username='mamanman',
|
||||||
email='man_man@email.com',
|
email='man_man@email.com',
|
||||||
@ -53,14 +43,17 @@ def add_test_data(db):
|
|||||||
address='113 Park St. City, AA 13433',
|
address='113 Park St. City, AA 13433',
|
||||||
password='realpassword')
|
password='realpassword')
|
||||||
|
|
||||||
# Add test users to db session
|
|
||||||
db.session.add(man_man)
|
db.session.add(man_man)
|
||||||
db.session.add(real_person)
|
db.session.add(real_person)
|
||||||
|
|
||||||
# Commit test users to db
|
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
# Create test dogs
|
def add_test_dogs(db):
|
||||||
|
man_man = db.session.execute(
|
||||||
|
db.select(User).where(User.name == 'Man Man')).scalar_one()
|
||||||
|
real_person = db.session.execute(
|
||||||
|
db.select(User).where(User.name == 'Real Person')).scalar_one()
|
||||||
|
|
||||||
lassey = Dog(name='Lassey',
|
lassey = Dog(name='Lassey',
|
||||||
breed_size=BreedSize.LARGE,
|
breed_size=BreedSize.LARGE,
|
||||||
owner_id=man_man.id)
|
owner_id=man_man.id)
|
||||||
@ -72,15 +65,26 @@ def add_test_data(db):
|
|||||||
air_bud = Dog(name='Air Bud',
|
air_bud = Dog(name='Air Bud',
|
||||||
owner_id=real_person.id)
|
owner_id=real_person.id)
|
||||||
|
|
||||||
# Add test dogs to db session
|
|
||||||
db.session.add(lassey)
|
db.session.add(lassey)
|
||||||
db.session.add(rufus)
|
db.session.add(rufus)
|
||||||
db.session.add(air_bud)
|
db.session.add(air_bud)
|
||||||
|
|
||||||
# Commit test dogs to db
|
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
# Create test visits
|
def add_test_visits(db):
|
||||||
|
# User objects
|
||||||
|
man_man = db.session.execute(
|
||||||
|
db.select(User).where(User.name == 'Man Man')).scalar_one()
|
||||||
|
real_person = db.session.execute(
|
||||||
|
db.select(User).where(User.name == 'Real Person')).scalar_one()
|
||||||
|
|
||||||
|
# Dog objects
|
||||||
|
lassey = db.session.execute(
|
||||||
|
db.select(Dog).where(Dog.name == 'Lassey')).scalar_one()
|
||||||
|
rufus = db.session.execute(
|
||||||
|
db.select(Dog).where(Dog.name == 'Rufus')).scalar_one()
|
||||||
|
air_bud = db.session.execute(
|
||||||
|
db.select(Dog).where(Dog.name == 'Air Bud')).scalar_one()
|
||||||
|
|
||||||
# Visit with the date in the past
|
# Visit with the date in the past
|
||||||
test_visit1 = Visit(date_time=datetime(2023,12,5,10,30),
|
test_visit1 = Visit(date_time=datetime(2023,12,5,10,30),
|
||||||
@ -99,15 +103,23 @@ def add_test_data(db):
|
|||||||
dog_id=air_bud.id)
|
dog_id=air_bud.id)
|
||||||
|
|
||||||
# Visit with more than one dog
|
# Visit with more than one dog
|
||||||
#test_visit3 = Visit(date_time=datetime(2034,12,5,10,30),
|
#test_visit4 = Visit(date_time=datetime(2034,12,5,10,30),
|
||||||
# owner_id=man_man.id,
|
# owner_id=man_man.id,
|
||||||
# dog_id=[lassey.id, rufus.id])
|
# dog_id=[lassey.id, rufus.id])
|
||||||
|
|
||||||
# Add test visits to db session
|
|
||||||
db.session.add(test_visit1)
|
db.session.add(test_visit1)
|
||||||
db.session.add(test_visit2)
|
db.session.add(test_visit2)
|
||||||
db.session.add(test_visit3)
|
db.session.add(test_visit3)
|
||||||
|
|
||||||
# Commit test visits to db
|
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
app = create_app()
|
||||||
|
_db.init_app(app)
|
||||||
|
|
||||||
|
with app.app_context():
|
||||||
|
_db.create_all()
|
||||||
|
add_test_data(_db)
|
||||||
|
|
||||||
|
app.run()
|
||||||
|
|
||||||
@ -1,19 +1,40 @@
|
|||||||
# Library imports
|
# Library imports
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
# Import testing flask app factory function
|
# Import flask app factory function
|
||||||
from tests.testing_app import create_app
|
from app_factory import add_test_data
|
||||||
|
from app_factory import create_app
|
||||||
|
|
||||||
|
# Import db
|
||||||
|
from accounts.models import db as _db
|
||||||
|
|
||||||
####
|
####
|
||||||
# Global fixtures
|
# Global fixtures
|
||||||
####
|
####
|
||||||
|
|
||||||
|
# App client for testing
|
||||||
|
@pytest.fixture(scope='module')
|
||||||
|
def app():
|
||||||
|
app = create_app(config_class='tests/testing_config.py')
|
||||||
|
with app.app_context():
|
||||||
|
yield app
|
||||||
|
|
||||||
# HTTP client for testing app requests
|
# DB for testing
|
||||||
@pytest.fixture(scope="module")
|
@pytest.fixture(scope='module')
|
||||||
def client():
|
def db(app):
|
||||||
app = create_app()
|
_db.init_app(app)
|
||||||
|
_db.create_all()
|
||||||
|
add_test_data(_db)
|
||||||
|
|
||||||
|
yield _db
|
||||||
|
|
||||||
|
_db.session.remove()
|
||||||
|
_db.drop_all()
|
||||||
|
|
||||||
|
# HTTP client for testing app routes
|
||||||
|
@pytest.fixture(scope='module')
|
||||||
|
def http_client():
|
||||||
|
app = create_app(config_class='testing_config.py')
|
||||||
|
|
||||||
return app.test_client()
|
return app.test_client()
|
||||||
|
|
||||||
|
|||||||
@ -10,19 +10,12 @@ from accounts.models.dog.breed_size import BreedSize
|
|||||||
from accounts.models.user import User
|
from accounts.models.user import User
|
||||||
from accounts.models.visit.visit_type import VisitType
|
from accounts.models.visit.visit_type import VisitType
|
||||||
|
|
||||||
# Import flask app factory function
|
def test_user_model(app, db):
|
||||||
from tests.testing_app import create_app
|
|
||||||
from tests.testing_app import create_app
|
|
||||||
|
|
||||||
def test_db():
|
|
||||||
app = create_app()
|
|
||||||
|
|
||||||
with app.app_context():
|
|
||||||
man_man = db.session.execute(db.select(User).where(User.name == 'Man Man')).scalar_one()
|
man_man = db.session.execute(db.select(User).where(User.name == 'Man Man')).scalar_one()
|
||||||
man_man_bookings = man_man.get_bookings()
|
man_man_visits = man_man.get_visits()
|
||||||
|
|
||||||
real_person = db.session.execute(db.select(User).where(User.name == 'Real Person')).scalar_one()
|
real_person = db.session.execute(db.select(User).where(User.name == 'Real Person')).scalar_one()
|
||||||
real_person_bookings = real_person.get_bookings()
|
real_person_visits = real_person.get_visits()
|
||||||
|
|
||||||
# Assert something
|
# Assert something
|
||||||
assert man_man.id == 1
|
assert man_man.id == 1
|
||||||
@ -31,7 +24,7 @@ def test_db():
|
|||||||
assert man_man.email == 'man_man@email.com'
|
assert man_man.email == 'man_man@email.com'
|
||||||
assert man_man.address == '123 Home Ln. City, AA 11223'
|
assert man_man.address == '123 Home Ln. City, AA 11223'
|
||||||
assert man_man.password == 'manword'
|
assert man_man.password == 'manword'
|
||||||
assert len(man_man_bookings) == 2
|
assert len(man_man_visits) == 2
|
||||||
|
|
||||||
assert real_person.id == 2
|
assert real_person.id == 2
|
||||||
assert real_person.name == 'Real Person'
|
assert real_person.name == 'Real Person'
|
||||||
@ -39,5 +32,5 @@ def test_db():
|
|||||||
assert real_person.email == 'real_person@email.com'
|
assert real_person.email == 'real_person@email.com'
|
||||||
assert real_person.address == '113 Park St. City, AA 13433'
|
assert real_person.address == '113 Park St. City, AA 13433'
|
||||||
assert real_person.password == 'realpassword'
|
assert real_person.password == 'realpassword'
|
||||||
assert len(real_person_bookings) == 1
|
assert len(real_person_visits) == 1
|
||||||
|
|
||||||
|
|||||||
2
tests/testing_config.py
Normal file
2
tests/testing_config.py
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
TESTING = True
|
||||||
|
SQLALCHEMY_DATABASE_URI = 'sqlite:///:memory:'
|
||||||
Loading…
x
Reference in New Issue
Block a user