Skip to content

Commit

Permalink
[Issue 3266] mod factories opp attach staging (#3370)
Browse files Browse the repository at this point in the history
## Summary
Fixes #{[3266](#3266)}

### Time to review: __10 mins__

## Changes proposed
> Added class method to staging and foreign Opportunity Factory
> Added test

---------

Co-authored-by: nava-platform-bot <[email protected]>
  • Loading branch information
babebe and nava-platform-bot authored Jan 7, 2025
1 parent 263f8b3 commit 89581c1
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 1 deletion.
13 changes: 12 additions & 1 deletion api/src/db/models/foreign/attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,21 @@
# match by oracle_fdw, but we are matching them for maintainability.
#

from sqlalchemy.orm import Mapped, foreign, relationship

from src.db.models.legacy_mixin import synopsis_mixin

from . import foreignbase
from .opportunity import Topportunity


class tsynopsisattachment(foreignbase.ForeignBase, synopsis_mixin.TsynopsisAttachmentMixin):
class TsynopsisAttachment(foreignbase.ForeignBase, synopsis_mixin.TsynopsisAttachmentMixin):
__tablename__ = "tsynopsisattachment"

opportunity: Mapped[Topportunity | None] = relationship(
Topportunity,
primaryjoin=lambda: TsynopsisAttachment.opportunity_id
== foreign(Topportunity.opportunity_id),
uselist=False,
overlaps="opportunity",
)
12 changes: 12 additions & 0 deletions api/src/db/models/staging/attachment.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
from sqlalchemy.orm import Mapped, foreign, relationship

from src.db.models.legacy_mixin import synopsis_mixin
from src.db.models.staging.staging_base import StagingBase, StagingParamMixin

from .opportunity import Topportunity


class TsynopsisAttachment(StagingBase, synopsis_mixin.TsynopsisAttachmentMixin, StagingParamMixin):
__tablename__ = "tsynopsisattachment"

opportunity: Mapped[Topportunity | None] = relationship(
Topportunity,
primaryjoin=lambda: TsynopsisAttachment.opportunity_id
== foreign(Topportunity.opportunity_id),
uselist=False,
overlaps="opportunity",
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from src.db.models.foreign.attachment import TsynopsisAttachment as TsynopsisAttachmentF
from src.db.models.staging.attachment import TsynopsisAttachment as TsynopsisAttachmentS
from tests.src.db.models.factories import (
ForeignTsynopsisAttachmentFactory,
StagingTsynopsisAttachmentFactory,
)


def test_uploading_attachment_staging(db_session, enable_factory_create, tmp_path):
att = StagingTsynopsisAttachmentFactory.create(file_lob=b"Testing attachment")
db_session.commit()
db_session.expire_all()

db_att = (
db_session.query(TsynopsisAttachmentS)
.filter(TsynopsisAttachmentS.opportunity_id == att.opportunity_id)
.one_or_none()
)
temp_file = tmp_path / "out_file.txt"
temp_file.write_bytes(db_att.file_lob)
file_content = temp_file.read_bytes()

assert file_content == db_att.file_lob


def test_uploading_attachment_foreign(db_session, enable_factory_create, tmp_path):
att = ForeignTsynopsisAttachmentFactory.create(file_lob=b"Testing attachment")
db_session.commit()
db_session.expire_all()

db_att = (
db_session.query(TsynopsisAttachmentF)
.filter(TsynopsisAttachmentF.opportunity_id == att.opportunity_id)
.one_or_none()
)

temp_file = tmp_path / "out_file.txt"
temp_file.write_bytes(db_att.file_lob)
file_content = temp_file.read_bytes()

assert file_content == db_att.file_lob
42 changes: 42 additions & 0 deletions api/tests/src/db/models/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -997,6 +997,32 @@ class Meta:
publisher_profile_id = sometimes_none(factory.Faker("random_int", min=1, max=99_999))


class TsynopsisAttachmentFactory(BaseFactory):
class Meta:
abstract = True

syn_att_id: factory.Sequence(lambda n: n)
opportunity_id: factory.Sequence(lambda n: n)
att_revision_number = factory.Faker("random_int", min=1000, max=10000000)
att_type: factory.Faker("att_type")
mime_type = factory.Faker("mime_type")
link_url = factory.Faker("relevant_url")
file_name = factory.Faker("file_name")
file_desc = factory.Faker("sentence")
file_lob = b"Test attachment"
file_lob_size = factory.LazyAttribute(lambda x: len(x.file_lob))
create_date = factory.Faker("date_time_between", start_date="-1y", end_date="now")
created_date = factory.LazyAttribute(
lambda o: fake.date_time_between(start_date=o.create_date, end_date="now")
)
last_upd_date = factory.LazyAttribute(
lambda o: fake.date_time_between(start_date=o.created_date, end_date="now")
)
creator_id = factory.Faker("first_name")
last_upd_id = factory.Faker("first_name")
syn_att_folder_id = factory.Faker("random_int", min=1000, max=10000000)


class TforecastFactory(BaseFactory):
class Meta:
abstract = True
Expand Down Expand Up @@ -1363,6 +1389,14 @@ class Meta:
creator_id = factory.Faker("first_name")


class StagingTsynopsisAttachmentFactory(TsynopsisAttachmentFactory, AbstractStagingFactory):
class Meta:
model = staging.attachment.TsynopsisAttachment

opportunity = factory.SubFactory(StagingTopportunityFactory)
opportunity_id = factory.LazyAttribute(lambda o: o.opportunity.opportunity_id)


####################################
# Transfer Table Factories
####################################
Expand Down Expand Up @@ -1593,6 +1627,14 @@ class Meta:
revision_number = factory.LazyAttribute(lambda s: s.synopsis.revision_number)


class ForeignTsynopsisAttachmentFactory(TsynopsisAttachmentFactory):
class Meta:
model = foreign.attachment.TsynopsisAttachment

opportunity = factory.SubFactory(ForeignTopportunityFactory)
opportunity_id = factory.LazyAttribute(lambda o: o.opportunity.opportunity_id)


##
# Pseudo-factories
##
Expand Down
Binary file modified documentation/api/database/erds/staging-schema.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 89581c1

Please sign in to comment.