Przemek Lewandowski
@haxoza
db = SQLAlchemy()
class Document(db.Model):
id = Column(Integer(), primary_key=True)
data = Column(MutableDict.as_mutable(JSONB), nullable=False)
def __init__(self, data):
self.data = data
#!/usr/bin/env python
from flask.ext.script import Manager, Command
from flask.ext.migrate import MigrateCommand
from app import create_app
from grabber import grab_feeds
from tasks import run_celery
from tests.command import PytestCommand
manager = Manager(create_app)
manager.add_option('-c', '--config', dest='config_file', required=False)
manager.add_command('db', MigrateCommand)
manager.add_command('test', PytestCommand)
manager.add_command('runcelery', Command(run_celery))
manager.command(grab_feeds)
if __name__ == '__main__':
manager.run()
def create_app(config_file=None, settings_override=None):
app = Flask(__name__)
if config_file:
app.config.from_pyfile(config_file)
else:
app.config.from_envvar(config_variable_name)
if settings_override:
app.config.update(settings_override)
init_app(app)
return app
def init_app(app):
db.init_app(app)
migrate.init_app(app, db)
api.init_app(app)
@pytest.yield_fixture(scope='session')
def app():
app = create_app(config_file='config/testing.py')
app.response_class = ApiTestingResponse
ctx = app.app_context()
ctx.push()
yield app
ctx.pop()
@pytest.yield_fixture(scope='session')
def db(app):
_db.create_all()
yield _db
_db.drop_all()
@pytest.fixture(scope='session')
def client(app):
return app.test_client()
@pytest.yield_fixture(scope='function')
def db_session(db):
connection = db.engine.connect()
transaction = connection.begin()
options = dict(bind=connection)
session = db.create_scoped_session(options=options)
db.session = session
yield session
transaction.rollback()
connection.close()
session.remove()
class FeedsResource(Resource):
def get(self):
# request parsing
return {
'data': 'transformed to json',
}
api = FeedsApi()
api.add_resource(FeedsResource, '/feeds')
def test_get_should_return_feed_data(client, session):
feed_entries_count = 3
factories.FeedDataFactory.create_batch(feed_entries_count)
response = client.get('/feeds')
assert response.status_code == 200
assert response.json['records_total'] == feed_entries_count
assert len(response.json['data']) == feed_entries_count
It's not about code size.
It's about common sense.