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 #1940] Fix factories to initialize sequence based on DB values #1941

Merged
merged 4 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
11 changes: 0 additions & 11 deletions api/tests/lib/seed_local_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import src.logging
import tests.src.db.models.factories as factories
from src.adapters.db import PostgresDBClient
from src.db.models.opportunity_models import Opportunity
from src.db.models.transfer.topportunity_models import TransferTopportunity
from src.util.local import error_if_not_local

Expand All @@ -17,16 +16,6 @@
def _build_opportunities(db_session: db.Session, iterations: int) -> None:
# Just create a variety of opportunities for local testing
# we can eventually look into creating more specific scenarios

# Since the factory always starts counting at the same value for the opportunity ID, we
# need to configure that so it doesn't clash with values already in the DB
max_opportunity_id = db_session.query(func.max(Opportunity.opportunity_id)).scalar()
if max_opportunity_id is None:
max_opportunity_id = 0

logger.info(f"Creating opportunities starting with opportunity_id {max_opportunity_id + 1}")
factories.OpportunityFactory.reset_sequence(value=max_opportunity_id + 1)

for i in range(iterations):
logger.info(f"Creating opportunity batch number {i}")
# Create a few opportunities in various scenarios
Expand Down
32 changes: 19 additions & 13 deletions api/tests/src/db/models/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ def sometimes_none(factory_value, none_chance: float = 0.5):
yes_declaration=factory_value,
no_declaration=None,
)
if random.random() > none_chance:
return factory_value

return None


class CustomProvider(BaseProvider):
Expand Down Expand Up @@ -234,17 +230,14 @@ class Meta:

@classmethod
def _setup_next_sequence(cls):
try:
value = (
get_db_session()
.query(func.max(opportunity_models.Opportunity.opportunity_id))
.scalar()
)
if _db_session is not None:
value = _db_session.query(
func.max(opportunity_models.Opportunity.opportunity_id)
).scalar()
if value is not None:
return value + 1
return 1
except Exception:
return 1

return 1

opportunity_id = factory.Sequence(lambda n: n)

Expand Down Expand Up @@ -568,6 +561,19 @@ class OpportunityAssistanceListingFactory(BaseFactory):
class Meta:
model = opportunity_models.OpportunityAssistanceListing

@classmethod
def _setup_next_sequence(cls):
if _db_session is not None:
value = _db_session.query(
func.max(
opportunity_models.OpportunityAssistanceListing.opportunity_assistance_listing_id
)
).scalar()
if value is not None:
return value + 1

return 1

opportunity_assistance_listing_id = factory.Sequence(lambda n: n)

opportunity = factory.SubFactory(OpportunityFactory)
Expand Down
Loading