データパイプラインのユニットテスト

Python で学ぶ ETL と ELT

Jake Roach

Data Engineer

ユニットテストでパイプラインを検証

ユニットテスト:

  • ソフトウェア開発で一般的
  • 期待どおりに動作するか確認
  • データの妥当性確認に役立つ

ユニットテストとエンドツーエンドテストを含む、典型的なデータパイプラインの検証フレームワーク。

Python で学ぶ ETL と ELT

ユニットテストに pytest を使用

from pipeline import extract, transform, load

# Build a unit test, asserting the type of clean_stock_data
def test_transformed_data():
    raw_stock_data = extract("raw_stock_data.csv")
    clean_stock_data = transform(raw_data)
    assert isinstance(clean_stock_data, pd.DataFrame)
> python -m pytest

test_transformed_data .                                                     [100%]
================================ 1 passed in 1.17s ===============================
Python で学ぶ ETL と ELT

assert と isinstance

pipeline_type = "ETL"

# Check if pipeline_type is an instance of a str
isinstance(pipeline_type, str)
True
# Assert that the pipeline does indeed take value "ETL"
assert pipeline_type == "ETL"
# Combine assert and isinstance
assert isinstance(pipeline_type, str)
Python で学ぶ ETL と ELT

AssertionError

pipeline_type = "ETL"

# Create an AssertionError
assert isinstance(pipeline_type, float)
Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
AssertionError
Python で学ぶ ETL と ELT

fixture でパイプラインをモック

import pytest

@pytest.fixture()
def clean_data():
    raw_stock_data = extract("raw_stock_data.csv")
    clean_stock_data = transform(raw_data)
    return clean_stock_data
def test_transformed_data(clean_data):
    assert isinstance(clean_data, pd.DataFrame)
Python で学ぶ ETL と ELT

DataFrame のユニットテスト

def test_transformed_data(clean_data):
    # Include other assert statements here
    ...

    # Check number of columns
    assert len(clean_data.columns) == 4

    # Check the lower bound of a column
    assert clean_data["open"].min() >= 0

    # Check the range of a column by chaining statements with "and"
    assert clean_data["open"].min() >= 0 and clean_data["open"].max() <= 1000

Python で学ぶ ETL と ELT

¡Vamos a practicar!

Python で学ぶ ETL と ELT

Preparing Video For Download...