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

[Issue 3266] mod factories opp attach staging #3370

Merged
merged 23 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import os

from src.db.models.foreign.opportunity import Topportunity as FTopportunity
from src.db.models.staging.opportunity import Topportunity as STopportunity
from tests.src.db.models.factories import ForeignTopportunityFactory, StagingTopportunityFactory


def test_uploading_attachment_staging(db_session, enable_factory_create):
opp = StagingTopportunityFactory.create(my_attachment=b"Testing saving to db")
db_session.commit()
db_session.expire_all()

db_opp = (
db_session.query(STopportunity)
.filter(STopportunity.opportunity_id == opp.opportunity_id)
.one_or_none()
)

try:
with open("out_file.txt", "wb") as outfile:
outfile.write(db_opp.my_attachment)

with open("out_file.txt", "rb") as infile:
file_content = infile.read()
assert file_content == db_opp.my_attachment
finally:
# Cleanup: delete the file after verifying
if os.path.exists("out_file.txt"):
os.remove("out_file.txt")
babebe marked this conversation as resolved.
Show resolved Hide resolved


def test_uploading_attachment_foreign(db_session, enable_factory_create):
opp = ForeignTopportunityFactory.create(my_attachment=b"Testing saving to db")
db_session.commit()
db_session.expire_all()

db_opp = (
db_session.query(FTopportunity)
.filter(FTopportunity.opportunity_id == opp.opportunity_id)
.one_or_none()
)

try:
with open("out_file.txt", "wb") as outfile:
outfile.write(db_opp.my_attachment)

with open("out_file.txt", "rb") as infile:
file_content = infile.read()
assert file_content == db_opp.my_attachment
finally:
# Cleanup: delete the file after verifying
if os.path.exists("out_file.txt"):
os.remove("out_file.txt")
82 changes: 82 additions & 0 deletions api/tests/src/db/models/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -1155,6 +1155,14 @@ class Params:
category_explanation=None,
)

# Set non-model fields after creation
@factory.post_generation
def my_attachment(self, create, extracted):
if extracted:
self.my_attachment = extracted
else:
self.my_attachment = b"Test attachment"

babebe marked this conversation as resolved.
Show resolved Hide resolved

class StagingTopportunityCfdaFactory(TopportunityCfdaFactory, AbstractStagingFactory):
class Meta:
Expand Down Expand Up @@ -1363,6 +1371,40 @@ class Meta:
creator_id = factory.Faker("first_name")


class StagingTopportunityAttachmentFactory(TopportunityFactory, AbstractStagingFactory):
babebe marked this conversation as resolved.
Show resolved Hide resolved
class Meta:
model = staging.attachment.TsynopsisAttachment

class Params:
# Trait to set all nullable fields to None
all_fields_null = factory.Trait(
att_revision_number=None,
att_type=None,
file_lob=None,
creator_id=None,
last_upd_id=None,
syn_att_folder_id=None,
)
babebe marked this conversation as resolved.
Show resolved Hide resolved

syn_att_id: factory.Sequence(lambda n: n)
opportunity_id: int = factory.LazyAttribute(lambda s: s.synopsis.opportunity_id)
mime_type = factory.Faker("mime_type")
link_url = factory.Faker("link_url")
file_name = factory.Faker("file_name")
file_desc = factory.Faker("sentence")
file_lob_size = factory.Faker("random_int", min=1000, max=10000000)

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.created_at, end_date="now")
)
last_upd_date = factory.LazyAttribute(
lambda o: fake.date_time_between(start_date=o.created_at, end_date="now")
)
creator_id = factory.Faker("creator_id")
last_upd_id = factory.Faker("last_upd_id")


####################################
# Transfer Table Factories
####################################
Expand Down Expand Up @@ -1427,6 +1469,12 @@ def _setup_next_sequence(cls):
size=lambda: random.randint(1, 3),
)

# Set non-model fields after creation
@factory.post_generation
def my_attachment(self, create, extracted):
if extracted:
self.my_attachment = extracted


class ForeignTopportunityCfdaFactory(TopportunityCfdaFactory):
class Meta:
Expand Down Expand Up @@ -1593,6 +1641,40 @@ class Meta:
revision_number = factory.LazyAttribute(lambda s: s.synopsis.revision_number)


class ForeignTopportunityAttachmentFactory(TopportunityFactory, AbstractStagingFactory):
class Meta:
model = foreign.attachment.tsynopsisattachment

class Params:
# Trait to set all nullable fields to None
all_fields_null = factory.Trait(
att_revision_number=None,
att_type=None,
file_lob=None,
creator_id=None,
last_upd_id=None,
syn_att_folder_id=None,
)

syn_att_id: factory.Sequence(lambda n: n)
opportunity_id: int = factory.LazyAttribute(lambda s: s.synopsis.opportunity_id)
mime_type = factory.Faker("mime_type")
link_url = factory.Faker("link_url")
file_name = factory.Faker("file_name")
file_desc = factory.Faker("sentence")
file_lob_size = factory.Faker("random_int", min=1000, max=10000000)

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.created_at, end_date="now")
)
last_upd_date = factory.LazyAttribute(
lambda o: fake.date_time_between(start_date=o.created_at, end_date="now")
)
creator_id = factory.Faker("creator_id")
last_upd_id = factory.Faker("last_upd_id")


##
# Pseudo-factories
##
Expand Down