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

Changes the get_institutions query to check for domain equality instead of a 'like' query #59

Merged
Show file tree
Hide file tree
Changes from 3 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
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)
lchen-2101 marked this conversation as resolved.
Show resolved Hide resolved
.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
40 changes: 30 additions & 10 deletions tests/entities/repos/test_institutions_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,47 @@ async def setup(
self,
transaction_session: AsyncSession,
):
fi_dao_123, fi_dao_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")],
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):
# 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")
assert len(res) == 0
Expand All @@ -63,7 +83,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
Loading