Back to Repositories

Testing DAG Run State Detection Implementation in Apache Airflow

This test suite validates the useGridData functionality in Apache Airflow, specifically focusing on the areActiveRuns() function that determines the active status of DAG runs. The tests ensure proper state detection and handling of various DAG run scenarios.

Test Coverage Overview

The test suite provides comprehensive coverage of the areActiveRuns() function, validating its behavior across different DAG run states.

Key areas tested include:
  • Detection of active runs when mixed states are present
  • Verification of resolved run states
  • Handling of empty run arrays
Integration points focus on the DagRun type interface and state management.

Implementation Analysis

The testing approach utilizes Jest’s describe/test pattern with TypeScript type safety. The implementation leverages mock DagRun objects with common parameters to test various state combinations.

Notable patterns include:
  • Common parameter object reuse
  • Type-safe test data construction
  • Explicit state verification

Technical Details

Testing infrastructure includes:
  • Jest as the primary testing framework
  • TypeScript for type safety
  • Custom DagRun type definitions
  • Mock data utilities
Configuration maintains Apache Airflow’s licensing and testing standards.

Best Practices Demonstrated

The test suite exemplifies high-quality testing practices through clear organization and comprehensive coverage.

Notable practices include:
  • Isolated test cases
  • Consistent test data structure
  • Clear test case descriptions
  • Type-safe implementation

apache/airflow

airflow/www/static/js/api/useGridData.test.ts

            
/*!
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

/* global describe, test, expect */

import type { DagRun } from "src/types";
import { areActiveRuns } from "./useGridData";

const commonDagRunParams = {
  runId: "runId",
  logicalDate: "2022-01-01T10:00+00:00",
  dataIntervalStart: "2022-01-01T05:00+00:00",
  dataIntervalEnd: "2022-01-01T10:00+00:00",
  runType: "scheduled" as DagRun["runType"],
  queuedAt: null,
  startDate: null,
  endDate: null,
  lastSchedulingDecision: null,
  externalTrigger: false,
  conf: null,
  note: "",
};

describe("Test areActiveRuns()", () => {
  test("Correctly detects active runs", () => {
    const runs: DagRun[] = [
      { state: "success", ...commonDagRunParams },
      { state: "queued", ...commonDagRunParams },
    ];
    expect(areActiveRuns(runs)).toBe(true);
  });

  test("Returns false when all runs are resolved", () => {
    const runs: DagRun[] = [
      { state: "success", ...commonDagRunParams },
      { state: "failed", ...commonDagRunParams },
      { state: "failed", ...commonDagRunParams },
    ];
    const result = areActiveRuns(runs);
    expect(result).toBe(false);
  });

  test("Returns false when there are no runs", () => {
    const result = areActiveRuns();
    expect(result).toBe(false);
  });
});