app factory now outside tests dir, other minor changes
This commit is contained in:
@@ -1,19 +1,40 @@
|
||||
# Library imports
|
||||
import pytest
|
||||
|
||||
# Import testing flask app factory function
|
||||
from tests.testing_app import create_app
|
||||
# Import flask app factory function
|
||||
from app_factory import add_test_data
|
||||
from app_factory import create_app
|
||||
|
||||
# Import db
|
||||
from accounts.models import db as _db
|
||||
|
||||
####
|
||||
# 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
|
||||
@pytest.fixture(scope="module")
|
||||
def client():
|
||||
app = create_app()
|
||||
# DB for testing
|
||||
@pytest.fixture(scope='module')
|
||||
def db(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()
|
||||
|
||||
|
||||
@@ -10,19 +10,12 @@ from accounts.models.dog.breed_size import BreedSize
|
||||
from accounts.models.user import User
|
||||
from accounts.models.visit.visit_type import VisitType
|
||||
|
||||
# Import flask app factory function
|
||||
from tests.testing_app import create_app
|
||||
from tests.testing_app import create_app
|
||||
def test_user_model(app, db):
|
||||
man_man = db.session.execute(db.select(User).where(User.name == 'Man Man')).scalar_one()
|
||||
man_man_visits = man_man.get_visits()
|
||||
|
||||
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_bookings = man_man.get_bookings()
|
||||
|
||||
real_person = db.session.execute(db.select(User).where(User.name == 'Real Person')).scalar_one()
|
||||
real_person_bookings = real_person.get_bookings()
|
||||
real_person = db.session.execute(db.select(User).where(User.name == 'Real Person')).scalar_one()
|
||||
real_person_visits = real_person.get_visits()
|
||||
|
||||
# Assert something
|
||||
assert man_man.id == 1
|
||||
@@ -31,7 +24,7 @@ def test_db():
|
||||
assert man_man.email == 'man_man@email.com'
|
||||
assert man_man.address == '123 Home Ln. City, AA 11223'
|
||||
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.name == 'Real Person'
|
||||
@@ -39,5 +32,5 @@ def test_db():
|
||||
assert real_person.email == 'real_person@email.com'
|
||||
assert real_person.address == '113 Park St. City, AA 13433'
|
||||
assert real_person.password == 'realpassword'
|
||||
assert len(real_person_bookings) == 1
|
||||
assert len(real_person_visits) == 1
|
||||
|
||||
|
||||
@@ -1,113 +0,0 @@
|
||||
# Python library imports
|
||||
from datetime import datetime
|
||||
|
||||
# Library imports
|
||||
from flask import Flask
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
|
||||
# Import accounts blueprint
|
||||
from accounts import accounts
|
||||
|
||||
# Import db
|
||||
from accounts.models import db
|
||||
|
||||
# Import db models
|
||||
from accounts.models.dog import Dog
|
||||
from accounts.models.dog.breed_size import BreedSize
|
||||
from accounts.models.user import User
|
||||
from accounts.models.visit import Visit
|
||||
from accounts.models.visit.visit_type import VisitType
|
||||
|
||||
def create_app():
|
||||
# Initialize flask app
|
||||
accounts_app = Flask(__name__, template_folder='templates')
|
||||
accounts_app.register_blueprint(accounts)
|
||||
accounts_app.config.from_pyfile('config.py')
|
||||
|
||||
# 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
|
||||
|
||||
def add_test_data(db):
|
||||
# Create test users
|
||||
man_man = User(name='Man Man',
|
||||
username='mamanman',
|
||||
email='man_man@email.com',
|
||||
address='123 Home Ln. City, AA 11223',
|
||||
password='manword')
|
||||
|
||||
real_person = User(name='Real Person',
|
||||
username='imarealperson',
|
||||
email='real_person@email.com',
|
||||
address='113 Park St. City, AA 13433',
|
||||
password='realpassword')
|
||||
|
||||
# Add test users to db session
|
||||
db.session.add(man_man)
|
||||
db.session.add(real_person)
|
||||
|
||||
# Commit test users to db
|
||||
db.session.commit()
|
||||
|
||||
# Create test dogs
|
||||
lassey = Dog(name='Lassey',
|
||||
breed_size=BreedSize.LARGE,
|
||||
owner_id=man_man.id)
|
||||
|
||||
rufus = Dog(name='Rufus',
|
||||
breed_size=BreedSize.SMALL,
|
||||
owner_id=man_man.id)
|
||||
|
||||
air_bud = Dog(name='Air Bud',
|
||||
owner_id=real_person.id)
|
||||
|
||||
# Add test dogs to db session
|
||||
db.session.add(lassey)
|
||||
db.session.add(rufus)
|
||||
db.session.add(air_bud)
|
||||
|
||||
# Commit test dogs to db
|
||||
db.session.commit()
|
||||
|
||||
# Create test visits
|
||||
|
||||
# Visit with the date in the past
|
||||
test_visit1 = Visit(date_time=datetime(2023,12,5,10,30),
|
||||
owner_id=man_man.id,
|
||||
dog_id=rufus.id)
|
||||
|
||||
# Visit with the date in the future
|
||||
test_visit2 = Visit(date_time=datetime(2034,12,5,10,30),
|
||||
owner_id=man_man.id,
|
||||
dog_id=lassey.id)
|
||||
|
||||
# Visit with non-default visit type
|
||||
test_visit3 = Visit(date_time=datetime(2034,12,5,10,30),
|
||||
owner_id=real_person.id,
|
||||
visit_type=VisitType.HOUSE_SITTING,
|
||||
dog_id=air_bud.id)
|
||||
|
||||
# Visit with more than one dog
|
||||
#test_visit3 = Visit(date_time=datetime(2034,12,5,10,30),
|
||||
# owner_id=man_man.id,
|
||||
# dog_id=[lassey.id, rufus.id])
|
||||
|
||||
# Add test visits to db session
|
||||
db.session.add(test_visit1)
|
||||
db.session.add(test_visit2)
|
||||
db.session.add(test_visit3)
|
||||
|
||||
# Commit test visits to db
|
||||
db.session.commit()
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
DEBUG = True
|
||||
TESTING = True
|
||||
SQLALCHEMY_DATABASE_URI = 'sqlite:///:memory:'
|
||||
Reference in New Issue
Block a user