<commit_before># -*- coding: utf8 -*-
# This file is part of PyBossa.
#
# Copyright (C) 2014 SF Isle of Man Limited
#
# PyBossa is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PyBossa is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with PyBossa.  If not, see <http://www.gnu.org/licenses/>.

from default import Test, db, with_context
from pybossa.cache import apps as cached_apps
from factories import UserFactory, AppFactory, TaskFactory, \
    FeaturedFactory, TaskRunFactory, AnonymousTaskRunFactory
from mock import patch


class TestAppsCache(Test):


    def create_app_with_tasks(self, completed_tasks, ongoing_tasks):
        app = AppFactory.create()
        TaskFactory.create_batch(completed_tasks, state='completed', app=app)
        TaskFactory.create_batch(ongoing_tasks, state='ongoing', app=app)
        return app

    def create_app_with_contributors(self, anonymous, registered,
                                     two_tasks=False, name='my_app', hidden=0):
        app = AppFactory.create(name=name, hidden=hidden)
        task = TaskFactory(app=app)
        if two_tasks:
            task2 = TaskFactory(app=app)
        for i in range(anonymous):
            task_run = AnonymousTaskRunFactory(task=task,
                               user_ip='127.0.0.%s' % i)
            if two_tasks:
                task_run2 = AnonymousTaskRunFactory(task=task2,
                               user_ip='127.0.0.%s' % i)
        for i in range(registered):
            user = UserFactory.create()
            task_run = TaskRunFactory(task=task, user=user)
            if two_tasks:
                task_run2 = TaskRunFactory(task=task2, user=user)
        return app


    def test_get_featured(self):
        """Test CACHE PROJECTS get_featured returns featured projects"""

        FeaturedFactory.create()

        featured = cached_apps.get_featured()

        assert len(featured) is 1, featured


    def test_get_featured_only_returns_featured(self):
        """Test CACHE PROJECTS get_featured returns only featured projects"""

        featured_app = AppFactory.create()
        non_featured_app = AppFactory.create()
        FeaturedFactory.create(app=featured_app)

        featured = cached_apps.get_featured()

        assert len(featured) is 1, featured


    def test_get_featured_not_returns_hidden_apps(self):
        """Test CACHE PROJECTS get_featured does not return hidden projects"""

        featured_app = AppFactory.create(hidden=1)
        FeaturedFactory.create(app=featured_app)

        featured = cached_apps.get_featured()

        assert len(featured) is 0, featured


    def test_get_featured_returns_required_fields(self):
        """Test CACHE PROJECTS get_featured returns the required info
        about each featured project"""

        fields = ('id', 'name', 'short_name', 'info', 'created', 'description',
                  'overall_progress', 'last_activity', 'last_activity_raw',
                  'owner', 'info')

        FeaturedFactory.create()

        featured = cached_apps.get_featured()[0]

        for field in fields:
            assert featured.has_key(field), "%s not in app info" % field


    def test_get_top_returns_apps_with_most_taskruns(self):
        """Test CACHE PROJECTS get_top returns the projects with most taskruns in order"""

        ranked_3_app = self.create_app_with_contributors(8, 0, name='three')
        ranked_2_app = self.create_app_with_contributors(9, 0, name='two')
        ranked_1_app = self.create_app_with_contributors(10, 0, name='one')
        ranked_4_app = self.create_app_with_contributors(7, 0, name='four')

        top_apps = cached_apps.get_top()

        assert top_apps[0]['name'] == 'one', top_apps
        assert top_apps[1]['name'] == 'two', top_apps
        assert top_apps[2]['name'] == 'three', top_apps
        assert top_apps[3]['name'] == 'four', top_apps


    def test_get_top_respects_limit(self):
        """Test CACHE PROJECTS get_top returns only the top n projects"""

        ranked_3_app = self.create_app_with_contributors(8, 0, name='three')
        ranked_2_app = self.create_app_with_contributors(9, 0, name='two')
        ranked_1_app = self.create_app_with_contributors(10, 0, name='one')<commit_msg>Added tests for get function in cached apps<commit_after># -*- coding: utf8 -*-
# This file is part of PyBossa.
#
# Copyright (C) 2014 SF Isle of Man Limited
#
# PyBossa is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PyBossa is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with PyBossa.  If not, see <http://www.gnu.org/licenses/>.

from default import Test, db, with_context
from pybossa.cache import apps as cached_apps
from factories import UserFactory, AppFactory, TaskFactory, \
    FeaturedFactory, TaskRunFactory, AnonymousTaskRunFactory
from mock import patch


