Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds testing to signal instance creation #3077

Merged
merged 5 commits into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/dispatch/document/flows.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def update_document(document: Document, project_id: int, db_session: SessionLoca
db_session=db_session, project_id=project_id, plugin_type="document"
)
if not plugin:
log.warning(f"Document {document.name} not updated. No document plugin enabled.")
log.warning("Document not updated. No document plugin enabled.")
return

document_kwargs = {}
Expand Down
23 changes: 16 additions & 7 deletions src/dispatch/signal/flows.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
from dispatch.auth.models import DispatchUser
from dispatch.case import flows as case_flows
from dispatch.case import service as case_service
from dispatch.case.models import CaseCreate
from dispatch.database.core import SessionLocal
from dispatch.project.models import Project
from dispatch.case import service as case_service
from dispatch.case import flows as case_flows
from dispatch.entity import service as entity_service
from dispatch.project.models import Project
from dispatch.signal import service as signal_service
from dispatch.signal.models import SignalInstanceCreate


def create_signal_instance(db_session: SessionLocal, project: Project, signal_instance_data: dict):
def create_signal_instance(
db_session: SessionLocal,
project: Project,
signal_instance_data: dict,
current_user: DispatchUser = None,
):
"""Creates a signal and a case if necessary."""
signal = signal_service.get_by_variant_or_external_id(
db_session=db_session,
project_id=project.id,
external_id=signal_instance_data["id"],
variant=signal_instance_data.variant,
external_id=signal_instance_data.get("id"),
variant=signal_instance_data["variant"],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should it be .get("variant") since it's optional field?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so, technically it's one or the other, I think we may need to eventually re-add something like raw signal back and enforce either 'id' or 'variant' (otherwise we can't do anything with it). But I need to figure out how to add arbitrary keys in a pydantic model (if that's even possible).

I'm going to merge this for the time being until I can figure that out.

)

if not signal:
Expand Down Expand Up @@ -45,9 +51,12 @@ def create_signal_instance(db_session: SessionLocal, project: Project, signal_in
title=signal.name,
description=signal.description,
case_priority=signal.case_priority,
project=project,
case_type=signal.case_type,
)
case = case_service.create(db_session=db_session, case_in=case_in)
case = case_service.create(
db_session=db_session, case_in=case_in, current_user=current_user
)

signal_instance.case = case
db_session.commit()
Expand Down
5 changes: 4 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pytest
from sqlalchemy_utils import drop_database
from sqlalchemy_utils import drop_database, database_exists
from starlette.config import environ
from starlette.testclient import TestClient

Expand Down Expand Up @@ -101,6 +101,9 @@ def testapp():

@pytest.fixture(scope="session")
def db():
if database_exists(str(config.SQLALCHEMY_DATABASE_URI)):
drop_database(str(config.SQLALCHEMY_DATABASE_URI))

init_database(engine)
schema_engine = engine.execution_options(
schema_translate_map={
Expand Down
14 changes: 12 additions & 2 deletions tests/factories.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import uuid
from datetime import datetime

from factory import LazyAttribute, LazyFunction, Sequence, SubFactory, post_generation
from factory import (
LazyAttribute,
LazyFunction,
Sequence,
SubFactory,
post_generation,
SelfAttribute,
)
from factory.alchemy import SQLAlchemyModelFactory
from factory.fuzzy import FuzzyChoice, FuzzyDateTime, FuzzyInteger, FuzzyText
from faker import Faker
Expand Down Expand Up @@ -116,7 +123,7 @@ class ProjectFactory(BaseFactory):

name = Sequence(lambda n: f"project{n}")
description = FuzzyText()
default = Faker().pybool()
default = False
color = Faker().color()

class Meta:
Expand Down Expand Up @@ -678,6 +685,7 @@ class CaseTypeFactory(BaseFactory):

name = FuzzyText()
description = FuzzyText()

project = SubFactory(ProjectFactory)

class Meta:
Expand Down Expand Up @@ -810,8 +818,10 @@ class SignalFactory(BaseFactory):
external_url = "https://test.com"
external_id = "1234"
variant = "Test Variant"
enabled = True
loopin_signal_identity = False
project = SubFactory(ProjectFactory)
case_type = SubFactory(CaseTypeFactory, project=SelfAttribute("..project"))

class Meta:
model = Signal
Expand Down
17 changes: 17 additions & 0 deletions tests/signal/test_signal_flow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
def test_create_signal_instance(session, signal, case_severity, case_priority, user):
from dispatch.signal.flows import create_signal_instance

case_priority.default = True
case_priority.project_id = signal.project_id

case_severity.default = True
case_severity.project_id = signal.project_id

instance_data = {"variant": signal.variant}

assert create_signal_instance(
db_session=session,
project=signal.project,
signal_instance_data=instance_data,
current_user=user,
)