-
-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
205 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from .. import Officer, StateID, Incident | ||
from sqlalchemy.orm import Session | ||
|
||
|
||
def officer_exists(db: Session, stateID: StateID) -> bool: | ||
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, case_id: str) -> bool: | ||
return ( | ||
db.query(Incident).filter(Incident.case_id == case_id).first() | ||
is not None | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
from unittest.mock import patch, Mock | ||
import pytest | ||
from backend.scraper.run_scrape import add_to_database | ||
|
||
|
||
@pytest.fixture | ||
def cache(): | ||
cache = Mock() | ||
cache.get_json.return_value = {"model": "data"} | ||
return cache | ||
|
||
|
||
@pytest.fixture | ||
def model(): | ||
model = Mock() | ||
model.__getstate__ = Mock(return_value={"model": "data"}) | ||
return model | ||
|
||
|
||
@pytest.fixture | ||
def uid(): | ||
return "123" | ||
|
||
|
||
@pytest.fixture | ||
def table(): | ||
return "officer" | ||
|
||
|
||
def test_add_to_database_existing_in_cache( | ||
cache: Mock, model: Mock, uid: str, table: str | ||
): | ||
# Calling the function | ||
add_to_database(model, cache, uid, table) | ||
|
||
# Assertions | ||
cache.get_json.assert_called_once_with(uid, table) | ||
model.create.assert_not_called() | ||
cache.set_json.assert_not_called() | ||
|
||
|
||
@patch("backend.scraper.run_scrape.officer_exists") | ||
def test_add_to_database_existing_in_database( | ||
mock_officer_exists: Mock, | ||
cache: Mock, | ||
model: Mock, | ||
uid: str, | ||
table: str, | ||
): | ||
cache.get_json.return_value = None | ||
mock_officer_exists.return_value = True | ||
|
||
# Calling the function | ||
add_to_database(model, cache, uid, table) | ||
|
||
# Assertions | ||
cache.get_json.assert_called_once_with(uid, table) | ||
model.create.assert_not_called() | ||
cache.set_json.assert_not_called() | ||
mock_officer_exists.assert_called_once() | ||
|
||
|
||
@patch("backend.scraper.run_scrape.officer_exists") | ||
def test_add_to_database_new_model( | ||
mock_officer_exists: Mock, cache: Mock, model: Mock, uid: str, table: str | ||
): | ||
cache.get_json.return_value = None | ||
mock_officer_exists.return_value = False | ||
|
||
# Calling the function | ||
add_to_database(model, cache, uid, table) | ||
|
||
# Assertions | ||
cache.get_json.assert_called_once_with(uid, table) | ||
model.create.assert_called_once() | ||
cache.set_json.assert_called_once_with(uid, model.__getstate__(), table) | ||
mock_officer_exists.assert_called_once() | ||
|
||
|
||
def test_add_to_database_invalid_table(cache: Mock, model: Mock, uid: str): | ||
table = "invalid_table" | ||
|
||
# Calling the function | ||
try: | ||
add_to_database(model, cache, uid, table) | ||
except ValueError as e: | ||
# Assertion | ||
assert str(e) == "Invalid table invalid_table" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from backend.database.queries.queries import officer_exists, incident_exists | ||
from backend.database import Officer, StateID, Incident | ||
from typing import Any | ||
|
||
|
||
def test_officer_exists(db_session: Any): | ||
# 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 manually that the officer exists | ||
assert ( | ||
db_session.query(Officer) | ||
.join(StateID) | ||
.filter( | ||
StateID.value == state_id.value and StateID.state == state_id.state | ||
) | ||
.first() | ||
is not None | ||
) | ||
|
||
# Test that the officer exists | ||
assert officer_exists(db_session, state_id) | ||
|
||
# Test that a non-existing officer returns False | ||
assert not officer_exists( | ||
db_session, StateID(**{"value": "DEF456", "state": "NY"}) | ||
) | ||
|
||
|
||
def test_incident_exists(db_session: Any): | ||
# 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 manually that the incident exists | ||
assert ( | ||
db_session.query(Incident) | ||
.filter(Incident.case_id == case_id_value) | ||
.first() | ||
is not None | ||
) | ||
|
||
# Test that the incident exists | ||
assert incident_exists(db_session, case_id_value) | ||
|
||
# Test that a non-existing incident returns False | ||
assert not incident_exists(db_session, "654321") |