Asked by blip

transformations unit test

All Answers 1

Answered by GPT-5 mini AI
Do you mean a specific language or testing framework (Python/pytest, Python/unittest, JavaScript/Jest, Java/JUnit, etc.) and what kind of "transformations" (string/number functions, data-frame transforms, image transforms, scikit-learn-style fit/transform, Rx streams, etc.)? If you tell me that I can give a tailored unit-test suite.

Below are several common examples and general guidance you can pick from.

Quick checklist for testing transformations
- Test pure inputs → deterministic output (basic correctness).
- Test edge cases (empty input, null/None, zero, very large/small values).
- Test invariants and properties (idempotence, reversibility, monotonicity).
- Test types and shape (for arrays/dataframes).
- Test error handling (invalid input types/values).
- Control randomness (set seeds or mock RNG).
- Use parameterized tests to cover many cases concisely.
- For numeric outputs, use tolerances (approx equal).
- For pipelines/stateful transformers, test fit/transform lifecycle and persistence (save/load).

Examples

1) Python — pure function using pytest
Assume transform(x) doubles numbers and trims strings:
```
# mymodule.py
def transform(x):
if x is None:
return None
if isinstance(x, (int, float)):
return x * 2
if isinstance(x, str):
return x.strip()
raise TypeError("unsupported")
```
Tests:
```
# test_mymodule.py
import pytest
from mymodule import transform

@pytest.mark.parametrize("input,expected", [
(2, 4),
(2.5, 5.0),
(" hi ", "hi"),
(None, None),
])
def test_transform_basic(input, expected):
assert transform(input) == expected

def test_transform_bad_type():
with pytest.raises(TypeError):
transform([1,2])
```

2) Python — pandas DataFrame transformation
If you have a function that cleans a dataframe column:
```
# transforms.py
def normalize_names(df):
# returns new df with 'name' column trimmed and lowercased
df = df.copy()
df['name'] = df['name'].astype(str).str.strip().str.lower()
return df
```
Tests:
```
import pandas as pd
from transforms import normalize_names

def test_normalize_names():
df = pd.DataFrame({'name': [' Alice ', 'BOB', None]})
out = normalize_names(df)
assert out['name'].tolist() == ['alice', 'bob', 'none'] # or handle None explicitly
# ensure original df not mutated
assert df['name'].tolist() == [' Alice ', 'BOB', None]
```

3) scikit-learn style Transformer (fit/transform)
```
# scalers.py
from sklearn.base import BaseEstimator, TransformerMixin
import numpy as np

class MeanScaler(BaseEstimator, TransformerMixin):
def fit(self, X, y=None):
self.mean_ = np.mean(X, axis=0)
return self
def transform(self, X):
return X - self.mean_
```
Tests:
```
import numpy as np
from scalers import MeanScaler

def test_mean_scaler():
X = np.array([[1.0],[3.0]])
s = MeanScaler().fit(X)
assert pytest.approx(s.mean_[0]) == 2.0
out = s.transform(X)
assert out.tolist() == [[-1.0],[1.0]]
```

4) JavaScript — Jest for transform function
```
/* transform.js */
function normalize(s) {
if (!s) return '';
return s.trim().toLowerCase();
}
module.exports = normalize;
```
Test:
```
// transform.test.js
const normalize = require('./transform');

test('normalize trims and lowercases', () => {
expect(normalize(' Hello ')).toBe('hello');
expect(normalize(null)).toBe('');
});
```

5) Randomized transformations — control randomness
- If transform uses randomness (augmentations, sampling), set a seed or inject RNG dependency.
Example dependency injection:
```
def sample_item(items, rng):
return rng.choice(items)
```
In tests pass deterministic rng (numpy.random.RandomState(0)).

6) Property-based testing (Hypothesis)
For e.g. a function that is idempotent:
```
from hypothesis import given
import hypothesis.strategies as st

@given(st.text())
def test_idempotent_strip(s):
assert strip(strip(s)) == strip(s)
```

7) Tests for performance or large inputs
- Keep unit tests fast. For heavy performance tests, use separate integration/perf tests.

Mocking and isolation
- Mock external calls (I/O, DB, network) so tests only test transformation logic.

Putting it together
If you tell me:
- language & test framework
- a snippet of the transformation or its specification
I’ll write a focused unit-test file (with parameterization, fixtures, edge cases, and suggestions for mocking/seeding).