how to use?
# inside your test
detector = LazySQLDetector()
with detector.track(), no_dirty_allowed():
your_heavy_read_only_api()
from typing import ( | |
Protocol, | |
Callable, | |
TypeVar, | |
ParamSpec, | |
overload, | |
Concatenate, | |
runtime_checkable | |
) |
import enum | |
from datetime import datetime | |
from sqlalchemy import Column | |
from sqlalchemy import DateTime | |
from sqlalchemy import Enum | |
from sqlalchemy import event | |
from sqlalchemy import func | |
from sqlalchemy import select | |
from sqlalchemy import String |
from sqlalchemy import select, text | |
def reset_sequences(engine, schema="public", gap=10000): | |
""" | |
Reset all sequences in a given schema to 10000, 20000, etc. so that ids won't overlap easily. | |
Ensures we don't get "lucky" and crossing ids between tables works because they are both id=1, | |
passing a test that should fail. | |
""" | |
with engine.connect() as conn: |
""" | |
This defines a decorator that works kind of like a type-guard (https://peps.python.org/pep-0647/) for the arguments | |
of a function. | |
The usefulness of this is for functions that "defer" their arguments to another function, e.g. places where you | |
would call functools.wraps(). | |
@same_params_as(f) | |
@functools.wraps(f) | |
def passes_args_through(*a, **kw): |
function deflate(obj) { | |
function _deflate(obj, _sofar = new Map(), _cur_ref = [0]) { | |
if (obj === null || typeof obj !== 'object') { | |
return obj; | |
} | |
if (Array.isArray(obj)) { | |
return obj.map(v => _deflate(v, _sofar, _cur_ref)); | |
} |
import re | |
import importlib | |
from pathlib import Path | |
def import_all(module_name: str, ignore: str = None) -> None: | |
""" | |
Helper to recursively import all .py files on disk. | |
This ensures that all side effects from importing a module "trigger". | |
(For example, registering a model with the ORM, or adding routes.) |
def inherit_annotations(cls): | |
""" | |
Inherit annotations from all base classes according to method-resolution-order. | |
This is the same way that type checkers will interpret annotations. | |
This allows for other class decorators such as attr.define() or dataclasses.dataclass() | |
to see the inherited annotations from plain-vanilla python classes. This, in turn, | |
allows base classes defining common fields to be shared among different class-decorator-annotation | |
libraries. | |
""" |
""" | |
This module is a helper for converting a dataclass-like annotation class into a sqlalchemy ORM. | |
""" | |
from dataclasses import dataclass | |
from datetime import datetime | |
import enum | |
import functools | |
import inspect | |
import re | |
import types |
""" | |
Utility for parsing .env files, as used by docker-compose and a few others. | |
""" | |
import re | |
QUOTED_STRING = r'("(?:(\\.|[^"\\])*)")' | |
SINGLE_QUOTED_STRING = r"('(?:(\\.|[^'\\])*)')" | |
VAL = fr'([^\s]+|{QUOTED_STRING}|{SINGLE_QUOTED_STRING})' | |
LINE = fr'^\s*(?P<key>{VAL})\s*=\s*(?P<val>{VAL})\s*(#.*)?$' |