<commit_before># -*- coding: utf-8 -*-
import sqlalchemy as sa
from sqlalchemy import Boolean, Integer, Unicode, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlalchemy.sql.expression import false, true

from sqlalchemy_defaults import Column, make_lazy_configured


class TestCase(object):
    def setup_method(self, method):
        self.engine = create_engine(
            'postgres://postgres@localhost/sqlalchemy_defaults_test'
        )
        self.Model = declarative_base()

        self.create_models(**self.column_options)
        sa.orm.configure_mappers()
        self.columns = self.User.__table__.c
        self.Model.metadata.create_all(self.engine)

        Session = sessionmaker(bind=self.engine)
        self.session = Session()

    def teardown_method(self, method):
        self.session.close_all()
        self.Model.metadata.drop_all(self.engine)
        self.engine.dispose()

    def create_models(self, **options):
        class User(self.Model):
            __tablename__ = 'user'
            __lazy_options__ = options

            id = Column(Integer, primary_key=True)
            name = Column(Unicode(255))
            age = Column(Integer, info={'min': 13, 'max': 120}, default=16)
            is_active = Column(Boolean)
            nullable_boolean = Column(Boolean, nullable=True)
            is_admin = Column(Boolean, default=True)
            hobbies = Column(Unicode(255), default=u'football')
            favorite_hobbies = Column(Unicode(255), default=lambda ctx: (
                ctx.current_parameters['hobbies']
                if ctx.current_parameters['hobbies'] is not None
                else u'football'
            ))
            favorite_buddy = Column(Unicode(255), default=u'Örrimörri')
            created_at = Column(sa.DateTime, info={'auto_now': True})
            description = Column(sa.UnicodeText)

        class Article(self.Model):
            __tablename__ = 'article'
            __lazy_options__ = options
            id = Column(Integer, primary_key=True)
            name = Column(Unicode(255))
            author_id = Column(Integer, sa.ForeignKey(User.id))

        self.User = User
        self.Article = Article


make_lazy_configured(
    sa.orm.mapper
)


class TestLazyConfigurableDefaults(TestCase):
    column_options = {}

    def test_creates_min_and_max_check_constraints(self):
        from sqlalchemy.schema import CreateTable

        sql = unicode(CreateTable(self.User.__table__).compile(self.engine))
        assert 'CHECK (age >= 13)' in sql
        assert 'CHECK (age <= 120)' in sql

    def test_booleans_not_nullable_by_default(self):
        assert self.columns.is_active.nullable is False

    def test_supports_nullable_booleans(self):
        assert self.columns.nullable_boolean.nullable<commit_msg>Fixed py3 support for tests<commit_after># -*- coding: utf-8 -*-
import six
import sqlalchemy as sa
from sqlalchemy import Boolean, Integer, Unicode, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlalchemy.sql.expression import false, true

from sqlalchemy_defaults import Column, make_lazy_configured


class TestCase(object):
    def setup_method(self, method):
        self.engine = create_engine(
            'postgres://postgres@localhost/sqlalchemy_defaults_test'
        )
        self.Model = declarative_base()

        self.create_models(**self.column_options)
        sa.orm.configure_mappers()
        self.columns = self.User.__table__.c
        self.Model.metadata.create_all(self.engine)

        Session = sessionmaker(bind=self.engine)
        self.session = Session()

    def teardown_method(self, method):
        self.session.close_all()
        self.Model.metadata.drop_all(self.engine)
        self.engine.dispose()

    def create_models(self, **options):
        class User(self.Model):
            __tablename__ = 'user'
            __lazy_options__ = options

            id = Column(Integer, primary_key=True)
            name = Column(Unicode(255))
            age = Column(Integer, info={'min': 13, 'max': 120}, default=16)
            is_active = Column(Boolean)
            nullable_boolean = Column(Boolean, nullable=True)
            is_admin = Column(Boolean, default=True)
            hobbies = Column(Unicode(255), default=u'football')
            favorite_hobbies = Column(Unicode(255), default=lambda ctx: (
                ctx.current_parameters['hobbies']
                if ctx.current_parameters['hobbies'] is not None
                else u'football'
            ))
            favorite_buddy = Column(Unicode(255), default=u'Örrimörri')
            created_at = Column(sa.DateTime, info={'auto_now': True})
            description = Column(sa.UnicodeText)

        class Article(self.Model):
            __tablename__ = 'article'
            __lazy_options__ = options
            id = Column(Integer, primary_key=True)
            name = Column(Unicode(255))
            author_id = Column(Integer, sa.ForeignKey(User.id))

        self.User = User
        self.Article = Article


make_lazy_configured(
    sa.orm.mapper
)


class TestLazyConfigurableDefaults(TestCase):
    column_options = {}

    def test_creates_min_and_max_check_constraints(self):
        from sqlalchemy.schema import CreateTable

        sql = six.text_type(
            CreateTable(self.User.__table__).compile(self.engine)
        )
        assert 'CHECK (age >= 13)' in sql
        assert 'CHECK (age <= 120)' in sql

    def test_booleans_not_nullable_by_default(self):
        assert self.columns.is_active.nullable is False

    def test_supports_nullable_booleans(self):
        assert self.columns.nullable_boolean.nullable
{'fraction_non_alphanumeric': 0.09649577075781116, 'fraction_numerical': 0.00983946141895391, 'mean_word_length': 2.681067344345616, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '4417460', 'n_tokens_mistral': 1731, 'n_tokens_neox': 1655, 'n_words': 332}