<commit_before>from calendar import timegm
from datetime import datetime, date
from time import sleep
import inspect
import jwt
import os

from django.core import management
from django.core.cache import cache
from django.db import models
from django.test import TestCase as BaseTestCase
from django.contrib.contenttypes.models import ContentType
from django.test.client import Client, RequestFactory
from django.core.urlresolvers import reverse
from django.contrib.auth.signals import user_logged_in
import django.contrib.sites.models
from django.contrib.sites.models import Site
from django.utils import timezone
from django.db.models.fields import BigIntegerField, BooleanField, CharField, \
    CommaSeparatedIntegerField, DateField, DateTimeField, DecimalField, \
    EmailField, FilePathField, FloatField, IPAddressField, IntegerField, \
    NullBooleanField, PositiveIntegerField, PositiveSmallIntegerField, \
    SlugField, SmallIntegerField, TextField, AutoField
from django.db.models.fields.files import ImageField
from django.db.models.fields.related import OneToOneField
from django.core.files.base import ContentFile
from django.conf import settings

from preferences import preferences
from category.models import Category, Tag
from jmbo.models import ModelBase
from post.models import Post
from zope.interface import implementedBy

from foundry.models import Member, Listing, Page, Row, Column, Tile, \
    Country, Menu, Navbar, ViewProxy
from foundry import views
from foundry.middleware import AG_TOKEN_PARAMETER_NAME, \
    AG_TOKEN_MAX_TIME_TO_EXPIRY
from foundry.utils import get_preference, generate_random_key
from foundry.templatetags.listing_styles import LISTING_CLASSES


RES_DIR = os.path.join(os.path.dirname(__file__), "res")
IMAGE_PATH = os.path.join(RES_DIR, "image.jpg")


def set_image(obj):
    obj.image.save(
        os.path.basename(IMAGE_PATH),
        ContentFile(open(IMAGE_PATH, "rb").read())
    )


class TestCase(BaseTestCase):

    @classmethod
    def setUpClass(cls):
        cls.request = RequestFactory()
        cls.client = Client()

        # Post-syncdb steps
        management.call_command('load_photosizes', interactive=False)

        # Editor
        cls.editor, dc = Member.objects.get_or_create(
            username='editor',
            email='<EMAIL>'
        )
        cls.editor.set_password("password")
        cls.editor.save()

        # Add an extra site
        site, dc = Site.objects.get_or_create(name='mobi', domain='mobi.com')

        # Categories
        for i in range(1, 5):
            cat, dc = Category.objects.get_or_create(
                title='Category %s' % i, slug='cat%s' % i
            )
            cat.sites = [1]
            cat.save()
            setattr(cls, 'cat%s' % i, cat)

        # Tags
        for i in range(1, 5):
            tag, dc = Tag.objects.get_or_create(
                title='Tag %s' % i, slug='tag%s' % i
            )
            tag.sites = [1]
            tag.save()
            setattr(cls, 'tag%s' % i, tag)

        # Published posts
        for i in range(1, 5):
            post, dc = Post.objects.get_or_create(
                title='Post %s' % i, content='<b>aaa</b>',
                owner=cls.editor, state='published',
            )
            # Toggle between categories and primary category
            if i % 2 == 1:
                post.categories = [getattr(cls, 'cat%s' % i)]
                post.tags = [getattr(cls, 'tag%s' % i)]
            else:
                post.primary_category = getattr(cls, 'cat%s' % i)
            post.sites = [1]
            set_image(post)
            post.save()
            setattr(cls, 'post%s' % i, post)

        # Unpublished posts
        for i in range(5,7):
            post, dc = Post.objects.get_or_create(
                title='Post %s' % i, content='<b>aaa</b>',
                owner=cls.editor, state='unpublished',
            )
            post.sites = [1]
            set_image(post)
            post.save()
            setattr(cls, 'post%s' % i, post)

        # Published galleries
        if "gallery" in settings.INSTALLED_APPS:
            from gallery.models import Gallery
            for i in range(1, 5):
                gallery, dc = Gallery.objects.get_or_create(
                    title='Gallery %s' % i, owner=cls.editor, state='published',
                )
                # Toggle between categories and primary category
                if i % 2 == 1:
                    gallery.categories = [getattr(cls, 'cat%s' % i)]
                    gallery.tags = [getattr(cls, 'tag%s' % i)]<commit_msg>Added primary category test to the correct index<commit_after>from calendar import timegm
from datetime import datetime, date
from time import sleep
import inspect
import jwt
import os

