/py/tests/__init__.py

https://github.com/andrewcooke/choochoo · Python · 42 lines · 35 code · 7 blank · 0 comment · 0 complexity · 91c53ee4bc64b0911dd155dae9491871 MD5 · raw file

  1. import datetime as dt
  2. from logging import getLogger, WARNING, INFO, DEBUG
  3. from unittest import TestCase
  4. from ch2 import PROGNAME
  5. from ch2.common.args import mm
  6. from ch2.common.io import data_hash
  7. from ch2.common.log import configure_log
  8. from ch2.commands.args import make_parser, NamespaceWithVariables, DB_VERSION
  9. from ch2.common.names import USER
  10. from ch2.common.user import make_user_database
  11. from ch2.sql.config import Config
  12. from ch2.sql.support import Base
  13. log = getLogger(__name__)
  14. class LogTestCase(TestCase):
  15. def setUp(self):
  16. configure_log(PROGNAME, '/tmp/ch2-test.log', verbosity=5, levels={
  17. 'sqlalchemy': WARNING,
  18. 'matplotlib': INFO,
  19. 'bokeh': DEBUG,
  20. 'tornado': INFO,
  21. 'sentinelsat': DEBUG,
  22. 'werkzeug': DEBUG,
  23. 'ch2': DEBUG,
  24. '__main__': DEBUG
  25. })
  26. def random_test_user(args=(mm(USER), 'postgres')):
  27. parser = make_parser()
  28. ns = NamespaceWithVariables._from_ns(parser.parse_args(args=args), PROGNAME, DB_VERSION)
  29. config = Config(ns)
  30. user = data_hash(str(dt.datetime.now()))[:6]
  31. log.info(f'User/database {user}')
  32. user_config = make_user_database(config, user, '')
  33. log.info('Creating tables')
  34. Base.metadata.create_all(user_config.db.engine)
  35. return user