<commit_before>from mock import patch
from nose.tools import eq_

from snippets.base.managers import SnippetManager
from snippets.base.models import Client, ClientMatchRule, Snippet, SnippetLocale
from snippets.base.tests import ClientMatchRuleFactory, SnippetFactory, TestCase


class ClientMatchRuleQuerySetTests(TestCase):
    manager = ClientMatchRule.cached_objects

    @patch.object(ClientMatchRule, 'matches', autospec=True)
    def test_basic(self, matches):
        rule1_pass = ClientMatchRuleFactory.create()
        rule2_pass = ClientMatchRuleFactory.create()
        rule3_fail = ClientMatchRuleFactory.create()
        rule4_pass = ClientMatchRuleFactory.create()
        rule5_fail = ClientMatchRuleFactory.create()

        return_values = {
            rule1_pass.id: True,
            rule2_pass.id: True,
            rule3_fail.id: False,
            rule4_pass.id: True,
            rule5_fail.id: False,
        }
        matches.side_effect = lambda self, client: return_values.get(self.id, False)

        passed, failed = self.manager.all().evaluate('asdf')
        eq_([rule1_pass, rule2_pass, rule4_pass], passed)
        eq_([rule3_fail, rule5_fail], failed)


class SnippetManagerTests(TestCase):
    def _build_client(self, **client_attrs):
        params = {'startpage_version': '4',
                  'name': 'Firefox',
                  'version': '23.0a1',
                  'appbuildid': '20130510041606',
                  'build_target': 'Darwin_Universal-gcc3',
                  'locale': 'en-US',
                  'channel': 'release',
                  'os_version': 'Darwin 10.8.0',
                  'distribution': 'default',
                  'distribution_version': 'default_version'}<commit_msg>Do not rely on queryset order for ClientMatchRuleQueryset test (redux).<commit_after>from mock import patch
from nose.tools import eq_

from snippets.base.managers import SnippetManager
from snippets.base.models import Client, ClientMatchRule, Snippet, SnippetLocale
from snippets.base.tests import ClientMatchRuleFactory, SnippetFactory, TestCase


class ClientMatchRuleQuerySetTests(TestCase):
    manager = ClientMatchRule.cached_objects

    @patch.object(ClientMatchRule, 'matches', autospec=True)
    def test_basic(self, matches):
        rule1_pass = ClientMatchRuleFactory.create()
        rule2_pass = ClientMatchRuleFactory.create()
        rule3_fail = ClientMatchRuleFactory.create()
        rule4_pass = ClientMatchRuleFactory.create()
        rule5_fail = ClientMatchRuleFactory.create()

        return_values = {
            rule1_pass.id: True,
            rule2_pass.id: True,
            rule3_fail.id: False,
            rule4_pass.id: True,
            rule5_fail.id: False,
        }
        matches.side_effect = lambda self, client: return_values.get(self.id, False)

        passed, failed = self.manager.all().evaluate('asdf')
        eq_(set([rule1_pass, rule2_pass, rule4_pass]), set(passed))
        eq_(set([rule3_fail, rule5_fail]), set(failed))


class SnippetManagerTests(TestCase):
    def _build_client(self, **client_attrs):
        params = {'startpage_version': '4',
                  'name': 'Firefox',
                  'version': '23.0a1',
                  'appbuildid': '20130510041606',
                  'build_target': 'Darwin_Universal-gcc3',
                  'locale': 'en-US',
                  'channel': 'release',
                  'os_version': 'Darwin 10.8.0',
                  'distribution': 'default',
                  'distribution_version': 'default_version'}
{'fraction_non_alphanumeric': 0.10403813796971396, 'fraction_numerical': 0.021873247335950644, 'mean_word_length': 2.6584615384615384, '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': '1541697', 'n_tokens_mistral': 1073, 'n_tokens_neox': 1006, 'n_words': 206}