<commit_before>import pytest
import sqlalchemy as sa

from sqlalchemy_utils import auto_delete_orphans, ImproperlyConfigured


@pytest.fixture
def tagging_tbl(Base):
    return sa.Table(
        'tagging',
        Base.metadata,
        sa.Column(
            'tag_id',
            sa.Integer,
            sa.ForeignKey('tag.id', ondelete='cascade'),
            primary_key=True
        ),
        sa.Column(
            'entry_id',
            sa.Integer,
            sa.ForeignKey('entry.id', ondelete='cascade'),
            primary_key=True
        )
    )


@pytest.fixture
def Tag(Base):
    class Tag(Base):
        __tablename__ = 'tag'
        id = sa.Column(sa.Integer, primary_key=True)
        name = sa.Column(sa.String(100), unique=True, nullable=False)

        def __init__(self, name=None):
            self.name = name
    return Tag


@pytest.fixture
def Entry(Base, Tag, tagging_tbl):
    class Entry(Base):
        __tablename__ = 'entry'

        id = sa.Column(sa.Integer, primary_key=True)

        tags = sa.orm.relationship(
            'Tag',
            secondary=tagging_tbl,
            backref='entries'
        )
    auto_delete_orphans(Entry.tags)
    return Entry


@pytest.fixture
def EntryWithoutTagsBackref(Base, Tag, tagging_tbl):
    class EntryWithoutTagsBackref(Base):
        __tablename__ = 'entry'

        id = sa.Column(sa.Integer, primary_key=True)

        tags = sa.orm.relationship(
            'Tag',
            secondary=tagging_tbl
        )
    return EntryWithoutTagsBackref


class TestAutoDeleteOrphans(object):

    @pytest.fixture
    def init_models(self, Entry, Tag):
        pass

    def test_orphan_deletion(self, session, Entry, Tag):
        r1 = Entry()
        r2 = Entry()
        r3 = Entry()
        t1, t2, t3, t4 = (
            Tag('t1'),
            Tag('t2'),
            Tag('t3'),
            Tag('t4')
        )

        r1.tags.extend([t1, t2])
        r2.tags.extend([t2, t3])
        r3.tags.extend([t4])
        session.add_all([r1, r2, r3])

        assert session.query(Tag).count() == 4
        r2.tags.remove(t2)
        assert session.query(Tag).count() == 4
        r1.tags.remove(t2)
        assert session.query(Tag).count() == 3
        r1.tags.remove(t1)
        assert session.query(Tag).count() == 2


class TestAutoDeleteOrphansWithoutBackref(object):

    @pytest.fixture
    def init_models(self, EntryWithoutTagsBackref, Tag):
        pass

    def test_orphan_deletion(self, EntryWithoutTagsBackref):
        with pytest.raises(ImproperlyConfigured):
            auto_delete_orphans(EntryWithoutTagsBackref.tags)<commit_msg>Added unit test for backref with keywords for auto_delete_orphans.<commit_after>import pytest
import sqlalchemy as sa
from sqlalchemy.orm import backref

from sqlalchemy_utils import auto_delete_orphans, ImproperlyConfigured


@pytest.fixture
def tagging_tbl(Base):
    return sa.Table(
        'tagging',
        Base.metadata,
        sa.Column(
            'tag_id',
            sa.Integer,
            sa.ForeignKey('tag.id', ondelete='cascade'),
            primary_key=True
        ),
        sa.Column(
            'entry_id',
            sa.Integer,
            sa.ForeignKey('entry.id', ondelete='cascade'),
            primary_key=True
        )
    )


@pytest.fixture
def Tag(Base):
    class Tag(Base):
        __tablename__ = 'tag'
        id = sa.Column(sa.Integer, primary_key=True)
        name = sa.Column(sa.String(100), unique=True, nullable=False)

        def __init__(self, name=None):
            self.name = name
    return Tag


@pytest.fixture
def Entry(Base, Tag, tagging_tbl):
    class Entry(Base):
        __tablename__ = 'entry'

        id = sa.Column(sa.Integer, primary_key=True)

        tags = sa.orm.relationship(
            'Tag',
            secondary=tagging_tbl,
            backref='entries'
        )
    auto_delete_orphans(Entry.tags)
    return Entry


@pytest.fixture
def EntryWithoutTagsBackref(Base, Tag, tagging_tbl):
    class EntryWithoutTagsBackref(Base):
        __tablename__ = 'entry'

        id = sa.Column(sa.Integer, primary_key=True)

        tags = sa.orm.relationship(
            'Tag',
            secondary=tagging_tbl
        )
    return EntryWithoutTagsBackref


@pytest.fixture
def EntryWithBackrefKeywords(Base, Tag, tagging_tbl):
    class Entry(Base):
        __tablename__ = 'entry'

        id = sa.Column(sa.Integer, primary_key=True)

        tags = sa.orm.relationship(
            'Tag',
            secondary=tagging_tbl,
            backref=backref('entries', lazy='select')
        )
    auto_delete_orphans(Entry.tags)
    return Entry


class TestAutoDeleteOrphans(object):

    @pytest.fixture
    def init_models(self, Entry, Tag):
        pass

    def test_orphan_deletion(self, session, Entry, Tag):
        r1 = Entry()
        r2 = Entry()
        r3 = Entry()
        t1, t2, t3, t4 = (
            Tag('t1'),
            Tag('t2'),
            Tag('t3'),
            Tag('t4')
        )

        r1.tags.extend([t1, t2])
        r2.tags.extend([t2, t3])
        r3.tags.extend([t4])
        session.add_all([r1, r2, r3])

        assert session.query(Tag).count() == 4
        r2.tags.remove(t2)
        assert session.query(Tag).count() == 4
        r1.tags.remove(t2)
        assert session.query(Tag).count() == 3
        r1.tags.remove(t1)
        assert session.query(Tag).count() == 2


class TestAutoDeleteOrphansWithoutBackref(object):

    @pytest.fixture
    def init_models(self, EntryWithoutTagsBackref, Tag):
        pass

    def test_orphan_deletion(self, EntryWithoutTagsBackref):
        with pytest.raises(ImproperlyConfigured):
            auto_delete_orphans(EntryWithoutTagsBackref.tags)


class TestAutoDeleteOrphansBackrefWithKeywords(object):

    @pytest.fixture
    def init_models(self, EntryWithBackrefKeywords, Tag):
        pass

    def test_orphan_deletion(self, session, EntryWithBackrefKeywords, Tag):
        r1 = EntryWithBackrefKeywords()
        r2 = EntryWithBackrefKeywords()
        r3 = EntryWithBackrefKeywords()
        t1, t2, t3, t4 = (
            Tag('t1'),
            Tag('t2'),
            Tag('t3'),
            Tag('t4')
        )

        r1.tags.extend([t1, t2])
        r2.tags.extend([t2, t3])
        r3.tags.extend([t4])
        session.add_all([r1, r2, r3])

        assert session.query(Tag).count() == 4
        r2.tags.remove(t2)
        assert session.query(Tag).count() == 4
        r1.tags.remove(t2)
        assert session.query(Tag).count() == 3
        r1.tags.remove(t1)
        assert session.query(Tag).count() == 2
{'fraction_non_alphanumeric': 0.11607142857142858, 'fraction_numerical': 0.015435835351089588, 'mean_word_length': 2.162200956937799, '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': '7228708', 'n_tokens_mistral': 2191, 'n_tokens_neox': 2123, 'n_words': 388}