test_logger.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import logging
  2. import pytest
  3. from django.db import (
  4. DatabaseError,
  5. IntegrityError,
  6. OperationalError,
  7. connection,
  8. )
  9. from dj_cqrs.logger import (
  10. _LastQueryCaptureWrapper,
  11. install_last_query_capturer,
  12. log_timed_out_queries,
  13. )
  14. from tests.dj_replica import models
  15. @pytest.mark.django_db(transaction=True)
  16. def test_install_last_query_capturer():
  17. for _ in range(2):
  18. install_last_query_capturer(models.AuthorRef)
  19. assert len(connection.execute_wrappers) == 1
  20. assert isinstance(connection.execute_wrappers[0], _LastQueryCaptureWrapper)
  21. with connection.cursor() as c:
  22. c.execute('SELECT 1')
  23. assert connection.execute_wrappers[0].query == 'SELECT 1'
  24. connection.execute_wrappers.pop()
  25. def test_log_timed_out_queries_not_supported(caplog):
  26. assert log_timed_out_queries(None, None) is None
  27. assert not caplog.record_tuples
  28. @pytest.mark.parametrize(
  29. 'error',
  30. [
  31. IntegrityError('some error'),
  32. DatabaseError(),
  33. OperationalError(),
  34. ],
  35. )
  36. def test_log_timed_out_queries_other_error(error, settings, caplog):
  37. settings.CQRS_LOG_TIMED_OUT_QUERIES = 1
  38. assert log_timed_out_queries(error, None) is None
  39. assert not caplog.record_tuples
  40. @pytest.mark.django_db(transaction=True)
  41. @pytest.mark.parametrize(
  42. 'engine, error, l_name, records',
  43. [
  44. ('sqlite', None, None, []),
  45. (
  46. 'postgres',
  47. OperationalError('canceling statement due to statement timeout'),
  48. None,
  49. [
  50. (
  51. 'django-cqrs',
  52. logging.ERROR,
  53. 'Timed out query:\nSELECT 1',
  54. )
  55. ],
  56. ),
  57. (
  58. 'postgres',
  59. OperationalError('canceling statement due to statement timeout'),
  60. 'long-query',
  61. [
  62. (
  63. 'long-query',
  64. logging.ERROR,
  65. 'Timed out query:\nSELECT 1',
  66. )
  67. ],
  68. ),
  69. (
  70. 'postgres',
  71. OperationalError('could not connect to server'),
  72. None,
  73. [],
  74. ),
  75. (
  76. 'postgres',
  77. OperationalError(125, 'Some error'),
  78. None,
  79. [],
  80. ),
  81. (
  82. 'mysql',
  83. OperationalError(3024),
  84. None,
  85. [
  86. (
  87. 'django-cqrs',
  88. logging.ERROR,
  89. 'Timed out query:\nSELECT 1',
  90. )
  91. ],
  92. ),
  93. (
  94. 'mysql',
  95. OperationalError(
  96. 3024, 'Query exec was interrupted, max statement execution time exceeded'
  97. ),
  98. 'long-query-1',
  99. [
  100. (
  101. 'long-query-1',
  102. logging.ERROR,
  103. 'Timed out query:\nSELECT 1',
  104. )
  105. ],
  106. ),
  107. (
  108. 'mysql',
  109. OperationalError(1040, 'Too many connections'),
  110. None,
  111. [],
  112. ),
  113. ],
  114. )
  115. def test_apply_query_timeouts(settings, engine, l_name, error, records, caplog):
  116. if settings.DB_ENGINE != engine:
  117. return
  118. settings.CQRS['replica']['CQRS_LOG_TIMED_OUT_QUERIES'] = True
  119. settings.CQRS['replica']['CQRS_QUERY_LOGGER'] = l_name
  120. model_cls = models.BasicFieldsModelRef
  121. install_last_query_capturer(model_cls)
  122. with connection.cursor() as c:
  123. c.execute('SELECT 1')
  124. assert log_timed_out_queries(error, model_cls) is None
  125. assert caplog.record_tuples == records
  126. connection.execute_wrappers.pop()