<commit_before>#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2014 uralbash <<EMAIL>>
#
# Distributed under terms of the MIT license.


from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy_mptt import mptt_sessionmaker


def add_fixture(model, fixtures, session):
    """
    Add fixtures to database.

    Example:

    .. code::

        hashes = ({'foo': {'foo': 'bar', '1': '2'}}, {'foo': {'test': 'data'}})
        add_fixture(TestHSTORE, hashes)
    """
    for fixture in fixtures:
        session.add(model(**fixture))


def add_mptt_tree(session, model):
    """ Init mptt tree

    .. code::

        level           Nested sets tree1
            1                    1(1)22
                    _______________|___________________
                   |               |                   |
            2    2(2)5           6(4)11             12(7)21
                   |               ^                   ^
            3    3(3)4       7(5)8   9(6)10    13(8)16   17(10)20
                                                  |          |
            4                                  14(9)15   18(11)19

            level           Nested sets tree2
            1                    1(12)22
                    _______________|___________________
                   |               |                   |
            2    2(13)5         6(15)11             12(18)21
                   |               ^                    ^
            3    3(14)4     7(16)8   9(17)10   13(19)16   17(21)20
                                                   |          |
            4                                  14(20)15   18(22)19

    """
    session.query(model).delete()
    session.commit()
    tree1 = (
        {'ppk': '1', 'parent_id': None},

        {'ppk': '2', 'visible': True, 'parent_id': '1'},
        {'ppk': '3', 'visible': True, 'parent_id': '2'},

        {'ppk': '4', 'visible': True, 'parent_id': '1'},
        {'ppk': '5', 'visible': True, 'parent_id': '4'},
        {'ppk': '6', 'visible': True, 'parent_id': '4'},

        {'ppk': '7', 'visible': True, 'parent_id': '1'},
        {'ppk': '8', 'visible': True, 'parent_id': '7'},
        {'ppk': '9', 'parent_id': '8'},
        {'ppk': '10', 'parent_id': '7'},
        {'ppk': '11', 'parent_id': '10'},
    )

    tree2 = (
        {'ppk': '12', 'parent_id': None},

        {'ppk': '13', 'parent_id': '12', 'tree_id': '2'},
        {'ppk': '14', 'parent_id': '13', 'tree_id': '2'},

        {'ppk': '15', 'parent_id': '12', 'tree_id': '2'},
        {'ppk': '16', 'parent_id': '15', 'tree_id': '2'},
        {'ppk': '17', 'parent_id': '15', 'tree_id': '2'},

        {'ppk': '18', 'parent_id': '12', 'tree_id': '2'},
        {'ppk': '19', 'parent_id': '18', 'tree_id': '2'},
        {'ppk': '20', 'parent_id': '19', 'tree_id': '2'},
        {'ppk': '21', 'parent_id': '18', 'tree_id': '2'},
        {'ppk': '22', 'parent_id': '21', 'tree_id': '2'},
    )
    add_fixture(model, tree1, session)
    add_fixture(model, tree2, session)


def get_obj(session, model, id):
    return session.query(model).filter_by(ppk=id).one()


class TreeTestingMixin(object):

    base = None
    model = None

    def setUp(self):
        self.engine = create_engine('sqlite:///:memory:')
        Session = mptt_sessionmaker(sessionmaker(bind=self.engine))
        self.session = Session()
        self.base.metadata.create_all(self.engine)
        add_mptt_tree(self.session, self.model)
        self.result = self.session.query(
            self.model.ppk, self.model.left, self.model.right,
            self.model.level, self.model.parent_id, self.model.tree_id)

    def tearDown(self):
        self.base.metadata.drop_all(self.engine)

    # TODO: assertWarns was added to python > 3.2
    # def test_explicit_registration(self):
    #     with self.assertWarns(DeprecationWarning):
    #         self.model.register_tree()<commit_msg>Back to 100% code coverage.<commit_after>#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2014 uralbash <<EMAIL>>
#
# Distributed under terms of the MIT license.


