Skip to content

Commit

Permalink
Fix Power BI trigger testcase (#43494)
Browse files Browse the repository at this point in the history
* Optimize test that check trigger after being timeout

* Fix typing error

* Update test_powerbi.py

* refactor: Refactored powerbi_trigger fixture and used pytest.mark.parametrize to parametrize timeout

* Refactor: Remove parameterize marker

---------

Co-authored-by: David Blain <[email protected]>
  • Loading branch information
ambika-garg and dabla authored Nov 9, 2024
1 parent de88182 commit b9513cc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,21 @@ async def run(self) -> AsyncIterator[TriggerEvent]:
dataset_id=self.dataset_id,
group_id=self.group_id,
)

async def fetch_refresh_status() -> str:
"""Fetch the current status of the dataset refresh."""
refresh_details = await self.hook.get_refresh_details_by_refresh_id(
dataset_id=self.dataset_id,
group_id=self.group_id,
refresh_id=self.dataset_refresh_id,
)
return refresh_details["status"]

try:
dataset_refresh_status = None
dataset_refresh_status = await fetch_refresh_status()
start_time = time.monotonic()
while start_time + self.timeout > time.monotonic():
refresh_details = await self.hook.get_refresh_details_by_refresh_id(
dataset_id=self.dataset_id,
group_id=self.group_id,
refresh_id=self.dataset_refresh_id,
)
dataset_refresh_status = refresh_details.get("status")
dataset_refresh_status = await fetch_refresh_status()

if dataset_refresh_status == PowerBIDatasetRefreshStatus.COMPLETED:
yield TriggerEvent(
Expand Down
19 changes: 9 additions & 10 deletions providers/tests/microsoft/azure/triggers/test_powerbi.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,26 @@
DATASET_ID = "dataset_id"
GROUP_ID = "group_id"
DATASET_REFRESH_ID = "dataset_refresh_id"
TIMEOUT = 30
TIMEOUT = 5
MODULE = "airflow.providers.microsoft.azure"
CHECK_INTERVAL = 10
CHECK_INTERVAL = 1
API_VERSION = "v1.0"


@pytest.fixture
def powerbi_trigger():
trigger = PowerBITrigger(
def powerbi_trigger(timeout=TIMEOUT, check_interval=CHECK_INTERVAL) -> PowerBITrigger:
"""Fixture for creating a PowerBITrigger with customizable timeout and check interval."""
return PowerBITrigger(
conn_id=POWERBI_CONN_ID,
proxies=None,
api_version=API_VERSION,
dataset_id=DATASET_ID,
group_id=GROUP_ID,
check_interval=CHECK_INTERVAL,
check_interval=check_interval,
wait_for_termination=True,
timeout=TIMEOUT,
timeout=timeout,
)

return trigger


class TestPowerBITrigger:
@mock.patch("airflow.hooks.base.BaseHook.get_connection", side_effect=get_airflow_connection)
Expand Down Expand Up @@ -232,7 +231,7 @@ async def test_powerbi_trigger_run_exception_without_refresh_id(
async def test_powerbi_trigger_run_timeout(
self, mock_trigger_dataset_refresh, mock_get_refresh_details_by_refresh_id, powerbi_trigger
):
"""Assert that powerbi run timesout after end_time elapses"""
"""Assert that powerbi run times out after end_time elapses"""
mock_get_refresh_details_by_refresh_id.return_value = {
"status": PowerBIDatasetRefreshStatus.IN_PROGRESS
}
Expand All @@ -243,7 +242,7 @@ async def test_powerbi_trigger_run_timeout(
expected = TriggerEvent(
{
"status": "error",
"message": f"Timeout occurred while waiting for dataset refresh to complete: The dataset refresh {DATASET_REFRESH_ID} has status In Progress.",
"message": f"Timeout occurred while waiting for dataset refresh to complete: The dataset refresh {DATASET_REFRESH_ID} has status {PowerBIDatasetRefreshStatus.IN_PROGRESS}.",
"dataset_refresh_id": DATASET_REFRESH_ID,
}
)
Expand Down

0 comments on commit b9513cc

Please sign in to comment.