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