डेटा पाइपलाइन का यूनिट परीक्षण

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

फिक्स्चर से डेटा पाइपलाइन कंपोनेंट्स मॉक करना

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

DataFrames का यूनिट परीक्षण

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

अभ्यास करते हैं!

Python में ETL और ELT

Preparing Video For Download...