Exemple #1
0
def test_basic(dialect: Dialect, metadata: MetaData, min_size: int,
               max_size: Optional[int]) -> None:
    result = factory(dialect=dialect,
                     metadata=metadata,
                     min_size=min_size,
                     max_size=max_size)

    assert isinstance(result, Strategy)
def test_examples(data: DataObject, metadatas: Strategy[MetaData],
                  min_size: int, max_size: Optional[int]) -> None:
    strategy = factory(metadatas=metadatas,
                       min_size=min_size,
                       max_size=max_size)

    result = data.draw(strategy)

    assert isinstance(result, Table)
    assert min_size <= len(result.columns)
    assert max_size is None or len(result.columns) <= max_size
    assert not all(map(is_column_unique, result.columns))
Exemple #3
0
def test_examples(data: DataObject, dialect: Dialect, metadata: MetaData,
                  min_size: int, max_size: Optional[int]) -> None:
    strategy = factory(dialect=dialect,
                       metadata=metadata,
                       min_size=min_size,
                       max_size=max_size)

    result = data.draw(strategy)

    assert isinstance(result, Table)
    assert min_size <= len(result.columns)
    assert max_size is None or len(result.columns) <= max_size
    assert not result.columns or result.primary_key
Exemple #4
0
def test_extending(data: DataObject, dialect_with_name: Tuple[Dialect, str],
                   metadata: MetaData, min_size: int,
                   max_size: Optional[int]) -> None:
    dialect, name = dialect_with_name

    strategy = factory(dialect=dialect,
                       metadata=metadata,
                       names=strategies.just(name),
                       min_size=min_size,
                       max_size=max_size,
                       extend_existing=strategies.just(True))

    first_result = data.draw(strategy)
    second_result = data.draw(strategy)

    assert first_result == second_result
def test_basic(metadatas: Strategy[MetaData], min_size: int,
               max_size: Optional[int]) -> None:
    result = factory(metadatas=metadatas, min_size=min_size, max_size=max_size)

    assert isinstance(result, Strategy)
Exemple #6
0
from typing import (Any, Dict, Tuple)

from hypothesis import strategies
from sqlalchemy.schema import (Column, Table)

from hypothesis_sqlalchemy import tabular
from hypothesis_sqlalchemy.columnar import values
from hypothesis_sqlalchemy.hints import Strategy
from tests.strategies import (data, dialects, max_sizes, metadatas, min_sizes)

data = data
min_sizes = min_sizes
max_sizes = max_sizes

tables = strategies.tuples(
    dialects, metadatas).flatmap(lambda dialect_with_metadata: tabular.factory(
        dialect=dialect_with_metadata[0], metadata=dialect_with_metadata[1]))


def fix_columns_values(
        table: Table) -> Strategy[Tuple[Table, Dict[str, Strategy[Any]]]]:
    def to_item(column: Column) -> Tuple[str, Strategy[Any]]:
        return column.name, values.factory(column)

    if table.columns:
        fixed_columns_values = (strategies.sets(
            strategies.sampled_from(list(table.columns))).map(
                partial(map, to_item)).map(dict))
    else:
        fixed_columns_values = strategies.builds(dict)
    return strategies.tuples(strategies.just(table), fixed_columns_values)
Exemple #7
0
from operator import attrgetter
from typing import (List, Tuple)

from hypothesis import strategies
from sqlalchemy.schema import (Column, MetaData)

from hypothesis_sqlalchemy import tabular
from hypothesis_sqlalchemy.hints import Strategy
from tests.strategies import (data, dialects)
from tests.utils import Bounds

data = data
columns_lists = (dialects.flatmap(
    lambda dialect: tabular.factory(dialect=dialect, metadata=MetaData())).map(
        attrgetter('columns')).map(list))


def to_columns_lists_with_bounds(
        columns: List[Column]) -> Strategy[Tuple[List[Column], Bounds]]:
    def min_size_to_bounds(min_size: int) -> Strategy[Bounds]:
        return strategies.tuples(
            strategies.just(min_size),
            strategies.none()
            | strategies.integers(min_size, len(columns)))

    return strategies.tuples(
        strategies.just(columns),
        strategies.integers(0, len(columns)).flatmap(min_size_to_bounds))


columns_lists_with_bounds = columns_lists.flatmap(to_columns_lists_with_bounds)
Exemple #8
0
                                   tabular)
from hypothesis_sqlalchemy.columnar import values
from hypothesis_sqlalchemy.hints import Strategy
from tests.strategies import (data,
                              max_sizes,
                              metadatas,
                              min_sizes)

data = data
min_sizes = min_sizes
max_sizes = max_sizes
non_unique_columns = columnar.non_primary_keys_factory(
        are_unique=strategies.just(False))
tables_without_unique_columns = tabular.factory(
        metadatas=metadatas,
        columns_factory=partial(strategies.lists,
                                non_unique_columns,
                                unique_by=attrgetter('name')))
tables_with_unique_columns = tabular.factory(metadatas=metadatas)
tables = tables_without_unique_columns | tables_with_unique_columns


def fix_columns_values(table: Table
                       ) -> Strategy[Tuple[Table, Dict[str, Strategy[Any]]]]:
    def to_item(column: Column) -> Tuple[str, Strategy[Any]]:
        return column.name, values.factory(column)

    fixed_columns_values = (strategies.sets(strategies
                                            .sampled_from(list(table.columns)))
                            .map(partial(map, to_item))
                            .map(dict))