Add tests for portfolio funding route
This adds a helper to grab a template's context. Using a helper from the Flask documentation: http://flask.pocoo.org/docs/1.0/signals/?highlight=template_rendered#subscribing-to-signals
This commit is contained in:
62
tests/routes/portfolios/test_task_orders.py
Normal file
62
tests/routes/portfolios/test_task_orders.py
Normal file
@@ -0,0 +1,62 @@
|
||||
from flask import url_for
|
||||
import pytest
|
||||
|
||||
from tests.factories import (
|
||||
PortfolioFactory,
|
||||
TaskOrderFactory,
|
||||
random_future_date,
|
||||
random_past_date,
|
||||
)
|
||||
from tests.utils import captured_templates
|
||||
|
||||
|
||||
class TestPortfolioFunding:
|
||||
def test_unfunded_portfolio(self, app, user_session):
|
||||
portfolio = PortfolioFactory.create()
|
||||
user_session(portfolio.owner)
|
||||
|
||||
with captured_templates(app) as templates:
|
||||
response = app.test_client().get(
|
||||
url_for("portfolios.portfolio_funding", portfolio_id=portfolio.id)
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
_, context = templates[0]
|
||||
assert context["funding_end_date"] is None
|
||||
assert context["total_balance"] == 0
|
||||
assert context["pending_task_orders"] == []
|
||||
assert context["active_task_orders"] == []
|
||||
assert context["expired_task_orders"] == []
|
||||
|
||||
def test_funded_portfolio(self, app, user_session):
|
||||
portfolio = PortfolioFactory.create()
|
||||
user_session(portfolio.owner)
|
||||
|
||||
pending_to = TaskOrderFactory.create(portfolio=portfolio)
|
||||
active_to1 = TaskOrderFactory.create(
|
||||
portfolio=portfolio,
|
||||
start_date=random_past_date(),
|
||||
end_date=random_future_date(),
|
||||
number="42",
|
||||
)
|
||||
active_to2 = TaskOrderFactory.create(
|
||||
portfolio=portfolio,
|
||||
start_date=random_past_date(),
|
||||
end_date=random_future_date(),
|
||||
number="43",
|
||||
)
|
||||
end_date = (
|
||||
active_to1.end_date
|
||||
if active_to1.end_date > active_to2.end_date
|
||||
else active_to2.end_date
|
||||
)
|
||||
|
||||
with captured_templates(app) as templates:
|
||||
response = app.test_client().get(
|
||||
url_for("portfolios.portfolio_funding", portfolio_id=portfolio.id)
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
_, context = templates[0]
|
||||
assert context["funding_end_date"] is end_date
|
||||
assert context["total_balance"] == active_to1.budget + active_to2.budget
|
16
tests/utils.py
Normal file
16
tests/utils.py
Normal file
@@ -0,0 +1,16 @@
|
||||
from flask import template_rendered
|
||||
from contextlib import contextmanager
|
||||
|
||||
|
||||
@contextmanager
|
||||
def captured_templates(app):
|
||||
recorded = []
|
||||
|
||||
def record(sender, template, context, **extra):
|
||||
recorded.append((template, context))
|
||||
|
||||
template_rendered.connect(record, app)
|
||||
try:
|
||||
yield recorded
|
||||
finally:
|
||||
template_rendered.disconnect(record, app)
|
Reference in New Issue
Block a user