<commit_before>import django.test
import urllib2
from urllib import urlencode
from django.test.client import Client
from django.contrib.auth.models import User, Group
from django.contrib.auth import REDIRECT_FIELD_NAME
from django.contrib.admin.sites import LOGIN_FORM_KEY
from metashare.settings import DJANGO_BASE, DJANGO_URL, ROOT_PATH
from metashare.stats.model_utils import _update_usage_stats, saveLRStats, getLRLast, saveQueryStats, getLastQuery, UPDATE_STAT, VIEW_STAT, RETRIEVE_STAT, DOWNLOAD_STAT
from metashare.repository.models import *

ADMINROOT = '/{0}editor/'.format(DJANGO_BASE)
TESTFIXTURES_ZIP = '{}/repository/fixtures/tworesources.zip'.format(ROOT_PATH)
PSP_XML = '{}/repository/test_fixtures/PSP/UIB-M10-9_v2.xml'.format(ROOT_PATH)

class StatsTest(django.test.TestCase):
    editor_login = None
    
    def setUp(self):
        """
        Sets up some resources with which to test.
        """
        self.stats_server_url = "http://metastats.fbk.eu/"
        editoruser = User.objects.create_user('editoruser', '<EMAIL>',
          'secret')
        editoruser.is_staff = True
        globaleditors = Group.objects.get(name='globaleditors')
        editoruser.groups.add(globaleditors)
        editoruser.save()
        
        StatsTest.editor_login = {
            REDIRECT_FIELD_NAME: ADMINROOT,
            LOGIN_FORM_KEY: 1,
            'username': 'editoruser',
            'password': '<PASSWORD>',
        }
        
    
    def testStatActions(self):
        """
        Testing statistics functions about LR
        """
        client = self.client_with_user_logged_in(StatsTest.editor_login)
        xmlfile = open(TESTFIXTURES_ZIP, 'rb')
        response = client.post(ADMINROOT+'upload_xml/', {'description': xmlfile, 'uploadTerms':'on' }, follow=True)
        # And verify that we have more than zero resources on the page where we
        # are being redirected:
        self.assertContains(response, "Editable Resources")
        self.assertNotContains(response, '0 Resources')
        
        statsdata = getLRLast(UPDATE_STAT, 2)
        self.assertEqual(len(statsdata), 2)
        
        for action in (VIEW_STAT, RETRIEVE_STAT, DOWNLOAD_STAT):
            for item in statsdata:
                resource =  resourceInfoType_model.objects.get(storage_object__identifier=item['lrid'])
                saveLRStats(resource, "anonymous", "", action)
            self.assertEqual(len(getLRLast(action, 10)), 2)
        
    def testTop10(self):
        """
        Tries to load the top 10 page of the META-SHARE website
        """
        client = Client()
        response = client.get('/{0}stats/top/'.format(DJANGO_BASE))<commit_msg>restored code for "my resources"<commit_after>import django.test
import urllib2
from urllib import urlencode
from django.test.client import Client
from django.contrib.auth.models import User, Group
from django.contrib.auth import REDIRECT_FIELD_NAME
from django.contrib.admin.sites import LOGIN_FORM_KEY
from metashare.settings import DJANGO_BASE, DJANGO_URL, ROOT_PATH
from metashare.stats.model_utils import _update_usage_stats, saveLRStats, getLRLast, saveQueryStats, getLastQuery, UPDATE_STAT, VIEW_STAT, RETRIEVE_STAT, DOWNLOAD_STAT
from metashare.repository.models import *

ADMINROOT = '/{0}editor/'.format(DJANGO_BASE)
TESTFIXTURES_ZIP = '{}/repository/fixtures/tworesources.zip'.format(ROOT_PATH)
PSP_XML = '{}/repository/test_fixtures/PSP/UIB-M10-9_v2.xml'.format(ROOT_PATH)

class StatsTest(django.test.TestCase):
    editor_login = None
    
    def setUp(self):
        """
        Sets up some resources with which to test.
        """
        self.stats_server_url = "http://metastats.fbk.eu/"
        editoruser = User.objects.create_user('editoruser', '<EMAIL>',
          '<PASSWORD>')
        editoruser.is_staff = True
        globaleditors = Group.objects.get(name='globaleditors')
        editoruser.groups.add(globaleditors)
        editoruser.save()
        
        StatsTest.editor_login = {
            REDIRECT_FIELD_NAME: ADMINROOT,
            LOGIN_FORM_KEY: 1,
            'username': 'editoruser',
            'password': '<PASSWORD>',
        }
        
    
    def testStatActions(self):
        """
        Testing statistics functions about LR
        """
        client = self.client_with_user_logged_in(StatsTest.editor_login)
        xmlfile = open(TESTFIXTURES_ZIP, 'rb')
        response = client.post(ADMINROOT+'upload_xml/', {'description': xmlfile, 'uploadTerms':'on' }, follow=True)
        # And verify that we have more than zero resources on the page where we
        # are being redirected:
        self.assertContains(response, "My Resources")
        self.assertNotContains(response, '0 Resources')
        
        statsdata = getLRLast(UPDATE_STAT, 2)
        self.assertEqual(len(statsdata), 2)
        
        for action in (VIEW_STAT, RETRIEVE_STAT, DOWNLOAD_STAT):
            for item in statsdata:
                resource =  resourceInfoType_model.objects.get(storage_object__identifier=item['lrid'])
                saveLRStats(resource, "anonymous", "", action)
            self.assertEqual(len(getLRLast(action, 10)), 2)
        
    def testTop10(self):
        """
        Tries to load the top 10 page of the META-SHARE website
        """
        client = Client()
        response = client.get('/{0}stats/top/'.format(DJANGO_BASE))
{'fraction_non_alphanumeric': 0.10321143493595693, 'fraction_numerical': 0.006682754780025988, 'mean_word_length': 2.935719503287071, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, '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': '7357466', 'n_tokens_mistral': 1669, 'n_tokens_neox': 1612, 'n_words': 339}