testing.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # -*- coding: utf-8 -*-
  2. # Copyright (c) 2017 Tomek Wójcik <tomek@bthlabs.pl>
  3. #
  4. # Permission is hereby granted, free of charge, to any person obtaining a copy
  5. # of this software and associated documentation files (the "Software"), to deal
  6. # in the Software without restriction, including without limitation the rights
  7. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. # copies of the Software, and to permit persons to whom the Software is
  9. # furnished to do so, subject to the following conditions:
  10. #
  11. # The above copyright notice and this permission notice shall be included in
  12. # all copies or substantial portions of the Software.
  13. #
  14. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. # THE SOFTWARE.
  21. #
  22. """
  23. q3stats.testing
  24. ===============
  25. This module contains testing utilites.
  26. """
  27. import json
  28. import os
  29. from flask import Response
  30. from q3stats.lib.config import get_config
  31. import q3stats.database
  32. import q3stats.models
  33. from q3stats.web_app.application import create_app
  34. class ResponseWrapper(Response):
  35. @property
  36. def json(self):
  37. if not hasattr(self, '_json_data'):
  38. self._json_data = json.loads(self.get_data().decode('utf-8'))
  39. return self._json_data
  40. class BaseQ3StatsTestCase(object):
  41. """Base class for Q3Stats test cases."""
  42. CONFIG_PATH = os.path.join(os.getcwd(), 'testing.cfg')
  43. @classmethod
  44. def setUpClass(cls):
  45. cls._config = get_config(cls.CONFIG_PATH)
  46. q3stats.database.db_engine = q3stats.database.create_engine(
  47. cls._config
  48. )
  49. q3stats.database.db_session = q3stats.database.create_session(
  50. q3stats.database.db_engine, cls._config
  51. )
  52. q3stats.database.Base.metadata.drop_all(
  53. bind=q3stats.database.db_engine
  54. )
  55. q3stats.database.Base.metadata.create_all(
  56. bind=q3stats.database.db_engine
  57. )
  58. @classmethod
  59. def tearDownClass(cls):
  60. pass
  61. class BaseQ3StatsWebAppTestCase(BaseQ3StatsTestCase):
  62. """Base class for Q3Stats Web App test cases."""
  63. @classmethod
  64. def setUpClass(cls):
  65. super(BaseQ3StatsWebAppTestCase, cls).setUpClass()
  66. cls.app = create_app(cls.CONFIG_PATH)
  67. cls.app.response_class = ResponseWrapper
  68. cls.client = cls.app.test_client()