Skip to content

Commit

Permalink
All unit test GREEN
Browse files Browse the repository at this point in the history
  • Loading branch information
seeker25 committed Nov 29, 2024
1 parent 63f73cf commit 4f84213
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 15 deletions.
5 changes: 4 additions & 1 deletion pay-api/src/pay_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@

import sentry_sdk # noqa: I001; pylint: disable=ungrouped-imports,wrong-import-order; conflicts with Flake8
from flask import Flask
from flask_executor import Executor
from flask_migrate import Migrate, upgrade
from sbc_common_components.exception_handling.exception_handler import ExceptionHandler
from sbc_common_components.utils.camel_case_response import convert_to_camel
from sentry_sdk.integrations.flask import FlaskIntegration # noqa: I001

# import pay_api.config as config
from pay_api import config
from pay_api.config import _Config
from pay_api.models import db, ma
Expand All @@ -52,6 +52,7 @@ def create_app(run_mode=os.getenv("DEPLOYMENT_ENV", "production")):
if run_mode != "testing":
if app.config.get("CLOUD_PLATFORM") != "OCP":
Migrate(app, db)
app.logger.info(f"Booting up with CPU count (useful for GCP): {os.cpu_count()}")
app.logger.info("Running migration upgrade.")
with app.app_context():
execute_migrations(app)
Expand All @@ -76,6 +77,8 @@ def create_app(run_mode=os.getenv("DEPLOYMENT_ENV", "production")):

ExceptionHandler(app)

app.extensions["flask_executor"] = Executor(app)

@app.after_request
def handle_after_request(response): # pylint: disable=unused-variable
add_version(response)
Expand Down
2 changes: 2 additions & 0 deletions pay-api/src/pay_api/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ class _Config: # pylint: disable=too-few-public-methods
# Used for DEV/TEST/SANDBOX only. If True, will skip payment and return success and send queue message.
ALLOW_SKIP_PAYMENT = os.getenv("ALLOW_SKIP_PAYMENT", "False").lower() == "true"

EXECUTOR_PROPAGATE_EXCEPTIONS = True

TESTING = False
DEBUG = True

Expand Down
8 changes: 4 additions & 4 deletions pay-api/src/pay_api/models/payment.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,9 +315,7 @@ def search_purchase_history( # noqa:E501; pylint:disable=too-many-arguments, to
"""Search for purchase history."""
user: UserContext = kwargs["user"]
search_filter["userProductCode"] = user.product_code
from flask_executor import Executor # TODO fix this

executor = Executor(current_app)
executor = current_app.extensions["flask_executor"]
query = cls.generate_base_transaction_query()
query = cls.filter(query, auth_account_id, search_filter)
if not return_all:
Expand All @@ -336,7 +334,9 @@ def search_purchase_history( # noqa:E501; pylint:disable=too-many-arguments, to
# If maximum number of records is provided, set the page with that number
sub_query = cls.generate_subquery(auth_account_id, search_filter, max_no_records, page=None)
result, count = (
query.filter(Invoice.id.in_(sub_query.subquery().select())).all(),
db.session.query(Invoice)
.from_statement(query.filter(Invoice.id.in_(sub_query.subquery().select())))
.all(),
sub_query.count(),
)
else:
Expand Down
20 changes: 20 additions & 0 deletions pay-api/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,3 +313,23 @@ def send_email(recipients, subject, body):
# Note this needs to be moved to a prism spec, we need to come up with one for NotifyAPI.
monkeypatch.setattr("pay_api.services.email_service.send_email", send_email)
monkeypatch.setattr("pay_api.services.eft_refund.send_email", send_email)


@pytest.fixture()
def executor_mock(app):
"""Mock executor extension."""

class SimpleMockFuture:
def __init__(self, func, *args, **kwargs):
self._func = func
self._args = args
self._kwargs = kwargs

def result(self):
return self._func(*self._args, **self._kwargs)

class SimpleMockExecutor:
def submit(self, func, *args, **kwargs):
return SimpleMockFuture(func, *args, **kwargs)

app.extensions["flask_executor"] = SimpleMockExecutor()
6 changes: 3 additions & 3 deletions pay-api/tests/unit/api/test_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def test_account_purchase_history_with_basic_account(session, client, jwt, app):
assert rv.status_code == 200


def test_account_purchase_history_pagination(session, client, jwt, app):
def test_account_purchase_history_pagination(session, client, jwt, app, executor_mock):
"""Assert that the endpoint returns 200."""
token = jwt.create_jwt(get_claims(), token_header)
headers = {"Authorization": f"Bearer {token}", "content-type": "application/json"}
Expand All @@ -155,7 +155,7 @@ def test_account_purchase_history_pagination(session, client, jwt, app):
assert len(rv.json.get("items")) == 5


def test_account_purchase_history_with_service_account(session, client, jwt, app):
def test_account_purchase_history_with_service_account(session, client, jwt, app, executor_mock):
"""Assert that purchase history returns only invoices for that product."""
# Point CSO fee schedule to a valid distribution code.
fee_schedule_id = FeeSchedule.find_by_filing_type_and_corp_type("CSO", "CSBVFEE").fee_schedule_id
Expand Down Expand Up @@ -357,7 +357,7 @@ def test_account_purchase_history_export_invalid_request(session, client, jwt, a
assert rv.status_code == 400


def test_account_purchase_history_default_list(session, client, jwt, app):
def test_account_purchase_history_default_list(session, client, jwt, app, executor_mock):
"""Assert that the endpoint returns 200."""
token = jwt.create_jwt(get_claims(), token_header)
headers = {"Authorization": f"Bearer {token}", "content-type": "application/json"}
Expand Down
8 changes: 1 addition & 7 deletions pay-api/tests/unit/services/test_payment.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,7 @@ def test_payment_with_no_active_invoice(session):
],
)
def test_search_payment_history(
session,
test_name,
search_filter,
view_all,
expected_total,
expected_key,
expected_value,
session, executor_mock, test_name, search_filter, view_all, expected_total, expected_key, expected_value
):
"""Assert that the search payment history is working."""
payment_account = factory_payment_account(name="account 1", auth_account_id="1")
Expand Down

0 comments on commit 4f84213

Please sign in to comment.