Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
talboren committed Dec 9, 2024
1 parent 347b393 commit 72d2c65
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 57 deletions.
55 changes: 27 additions & 28 deletions keep/api/core/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -4688,35 +4688,34 @@ def set_last_alert(
) -> None:
logger.info(f"Set last alert for `{alert.fingerprint}`")
with existed_or_new_session(session) as session:
with session.begin_nested() as transaction:
last_alert = get_last_alert_by_fingerprint(
tenant_id, alert.fingerprint, session, for_update=True
)
last_alert = get_last_alert_by_fingerprint(
tenant_id, alert.fingerprint, session, for_update=True
)

# To prevent rare, but possible race condition
# For example if older alert failed to process
# and retried after new one
if last_alert and last_alert.timestamp.replace(
tzinfo=tz.UTC
) < alert.timestamp.replace(tzinfo=tz.UTC):
# To prevent rare, but possible race condition
# For example if older alert failed to process
# and retried after new one
if last_alert and last_alert.timestamp.replace(
tzinfo=tz.UTC
) < alert.timestamp.replace(tzinfo=tz.UTC):

logger.info(
f"Update last alert for `{alert.fingerprint}`: {last_alert.alert_id} -> {alert.id}"
)
last_alert.timestamp = alert.timestamp
last_alert.alert_id = alert.id
session.add(last_alert)
logger.info(
f"Update last alert for `{alert.fingerprint}`: {last_alert.alert_id} -> {alert.id}"
)
last_alert.timestamp = alert.timestamp
last_alert.alert_id = alert.id
session.add(last_alert)

elif not last_alert:
logger.info(f"No last alert for `{alert.fingerprint}`, creating new")
last_alert = LastAlert(
tenant_id=tenant_id,
fingerprint=alert.fingerprint,
timestamp=alert.timestamp,
first_timestamp=alert.timestamp,
alert_id=alert.id,
alert_hash=alert.alert_hash,
)
elif not last_alert:
logger.info(f"No last alert for `{alert.fingerprint}`, creating new")
last_alert = LastAlert(
tenant_id=tenant_id,
fingerprint=alert.fingerprint,
timestamp=alert.timestamp,
first_timestamp=alert.timestamp,
alert_id=alert.id,
alert_hash=alert.alert_hash,
)

session.add(last_alert)
transaction.commit()
session.add(last_alert)
session.commit()
16 changes: 15 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ pytest-docker = "^2.0.1"
playwright = "^1.44.0"

freezegun = "^1.5.1"
pytest-timeout = "^2.3.1"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
Expand Down
68 changes: 44 additions & 24 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
import requests
from dotenv import find_dotenv, load_dotenv
from pytest_docker.plugin import get_docker_services
from sqlalchemy import event, text
from sqlalchemy.orm import sessionmaker
from sqlalchemy.pool import StaticPool
from sqlmodel import SQLModel, Session, create_engine
from sqlmodel import Session, SQLModel, create_engine
from starlette_context import context, request_cycle_context

from keep.api.core.db import set_last_alert
# This import is required to create the tables
from keep.api.core.dependencies import SINGLE_TENANT_UUID
from keep.api.core.elastic import ElasticClient
Expand Down Expand Up @@ -177,14 +177,33 @@ def db_session(request):
connect_args={"check_same_thread": False},
poolclass=StaticPool,
)

# @tb: leaving this here if anybody else gets to problem with nested transactions
# https://docs.sqlalchemy.org/en/20/dialects/sqlite.html#serializable-isolation-savepoints-transactional-ddl
@event.listens_for(mock_engine, "connect")
def do_connect(dbapi_connection, connection_record):
# disable pysqlite's emitting of the BEGIN statement entirely.
# also stops it from emitting COMMIT before any DDL.
dbapi_connection.isolation_level = None

@event.listens_for(mock_engine, "begin")
def do_begin(conn):
# emit our own BEGIN
try:
conn.exec_driver_sql(text("BEGIN EXCLUSIVE"))
except Exception:
pass

SQLModel.metadata.create_all(mock_engine)

# Mock the environment variables so db.py will use it
os.environ["DATABASE_CONNECTION_STRING"] = db_connection_string

