<commit_before>from django.test import TestCase

from .taggit_extras.models import Food


class NamespacedTaggableManagerTest(TestCase):
    food_model = Food

    def assert_tags_equal(self, qs, tags, sort=True, attr="name"):
        got = map(lambda tag: getattr(tag, attr), qs)
        if sort:
            got.sort()
            tags.sort()
        self.assertEqual(got, tags)

    def test_all_ns(self):
        """Tags can be collated or fetched by namespace"""
        apple = self.food_model.objects.create(name="apple")

        expected_tags = {
            u'': [u'foo', u'bar', u'baz'],
            u'int:': [u'int:1', u'int:2'],
            u'string:': [u'string:asdf'],
            u'color:': [u'color:red'],
            u'system:contest:': [u'system:contest:finalist']
        }

        for tags in expected_tags.values():
            apple.tags.add(*tags)

        ns_tags = apple.tags.all_ns()

        expected_ns = expected_tags.keys()
        expected_ns.sort()
        result_ns = ns_tags.keys()
        result_ns.sort()
        self.assertEqual(expected_ns, result_ns)

        for ns in expected_ns:
            self.assert_tags_equal(ns_tags[ns], expected_tags[ns])
            self.assert_tags_equal(apple.tags.all_ns(ns), expected_tags[ns])

    def test_clear_ns(self):
        """Tags can be selectively cleared by namespace"""
        apple = self.food_model.objects.create(name="apple")
        tags_cleared = ['a:1', 'a:2', 'a:3']
        tags_not_cleared = ['1', '2', 'b:1', 'b:2', 'c:1']
        apple.tags.add(*tags_cleared)
        apple.tags.add(*tags_not_cleared)
        apple.tags.clear_ns('a:')
        self.assert_tags_equal(apple.tags.all(), tags_not_cleared)

    def test_set_ns(self):
        """Tags can be selectively set by namespace"""
        apple = self.food_model.objects.create(name="apple")

        tags_before_set = ['a:1', 'a:2', 'a:3']
        tags_after_set = ['a:4', 'a:5', 'a:6']
        tags_not_set = ['1', '2', 'b:1', 'b:2', 'c:1']

        apple.tags.add(*tags_before_set)
        apple.tags.add(*tags_not_set)

        apple.tags.set_ns('a:', *tags_after_set)

        self.assert_tags_equal(apple.tags.all(), tags_after_set + tags_not_set)

    def test_add_ns(self):
        """Tags that do not start with the namespace will be added with the
        namespace tacked on."""
        apple = self.food_model.objects.create(name="apple")

        tags = ['foo', 'bar', 'baz']
        apple.tags.add_ns('a:', *tags)

        self.assert_tags_equal(apple.tags.all(), ['a:%s' % t for t in tags])<commit_msg>bug 1330357: Test duplicate profile tags<commit_after>from django.test import TestCase

from taggit.models import Tag
import pytest

from .taggit_extras.models import Food


@pytest.mark.tags
class NamespacedTaggableManagerTest(TestCase):
    food_model = Food

    def assert_tags_equal(self, qs, tags, sort=True, attr="name"):
        got = map(lambda tag: getattr(tag, attr), qs)
        if sort:
            got.sort()
            tags.sort()
        self.assertEqual(got, tags)

    def test_all_ns(self):
        """Tags can be collated or fetched by namespace"""
        apple = self.food_model.objects.create(name="apple")

        expected_tags = {
            u'': [u'foo', u'bar', u'baz'],
            u'int:': [u'int:1', u'int:2'],
            u'string:': [u'string:asdf'],
            u'color:': [u'color:red'],
            u'system:contest:': [u'system:contest:finalist']
        }

        for tags in expected_tags.values():
            apple.tags.add(*tags)

        ns_tags = apple.tags.all_ns()

        expected_ns = expected_tags.keys()
        expected_ns.sort()
        result_ns = ns_tags.keys()
        result_ns.sort()
        self.assertEqual(expected_ns, result_ns)

        for ns in expected_ns:
            self.assert_tags_equal(ns_tags[ns], expected_tags[ns])
            self.assert_tags_equal(apple.tags.all_ns(ns), expected_tags[ns])

    def test_clear_ns(self):
        """Tags can be selectively cleared by namespace"""
        apple = self.food_model.objects.create(name="apple")
        tags_cleared = ['a:1', 'a:2', 'a:3']
        tags_not_cleared = ['1', '2', 'b:1', 'b:2', 'c:1']
        apple.tags.add(*tags_cleared)
        apple.tags.add(*tags_not_cleared)
        apple.tags.clear_ns('a:')
        self.assert_tags_equal(apple.tags.all(), tags_not_cleared)

    def test_set_ns(self):
        """Tags can be selectively set by namespace"""
        apple = self.food_model.objects.create(name="apple")

        tags_before_set = ['a:1', 'a:2', 'a:3']
        tags_after_set = ['a:4', 'a:5', 'a:6']
        tags_not_set = ['1', '2', 'b:1', 'b:2', 'c:1']

        apple.tags.add(*tags_before_set)
        apple.tags.add(*tags_not_set)

        apple.tags.set_ns('a:', *tags_after_set)

        self.assert_tags_equal(apple.tags.all(), tags_after_set + tags_not_set)

    def test_add_ns(self):
        """Tags that do not start with the namespace will be added with the
        namespace tacked on."""
        apple = self.food_model.objects.create(name="apple")

        tags = ['foo', 'bar', 'baz']
        apple.tags.add_ns('a:', *tags)

        self.assert_tags_equal(apple.tags.all(), ['a:%s' % t for t in tags])

    @pytest.mark.xfail
    def test_duplicate_names_to_create(self):
        apple = self.food_model.objects.create(name="apple")
        tags = ['tasty', 'Tasty']
        apple.tags.add_ns('a:', *tags)
        assert apple.tags.count() == 1
        tag = apple.tags.get()
        assert tag.name in ('a:tasty', 'a:Tasty')

    def test_duplicate_names_existing(self):
        apple = self.food_model.objects.create(name="apple")
        Tag.objects.create(name='a:Red')
        Tag.objects.create(name='a:Tasty')
        tags = ['tasty', 'Tasty', 'Red', 'red']
        apple.tags.add_ns('a:', *tags)
        self.assert_tags_equal(apple.tags.all(), ['a:Tasty', 'a:Red'])
{'fraction_non_alphanumeric': 0.15540883697308278, 'fraction_numerical': 0.008464533604198408, 'mean_word_length': 2.653679653679654, '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': '7232969', 'n_tokens_mistral': 2038, 'n_tokens_neox': 1994, 'n_words': 387}