Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added endpoint to return lei data from list of leis #20

Merged
merged 15 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
from typing import Annotated
from fastapi import Depends, HTTPException, Request
from sqlalchemy.ext.asyncio import AsyncSession
from typing import List, Optional
from itertools import chain

from fastapi import Query

from entities.engine import get_session
from entities.repos import institutions_repo as repo
Expand Down Expand Up @@ -35,3 +39,17 @@ def request_needs_domain_check(request: Request) -> bool:

async def email_domain_denied(session: AsyncSession, email: str) -> bool:
return not await repo.is_email_domain_allowed(session, email)


def parse_leis(leis: List[str] = Query(None)) -> Optional[List]:
"""
Parses leis from list of one or multiple strings to a list of multiple distinct lei strings
Returns empty list when nothing is passed in
Ex1: ['lei1,lei2'] -> ['lei1', 'lei2']
Ex2: ['lei1,lei2', 'lei3,lei4'] -> ['lei1','lei2','lei3','lei4']
"""

if leis:
return list(chain.from_iterable([x.split(',') for x in leis]))
else:
return None
6 changes: 4 additions & 2 deletions src/entities/repos/institutions_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@


async def get_institutions(
session: AsyncSession, domain: str = "", page: int = 0, count: int = 100
session: AsyncSession, leis: List[str] = None, domain: str = "", page: int = 0, count: int = 100,
) -> List[FinancialInstitutionDao]:
async with session.begin():
stmt = (
Expand All @@ -23,7 +23,9 @@ async def get_institutions(
.limit(count)
.offset(page * count)
)
if d := domain.strip():
if leis:
stmt = stmt.filter(FinancialInstitutionDao.lei.in_(leis))
elif d := domain.strip():
search = "%{}%".format(d)
stmt = stmt.join(
FinancialInstitutionDomainDao,
Expand Down
6 changes: 4 additions & 2 deletions src/routers/institutions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from fastapi import Depends, Request, HTTPException
from fastapi import Depends, Request, HTTPException, Query
from http import HTTPStatus
from oauth2 import oauth2_admin
from util import Router
from dependencies import parse_leis
from typing import Annotated, List, Tuple
from entities.engine import get_session
from entities.repos import institutions_repo as repo
Expand All @@ -28,11 +29,12 @@ async def set_db(
@requires("authenticated")
async def get_institutions(
request: Request,
leis: List[str] = Depends(parse_leis),
domain: str = "",
page: int = 0,
count: int = 100,
):
return await repo.get_institutions(request.state.db_session, domain, page, count)
return await repo.get_institutions(request.state.db_session, leis, domain, page, count)


@router.post("/", response_model=Tuple[str, FinancialInstitutionDto])
Expand Down
29 changes: 22 additions & 7 deletions tests/entities/repos/test_institutions_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,41 +16,56 @@ async def setup(
self,
session: AsyncSession,
):
fi_dao = FinancialInstitutionDao(
fi_dao_123, fi_dao_456 = FinancialInstitutionDao(
name="Test Bank 123",
lei="TESTBANK123",
domains=[
FinancialInstitutionDomainDao(domain="test.bank", lei="TESTBANK123")
FinancialInstitutionDomainDao(domain="test.bank.1", lei="TESTBANK123")
],
), FinancialInstitutionDao(
name="Test Bank 456",
lei="TESTBANK456",
domains=[
FinancialInstitutionDomainDao(domain="test.bank.2", lei="TESTBANK456")
],
)
session.add(fi_dao)
session.add(fi_dao_123)
session.add(fi_dao_456)
await session.commit()

async def test_get_institutions(self, session: AsyncSession):
res = await repo.get_institutions(session)
assert len(res) == 1
assert len(res) == 2

async def test_get_institutions_by_domain(self, session: AsyncSession):
res = await repo.get_institutions(session, domain="test.bank")
res = await repo.get_institutions(session, domain="test.bank.1")
assert len(res) == 1

async def test_get_institutions_by_domain_not_existing(self, session: AsyncSession):
res = await repo.get_institutions(session, domain="testing.bank")
assert len(res) == 0

async def test_get_institutions_by_lei_list(self, session: AsyncSession):
res = await repo.get_institutions(session, leis=['TESTBANK123', 'TESTBANK456'])
assert len(res) == 2

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also test scenarios with multiple LEIs. Probably want scenarios where all requested values are present, and where only some are present. Looks like that'll require adding at least one more FI instance to the above fixture.

async def test_get_institutions_by_lei_list_item_not_existing(self, session: AsyncSession):
res = await repo.get_institutions(session, leis=["NOTTESTBANK"])
assert len(res) == 0

async def test_add_institution(self, session: AsyncSession):
await repo.upsert_institution(
session, FinancialInstitutionDao(name="New Bank 123", lei="NEWBANK123")
)
res = await repo.get_institutions(session)
assert len(res) == 2
assert len(res) == 3

async def test_update_institution(self, session: AsyncSession):
await repo.upsert_institution(
session, FinancialInstitutionDao(name="Test Bank 234", lei="TESTBANK123")
)
res = await repo.get_institutions(session)
assert len(res) == 1
assert len(res) == 2
assert res[0].name == "Test Bank 234"

async def test_add_domains(self, session: AsyncSession):
Expand Down