# Create a session
# Passing class_=Session to use the Session class from sqlmodel (https://github.com/fastapi/sqlmodel/issues/75#issuecomment-2109911909)
SessionLocal = sessionmaker(class_=Session, autocommit=False, autoflush=False, bind=mock_engine)
SessionLocal = sessionmaker(
class_=Session, autocommit=False, autoflush=False, bind=mock_engine
)
session = SessionLocal()
# Prepopulate the database with test data

Expand Down Expand Up @@ -510,8 +529,7 @@ def setup_alerts(elastic_client, db_session, request):

existed_last_alerts = db_session.query(LastAlert).all()
existed_last_alerts_dict = {
last_alert.fingerprint: last_alert
for last_alert in existed_last_alerts
last_alert.fingerprint: last_alert for last_alert in existed_last_alerts
}

last_alerts = []
Expand All @@ -520,9 +538,7 @@ def setup_alerts(elastic_client, db_session, request):
last_alert = existed_last_alerts_dict[alert.fingerprint]
last_alert.alert_id = alert.id
last_alert.timestamp = alert.timestamp
last_alerts.append(
last_alert
)
last_alerts.append(last_alert)
else:
last_alerts.append(
LastAlert(
Expand Down Expand Up @@ -580,18 +596,15 @@ def _setup_stress_alerts_no_elastic(num_alerts):

existed_last_alerts = db_session.query(LastAlert).all()
existed_last_alerts_dict = {
last_alert.fingerprint: last_alert
for last_alert in existed_last_alerts
last_alert.fingerprint: last_alert for last_alert in existed_last_alerts
}
last_alerts = []
for alert in alerts:
if alert.fingerprint in existed_last_alerts_dict:
last_alert = existed_last_alerts_dict[alert.fingerprint]
last_alert.alert_id = alert.id
last_alert.timestamp=alert.timestamp
last_alerts.append(
last_alert
)
last_alert.timestamp = alert.timestamp
last_alerts.append(last_alert)
else:
last_alerts.append(
LastAlert(
Expand Down Expand Up @@ -625,7 +638,9 @@ def setup_stress_alerts(

@pytest.fixture
def create_alert(db_session):
def _create_alert(fingerprint, status, timestamp, details=None, tenant_id=SINGLE_TENANT_UUID):
def _create_alert(
fingerprint, status, timestamp, details=None, tenant_id=SINGLE_TENANT_UUID
):
details = details or {}
random_name = "test-{}".format(fingerprint)
process_event(
Expand All @@ -634,7 +649,9 @@ def _create_alert(fingerprint, status, timestamp, details=None, tenant_id=SINGLE
tenant_id=tenant_id,
provider_id="test",
provider_type=(
details["source"][0] if details and "source" in details and details["source"] else None
details["source"][0]
if details and "source" in details and details["source"]
else None
),
fingerprint=fingerprint,
api_key_name="test",
Expand All @@ -658,11 +675,14 @@ def pytest_addoption(parser):
"""

parser.addoption(
"--integration", action="store_const", const=True,
dest="run_integration")
"--integration", action="store_const", const=True, dest="run_integration"
)
parser.addoption(
"--non-integration", action="store_const", const=True,
dest="run_non_integration")
"--non-integration",
action="store_const",
const=True,
dest="run_non_integration",
)


def pytest_configure(config):
Expand Down Expand Up @@ -706,9 +726,9 @@ def pytest_collection_modifyitems(items):
elif "keycloak_client" in fixturenames:
item.add_marker("integration")
elif (
hasattr(item, "callspec")
and "db_session" in item.callspec.params
and item.callspec.params["db_session"]
and "db" in item.callspec.params["db_session"]
hasattr(item, "callspec")
and "db_session" in item.callspec.params
and item.callspec.params["db_session"]
and "db" in item.callspec.params["db_session"]
):
item.add_marker("integration")
3 changes: 1 addition & 2 deletions tests/fixtures/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ def test_app(monkeypatch, request):

provision_resources()
app = get_app()

yield app
return app


# Fixture for TestClient using the test_app fixture
Expand Down
Loading

0 comments on commit 72d2c65

Please sign in to comment.