helpers.py :  » Blog » PyBlosxom » pyblosxom-1.5-rc1 » Pyblosxom » 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 » Blog » PyBlosxom 
PyBlosxom » pyblosxom 1.5 rc1 » Pyblosxom » tests » helpers.py
import os
import os.path
import shutil
import tempfile
import StringIO
import unittest

from Pyblosxom.pyblosxom import Request
from Pyblosxom import tools

def req_():
    return Request({}, {}, {})

class UnitTestBase(unittest.TestCase):
    def setUp(self):
        self._tempdir = None

    def tearDown(self):
        if self._tempdir:
            try:
                shutil.rmtree(self._tempdir)
            except OSError:
                pass

    def eq_(self, a, b, text=None):
        self.assertEquals(a, b, text)

    def get_temp_dir(self):
        if self._tempdir == None:
            self._tempdir = tempfile.mkdtemp()
        return self._tempdir

    def setup_files(self, files):
        # sort so that we're building the directories in order
        files.sort()

        tempdir = self.get_temp_dir()
        os.makedirs(os.path.join(tempdir, "entries"))

        for fn in files:
            d, f = os.path.split(fn)

            try:
                os.makedirs(d)
            except OSError, e:
                pass

            if f:
                f = open(fn, "w")
                f.write("test file: %s\n" % fn)
                f.close()
            
    def build_file_set(self, filelist):
        return [os.path.join(self.get_temp_dir(), "entries/%s" % fn)
                for fn in filelist]

    def build_request(self, cfg=None, http=None, data=None, inputstream=""):
        """
        process_path_info uses:
        - req.pyhttp["PATH_INFO"]         - string

        - req.config["default_flavour"]   - string
        - req.config["datadir"]           - string
        - req.config["blog_title"]        - string
        - req.config["base_url"]          - string

        - req.data["extensions"]          - dict of string -> func

        if using req.getForm():
        - req.pyhttp["wsgi.input"]        - StringIO instance
        - req.pyhttp["REQUEST_METHOD"]    - GET or POST
        - req.pyhttp["CONTENT_LENGTH"]    - integer
        """
        _config = {"default_flavour": "html",
                   "datadir": os.path.join(self.get_temp_dir(), "entries"),
                   "blog_title": "Joe's blog",
                   "base_url": "http://www.example.com/"}
        if cfg:
            _config.update(cfg)

        _data = {"extensions": {"txt": 0}}
        if data:
            _data.update(data)

        _http = {"wsgi.input": StringIO.StringIO(inputstream),
                 "REQUEST_METHOD": len(inputstream) and "GET" or "POST",
                 "CONTENT_LENGTH": len(inputstream)}
        if http: _http.update(http)

        return Request(_config, _http, _data)
        
    def test_setup_teardown(self):
        fileset1 = self.build_file_set(["file.txt",
                                        "cata/file.txt",
                                        "cata/subcatb/file.txt"])

        self.setup_files(fileset1)
        try:
            for mem in fileset1:
                assert os.path.isfile(mem)

        finally:
            self.tearDown()

        for mem in fileset1:
            assert not os.path.isfile(mem)

    def cmpdict(self, expected, actual):
        """expected <= actual
        """
        for mem in expected.keys():
            if mem in actual:
                assert expected[mem] == actual[mem]
            else:
                assert False

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.