# pysparky.quality

*module*

[View source](https://github.com/PySparky/pysparky-pyspark-helper/blob/327582f8b776fe7638082ce4b5be8153d3ce63d3/pysparky/quality.py#L1-L1)

## pysparky.quality.expect_any_to_one

*function*

```python
def expect_any_to_one(col1: str | Sequence[str], col2: str | Sequence[str])
```

A decorator function that ensures an N:1 relationship between col1 and col2,
meaning each value in col1 corresponds to only one distinct value in col2.

**Parameters**

- `col1` (`str | Sequence[str]`) — Name of the column or a tuple of column names.
- `col2` (`str | Sequence[str]`) — Name of the column or a tuple of column names.

> **Example**
>
> ```python
> >>> @expect_any_to_one("city", "country")
> ... def get_data():
> ...     return spark.createDataFrame([("Paris", "France"), ("Lyon", "France")], ["city", "country"])
> >>> df = get_data()
> ✅: city:country is N:1
> ```

[View source](https://github.com/PySparky/pysparky-pyspark-helper/blob/327582f8b776fe7638082ce4b5be8153d3ce63d3/pysparky/quality.py#L126-L162)

## pysparky.quality.expect_criteria

*function*

```python
def expect_criteria(criteria)
```

A decorator function that ensures a specific criterion on a Spark DataFrame.

**Parameters**

- `criteria` (`pyspark.sql.column.Column`) — The filter criterion to be applied to the DataFrame.

**Returns**

- `function` — A decorated function that checks the criterion.

**Raises**

- `AssertionError` — If the filtered count and unfiltered count of the DataFrame are not equal.

> **Example**
>
> ```python
> >>> @expect_criteria(F.col("age") > 0)
> ... def get_data():
> ...     return spark.createDataFrame([(10,), (20,)], ["age"])
> >>> df = get_data()
> ✅: Criteria '(age > 0)' passed
> ```

[View source](https://github.com/PySparky/pysparky-pyspark-helper/blob/327582f8b776fe7638082ce4b5be8153d3ce63d3/pysparky/quality.py#L87-L123)

## pysparky.quality.expect_one_to_one

*function*

```python
def expect_one_to_one(col1: str | Sequence[str], col2: str | Sequence[str])
```

A decorator function that ensures a 1:1 relationship between col1 and col2,
meaning each value in col1 corresponds to only one distinct value in col2 and vice-versa.

**Parameters**

- `col1` (`str | Sequence[str]`) — Name of the column or a tuple of column names.
- `col2` (`str | Sequence[str]`) — Name of the column or a tuple of column names.

> **Example**
>
> ```python
> >>> @expect_one_to_one("id", "name")
> ... def get_data():
> ...     return spark.createDataFrame([(1, "Alice"), (2, "Bob")], ["id", "name"])
> >>> df = get_data()
> ✅: id:name is N:1
> ✅: name:id is N:1
> ```

[View source](https://github.com/PySparky/pysparky-pyspark-helper/blob/327582f8b776fe7638082ce4b5be8153d3ce63d3/pysparky/quality.py#L165-L191)

## pysparky.quality.expect_type

*function*

```python
def expect_type(col_name, col_type)
```

A decorator function that verifies the data type of a specified column in a Spark DataFrame.

**Parameters**

- `col_name` (`str`) — The column's name.
- `col_type` (`pyspark.sql.types.DataType`) — The expected data type for the column.

**Returns**

- `function` — A decorated function that checks the column's data type.

**Raises**

- `AssertionError` — If the column's data type does not match the expected type.

> **Example**
>
> ```python
> >>> from pyspark.sql.types import IntegerType
> >>> @expect_type("id", IntegerType())
> ... def get_data():
> ...     return spark.createDataFrame([(1,), (2,)], ["id"])
> >>> df = get_data()
> ✅: Column 'id' has the expected data type IntegerType()
> ```

[View source](https://github.com/PySparky/pysparky-pyspark-helper/blob/327582f8b776fe7638082ce4b5be8153d3ce63d3/pysparky/quality.py#L6-L44)

## pysparky.quality.expect_unique

*function*

```python
def expect_unique(col_name)
```

A decorator function that ensures the uniqueness of a column in a Spark DataFrame.

**Parameters**

- `col_name` (`str`) — The column's name.

**Returns**

- `function` — A decorated function that checks the column's uniqueness.

**Raises**

- `AssertionError` — If the column's count and distinct count are not equal.

> **Example**
>
> ```python
> >>> @expect_unique("id")
> ... def get_data():
> ...     return spark.createDataFrame([(1,), (2,)], ["id"])
> >>> df = get_data()
> ✅: Column 'id' is distinct
> ```

[View source](https://github.com/PySparky/pysparky-pyspark-helper/blob/327582f8b776fe7638082ce4b5be8153d3ce63d3/pysparky/quality.py#L47-L84)
