Skip to content

Commit 7f9cac9

Browse files
committed
Add test setup
1 parent d0f73d5 commit 7f9cac9

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,5 @@ ENV/
8787

8888
# Rope project settings
8989
.ropeproject
90+
91+
.idea

app.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from flask import Flask
2+
from flask.ext.sqlalchemy import SQLAlchemy
3+
from models import db, TestTicket, TicketStatus, Ticket
4+
5+
app = Flask(__name__)
6+
7+
db.init_app(app)
8+
9+
with app.app_context():
10+
db.create_all()
11+
db.session.commit()
12+
13+
14+
@app.route('/')
15+
def index():
16+
# This will fail on the first run
17+
try:
18+
result = db.session.query(Ticket) \
19+
.filter(Ticket.status == TicketStatus.open) \
20+
.all()
21+
except:
22+
print('query failed')
23+
else:
24+
print('query successful')
25+
26+
# If I query the subclass, everything will work fine
27+
db.session.query(TestTicket) \
28+
.filter(Ticket.status == TicketStatus.open) \
29+
.all()
30+
31+
return '', 200
32+
33+
34+
if __name__ == '__main__':
35+
app.run(debug=True)

models.py

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import enum
2+
3+
from flask.ext.sqlalchemy import SQLAlchemy
4+
from sqlalchemy import func
5+
from sqlalchemy.ext.declarative import AbstractConcreteBase
6+
from sqlalchemy.ext.declarative import declared_attr
7+
8+
9+
db = SQLAlchemy()
10+
11+
12+
class Base(db.Model):
13+
__abstract__ = True
14+
15+
@declared_attr
16+
def __tablename__(cls):
17+
return cls.__name__.lower()
18+
19+
id = db.Column(db.Integer, primary_key=True)
20+
21+
22+
class TimestampMixin(object):
23+
created_on = db.Column(db.DateTime, server_default=func.now())
24+
updated_on = db.Column(db.DateTime, server_default=func.now(),
25+
onupdate=func.now())
26+
27+
28+
class TicketStatus(enum.Enum):
29+
open = 'open'
30+
in_progess = 'in_progress'
31+
rejected = 'rejected'
32+
closed = 'closed'
33+
34+
all = (open, in_progess, rejected, closed)
35+
36+
37+
class Ticket(AbstractConcreteBase, TimestampMixin, Base):
38+
status = db.Column(db.Enum(TicketStatus), nullable=False,
39+
default=TicketStatus.open)
40+
41+
@declared_attr
42+
def __mapper_args__(cls):
43+
if cls == Ticket:
44+
return {'concrete': False}
45+
return {
46+
'polymorphic_identity': cls.__name__,
47+
'concrete': True
48+
}
49+
50+
51+
class TestTicket(Ticket):
52+
data = db.Column(db.String, nullable=False)

0 commit comments

Comments
 (0)