Skip to content

Commit

Permalink
Changes the get_institutions query to check for domain equality inste…
Browse files Browse the repository at this point in the history
…ad of a 'like' query

Updates the pytest in test_institutions_repo.test_get_institutions_by_domain to check for
domain equality and to verify generic text strings won't find domains

Domain query will use the exact user domain instead of a generic text string like 'bank'.
This forces that concept.
  • Loading branch information
jcadam14 committed Dec 1, 2023
1 parent 62b1ecd commit b1ebef5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
7 changes: 2 additions & 5 deletions src/entities/repos/institutions_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,15 @@ async def get_institutions(
async with session.begin():
stmt = (
select(FinancialInstitutionDao)
.join(FinancialInstitutionDomainDao)
.options(joinedload(FinancialInstitutionDao.domains))
.limit(count)
.offset(page * count)
)
if leis is not None:
stmt = stmt.filter(FinancialInstitutionDao.lei.in_(leis))
elif d := domain.strip():
search = "%{}%".format(d)
stmt = stmt.join(
FinancialInstitutionDomainDao,
FinancialInstitutionDao.lei == FinancialInstitutionDomainDao.lei,
).filter(FinancialInstitutionDomainDao.domain.like(search))
stmt = stmt.filter(FinancialInstitutionDomainDao.domain == d)
res = await session.scalars(stmt)
return res.unique().all()

Expand Down
24 changes: 20 additions & 4 deletions tests/entities/repos/test_institutions_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,42 @@ async def setup(
self,
transaction_session: AsyncSession,
):
fi_dao_123, fi_dao_456 = FinancialInstitutionDao(
fi_dao_123, fi_dao_456, fi_dao_sub_456 = FinancialInstitutionDao(
name="Test Bank 123",
lei="TESTBANK123",
domains=[FinancialInstitutionDomainDao(domain="test.bank.1", lei="TESTBANK123")],
), FinancialInstitutionDao(
name="Test Bank 456",
lei="TESTBANK456",
domains=[FinancialInstitutionDomainDao(domain="test.bank.2", lei="TESTBANK456")],
), FinancialInstitutionDao(
name="Test Sub Bank 456",
lei="TESTSUBBANK456",
domains=[FinancialInstitutionDomainDao(domain="sub.test.bank.2", lei="TESTSUBBANK456")],
)
transaction_session.add(fi_dao_123)
transaction_session.add(fi_dao_456)
transaction_session.add(fi_dao_sub_456)
await transaction_session.commit()

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

async def test_get_institutions_by_domain(self, query_session: AsyncSession):
async def test_get_institutions_by_domain(self, query_session: AsyncSession):
#verify 'generic' domain queries don't work
res = await repo.get_institutions(query_session, domain="bank")
assert len(res) == 0

res = await repo.get_institutions(query_session, domain="test.bank.1")
assert len(res) == 1

#shouldn't find sub.test.bank.2
res = await repo.get_institutions(query_session, domain="test.bank.2")
assert len(res) == 1

res = await repo.get_institutions(query_session, domain="sub.test.bank.2")
assert len(res) == 1

async def test_get_institutions_by_domain_not_existing(self, query_session: AsyncSession):
res = await repo.get_institutions(query_session, domain="testing.bank")
Expand Down Expand Up @@ -63,7 +79,7 @@ async def test_update_institution(self, transaction_session: AsyncSession):
FinancialInstitutionDao(name="Test Bank 234", lei="TESTBANK123"),
)
res = await repo.get_institutions(transaction_session)
assert len(res) == 2
assert len(res) == 3
assert res[0].name == "Test Bank 234"

async def test_add_domains(self, transaction_session: AsyncSession, query_session: AsyncSession):
Expand Down

0 comments on commit b1ebef5

Please sign in to comment.