<commit_before>from django.contrib.auth.models import User
from tastypie.test import ResourceTestCase
from projects.tests.factories import ProjectFactory
from tools.mongo import MongoFlushMixin
from .. import models


class BaseTaskResourceCase(MongoFlushMixin, ResourceTestCase):
    """Base task resource case"""
    mongo_flush = ['tasks']

    def setUp(self):
        MongoFlushMixin.setUp(self)
        ResourceTestCase.setUp(self)

        ProjectFactory(name='test', is_enabled=True)


class RawTaskResourceCase(BaseTaskResourceCase):
    """Create task case"""

    def setUp(self):
        super(RawTaskResourceCase, self).setUp()
        self.url = '/api/v1/tasks/raw/'

    def test_create_on_post(self):
        """Test create on post"""
        self.api_client.post(self.url, data={
            'service': {
                'name': 'dummy',
            },
            'project': 'test',
            'commit': {
                'branch': 'develop',
                'commit': 'asdfg',
                'author': 'nvbn',
            },
            'violations': [
                {'name': 'dummy', 'raw': '1'},
            ]
        })
        self.assertEqual(1, models.Tasks.count())

    def test_error_on_wrong_service(self):
        """Test error on wrong service"""
        response = self.api_client.post(self.url, data={
            'service': {
                'name': 'dummy!!!',
            },
            'project': 'test',
            'commit': {
                'branch': 'develop',
                'commit': 'asdfg',
                'author': 'nvbn',
            },
            'violations': [
                {'name': 'dummy', 'raw': '1'},
            ]
        })
        self.assertEqual(response.status_code, 404)

    def test_error_on_wrong_project(self):
        """Test error on wrong project"""
        response = self.api_client.post(self.url, data={
            'service': {
                'name': 'dummy',
            },
            'project': 'test!!',
            'commit': {
                'branch': 'develop',
                'commit': 'asdfg',
                'author': 'nvbn',
            },
            'violations': [
                {'name': 'dummy', 'raw': '1'},
            ]
        })
        self.assertEqual(response.status_code, 404)


class TaskResourceCase(BaseTaskResourceCase):
    """Get tasks resource case"""

    def setUp(self):
        super(TaskResourceCase, self).setUp()
        self.url = '/api/v1/tasks/task/'

    def _create_tasks(self, project='test', count=20, **kwargs):
        """Create tasks"""
        models.Tasks.insert([dict(
            service={
                'name': 'dummy',
            },
            project=project,
            commit={
                'branch': 'develop',
                'commit': 'asdfg',
                'author': 'nvbn',
            },
            violations=[{
                'name': 'dummy',
                'raw': '1',
                'status': 1,<commit_msg>Fix tests<commit_after>from django.contrib.auth.models import User
from tastypie.test import ResourceTestCase
from projects.tests.factories import ProjectFactory
from tools.mongo import MongoFlushMixin
from .. import models
from ..const import STATUS_SUCCESS


class BaseTaskResourceCase(MongoFlushMixin, ResourceTestCase):
    """Base task resource case"""
    mongo_flush = ['tasks']

    def setUp(self):
        MongoFlushMixin.setUp(self)
        ResourceTestCase.setUp(self)

        ProjectFactory(name='test', is_enabled=True)


class RawTaskResourceCase(BaseTaskResourceCase):
    """Create task case"""

    def setUp(self):
        super(RawTaskResourceCase, self).setUp()
        self.url = '/api/v1/tasks/raw/'

    def test_create_on_post(self):
        """Test create on post"""
        self.api_client.post(self.url, data={
            'service': {
                'name': 'dummy',
            },
            'project': 'test',
            'commit': {
                'branch': 'develop',
                'commit': 'asdfg',
                'author': 'nvbn',
            },
            'violations': [
                {'name': 'dummy', 'raw': '1'},
            ]
        })
        self.assertEqual(1, models.Tasks.count())

    def test_error_on_wrong_service(self):
        """Test error on wrong service"""
        response = self.api_client.post(self.url, data={
            'service': {
                'name': 'dummy!!!',
            },
            'project': 'test',
            'commit': {
                'branch': 'develop',
                'commit': 'asdfg',
                'author': 'nvbn',
            },
            'violations': [
                {'name': 'dummy', 'raw': '1'},
            ]
        })
        self.assertEqual(response.status_code, 404)

    def test_error_on_wrong_project(self):
        """Test error on wrong project"""
        response = self.api_client.post(self.url, data={
            'service': {
                'name': 'dummy',
            },
            'project': 'test!!',
            'commit': {
                'branch': 'develop',
                'commit': 'asdfg',
                'author': 'nvbn',
            },
            'violations': [
                {'name': 'dummy', 'raw': '1'},
            ]
        })
        self.assertEqual(response.status_code, 404)


class TaskResourceCase(BaseTaskResourceCase):
    """Get tasks resource case"""

    def setUp(self):
        super(TaskResourceCase, self).setUp()
        self.url = '/api/v1/tasks/task/'

    def _create_tasks(self, project='test', count=20, **kwargs):
        """Create tasks"""
        models.Tasks.insert([dict(
            service={
                'name': 'dummy',
            },
            project=project,
            status=STATUS_SUCCESS,
            commit={
                'branch': 'develop',
                'commit': 'asdfg',
                'author': 'nvbn',
            },
            violations=[{
                'name': 'dummy',
                'raw': '1',
                'status': 1,
{'fraction_non_alphanumeric': 0.14523097374543037, 'fraction_numerical': 0.005317381189764041, 'mean_word_length': 1.6135475466782458, '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': '5976277', 'n_tokens_mistral': 1721, 'n_tokens_neox': 1628, 'n_words': 341}