This is a sample for a doctest file
Here you can simply write python expressions
and compare the output results like this:

>>> i=1
>>> print i
1

>>> self.loginAsPortalOwner()

>>> from xm.hitcounter.interfaces import IHitcounter, IHitcountable
>>> from zope.interface import alsoProvides
>>> from zope.publisher.browser import TestRequest
>>> request = TestRequest(HTTP_USER_AGENT='Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/533.1 (KHTML, like Gecko) Chrome/5.0.322.2 Safari/533.1')

Get more rights
>>> self.setRoles(('Manager',))

>>> from xm.hitcounter.core import _hit_manager
>>> _hit_manager.cache_max = 2
>>> import time

Let's see if we can create an POI object
>>> self.portal.invokeFactory('Document', id='ob1', text='foo')
'ob1'

>>> self.portal.invokeFactory('Document', id='ob2', text='foo')
'ob2'

>>> doc2 = getattr(self.portal, 'ob2', None)

Let's use newly added POI object for testing init state of the one
>>> doc = getattr(self.portal, 'ob1', None)
>>> alsoProvides(doc , IHitcountable)
>>> doc.getId()
'ob1'

>>> doc.reindexObject()
>>> IHitcounter(doc).hitCount(request)
1

>>> 'hit_count' in self.portal.portal_catalog.indexes()
True

>>> 'hit_countable' in self.portal.portal_catalog.indexes()
True

>>> doc.reindexObject()
>>> results = self.portal.queryCatalog({"hit_count":'%012d' % 1 })
>>> objects = [ brain.getObject() for brain in results ]
>>> [ x.id for x in objects ]
['ob1']



>>> self.portal.invokeFactory('Document', id='doc1', text='foo')
'doc1'

>>> self.portal.invokeFactory('Document', id='doc2', text='foo')
'doc2'

>>> doc2 = getattr(self.portal, 'doc2', None)
>>> doc = getattr(self.portal, 'doc1', None)
>>> alsoProvides(doc2 , IHitcountable)
>>> doc2.reindexObject()
>>> doc2 = IHitcounter(doc2)
>>> doc2.hitCount(request)
1

>>> doc2.hitCount(request)
2

>>> alsoProvides(doc , IHitcountable)
>>> doc.reindexObject()
>>> doc = IHitcounter(doc)
>>> doc.hitCount(request)
1

>>> doc.hitCount(request)
2

>>> time.sleep(3)
>>> doc.hitCount(request)
3

>>> results = self.portal.queryCatalog( { 'hit_countable':'True', 'sort_on':'hit_count', 'sort_limit':10, 'sort_order':'descending'} )
>>> [ int(x.hit_count) for x in results  ]
[1, 0, 0]

>>> doc.hitCount(request)
4

>>> results = self.portal.queryCatalog( { 'hit_countable':'True', 'sort_on':'hit_count', 'sort_limit':10, 'sort_order':'descending'} )
>>> [ int(x.hit_count) for x in results  ]
[2, 1, 0]


>>> doc.hitCount(request)
5

>>> results = self.portal.queryCatalog( { 'hit_countable':'True', 'sort_on':'hit_count', 'sort_limit':10, 'sort_order':'descending'} )
>>> [ int(x.hit_count) for x in results  ]
[5, 2, 1]


>>> from xm.hitcounter.doc import stress

> stress.test_performance(doc)

>>> doc.getHitCount()
5

-----------------------------------------------------------------------------------------
>>> key = 'location'
>>> from xm.hitcounter.interfaces import IPopularityCounter

> from zope.component import getUtility
> pc = getUtility(IPopularityCounter)

>>> from Products.CMFCore.utils import getToolByName
>>> pc = getToolByName(self.portal, 'portal_hitcounter')

>>> pc.addItem(key, '', 'Limburg (NL)', 'Beitel', 'Zuid-Limburg' )
>>> pc.addItem(key, '', 'Limburg (NL)', 'Beitel', 'Zuid-Limburg' )
>>> pc.addItem(key, '', 'Luik (B)' )
>>> pc.addItem(key, '', 'Luik (B)' )
>>> pc.addItem(key, '', 'Luik (B)' )
>>> pc.addItem(key, '', 'Kaulille', 'Arr. Maaseik', 'Limburg (B)'  )
>>> pc.getMostPopular(key, 2)
[(['', 'Limburg (NL)', 'Beitel', 'Zuid-Limburg'], 2, 130), ...

-----------------------------------------------------------------------------------------
check named hits now
-----------------------------------------------------------------------------------------


>>> self.portal.invokeFactory('Document', id='doc_hc', Title='hc_doc', text='foo')
'doc_hc'

>>> doc = getattr(self.portal, 'doc_hc', None)
>>> alsoProvides(doc , IHitcountable)
>>> doc.reindexObject()

>>> wftool = getToolByName(self.portal, "portal_workflow")
>>> wftool.doActionFor(doc, 'publish')


>>> from Products.Five.testbrowser import Browser
>>> browser = Browser()
>>> browser.handleErrors = False
>>> portal_url = self.portal.absolute_url()

The following is useful when writing and debugging testbrowser tests. It lets
us see all error messages in the error_log.

>>> self.portal.error_log._ignored_exceptions = ()

Let's login as admin
>>> browser.open(portal_url)
>>> browser.getLink('Log in').click()
>>> browser.getControl('Login Name').value = 'admin'
>>> browser.getControl('Password').value = '1'
>>> browser.getControl(name='submit').click()


>>> from Products.PloneTestCase.setup import portal_owner, default_password
>>> browser.open(portal_url+"/doc_hc/hit_image")
>>> browser.contents
 '\x89PNG...'

 >>> browser.open(portal_url+"/doc_hc/hit_image?small=&key=searched")
 >>> browser.contents
  '\x89PNG...'

 >>> browser.open(portal_url+"/doc_hc/hit_image?key=searched")
 >>> browser.contents
  'GIF89a...'

  >>> browser.open(portal_url+"/doc_hc/hit_image?small=0")
  >>> browser.contents
  'GIF89a...'