class TestAppsCache(Test):


    def create_app_with_tasks(self, completed_tasks, ongoing_tasks):
        app = AppFactory.create()
        TaskFactory.create_batch(completed_tasks, state='completed', app=app)
        TaskFactory.create_batch(ongoing_tasks, state='ongoing', app=app)
        return app

    def create_app_with_contributors(self, anonymous, registered,
                                     two_tasks=False, name='my_app', hidden=0):
        app = AppFactory.create(name=name, hidden=hidden)
        task = TaskFactory(app=app)
        if two_tasks:
            task2 = TaskFactory(app=app)
        for i in range(anonymous):
            task_run = AnonymousTaskRunFactory(task=task,
                               user_ip='127.0.0.%s' % i)
            if two_tasks:
                task_run2 = AnonymousTaskRunFactory(task=task2,
                               user_ip='127.0.0.%s' % i)
        for i in range(registered):
            user = UserFactory.create()
            task_run = TaskRunFactory(task=task, user=user)
            if two_tasks:
                task_run2 = TaskRunFactory(task=task2, user=user)
        return app


    def test_get_featured(self):
        """Test CACHE PROJECTS get_featured returns featured projects"""

        FeaturedFactory.create()

        featured = cached_apps.get_featured()

        assert len(featured) is 1, featured


    def test_get_featured_only_returns_featured(self):
        """Test CACHE PROJECTS get_featured returns only featured projects"""

        featured_app = AppFactory.create()
        non_featured_app = AppFactory.create()
        FeaturedFactory.create(app=featured_app)

        featured = cached_apps.get_featured()

        assert len(featured) is 1, featured


    def test_get_featured_not_returns_hidden_apps(self):
        """Test CACHE PROJECTS get_featured does not return hidden projects"""

        featured_app = AppFactory.create(hidden=1)
        FeaturedFactory.create(app=featured_app)

        featured = cached_apps.get_featured()

        assert len(featured) is 0, featured


    def test_get_featured_returns_required_fields(self):
        """Test CACHE PROJECTS get_featured returns the required info
        about each featured project"""

        fields = ('id', 'name', 'short_name', 'info', 'created', 'description',
                  'last_activity', 'last_activity_raw', 'overall_progress',
                   'n_tasks', 'n_volunteers', 'owner', 'info')

        FeaturedFactory.create()

        featured = cached_apps.get_featured()[0]

        for field in fields:
            assert featured.has_key(field), "%s not in app info" % field


    def test_get_category(self):
        """Test CACHE PROJECTS get returns projects from given category"""

        project = self.create_app_with_tasks(1, 0)

        projects = cached_apps.get(project.category.short_name)

        assert len(projects) is 1, projects


    def test_get_only_returns_category_projects(self):
        """Test CACHE PROJECTS get returns only projects from required category"""

        project = self.create_app_with_tasks(1, 0)
        #create a non published project too
        AppFactory.create()

        projects = cached_apps.get(project.category.short_name)

        assert len(projects) is 1, projects


    def test_get_not_returns_hidden_apps(self):
        """Test CACHE PROJECTS get does not return hidden projects"""

        project = self.create_app_with_contributors(1, 0, hidden=1)

        projects = cached_apps.get(project.category.short_name)

        assert len(projects) is 0, projects


    def test_get_not_returns_draft_apps(self):
        """Test CACHE PROJECTS get does not return draft (non-published) projects"""

        project = self.create_app_with_contributors(1, 0)
        # Create a project wothout presenter
        AppFactory.create(info={}, category=project.category)

        projects = cached_apps.get(project.category.short_name)

        assert len(projects) is 1, projects


    def test_get_returns_required_fields(self):
        """Test CACHE PROJECTS get returns the required info
        about each project"""

        fields = ('id', 'name', 'short_name', 'info', 'created', 'description',
                  'last_activity', 'last_activity_raw', 'overall_progress',
                   'n_tasks', 'n_volunteers', 'owner', 'info')

        FeaturedFactory.create()

        featured = cached_apps.get_featured()[0]

        for field in fields:
            assert featured.has_key(field), "%s not in app info" % field


    def test_get_top_returns_apps_with_most_taskruns(self):
        """Test CACHE PROJECTS get_top returns the projects with most taskruns in order"""

        ranked_3_app = self.create_app_with_contributors(8, 0, name='three')
        ranked_2_app = self.create_app_with_contributors(9, 0, name='two')
        ranked_1_app = self.create_app_with_contributors(10, 0, name='one')
        ranked_4_app = self.create_app_with_contributors(7, 0, name='four')

        top_apps = cached_apps.get_top()

        assert top_apps[0]['name'] == 'one', top_apps
        assert top_apps[1]['name'] == 'two', top_apps
        assert top_apps[2]['name'] == 'three', top_apps
        assert top_apps[3]['name'] == 'four', top_apps


    def test_get_top_respects_limit(self):
        """Test CACHE PROJECTS get_top returns only the top n projects"""

        ranked_3_app = self.create_app_with_contributors(8, 0, name='three')
        ranked_2_app = self.create_app_with_contributors(9, 0, name='two')
        ranked_1_app = self.create_app_with_contributors(10, 0, name='one')
{'fraction_non_alphanumeric': 0.09260399261278691, 'fraction_numerical': 0.010729047577169994, 'mean_word_length': 3.0440967283072546, 'pattern_counts': {'":': 0, '<': 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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1990018', 'n_tokens_mistral': 3422, 'n_tokens_neox': 3244, 'n_words': 903}