from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy_mptt import mptt_sessionmaker


def add_fixture(model, fixtures, session):
    """
    Add fixtures to database.

    Example:

    .. code::

        hashes = ({'foo': {'foo': 'bar', '1': '2'}}, {'foo': {'test': 'data'}})
        add_fixture(TestHSTORE, hashes)
    """
    for fixture in fixtures:
        session.add(model(**fixture))


def add_mptt_tree(session, model):
    """ Init mptt tree

    .. code::

        level           Nested sets tree1
            1                    1(1)22
                    _______________|___________________
                   |               |                   |
            2    2(2)5           6(4)11             12(7)21
                   |               ^                   ^
            3    3(3)4       7(5)8   9(6)10    13(8)16   17(10)20
                                                  |          |
            4                                  14(9)15   18(11)19

            level           Nested sets tree2
            1                    1(12)22
                    _______________|___________________
                   |               |                   |
            2    2(13)5         6(15)11             12(18)21
                   |               ^                    ^
            3    3(14)4     7(16)8   9(17)10   13(19)16   17(21)20
                                                   |          |
            4                                  14(20)15   18(22)19

    """
    session.query(model).delete()
    session.commit()
    tree1 = (
        {'ppk': '1', 'parent_id': None},

        {'ppk': '2', 'visible': True, 'parent_id': '1'},
        {'ppk': '3', 'visible': True, 'parent_id': '2'},

        {'ppk': '4', 'visible': True, 'parent_id': '1'},
        {'ppk': '5', 'visible': True, 'parent_id': '4'},
        {'ppk': '6', 'visible': True, 'parent_id': '4'},

        {'ppk': '7', 'visible': True, 'parent_id': '1'},
        {'ppk': '8', 'visible': True, 'parent_id': '7'},
        {'ppk': '9', 'parent_id': '8'},
        {'ppk': '10', 'parent_id': '7'},
        {'ppk': '11', 'parent_id': '10'},
    )

    tree2 = (
        {'ppk': '12', 'parent_id': None},

        {'ppk': '13', 'parent_id': '12', 'tree_id': '2'},
        {'ppk': '14', 'parent_id': '13', 'tree_id': '2'},

        {'ppk': '15', 'parent_id': '12', 'tree_id': '2'},
        {'ppk': '16', 'parent_id': '15', 'tree_id': '2'},
        {'ppk': '17', 'parent_id': '15', 'tree_id': '2'},

        {'ppk': '18', 'parent_id': '12', 'tree_id': '2'},
        {'ppk': '19', 'parent_id': '18', 'tree_id': '2'},
        {'ppk': '20', 'parent_id': '19', 'tree_id': '2'},
        {'ppk': '21', 'parent_id': '18', 'tree_id': '2'},
        {'ppk': '22', 'parent_id': '21', 'tree_id': '2'},
    )
    add_fixture(model, tree1, session)
    add_fixture(model, tree2, session)


def get_obj(session, model, id):
    return session.query(model).filter_by(ppk=id).one()


class TreeTestingMixin(object):

    base = None
    model = None

    def setUp(self):
        self.engine = create_engine('sqlite:///:memory:')
        Session = mptt_sessionmaker(sessionmaker(bind=self.engine))
        self.session = Session()
        self.base.metadata.create_all(self.engine)
        add_mptt_tree(self.session, self.model)
        self.result = self.session.query(
            self.model.ppk, self.model.left, self.model.right,
            self.model.level, self.model.parent_id, self.model.tree_id)

    def tearDown(self):
        self.base.metadata.drop_all(self.engine)

    def test_explicit_registration(self):
        # TODO: assertWarns was added to python > 3.2
        # with self.assertWarns(DeprecationWarning):
        self.model.register_tree()
{'fraction_non_alphanumeric': 0.1743073047858942, 'fraction_numerical': 0.052015113350125944, 'mean_word_length': 1.6523046092184368, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 32, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7238102', 'n_tokens_mistral': 2795, 'n_tokens_neox': 2557, 'n_words': 561}