Created
June 4, 2012 11:18
-
-
Save saxicek/2867804 to your computer and use it in GitHub Desktop.
flask-restless: POST method fails when called first
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Flask | |
SQLAlchemy | |
Flask-Restless |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask | |
from flask.ext.restless import APIManager | |
from sqlalchemy import create_engine, Column, Integer, String | |
from sqlalchemy.orm import scoped_session, create_session, relationship | |
from sqlalchemy.ext.declarative import declarative_base | |
from sqlalchemy.schema import ForeignKey | |
# lazy SQLAlchemy setup (http://flask.pocoo.org/snippets/22/) | |
engine = None | |
db_session = scoped_session(lambda: create_session(bind=engine,autocommit=False,autoflush=False)) | |
def init_engine(uri, **kwargs): | |
global engine | |
engine = create_engine(uri, **kwargs) | |
return engine | |
Base = declarative_base() | |
Base.query = db_session.query_property() | |
class UserVoting(Base): | |
__tablename__ = 'user_voting' | |
id = Column(Integer, primary_key=True) | |
vote = Column(String(1), nullable=False) | |
user_id = Column(Integer, ForeignKey('app_user.id'), nullable=False) | |
voting_review_id = Column(Integer, ForeignKey('voting_review.id'), nullable=False) | |
class VotingReview(Base): | |
__tablename__ = 'voting_review' | |
id = Column(Integer, primary_key=True) | |
title = Column(String(160), nullable=False) | |
user_id = Column(Integer, ForeignKey('app_user.id'), nullable=False) | |
userVotings = relationship('UserVoting', backref='votingReview') | |
class User(Base): | |
__tablename__ = 'app_user' | |
id = Column(Integer, primary_key=True) | |
name = Column(String(255)) | |
votingReviews = relationship('VotingReview', backref='user') | |
userVotings = relationship('UserVoting', backref='user') | |
app = Flask(__name__) | |
# Create the Flask-Restless API manager. | |
manager = APIManager(app, session=db_session) | |
manager.create_api(VotingReview, methods=['GET', 'POST', 'DELETE']) | |
@app.teardown_request | |
def shutdown_session(exception=None): | |
db_session.remove() | |
if __name__ == '__main__': | |
init_engine(app.config['DATABASE_URL']) | |
app.run(host='0.0.0.0') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import unittest | |
import tempfile | |
import json | |
from testapp import app, init_engine | |
class TestappTestCase(unittest.TestCase): | |
def setUp(self): | |
self.db_fd, app.config['DATABASE'] = tempfile.mkstemp() | |
database_url = 'sqlite:///%s' % self.db_fd | |
init_engine(database_url) | |
app.config['TESTING'] = True | |
self.app = app.test_client() | |
def tearDown(self): | |
os.close(self.db_fd) | |
os.unlink(app.config['DATABASE']) | |
def test_root(self): | |
response = self.app.post('/api/voting_review', | |
data=json.dumps({'title': u'title', 'voting_id': 23})) | |
self.assertEqual(response.status_code, 200) | |
if __name__ == '__main__': | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment