<commit_before># Copyright 2013 <NAME>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import absolute_import, division, print_function
from __future__ import unicode_literals

import os

import pretend
import pytest

from elasticsearch import TransportError

from warehouse.search import indexes
from warehouse.search.indexes import Index, BaseMapping


class TestIndex:

    def test_reindex(self, monkeypatch):
        urandom = pretend.call_recorder(lambda s: b"0" * s)
        monkeypatch.setattr(os, "urandom", urandom)

        models = pretend.stub()
        config = pretend.stub(hosts=[], get=lambda *a: {})

        index = Index(models, config)
        index.es = pretend.stub(
            indices=pretend.stub(
                create=pretend.call_recorder(lambda idx, body: None),
            ),
        )
        index.types = {
            "fake": pretend.stub(
                _type="fake",
                get_mapping=pretend.call_recorder(lambda: {"foo": "bar"}),
                index_all=pretend.call_recorder(lambda index: None),
            ),
        }
        index.update_alias = pretend.call_recorder(lambda *a, **kw: None)

        index.reindex()

        assert index.es.indices.create.calls == [
            pretend.call(
                "warehouse1e4a1b03",
                {"mappings": {"fake": {"foo": "bar"}}},
            ),
        ]
        assert index.update_alias.calls == [
            pretend.call("warehouse", "warehouse1e4a1b03", keep_old=False),
        ]
        assert index.types["fake"].index_all.calls == [
            pretend.call(index="warehouse1e4a1b03"),
        ]

    def test_reindex_no_alias(self, monkeypatch):
        urandom = pretend.call_recorder(lambda s: b"0" * s)
        monkeypatch.setattr(os, "urandom", urandom)

        models = pretend.stub()
        config = pretend.stub(hosts=[], get=lambda *a: {})

        index = Index(models, config)
        index.es = pretend.stub(
            indices=pretend.stub(
                create=pretend.call_recorder(lambda idx, body: None),
            ),
        )
        index.types = {
            "fake": pretend.stub(
                _type="fake",
                get_mapping=pretend.call_recorder(lambda: {"foo": "bar"}),
                index_all=pretend.call_recorder(lambda index: None),
            ),
        }
        index.update_alias = pretend.call_recorder(lambda *a, **kw: None)

        index.reindex(alias=False)

        assert index.es.indices.create.calls == [
            pretend.call(
                "warehouse1e4a1b03",
                {"mappings": {"fake": {"foo": "bar"}}},
            ),
        ]
        assert index.update_alias.calls == []
        assert index.types["fake"].index_all.calls == [
            pretend.call(index="warehouse1e4a1b03"),
        ]
<commit_msg>Fixed tests.<commit_after># Copyright 2013 <NAME>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import absolute_import, division, print_function
from __future__ import unicode_literals

import os

import pretend
import pytest

from elasticsearch import TransportError

from warehouse.search import indexes
from warehouse.search.indexes import Index, BaseMapping


class TestIndex:

    def test_reindex(self, monkeypatch):
        urandom = pretend.call_recorder(lambda s: b"0" * s)
        monkeypatch.setattr(os, "urandom", urandom)

        models = pretend.stub()
        config = pretend.stub(hosts=[], get=lambda *a: {})

        index = Index(models, config)
        index.es = pretend.stub(
            indices=pretend.stub(
                create=pretend.call_recorder(lambda idx, body: None),
            ),
        )
        index.types = {
            "fake": pretend.stub(
                _type="fake",
                get_mapping=pretend.call_recorder(lambda: {"foo": "bar"}),
                index_all=pretend.call_recorder(lambda index: None),
            ),
        }
        index.update_alias = pretend.call_recorder(lambda *a, **kw: None)

        index.reindex()

        assert index.es.indices.create.calls == [
            pretend.call(
                "warehouse30303030",
                {"mappings": {"fake": {"foo": "bar"}}},
            ),
        ]
        assert index.update_alias.calls == [
            pretend.call("warehouse", "warehouse1e4a1b03", keep_old=False),
        ]
        assert index.types["fake"].index_all.calls == [
            pretend.call(index="warehouse1e4a1b03"),
        ]

    def test_reindex_no_alias(self, monkeypatch):
        urandom = pretend.call_recorder(lambda s: b"0" * s)
        monkeypatch.setattr(os, "urandom", urandom)

        models = pretend.stub()
        config = pretend.stub(hosts=[], get=lambda *a: {})

        index = Index(models, config)
        index.es = pretend.stub(
            indices=pretend.stub(
                create=pretend.call_recorder(lambda idx, body: None),
            ),
        )
        index.types = {
            "fake": pretend.stub(
                _type="fake",
                get_mapping=pretend.call_recorder(lambda: {"foo": "bar"}),
                index_all=pretend.call_recorder(lambda index: None),
            ),
        }
        index.update_alias = pretend.call_recorder(lambda *a, **kw: None)

        index.reindex(alias=False)

        assert index.es.indices.create.calls == [
            pretend.call(
                "warehouse30303030",
                {"mappings": {"fake": {"foo": "bar"}}},
            ),
        ]
        assert index.update_alias.calls == []
        assert index.types["fake"].index_all.calls == [
            pretend.call(index="warehouse1e4a1b03"),
        ]

{'fraction_non_alphanumeric': 0.11535569258535118, 'fraction_numerical': 0.011430290269213416, 'mean_word_length': 2.2710280373831777, 'pattern_counts': {'":': 20, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '334182', 'n_tokens_mistral': 1956, 'n_tokens_neox': 1903, 'n_words': 457}