# -*- coding: utf-8 -*- """ Copyright © 2018 PocketBudgetTracker. All rights reserved. Author: Andrey Shelest ([email protected]) Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. """ from tornado_sqlalchemy import declarative_base BASE_MODEL = declarative_base()
from sqlalchemy import Column, BigInteger, String from tornado.gen import coroutine from tornado.ioloop import IOLoop from tornado.options import options from tornado_sqlalchemy import (as_future, declarative_base, make_session_factory, SessionMixin) from tornado.web import RequestHandler, Application DeclarativeBase = declarative_base() class User(DeclarativeBase): __tablename__ = 'users' id = Column(BigInteger, primary_key=True) username = Column(String(255), unique=True) class SyncWebRequestHandler(RequestHandler, SessionMixin): def get(self): with self.make_session() as session: count = session.query(User).count() self.write('{} users so far!'.format(count)) class AsyncWebRequestHandler(RequestHandler, SessionMixin): @coroutine def get(self): with self.make_session() as session:
def test_multiple_calls_return_the_same_instance(self): first = declarative_base() second = declarative_base() self.assertTrue(first is second) self.assertEqual(id(first), id(second))