TestPickleCache.py :  » Web-Frameworks » Webware » Webware-1.0.2 » MiscUtils » Tests » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Frameworks » Webware 
Webware » Webware 1.0.2 » MiscUtils » Tests » TestPickleCache.py
import os
import time
import unittest

import FixPath
from MiscUtils.PickleCache import *

# the directory that this file is in:
progPath = os.path.join(os.getcwd(), __file__)
progDir = os.path.dirname(progPath)
assert os.path.basename(progDir) == 'Tests' and \
  os.path.basename(os.path.dirname(progDir)) == 'MiscUtils', \
  'Test needs to run in Tests/MiscUtils.'


class TestPickleCache(unittest.TestCase):

  def test(self):
    # print 'Testing PickleCache...'
    iterations = 2
    for iter in range(iterations):
      # print 'Iteration', iter + 1
      self.oneIterTest()
    # print 'Success.'

  def oneIterTest(self):
    sourcePath = self._sourcePath = os.path.join(progDir, 'foo.dict')
    picklePath = self._picklePath = PickleCache().picklePath(sourcePath)
    self.remove(picklePath) # make sure we're clean
    data = self._data = {'x': 1}
    self.writeSource()
    try:
      # test 1: no pickle cache yet
      assert readPickleCache(sourcePath) is None
      self.writePickle()
      if havePython22OrGreater:
        # test 2: correctness
        assert readPickleCache(sourcePath) == data, \
          repr(readPickleCache(sourcePath))
        # test 3: wrong pickle version
        assert readPickleCache(sourcePath, pickleVersion=2) is None
        self.writePickle() # restore
        # test 4: wrong data source
        assert readPickleCache(sourcePath, source='notTest') is None
        self.writePickle() # restore
        # test 5: wrong Python version
        try:
          saveVersion = sys.version_info
          sys.version_info = (sys.version_info[0] + 1,) \
            + sys.version_info[1:]
          assert readPickleCache(sourcePath) is None
          self.writePickle() # restore
        finally:
          sys.version_info = saveVersion
        # test 6: source is newer
        self.remove(picklePath)
        self.writePickle()
        # we have to allow for the granularity of getmtime()
        # (see the comment in the docstring of PickleCache.py)
        time.sleep(2)
        self.writeSource()
        assert readPickleCache(sourcePath) is None
        self.writePickle() # restore
    finally:
      self.remove(sourcePath)
      self.remove(picklePath)

  def remove(self, filename):
    try:
      os.remove(filename)
    except OSError:
      pass

  def writeSource(self):
    f = open(self._sourcePath, 'w')
    f.write(str(self._data))
    f.close()

  def writePickle(self):
    assert not os.path.exists(self._picklePath)
    writePickleCache(self._data, self._sourcePath, pickleVersion=1, source='test')
    if havePython22OrGreater:
      assert os.path.exists(self._picklePath)


if __name__ == '__main__':
  unittest.main()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.