Skip to content

Commit

Permalink
feat: add is_active to institution model (#73)
Browse files Browse the repository at this point in the history
closes #70
  • Loading branch information
lchen-2101 authored Dec 28, 2023
1 parent 05b9237 commit 219a8ae
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ def downgrade() -> None:
op.drop_constraint(
constraint_name="fk_federal_regulator_financial_institutions", table_name="financial_institutions"
)
op.drop_constraint(constraint_name="fk_address_state_financial_institutions", table_name="financial_institutions")
op.drop_constraint(
constraint_name="fk_address_state_code_financial_institutions", table_name="financial_institutions"
)
op.drop_constraint(
constraint_name="fk_hmda_institution_type_financial_institutions", table_name="financial_institutions"
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""231227 add active field to institutions table
Revision ID: 383ab402c8c2
Revises: a41281b1e109
Create Date: 2023-12-27 14:21:33.567414
"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = "383ab402c8c2"
down_revision: Union[str, None] = "a41281b1e109"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
with op.batch_alter_table("financial_institutions") as batch_op:
batch_op.add_column(sa.Column(name="is_active", type_=sa.Boolean(), nullable=False, server_default=sa.true()))
batch_op.create_index(
index_name=batch_op.f("ix_financial_institutions_is_active"), columns=["is_active"], unique=False
)


def downgrade() -> None:
op.drop_index(index_name="ix_financial_institutions_is_active", table_name="financial_institutions")
op.drop_column(table_name="financial_institutions", column_name="is_active")
1 change: 1 addition & 0 deletions src/entities/models/dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class FinancialInstitutionDao(AuditMixin, Base):
__tablename__ = "financial_institutions"
lei: Mapped[str] = mapped_column(unique=True, index=True, primary_key=True)
name: Mapped[str] = mapped_column(index=True)
is_active: Mapped[bool] = mapped_column(index=True)
domains: Mapped[List["FinancialInstitutionDomainDao"]] = relationship(
"FinancialInstitutionDomainDao", back_populates="fi"
)
Expand Down
1 change: 1 addition & 0 deletions src/entities/models/dto.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Config:
class FinancialInstitutionBase(BaseModel):
lei: str
name: str
is_active: bool


class FinancialInstitutionDto(FinancialInstitutionBase):
Expand Down
42 changes: 5 additions & 37 deletions src/entities/repos/institutions_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,46 +50,14 @@ async def upsert_institution(session: AsyncSession, fi: FinancialInstitutionDto)
stmt = select(FinancialInstitutionDao).filter(FinancialInstitutionDao.lei == fi.lei)
res = await session.execute(stmt)
db_fi = res.scalar_one_or_none()
fi_data = fi.__dict__.copy()
fi_data.pop("_sa_instance_state", None)
if db_fi is None:
db_fi = FinancialInstitutionDao(
lei=fi.lei,
name=fi.name,
tax_id=fi.tax_id,
rssd_id=fi.rssd_id,
primary_federal_regulator_id=fi.primary_federal_regulator_id,
hmda_institution_type_id=fi.hmda_institution_type_id,
sbl_institution_type_id=fi.sbl_institution_type_id,
hq_address_street_1=fi.hq_address_street_1,
hq_address_street_2=fi.hq_address_street_2,
hq_address_city=fi.hq_address_city,
hq_address_state_code=fi.hq_address_state_code,
hq_address_zip=fi.hq_address_zip,
parent_lei=fi.parent_lei,
parent_legal_name=fi.parent_legal_name,
parent_rssd_id=fi.parent_rssd_id,
top_holder_lei=fi.top_holder_lei,
top_holder_legal_name=fi.top_holder_legal_name,
top_holder_rssd_id=fi.top_holder_rssd_id,
)
db_fi = FinancialInstitutionDao(**fi_data)
session.add(db_fi)
else:
db_fi.name = fi.name
db_fi.tax_id = fi.tax_id
db_fi.rssd_id = fi.rssd_id
db_fi.primary_federal_regulator_id = fi.primary_federal_regulator_id
db_fi.hmda_institution_type_id = fi.hmda_institution_type_id
db_fi.sbl_institution_type_id = fi.sbl_institution_type_id
db_fi.hq_address_street_1 = fi.hq_address_street_1
db_fi.hq_address_street_2 = fi.hq_address_street_2
db_fi.hq_address_city = fi.hq_address_city
db_fi.hq_address_state_code = fi.hq_address_state_code
db_fi.hq_address_zip = fi.hq_address_zip
db_fi.parent_lei = fi.parent_lei
db_fi.parent_legal_name = fi.parent_legal_name
db_fi.parent_rssd_id = fi.parent_rssd_id
db_fi.top_holder_lei = fi.top_holder_lei
db_fi.top_holder_legal_name = fi.top_holder_legal_name
db_fi.top_holder_rssd_id = fi.top_holder_rssd_id
for key, value in fi_data.items():
setattr(db_fi, key, value)
await session.commit()
return db_fi

Expand Down
1 change: 1 addition & 0 deletions tests/api/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def get_institutions_mock(mocker: MockerFixture) -> Mock:
FinancialInstitutionDao(
name="Test Bank 123",
lei="TESTBANK123",
is_active=True,
domains=[FinancialInstitutionDomainDao(domain="test.bank", lei="TESTBANK123")],
tax_id="123456789",
rssd_id=1234,
Expand Down
8 changes: 8 additions & 0 deletions tests/api/routers/test_institutions_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def test_create_institution_authed(self, mocker: MockerFixture, app_fixture: Fas
upsert_institution_mock.return_value = FinancialInstitutionDao(
name="testName",
lei="testLei",
is_active=True,
domains=[FinancialInstitutionDomainDao(domain="test.bank", lei="TESTBANK123")],
tax_id="123456789",
rssd_id=1234,
Expand Down Expand Up @@ -69,6 +70,7 @@ def test_create_institution_authed(self, mocker: MockerFixture, app_fixture: Fas
json={
"name": "testName",
"lei": "testLei",
"is_active": True,
"tax_id": "123456789",
"rssd_id": 12344,
"primary_federal_regulator_id": "FRI2",
Expand Down Expand Up @@ -97,6 +99,7 @@ def test_create_institution_only_required_fields(
upsert_institution_mock.return_value = FinancialInstitutionDao(
name="testName",
lei="testLei",
is_active=True,
hq_address_street_1="Test Address Street 1",
hq_address_city="Test City 1",
hq_address_state_code="VA",
Expand All @@ -111,6 +114,7 @@ def test_create_institution_only_required_fields(
json={
"name": "testName",
"lei": "testLei",
"is_active": True,
"hq_address_street_1": "Test Address Street 1",
"hq_address_city": "Test City 1",
"hq_address_state_code": "VA",
Expand Down Expand Up @@ -154,6 +158,7 @@ def test_create_institution_authed_no_permission(self, app_fixture: FastAPI, aut
json={
"name": "testName",
"lei": "testLei",
"is_active": True,
"tax_id": "123456789",
"rssd_id": 12344,
"primary_federal_regulator_id": "FIR2",
Expand Down Expand Up @@ -185,6 +190,7 @@ def test_get_institution_authed(self, mocker: MockerFixture, app_fixture: FastAP
get_institution_mock.return_value = FinancialInstitutionDao(
name="Test Bank 123",
lei="TESTBANK123",
is_active=True,
domains=[FinancialInstitutionDomainDao(domain="test.bank", lei="TESTBANK123")],
tax_id="123456789",
rssd_id=1234,
Expand Down Expand Up @@ -282,6 +288,7 @@ def test_get_associated_institutions(
FinancialInstitutionDao(
name="Test Bank 123",
lei="TESTBANK123",
is_active=True,
domains=[FinancialInstitutionDomainDao(domain="test123.bank", lei="TESTBANK123")],
tax_id="123456789",
rssd_id=1234,
Expand All @@ -307,6 +314,7 @@ def test_get_associated_institutions(
FinancialInstitutionDao(
name="Test Bank 234",
lei="TESTBANK234",
is_active=True,
domains=[FinancialInstitutionDomainDao(domain="test234.bank", lei="TESTBANK234")],
tax_id="123456879",
rssd_id=6879,
Expand Down
6 changes: 6 additions & 0 deletions tests/entities/repos/test_institutions_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ async def setup(
FinancialInstitutionDao(
name="Test Bank 123",
lei="TESTBANK123",
is_active=True,
domains=[FinancialInstitutionDomainDao(domain="test.bank.1", lei="TESTBANK123")],
tax_id="123456789",
rssd_id=1234,
Expand All @@ -67,6 +68,7 @@ async def setup(
FinancialInstitutionDao(
name="Test Bank 456",
lei="TESTBANK456",
is_active=True,
domains=[FinancialInstitutionDomainDao(domain="test.bank.2", lei="TESTBANK456")],
tax_id="987654321",
rssd_id=4321,
Expand All @@ -88,6 +90,7 @@ async def setup(
FinancialInstitutionDao(
name="Test Sub Bank 456",
lei="TESTSUBBANK456",
is_active=True,
domains=[FinancialInstitutionDomainDao(domain="sub.test.bank.2", lei="TESTSUBBANK456")],
tax_id="765432198",
rssd_id=2134,
Expand Down Expand Up @@ -166,6 +169,7 @@ async def test_add_institution(self, transaction_session: AsyncSession):
FinancialInstitutionDao(
name="New Bank 123",
lei="NEWBANK123",
is_active=True,
tax_id="654321987",
rssd_id=6543,
primary_federal_regulator_id="FRI3",
Expand Down Expand Up @@ -195,6 +199,7 @@ async def test_add_institution_only_required_fields(
FinancialInstitutionDao(
name="Minimal Bank 123",
lei="MINBANK123",
is_active=True,
hq_address_street_1="Minimal Address Street 1",
hq_address_city="Minimal City 1",
hq_address_state_code="FL",
Expand Down Expand Up @@ -226,6 +231,7 @@ async def test_update_institution(self, transaction_session: AsyncSession):
FinancialInstitutionDao(
name="Test Bank 234",
lei="TESTBANK123",
is_active=True,
hq_address_street_1="Test Address Street 1",
hq_address_city="Test City 1",
hq_address_state_code="GA",
Expand Down

0 comments on commit 219a8ae

Please sign in to comment.