vendredi 29 janvier 2016

Sqlalchemy, tornado and testing unit

I'm having some trouble figuring out how to handle testing with tornado and sqlalchemy... For now all the calls are synchronous to the DB,( WS will handle only real time date from the PLC, not implemented yet).

The biggest "challenge" is how to handle testing that require writes to the mysql DB. I need a second testing schema or to encapsulate every query in a transaction that will be rolled back?

The sqlalchemy file is like this..

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, ForeignKey, ForeignKeyConstraint
from sqlalchemy.dialects.mysql import [...]
from sqlalchemy.orm import sessionmaker
import sqlalchemy
from sqlalchemy.engine import create_engine


engine = create_engine('mysql+pymysql://root:@localhost/HMI')
Base = declarative_base()


class Alarm(Base):
    __tablename__ = 'alarms'

    id = Column(Integer, primary_key=True)
    event_type = Column(sqlalchemy.types.NVARCHAR(255))
    sub_type = Column(sqlalchemy.types.NVARCHAR(255))
    alarm_id = Column(sqlalchemy.types.DATETIME)
    alarm_event_type = Column(sqlalchemy.types.FLOA
   [... other tables...]
try:
    Base.metadata.create_all(engine)
except Exception as e:
    print (e)
Session = sessionmaker(bind=engine)

While the application is runned by:

class TornadoBoilerplate(tornado.web.Application):
    def __init__(self):
        tornado.web.Application.__init__(self, urls.url_patterns, **settings)

def main():
    app = TornadoBoilerplate()
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()

and every query is performed by a :

session = Session()
session.query([...])

How can I perform testing? Any tips?

Aucun commentaire:

Enregistrer un commentaire