<commit_before>"""
:Copyright: 2006-2019 <NAME>
:License: Modified BSD, see LICENSE for details.
"""

from datetime import datetime

from byceps.services.snippet import service as snippet_service
from byceps.services.snippet.transfer.models import Scope

from testfixtures.snippet import create_current_version_association, \
    create_fragment, create_snippet_version

from tests.base import AbstractAppTestCase
from tests.helpers import create_brand, create_party, create_user


class GetCurrentVersionOfSnippetTestCase(AbstractAppTestCase):

    def setUp(self):
        super().setUp()

        self.brand = create_brand('lafiesta', 'La Fiesta')

        party2014 = create_party(self.brand.id, 'lafiesta-2014', 'La Fiesta 2014')
        party2015 = create_party(self.brand.id, 'lafiesta-2015', 'La Fiesta 2015')

        self.scope_site2014 = Scope.for_site(party2014.id)
        self.scope_site2015 = Scope.for_site(party2015.id)

        self.creator = create_user()

    def test_current_party_is_considered(self):
        fragment_info2014_version = self.create_fragment_with_version(
                self.scope_site2014, 'info', '2014-10-23 14:55:00')
        fragment_info2015_version = self.create_fragment_with_version(
                self.scope_site2015, 'info', '2014-10-23 18:21:00')

        actual = snippet_service.find_current_version_of_snippet_with_name(
            self.scope_site2014, 'info')

        assert actual == fragment_info2014_version

    def test_unknown_name(self):
        actual = snippet_service.find_current_version_of_snippet_with_name(
            self.scope_site2014, 'totally-unknown-snippet-name')

        assert actual is None

    # -------------------------------------------------------------------- #
    # helpers

    def create_fragment_with_version(self, scope, name, created_at_text):
        snippet = create_fragment(scope, name)
        self.db.session.add(snippet)

        created_at = datetime.strptime(created_at_text, '%Y-%m-%d %H:%M:%S')
        version = create_snippet_version(snippet, self.creator.id, created_at=created_at)
        self.db.session.add(version)

        current_version_association = create_current_version_association(snippet, version)
        self.db.session.add(current_version_association)

        self.db.session.commit()
        return version<commit_msg>Port snippet lookup test to pytest fixtures<commit_after>"""
:Copyright: 2006-2019 <NAME>
:License: Modified BSD, see LICENSE for details.
"""

from datetime import datetime

from byceps.database import db
from byceps.services.snippet import service as snippet_service
from byceps.services.snippet.transfer.models import Scope

from testfixtures.snippet import create_current_version_association, \
    create_fragment, create_snippet_version

from tests.helpers import create_brand, create_party, create_user


def test_current_party_is_considered(admin_app_with_db, admin_user):
    brand = create_brand('lafiesta', 'La Fiesta')

    party2014 = create_party(brand.id, 'lafiesta-2014', 'La Fiesta 2014')
    party2015 = create_party(brand.id, 'lafiesta-2015', 'La Fiesta 2015')

    scope_site2014 = Scope.for_site(party2014.id)
    scope_site2015 = Scope.for_site(party2015.id)

    creator = admin_user

    fragment_info2014_version = create_fragment_with_version(
            scope_site2014, 'info', creator.id, '2014-10-23 14:55:00')
    fragment_info2015_version = create_fragment_with_version(
            scope_site2015, 'info', creator.id, '2014-10-23 18:21:00')

    actual = snippet_service.find_current_version_of_snippet_with_name(
        scope_site2014, 'info')

    assert actual == fragment_info2014_version


def test_unknown_name(admin_app_with_db):
    brand = create_brand('lafiesta', 'La Fiesta')

    party = create_party(brand.id, 'lafiesta-2014', 'La Fiesta 2014')

    scope = Scope.for_site(party.id)

    actual = snippet_service.find_current_version_of_snippet_with_name(
        scope, 'totally-unknown-snippet-name')

    assert actual is None


# helpers


def create_fragment_with_version(scope, name, creator_id, created_at_text):
    snippet = create_fragment(scope, name)
    db.session.add(snippet)

    created_at = datetime.strptime(created_at_text, '%Y-%m-%d %H:%M:%S')
    version = create_snippet_version(snippet, creator_id, created_at=created_at)
    db.session.add(version)

    current_version_association = create_current_version_association(snippet, version)
    db.session.add(current_version_association)

    db.session.commit()
    return version
{'fraction_non_alphanumeric': 0.10678971654581411, 'fraction_numerical': 0.04658316853438805, 'mean_word_length': 3.915766738660907, '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': '2963603', 'n_tokens_mistral': 1589, 'n_tokens_neox': 1399, 'n_words': 265}