CHANGED dev-server.py logic moved into tests/testing_app.py as create_app function, UPDATED tests to use new create_app function, UPDATED shell.nix to work with create_app and testing_app.py, ADDED DB models now defined in accounts/models, ADDED Visit DB model
This commit is contained in:
parent
994bad056b
commit
bc33385ebe
2
Makefile
2
Makefile
@ -7,5 +7,5 @@
|
|||||||
|
|
||||||
.PHONY: development-server
|
.PHONY: development-server
|
||||||
development-server: # Start a development server
|
development-server: # Start a development server
|
||||||
python3 dev-server.py
|
flask run --debug
|
||||||
|
|
||||||
|
|||||||
4
accounts/models/__init__.py
Normal file
4
accounts/models/__init__.py
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
from flask_sqlalchemy import SQLAlchemy
|
||||||
|
|
||||||
|
db = SQLAlchemy()
|
||||||
|
|
||||||
21
accounts/models/dog.py
Normal file
21
accounts/models/dog.py
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
from accounts.models import db
|
||||||
|
|
||||||
|
class Dog(db.Model):
|
||||||
|
__tablename__ = 'dogs'
|
||||||
|
|
||||||
|
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
|
||||||
|
name = db.Column(db.String, nullable=False)
|
||||||
|
breed = db.Column(db.String, nullable=False)
|
||||||
|
owner_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
|
||||||
|
owner = db.relationship('User', backref='dogs')
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f'{self.name}'
|
||||||
|
|
||||||
|
def to_dict(self):
|
||||||
|
return {
|
||||||
|
'name': self.name,
|
||||||
|
'breed': self.breed,
|
||||||
|
'owner': self.owner
|
||||||
|
}
|
||||||
|
|
||||||
34
accounts/models/user.py
Normal file
34
accounts/models/user.py
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
# Import db
|
||||||
|
from accounts.models import db
|
||||||
|
|
||||||
|
class User(db.Model):
|
||||||
|
__tablename__ = 'users'
|
||||||
|
|
||||||
|
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
|
||||||
|
username = db.Column(db.String, unique=True, nullable=False)
|
||||||
|
email = db.Column(db.String, nullable=False)
|
||||||
|
address = db.Column(db.String, nullable=False)
|
||||||
|
password = db.Column(db.String, nullable=False)
|
||||||
|
visits = db.relationship('Visit', back_populates='owner')
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f'{self.username}'
|
||||||
|
|
||||||
|
# Retrieve all booked visits
|
||||||
|
def get_bookings(self):
|
||||||
|
return [visit for visit in self.visits]
|
||||||
|
|
||||||
|
# Retrieve past booked visits
|
||||||
|
def get_bookings_history(self):
|
||||||
|
from datetime import datetime
|
||||||
|
current_time = datetime.now()
|
||||||
|
|
||||||
|
return [visit for visit in self.visits if visit.date_time < current_time]
|
||||||
|
|
||||||
|
def to_dict(self):
|
||||||
|
return {
|
||||||
|
'username': self.username,
|
||||||
|
'email': self.email,
|
||||||
|
'password': self.password,
|
||||||
|
}
|
||||||
|
|
||||||
21
accounts/models/visit.py
Normal file
21
accounts/models/visit.py
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
# Import db
|
||||||
|
from accounts.models import db
|
||||||
|
|
||||||
|
class Visit(db.Model):
|
||||||
|
__tablename__ = 'visits'
|
||||||
|
|
||||||
|
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
|
||||||
|
date_time = db.Column(db.DateTime, nullable=False)
|
||||||
|
owner_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
|
||||||
|
owner = db.relationship('User', back_populates='visits')
|
||||||
|
dog_id = db.Column(db.Integer, db.ForeignKey('dogs.id'), nullable=False)
|
||||||
|
dog = db.relationship('Dog', backref='visits')
|
||||||
|
|
||||||
|
def to_dict(self):
|
||||||
|
return {
|
||||||
|
'ID': self.id,
|
||||||
|
'Date-Time': self.date_time,
|
||||||
|
'Owner-ID': self.owner_id,
|
||||||
|
'Dog-ID': self.dog_id
|
||||||
|
}
|
||||||
|
|
||||||
@ -1,16 +0,0 @@
|
|||||||
#! /usr/bin/env python3
|
|
||||||
|
|
||||||
from flask import Flask
|
|
||||||
|
|
||||||
from accounts import accounts
|
|
||||||
from tests.config import TestConfig
|
|
||||||
|
|
||||||
def create_app():
|
|
||||||
accounts_app = Flask(__name__, template_folder='tests/templates')
|
|
||||||
accounts_app.register_blueprint(accounts)
|
|
||||||
accounts_app.config.from_object(TestConfig)
|
|
||||||
|
|
||||||
return accounts_app
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
create_app().run(debug=True)
|
|
||||||
@ -21,10 +21,13 @@ pkgs.mkShell {
|
|||||||
# Database
|
# Database
|
||||||
alembic # migrations
|
alembic # migrations
|
||||||
flask-sqlalchemy # orm
|
flask-sqlalchemy # orm
|
||||||
sqlalchemy # orm
|
|
||||||
sqlite # driver
|
sqlite # driver
|
||||||
sqlite-utils # utilities
|
sqlite-utils # utilities
|
||||||
]))
|
]))
|
||||||
];
|
];
|
||||||
|
|
||||||
|
shellHook = ''
|
||||||
|
export FLASK_APP=tests.testing_app
|
||||||
|
'';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,3 +1,2 @@
|
|||||||
class TestConfig():
|
DEBUG = True
|
||||||
pass
|
SQLALCHEMY_DATABASE_URI = 'sqlite:///:memory:'
|
||||||
|
|
||||||
|
|||||||
@ -1,19 +1,19 @@
|
|||||||
|
# Library imports
|
||||||
import pytest
|
import pytest
|
||||||
from flask import Flask
|
|
||||||
|
|
||||||
from accounts import accounts
|
# Import testing flask app factory function
|
||||||
|
from tests.testing_app import create_app
|
||||||
|
|
||||||
from .config import TestConfig
|
|
||||||
|
|
||||||
|
####
|
||||||
|
# Global fixtures
|
||||||
|
####
|
||||||
|
|
||||||
|
|
||||||
|
# HTTP client for testing app requests
|
||||||
@pytest.fixture(scope="module")
|
@pytest.fixture(scope="module")
|
||||||
def accounts_app():
|
def client():
|
||||||
accounts_app = Flask(__name__, template_folder='templates')
|
app = create_app()
|
||||||
accounts_app.register_blueprint(accounts)
|
|
||||||
accounts_app.config.from_object(TestConfig)
|
|
||||||
|
|
||||||
yield accounts_app
|
return app.test_client()
|
||||||
|
|
||||||
@pytest.fixture(scope="module")
|
|
||||||
def client(accounts_app):
|
|
||||||
return accounts_app.test_client()
|
|
||||||
|
|
||||||
|
|||||||
78
tests/testing_app.py
Executable file
78
tests/testing_app.py
Executable file
@ -0,0 +1,78 @@
|
|||||||
|
# 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.user import User
|
||||||
|
from accounts.models.visit import Visit
|
||||||
|
|
||||||
|
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
|
||||||
|
test_user1 = User(username='test_user1',
|
||||||
|
email='testing@email.com',
|
||||||
|
address='123 Home Ln. Baltimore,MD 12345',
|
||||||
|
password='12345')
|
||||||
|
|
||||||
|
# Add test users to db session
|
||||||
|
db.session.add(test_user1)
|
||||||
|
|
||||||
|
# Commit test users to db
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
# Create test dogs
|
||||||
|
test_dog1 = Dog(name='Lassey',
|
||||||
|
breed='Golden Retreiver',
|
||||||
|
owner_id=test_user1.id)
|
||||||
|
test_dog2 = Dog(name='Rufus',
|
||||||
|
breed='Basset Hound',
|
||||||
|
owner_id=test_user1.id)
|
||||||
|
|
||||||
|
# Add test dogs to db session
|
||||||
|
db.session.add(test_dog1)
|
||||||
|
db.session.add(test_dog2)
|
||||||
|
|
||||||
|
# Commit test dogs to db
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
# Create test visits
|
||||||
|
test_visit1 = Visit(date_time=datetime(2023,12,5,10,30),
|
||||||
|
owner_id=test_user1.id,
|
||||||
|
dog_id=test_dog2.id)
|
||||||
|
|
||||||
|
# Add test visits to db session
|
||||||
|
db.session.add(test_visit1)
|
||||||
|
|
||||||
|
# Commit test visits to db
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user