Skip to content

Commit

Permalink
Update queries and hard coded delete
Browse files Browse the repository at this point in the history
  • Loading branch information
RyEggGit committed Jan 11, 2024
1 parent d81bdb3 commit 59ded69
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
7 changes: 3 additions & 4 deletions backend/database/queries/queries.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
from .. import Officer, StateID, Incident
from sqlalchemy.orm import Session
from typing import Optional


def officer_exists(db: Session, stateID: StateID) -> bool:
def officer_exists(db: Session, stateID: StateID) -> Optional[Officer]:
return (
db.query(Officer)
.join(StateID)
.filter(
StateID.value == stateID.value and StateID.state == stateID.state
)
.first()
is not None
)


def incident_exists(db: Session, incident: Incident) -> bool:
def incident_exists(db: Session, incident: Incident) -> Optional[Incident]:
return (
db.query(Incident).filter(Incident.case_id == incident.case_id).first()
is not None
)
3 changes: 1 addition & 2 deletions backend/scraper/run_scrape.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ def add_to_database(
raise ValueError(f"Invalid table {table}")
if model_exists: # type: ignore
logger.info(f"{table} {uid} already in database")
return

model.delete()
model.create()

# add the model to the cache
Expand Down
37 changes: 37 additions & 0 deletions backend/tests/test_queries.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from backend.database.queries.queries import officer_exists, incident_exists
from backend.database import Officer, StateID, Incident
from typing import Any
from sqlalchemy.orm import Session


def test_officer_exists(db_session: Any):
Expand Down Expand Up @@ -52,3 +53,39 @@ def test_incident_exists(db_session: Any):

# Test that a non-existing incident returns False
assert not incident_exists(db_session, Incident(**{"case_id": "654321"}))


def test_officer_exists_with_state_id(db_session: Session):
# Create a test officer with a state ID value
state_id_value = "ABC123"
officer = Officer(**{"first_name": "Test Officer"})
state_id = StateID(**{"value": state_id_value, "state": "NY"})
officer.stateId = state_id # type: ignore
db_session.add(officer)
db_session.commit()

# Test that the officer exists
assert officer_exists(db_session, state_id) is not None


def test_officer_exists_with_invalid_state_id(db_session: Session):
# Test that a non-existing officer returns False
assert not officer_exists(
db_session, StateID(**{"value": "DEF456", "state": "NY"})
)


def test_incident_exists_with_case_id(db_session: Session):
# Create a test incident with a case ID value
case_id_value = "123456"
incident = Incident(**{"case_id": case_id_value})
db_session.add(incident)
db_session.commit()

# Test that the incident exists
assert incident_exists(db_session, incident) is not None


def test_incident_exists_with_invalid_case_id(db_session: Session):
# Test that a non-existing incident returns False
assert not incident_exists(db_session, Incident(**{"case_id": "654321"}))

0 comments on commit 59ded69

Please sign in to comment.