diff --git a/etl/hubspot/hubspotDataTodB.py b/etl/hubspot/hubspotDataTodB.py index 06cc3be9..4f43f1f7 100644 --- a/etl/hubspot/hubspotDataTodB.py +++ b/etl/hubspot/hubspotDataTodB.py @@ -2,7 +2,7 @@ from backend.app.db.connection import db_read_session from backend.app.db.models.organisation import Organisation, HubspotDealData from sqlmodel import select from datetime import datetime, timezone -from typing import Dict, Optional +from typing import Dict, Optional, Tuple from etl.hubspot.company_data import CompanyData from etl.hubspot.hubspotClient import HubspotClient from etl.hubspot.s3_uploader import S3Uploader @@ -103,7 +103,7 @@ class HubspotDataToDb: Also handles major_condition_issue_photos file upload to S3 with integrity check. """ - def soft_assert(condition, message="Assertion Failed"): + def soft_assert(condition: bool, message: str = "Assertion Failed"): if not condition: print(f"⚠️ Soft Assert Failed: {message}") return False @@ -111,6 +111,10 @@ class HubspotDataToDb: print(f"🔍 Checking if deal needs updating (deal_id={deal_in_db.deal_id})") + hs_deal: Dict[str, str] + hs_company_id: Optional[str] + hs_listing: Optional[Dict[str, str]] + hs_deal, hs_company_id, hs_listing = ( hubspot_client.get_deal_and_company_and_listing(deal_in_db.deal_id) ) diff --git a/etl/hubspot/tests/test_hubspot_deal_differ.py b/etl/hubspot/tests/test_hubspot_deal_differ.py index 12c5a288..876fcab9 100644 --- a/etl/hubspot/tests/test_hubspot_deal_differ.py +++ b/etl/hubspot/tests/test_hubspot_deal_differ.py @@ -31,9 +31,9 @@ def make_new_deal(deal_id: uuid.UUID, **overrides: Any) -> Dict[str, str]: } -# ------------------------------------- -# Random change we aren't interested in -# ------------------------------------- +# ==================== +# PASHUB TRIGGER TESTS +# ==================== @pytest.mark.parametrize( @@ -59,11 +59,6 @@ def test_pashub_trigger__outcome_note_added__returns_false( ) -# ------------------------- -# Pashub link changes -# ------------------------- - - @pytest.mark.parametrize( "old_overrides,new_overrides,expected", [ @@ -92,11 +87,6 @@ def test_pashub_trigger__pashub_link_changed__returns_true( ) -# ------------------------- -# Coordination -# ------------------------- - - @pytest.mark.parametrize( "coordination_status,expected", [ @@ -155,11 +145,6 @@ def test_pashub_trigger__coordination_completed_and_pashub_link_not_set__returns ) -# ------------------------- -# Design -# ------------------------- - - def test_pashub_trigger__design_completed_and_pashub_link_set__returns_true() -> None: deal_id = uuid.uuid4() @@ -204,11 +189,6 @@ def test_pashub_trigger__design_completed_and_pashub_link_not_set__returns_false ) -# ------------------------- -# Lodgement -# ------------------------- - - @pytest.mark.parametrize( "lodgement_status,expected", [ @@ -263,11 +243,6 @@ def test_pashub_trigger__lodgement_completed_and_pashub_link_not_set__returns_fa ) -# ------------------------- -# Negative case -# ------------------------- - - def test_pashub_trigger__coordination_design_lodgement_not_completed_and_pashub_link_set__returns_false() -> ( None ): @@ -293,3 +268,110 @@ def test_pashub_trigger__coordination_design_lodgement_not_completed_and_pashub_ ) is False ) + + +# ======================= +# DB UPDATE TRIGGER TESTS +# ======================= + + +def test_db_update_trigger__no_changes__returns_false() -> None: + deal_id = uuid.uuid4() + + old_deal = make_old_deal( + id=deal_id, + dealname="Test Deal", + dealstage="stage_1", + outcome="won", + ) + + new_deal = make_new_deal( + deal_id, + hs_object_id="1", + dealname="Test Deal", + dealstage="stage_1", + outcome="won", + ) + + result = HubspotDealDiffer.check_for_db_update_trigger( + new_deal=new_deal, + new_company=None, + new_listing=None, + old_deal=old_deal, + ) + + assert result is False + + +def test_db_update_trigger__dealname_changed__returns_true() -> None: + deal_id = uuid.uuid4() + + old_deal = make_old_deal( + id=deal_id, + dealname="Old Name", + ) + + new_deal = make_new_deal( + deal_id, + hs_object_id="1", + dealname="New Name", + ) + + result = HubspotDealDiffer.check_for_db_update_trigger( + new_deal=new_deal, + new_company=None, + new_listing=None, + old_deal=old_deal, + ) + + assert result is True + + +def test_db_update_trigger__company_changed__returns_true() -> None: + deal_id = uuid.uuid4() + + old_deal = make_old_deal( + id=deal_id, + company_id="old_company", + ) + + new_deal = make_new_deal( + deal_id, + hs_object_id="1", + ) + + new_company = "new_company" + + result = HubspotDealDiffer.check_for_db_update_trigger( + new_deal=new_deal, + new_company=new_company, + new_listing=None, + old_deal=old_deal, + ) + + assert result is True + + +def test_db_update_trigger__listing_changed__returns_true() -> None: + deal_id = uuid.uuid4() + + old_deal = make_old_deal( + id=deal_id, + listing_id="abc", + ) + + new_deal = make_new_deal( + deal_id, + hs_object_id="1", + ) + + new_listing = {"listing_id": "xyz"} + + result = HubspotDealDiffer.check_for_db_update_trigger( + new_deal=new_deal, + new_company=None, + new_listing=new_listing, + old_deal=old_deal, + ) + + assert result is True