ETL and ELT in Python
Jake Roach
Data Engineer
Unit test:

from pipeline import extract, transform, load
# Crea un unit test che verifichi il tipo di 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 ===============================
pipeline_type = "ETL"
# Controlla se pipeline_type è un'istanza di str
isinstance(pipeline_type, str)
True
# Verifica che la pipeline abbia davvero valore "ETL"
assert pipeline_type == "ETL"
# Combina assert e isinstance
assert isinstance(pipeline_type, str)
pipeline_type = "ETL"
# Genera un'AssertionError
assert isinstance(pipeline_type, float)
Traceback (most recent call last):
File "<stdin>", line 4, in <module>
AssertionError
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)
def test_transformed_data(clean_data):
# Aggiungi qui altre asserzioni
...
# Controlla il numero di colonne
assert len(clean_data.columns) == 4
# Controlla il limite inferiore di una colonna
assert clean_data["open"].min() >= 0
# Controlla l'intervallo di una colonna concatenando con "and"
assert clean_data["open"].min() >= 0 and clean_data["open"].max() <= 1000
ETL and ELT in Python