-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduces tests for cron_job_injest_events.py plus refactoring for e…
…asier tests
- Loading branch information
Mark Kasaboski
committed
Nov 5, 2024
1 parent
d00305c
commit 1c681de
Showing
3 changed files
with
139 additions
and
41 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
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,74 @@ | ||
|
||
import json | ||
import os | ||
import pytest | ||
import sys | ||
|
||
from unittest.mock import MagicMock | ||
from unittest.mock import patch | ||
|
||
|
||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "../../python")) | ||
from constants import KV_COLLECTION_NAME | ||
from cron_job_ingest_events import fetch_and_print_feed_results | ||
from cron_job_ingest_events import get_collection_value | ||
from cron_job_ingest_events import get_next | ||
from cron_job_ingest_events import main | ||
from cron_job_ingest_events import save_collection_value | ||
|
||
|
||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "../../python/vendor")) | ||
import vendor.splunklib.client as client | ||
|
||
|
||
def test_get_collection_value_expect_none() -> None: | ||
app = MagicMock() | ||
assert get_collection_value(app=app, key="some_key") is None | ||
|
||
|
||
def test_get_collection_value_expect_result() -> None: | ||
app = MagicMock() | ||
app.service.kvstore.__contains__.side_effect = lambda x: x == KV_COLLECTION_NAME | ||
app.service.kvstore[KV_COLLECTION_NAME].data.query.return_value = [ | ||
{ | ||
"_key": "some_key", | ||
"value": "some_value", | ||
}, | ||
] | ||
|
||
assert get_collection_value(app=app, key="some_key") == "some_value" | ||
|
||
|
||
def test_save_collection_value_expect_insert() -> None: | ||
key = "some_key" | ||
value = "some_value" | ||
app = MagicMock() | ||
save_collection_value(app=app, key=key, value=value) | ||
app.service.kvstore[KV_COLLECTION_NAME].data.insert.assert_called_once_with( | ||
json.dumps({"_key": key, "value": value}) | ||
) | ||
|
||
def test_save_collection_value_expect_update() -> None: | ||
key = "some_key" | ||
value = "update_value" | ||
app = MagicMock() | ||
app.service.kvstore.__contains__.side_effect = lambda x: x == KV_COLLECTION_NAME | ||
app.service.kvstore[KV_COLLECTION_NAME].data.query.return_value = [ | ||
{ | ||
"_key": key, | ||
"value": "old_value", | ||
}, | ||
] | ||
save_collection_value(app=app, key=key, value=value) | ||
app.service.kvstore[KV_COLLECTION_NAME].data.update.assert_called_once_with( | ||
id=key, | ||
data=json.dumps({"value": value}), | ||
) | ||
|
||
def test_fetch_and_print_feed_results_expect_exception() -> None: | ||
logger = MagicMock() | ||
app = MagicMock() | ||
with patch.object(app.service, "confs", {"flare": {"endpoints": {"me_feed_endpoint": "https://api.flare.io/firework/api2/me/feed"}}}): | ||
fetch_and_print_feed_results(logger=logger, app=app, api_key="some_key", tenant_id=11111) | ||
|
||
logger.error.assert_called_once_with("Exception=Failed to fetch API Token") |