<commit_before>import pytest
import trafaret as t
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
from aiohttp_admin.backends.sa_utils import validator_from_table


@pytest.fixture
def table():

    meta = sa.MetaData()
    post = sa.Table(
        'post', meta,
        sa.Column('id', sa.Integer, nullable=False),
        sa.Column('title', sa.String(200), nullable=False),
        sa.Column('body', sa.Text, nullable=False),
        sa.Column('views', sa.Integer, nullable=False),
        sa.Column('average_note', sa.Float, nullable=False),
        sa.Column('pictures', postgresql.JSON, server_default='{}'),
        sa.Column('published_at', sa.Date, nullable=False),
        sa.Column('tags', postgresql.ARRAY(sa.Integer), server_default='[]'),

        # Indexes #
        sa.PrimaryKeyConstraint('id', name='post_id_pkey'))
    return post


def test_keys(table):
    names = sorted(['id', 'title', 'body', 'views', 'average_note', 'pictures',
                    'published_at', 'tags'])
    traf = validator_from_table(table, skip_pk=False)
    result_names = sorted([key.name for key in traf.keys])
    assert result_names == names


def test_skip_pk(table):
    names = sorted(['title', 'body', 'views', 'average_note', 'pictures',
                    'published_at', 'tags'])
    traf = validator_from_table(table, skip_pk=True)
    result_names = sorted([key.name for key in traf.keys])
    assert result_names == names


def test_validation(table):
    traf = validator_from_table(table, skip_pk=False)
    data = {'id': '1',
            'title': 'title string',
            'body': 'body text',
            'views': '42',
            'average_note': '0.11',
            'pictures': {'foo': 'bar'},
            'published_at': '2015-12-15',
            'tags': [1, 2, 3]}
    data = traf(data)
    assert data['id'] == 1
    assert data['views'] == 42
    assert data['average_note'] == 0.11


def test_validation_bad_input(table):
    traf = validator_from_table(table, skip_pk=False)
    with pytest.raises(t.DataError):
        data = {'id': 'not a string',  # not valid data
                'title': 'title string',
                'body': 'body text',<commit_msg>hack to make test pass<commit_after>import pytest
import trafaret as t
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
from aiohttp_admin.backends.sa_utils import validator_from_table


@pytest.fixture
def table():

    meta = sa.MetaData()
    post = sa.Table(
        'post', meta,
        sa.Column('id', sa.Integer, nullable=False),
        sa.Column('title', sa.String(200), nullable=False),
        sa.Column('body', sa.Text, nullable=False),
        sa.Column('views', sa.Integer, nullable=False),
        sa.Column('average_note', sa.Float, nullable=False),
        sa.Column('pictures', postgresql.JSON, server_default='{}'),
        sa.Column('published_at', sa.Date, nullable=False),
        sa.Column('tags', postgresql.ARRAY(sa.Integer), server_default='[]'),

        # Indexes #
        sa.PrimaryKeyConstraint('id', name='post_id_pkey'))
    return post


def test_keys(table):
    names = sorted(['id', 'title', 'body', 'views', 'average_note', 'pictures',
                    'published_at', 'tags'])
    traf = validator_from_table(table, 'id', skip_pk=False)
    result_names = sorted([key.name for key in traf.keys])
    assert result_names == names


def test_skip_pk(table):
    names = sorted(['title', 'body', 'views', 'average_note', 'pictures',
                    'published_at', 'tags'])
    traf = validator_from_table(table, 'id', skip_pk=True)
    result_names = sorted([key.name for key in traf.keys])
    assert result_names == names


def test_validation(table):
    traf = validator_from_table(table, 'id', skip_pk=False)
    data = {'id': '1',
            'title': 'title string',
            'body': 'body text',
            'views': '42',
            'average_note': '0.11',
            'pictures': {'foo': 'bar'},
            'published_at': '2015-12-15',
            'tags': [1, 2, 3]}
    data = traf(data)
    assert data['id'] == 1
    assert data['views'] == 42
    assert data['average_note'] == 0.11


def test_validation_bad_input(table):
    traf = validator_from_table(table, 'id', skip_pk=False)
    with pytest.raises(t.DataError):
        data = {'id': 'not a string',  # not valid data
                'title': 'title string',
                'body': 'body text',
{'fraction_non_alphanumeric': 0.15498986258166253, 'fraction_numerical': 0.011714350078846587, 'mean_word_length': 2.967828418230563, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4595952', 'n_tokens_mistral': 1488, 'n_tokens_neox': 1417, 'n_words': 327}