from django.core import management
from django.core.cache import cache
from django.db import models
from django.test import TestCase as BaseTestCase
from django.contrib.contenttypes.models import ContentType
from django.test.client import Client, RequestFactory
from django.core.urlresolvers import reverse
from django.contrib.auth.signals import user_logged_in
import django.contrib.sites.models
from django.contrib.sites.models import Site
from django.utils import timezone
from django.db.models.fields import BigIntegerField, BooleanField, CharField, \
    CommaSeparatedIntegerField, DateField, DateTimeField, DecimalField, \
    EmailField, FilePathField, FloatField, IPAddressField, IntegerField, \
    NullBooleanField, PositiveIntegerField, PositiveSmallIntegerField, \
    SlugField, SmallIntegerField, TextField, AutoField
from django.db.models.fields.files import ImageField
from django.db.models.fields.related import OneToOneField
from django.core.files.base import ContentFile
from django.conf import settings

from preferences import preferences
from category.models import Category, Tag
from jmbo.models import ModelBase
from post.models import Post
from zope.interface import implementedBy

from foundry.models import Member, Listing, Page, Row, Column, Tile, \
    Country, Menu, Navbar, ViewProxy
from foundry import views
from foundry.middleware import AG_TOKEN_PARAMETER_NAME, \
    AG_TOKEN_MAX_TIME_TO_EXPIRY
from foundry.utils import get_preference, generate_random_key
from foundry.templatetags.listing_styles import LISTING_CLASSES


RES_DIR = os.path.join(os.path.dirname(__file__), "res")
IMAGE_PATH = os.path.join(RES_DIR, "image.jpg")


def set_image(obj):
    obj.image.save(
        os.path.basename(IMAGE_PATH),
        ContentFile(open(IMAGE_PATH, "rb").read())
    )


class TestCase(BaseTestCase):

    @classmethod
    def setUpClass(cls):
        cls.request = RequestFactory()
        cls.client = Client()

        # Post-syncdb steps
        management.call_command('load_photosizes', interactive=False)

        # Editor
        cls.editor, dc = Member.objects.get_or_create(
            username='editor',
            email='<EMAIL>'
        )
        cls.editor.set_password("password")
        cls.editor.save()

        # Add an extra site
        site, dc = Site.objects.get_or_create(name='mobi', domain='mobi.com')

        # Categories
        for i in range(1, 5):
            cat, dc = Category.objects.get_or_create(
                title='Category %s' % i, slug='cat%s' % i
            )
            cat.sites = [1]
            cat.save()
            setattr(cls, 'cat%s' % i, cat)

        # Tags
        for i in range(1, 5):
            tag, dc = Tag.objects.get_or_create(
                title='Tag %s' % i, slug='tag%s' % i
            )
            tag.sites = [1]
            tag.save()
            setattr(cls, 'tag%s' % i, tag)

        # Published posts
        for i in range(1, 5):
            post, dc = Post.objects.get_or_create(
                title='Post %s' % i, content='<b>aaa</b>',
                owner=cls.editor, state='published',
            )
            # Toggle between categories and primary category
            if i % 2 == 1:
                post.categories = [getattr(cls, 'cat%s' % i)]
                post.tags = [getattr(cls, 'tag%s' % i)]
            else:
                post.primary_category = getattr(cls, 'cat%s' % i)

            # create primary category on one post with categories
            if i == 1:
                post.primary_category = getattr(cls, 'cat%s' % i)

            post.sites = [1]
            set_image(post)
            post.save()
            setattr(cls, 'post%s' % i, post)

        # Unpublished posts
        for i in range(5,7):
            post, dc = Post.objects.get_or_create(
                title='Post %s' % i, content='<b>aaa</b>',
                owner=cls.editor, state='unpublished',
            )
            post.sites = [1]
            set_image(post)
            post.save()
            setattr(cls, 'post%s' % i, post)

        # Published galleries
        if "gallery" in settings.INSTALLED_APPS:
            from gallery.models import Gallery
            for i in range(1, 5):
                gallery, dc = Gallery.objects.get_or_create(
                    title='Gallery %s' % i, owner=cls.editor, state='published',
                )
                # Toggle between categories and primary category
                if i % 2 == 1:
                    gallery.categories = [getattr(cls, 'cat%s' % i)]
                    gallery.tags = [getattr(cls, 'tag%s' % i)]
{'fraction_non_alphanumeric': 0.0998936170212766, 'fraction_numerical': 0.003936170212765958, 'mean_word_length': 2.3313253012048194, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '781605', 'n_tokens_mistral': 2783, 'n_tokens_neox': 2674, 'n_words': 717}