Back to index

moin  1.9.0~rc2
test_Hits.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003     MoinMoin - MoinMoin.macro Hits tested
00004 
00005     @copyright: 2007-2008 MoinMoin:ReimarBauer
00006     @license: GNU GPL, see COPYING for details.
00007 """
00008 import os
00009 
00010 from MoinMoin import caching, macro
00011 from MoinMoin.logfile import eventlog
00012 from MoinMoin.PageEditor import PageEditor
00013 from MoinMoin.Page import Page
00014 from MoinMoin._tests import become_trusted, create_page, make_macro, nuke_eventlog, nuke_page
00015 
00016 class TestHits:
00017     """Hits: testing Hits macro """
00018     pagename = u'AutoCreatedMoinMoinTemporaryTestPageForHits'
00019 
00020     def setup_class(self):
00021         request = self.request
00022         become_trusted(request)
00023         self.page = create_page(request, self.pagename, u"Foo!")
00024         # for that test eventlog needs to be empty
00025         nuke_eventlog(request)
00026         # hits is based on hitcounts which reads the cache
00027         caching.CacheEntry(request, 'charts', 'hitcounts', scope='wiki').remove()
00028 
00029     def teardown_class(self):
00030         nuke_page(self.request, self.pagename)
00031 
00032     def _test_macro(self, name, args):
00033         m = make_macro(self.request, self.page)
00034         return m.execute(name, args)
00035 
00036     def _cleanStats(self):
00037         # cleans all involved cache and log files
00038         nuke_eventlog(self.request)
00039         # hits is based on hitcounts which reads the cache
00040         caching.CacheEntry(self.request, 'charts', 'hitcounts', scope='wiki').remove()
00041         arena = Page(self.request, self.pagename)
00042         caching.CacheEntry(self.request, arena, 'hitcounts', scope='item').remove()
00043 
00044     def testHitsNoArg(self):
00045         """ macro Hits test: 'no args for Hits (Hits is executed on current page) """
00046         # <count> log entries for the current page and one for WikiSandBox simulating viewing
00047         count = 3
00048         eventlog.EventLog(self.request).add(self.request, 'VIEWPAGE', {'pagename': 'WikiSandBox'})
00049         for i in range(count):
00050             eventlog.EventLog(self.request).add(self.request, 'VIEWPAGE', {'pagename': self.pagename})
00051         result = self._test_macro(u'Hits', u'')
00052         self._cleanStats()
00053         assert result == str(count)
00054 
00055     def testHitsForAll(self):
00056         """ macro Hits test: 'all=True' for Hits (all pages are counted for VIEWPAGE) """
00057         # <count> * <num_pages> log entries for simulating viewing
00058         pagenames = ['WikiSandBox', self.pagename]
00059         num_pages = len(pagenames)
00060         count = 2
00061         for i in range(count):
00062             for pagename in pagenames:
00063                 eventlog.EventLog(self.request).add(self.request, 'VIEWPAGE', {'pagename': pagename})
00064         result = self._test_macro(u'Hits', u'all=True')
00065         self._cleanStats()
00066         assert result == str(count * num_pages)
00067 
00068     def testHitsForFilter(self):
00069         """ macro Hits test: 'event_type=SAVEPAGE' for Hits (SAVEPAGE counted for current page)"""
00070         eventlog.EventLog(self.request).add(self.request, 'SAVEPAGE', {'pagename': self.pagename})
00071         # simulate a log entry SAVEPAGE for WikiSandBox to destinguish current page
00072         eventlog.EventLog(self.request).add(self.request, 'SAVEPAGE', {'pagename': 'WikiSandBox'})
00073         result = self._test_macro(u'Hits', u'event_type=SAVEPAGE')
00074         self._cleanStats()
00075         assert result == "1"
00076 
00077     def testHitsForAllAndFilter(self):
00078         """ macro test: 'all=True, event_type=SAVEPAGE' for Hits (all pages are counted for SAVEPAGE)"""
00079         eventlog.EventLog(self.request).add(self.request, 'SAVEPAGE', {'pagename': 'WikiSandBox'})
00080         eventlog.EventLog(self.request).add(self.request, 'SAVEPAGE', {'pagename': self.pagename})
00081         result = self._test_macro(u'Hits', u'all=True, event_type=SAVEPAGE')
00082         self._cleanStats()
00083         assert result == "2"
00084 
00085 coverage_modules = ['MoinMoin.macro.Hits']