Changeset 120

Show
Ignore:
Timestamp:
Fri Sep 16 17:42:40 2005
Author:
drew
Message:

more config-related unit tests

Files:

Legend:

Unmodified
Added
Removed
Modified
  • trunk/tests/doctests/utils/__init__.py

    r112 r120  
    1 1 import config  
      2 import python  
     
  • trunk/tests/doctests/utils/config.py

    r116 r120  
    6 6 __version__ = '$Revision$'[11:-2]  
    7 7  
    8   from xix.utils.config import ConfigLoader  
      8 from xix.utils.config import Config, ConfigLoader, ConfigFactory, configFactory  
    8 8 from xix.utils.config import ModuleWrapper  
    9 9  
     
    18 18  
    19 19 class ConfigLoaderTest:  
    20       def example1(self):  
      20     def example_noDuplicateEntries(self):  
    20 20         '''  
    21 21         Duplicate entries are strictly not allowed:  
     
    31 31         ConfigLoaderException: Duplicate entry foo.bar  
    32 32         '''  
    33       def example2(self):  
      33     def example_cannotOverrideTerminalNodes(self):  
    33 33         '''  
    34           Cannot extent terminal nodes:  
      34         Cannot override terminal nodes:  
    34 34  
    35 35         >>> fd = File("""  
     
    44 44         ConfigLoaderException: Duplicate entry foo.bar  
    45 45         '''  
    46       def example3():  
      46     def example_wellFormedFiles():  
    46 46         '''  
    47 47         Configuration files must be well formed:  
     
    54 54         ConfigLoaderException: Malformed line found in configuration: crap here  
    55 55         '''  
    56       def example3(self):  
      56     def example_validNodeName(self):  
    56 56         '''  
    57 57         Configuration nodes must be valid python names:  
     
    64 64         ConfigLoaderException: Config node 123 does not map to valid Python name  
    65 65         '''  
    66       def example4(self):  
      66     def example_valueImplicitPythonSyntax(self):  
    66 66         '''  
    67 67         Type values are implicit by given python syntax:  
     
    73 73         <type 'list'>  
    74 74         '''  
    75       def example5(self):  
      75     def example_mustIndicateStringsWithQuotes(self):  
    75 75         '''  
    76 76         Strings MUST be indicated with quotes (single or double):  
     
    83 83         ConfigLoaderException: Cannot resolve type : not good dude.  
    84 84         '''  
    85       def example6(self):  
      85     def example_someTypesNotSupported(self):  
    85 85         '''  
    86 86         Not all types are supported:  
     
    93 93         ConfigLoaderException: Unsupported type: <type 'type'>.  
    94 94         '''  
    95       def example6(self):  
      95     def example_moduleWrapper(self):  
    95 95         '''  
    96 96         We can also get a module wrapper ...  
     
    104 104         '''  
    105 105  
    106   class ModuleWrapperTest:  
    107       def importAll(self):  
    108           '''Example Usage:  
    109    
    110           >>> "uppercase" not in locals()  
    111           True  
    112           >>> wrapper = ModuleWrapper("string")  
    113           >>> wrapper.importAll()  
    114           >>> print uppercase  
    115           ABCDEFGHIJKLMNOPQRSTUVWXYZ  
    116           '''  
    117       def importModule(self):  
    118           '''Example Usage:  
    119    
    120           >>> "sre" not in locals()  
    121           True  
    122           >>> wrapper = ModuleWrapper("sre")  
    123           >>> wrapper.importModule()  
    124           >>> type(sre)  
    125           <type 'module'>  
    126           '''  
    127       def importNames(self, *names):  
    128           '''Example Usage:  
    129    
    130           >>> "uppercase" not in locals() and "upper" not in locals()  
    131           True  
    132           >>> wrapper = ModuleWrapper("string")  
    133           >>> wrapper.importNames("uppercase", "upper")  
    134           >>> print uppercase  
    135           ABCDEFGHIJKLMNOPQRSTUVWXYZ  
    136           >>> print upper("lower")  
    137           LOWER  
      106  
      107 def _resetConfigFactory():  
      108     try: del configFactory.resources["test.cfg"]  
      109     except: pass  
      110  
      111 class ConfigFactoryTest:  
      112     '''ConfigFactory unit tests  
      113     '''  
      114  
      115     def verifySingletonInstance(self):  
      116         '''Let's make sure these tests are valid - i.e. configFactory  
      117         is an instance of ConfigFactory:  
      118  
      119         >>> configFactory.__class__ == ConfigFactory  
      120         True  
      121         '''  
      122  
      123     def getConfig(self):  
      124         '''Example usage of config API.  You should make a __appcfg__.py  
      125         module for you package which does all the addResource shit ...  
      126  
      127         >>> _resetConfigFactory()  
      128         >>> from xix.utils.python import fileHere  
      129         >>> configFactory.addResource("test.cfg", fileHere("__testcfg__.cfg"))  
      130         >>> cfg = configFactory.getConfig("test.cfg")  
      131         >>> cfg.my.little.test  
      132         'Hello World'  
      133         '''  
      134  
      135     def reloadConfig(self):  
      136         '''Reloading a config forces the config to be loaded again into memory.  
      137         This will reload from file of course, which means any references to config  
      138         object you have elsewhere may be out of sync with what's freshly loaded.  
      139  
      140         >>> _resetConfigFactory()  
      141         >>> from xix.utils.python import fileHere  
      142         >>> configFactory.addResource("test.cfg", fileHere("__testcfg__.cfg"))  
      143         >>> cfg = configFactory.getConfig("test.cfg")  
      144         >>> cfg.my.little.test = "Shit Mate"  
      145         >>> cfg.my.little.test  
      146         'Shit Mate'  
      147         >>> cfg = configFactory.reloadConfig("test.cfg")  
      148         >>> cfg.my.little.test  
      149         'Hello World'  
      150         >>> cfg = configFactory.getConfig("test.cfg")  
      151         >>> cfg.my.little.test  
      152         'Hello World'  
      153         '''  
      154  
      155     def addResource_url(self):  
      156         '''Adding a reource ... This is demonstrated well in other tests, but here  
      157         goes.  
      158  
      159         >>> _resetConfigFactory()  
      160         >>> from xix.utils.python import fileHere  
      161         >>> from xix.utils.interfaces import IConfig  
      162         >>> configFactory.addResource("test.cfg", fileHere("__testcfg__.cfg"))  
      163         >>> cfg = configFactory.getConfig("test.cfg")  
      164         >>> IConfig.providedBy(cfg)  
      165         True  
      166         '''  
      167      
      168     def addResource_config(self):  
      169         '''Using config keyword argument:  
      170  
      171         >>> _resetConfigFactory()  
      172         >>> from xix.utils.python import fileHere  
      173         >>> from xix.utils.interfaces import IConfig  
      174         >>> cfg = Config({"blue":3.14, "red":21})  
      175         >>> configFactory.addResource("test.cfg", config=cfg)  
      176         >>> "test.cfg" in configFactory.loaded  
      177         True  
      178         >>> cfg = configFactory.getConfig("test.cfg")  
      179         >>> IConfig.providedBy(cfg)  
      180         True  
      181         '''  
      182  
      183     def addResource_urlAndConfig(self):  
      184         '''Using url and config keyword arguments together:  
      185  
      186         >>> _resetConfigFactory()  
      187         >>> from xix.utils.python import fileHere  
      188         >>> from xix.utils.interfaces import IConfig  
      189         >>> cfg = Config({"blue":3.14, "red":21})  
      190         >>> configFactory.addResource("test.cfg", url=fileHere("__testcfg__.cfg"), config=cfg)  
      191         >>> "test.cfg" in configFactory.loaded  
      192         True  
      193         >>> cfg = configFactory.getConfig("test.cfg")  
      194         >>> IConfig.providedBy(cfg)  
      195         True  
      196         >>> print cfg.blue  
      197         3.14  
      198         >>> print cfg.red  
      199         21  
      200         >>> cfg = configFactory.reloadConfig("test.cfg")  
      201         >>> cfg.my.little.test  
      202         'Hello World'  
    138 203         '''  
    139 204  
  • trunk/xix/__init__.py

    r112 r120  
    37 37 import inspect, sys  
    38 38  
    39   import _io  
    40 39 import os  
    41 40 from os.path import join as joinpath  
      41 import __xixcfg__  
      42 import _io  
    42 43  
    43 44 ##########################################################################  
  • trunk/xix/rend/container.py

    r112 r120  
    3 3 '''  
    4 4  
    5   from zope.interface import implements, directlyProvides  
      5 from xix.utils.comp.interface import implements, directlyProvides  
    5 5 #from xix.rend.pagelets import Pagelet, IPagelet, IDocFactory  
    6 6 from xix.rend.pagelets import Pagelet  
  • trunk/xix/utils/rules.py

    r119 r120  
    7 7 '''  
    8 8  
      9 from xix.utils.interfaces import IRule, IRuleChain  
    9 10 from xix.utils.python import allexcept  
    10 11 from xix.utils.config import configFactory  
      12 from xix.utils.comp.interface import implements  
    11 13  
    12 14 __author__    = 'Drew Smathers <drew.smathers@gmail.com>'  
     
    14 16 __copyright__ = 'Copyright (C) 2005, Drew Smathers'  
    15 17  
    16   _rulescfg = configFactory.getConfig()  
    17 18  
      19 _rulescfg = configFactory.getConfig()  
      20      
    18 21 # Our engine registry  
    19   engineRegistry = _rulescfg.rules.engineRegistry  
      22 engineRegistry = _rulescfg.engineRegistry  
      23  
    20 24  
    21 25 class RulesException(Exception):  
     
    25 29     pass  
    26 30  
      31 class Rule:     
      32     implements(IRule)  
      33      
      34     def __call__(self, *pargs, **kwargs):  
      35         '''Example:  
      36  
      37         >>> rule  = Rule()  
      38         >>> rule("the world")  
      39         True  
      40         '''  
      41         return True  
      42  
      43 class RuleChain:  
      44     implements(IRuleChain)  
      45  
      46     def __init__(self, rules=None):  
      47         self.rules = rules or []  
      48  
      49     def __call__(self, *pargs, **kwargs):  
      50         '''Example:  
      51  
      52         >>> class MyRule(Rule):  
      53         ...     def __call__(self, *pargs, **kwargs):  
      54         ...         return False  
      55         ...  
      56         >>> rule1, rule2 = Rule(), MyRule()  
      57         >>> chain = RuleChain([rule1, rule2])  
      58         >>> chain("me up an beat me with a shoe")  
      59         False  
      60         '''  
      61         result = True  
      62         for rule in self.rules:  
      63             result &= rule(*pargs, **kwargs)  
      64             if not result: break # short-circuit  
      65         return result  
      66  
    27 67 def registerEngine(engineName, rulesEgine):  
    28 68     if engineName in engineRegistry:  
    29 69         raise RulesEngineAlreadyRegisteredException, \  
    30 70             'Engine with name %s already registered' % engineName  
    31      engineRegistry[engineName] = rulesEngine  
      71     engineRegistry[engineName] = rulesEngine  
    31 71  
    32 72  
      73 __all__ = allexcept('IRule', 'IRuleChain', 'allexcept', 'configFactory', 'implements')     
  • trunk/xix/utils/__init__.py

    r112 r120  
    1   '''  
    2    
    3   The Kitchen Sink  
      1 '''The Kitchen Sink  
    4 2  
    5 3 $License$  
     
    8 6 '''  
    9 7  
    10   __all__ = ['aspout', 'source', 'console', 'config']  
      8 __all__ = ['aspout', 'source', 'console', 'config', 'rules']  
    10 8  
    11 9 import xix.utils.string  
    12 10 import xix.utils.config  
      11 #import xix.utils.rules # we can't load this here due to config setup dependency  
      12  
      13 __author__    = 'Drew Smathers <drew.smathers@gmail.com>'  
      14 __version__   = '$Revision$'[11:-2]  
      15 __copyright__ = 'Copyright (C) 2005, Drew Smathers'  
      16 __license__   = 'MIT'  
      17  
  • trunk/xix/utils/interfaces.py

    r116 r120  
    115 115         '''Import all visible names from module into caller's global namespace.  
    116 116         '''  
      117  
      118 ##########################################################################  
      119 # Rule interfaces  
      120 ##########################################################################  
      121  
      122 class IRule(Interface):  
      123     '''Rule interface.  A rule is closure which performs validation based  
      124     on arguments supplied.  A rule may be statefule.  
      125     '''  
      126  
      127     def __call__(*pargs, **kwargs):  
      128         ''' Rules return True if validation based on arguments/state passes, False otherwise.  
      129          
      130         @param pargs positional arguments for validation  
      131         @param kwargs keyword arguments for validation  
      132         @return True if all rules pass, False otherwise  
      133         '''  
      134 class IRuleChain(Interface):  
      135     '''RuleChain interface.  A rule chain acts as an aggregation of several rules.  
      136     RuleChains pass only if all rules in the chain pass.  
      137     '''  
      138  
      139     rules = Attribute('''Chain of rules.''')  
      140  
      141     def __call__(*pargs, **kwargs):  
      142         '''Validate rules.  Return True if all rules pass, false otherwise.  
      143  
      144         @param pargs positional arguments for validation  
      145         @param kwargs keyword arguments for validation  
      146         @return True if all rules pass, False otherwise  
      147         '''  
      148          
  • trunk/xix/utils/config.py

    r117 r120  
    222 222         self.loader = loader or ConfigLoader  
    223 223         self.resources = {}  
    224           self.cache = {}  
      224         self.loaded = {}  
    224 224  
    225       def getConfig(self, name):  
    226           if name in self.cache:  
    227               return self.cache[name]  
    228           self.cache[name] = self.loader.load(open(self.resources[name]))  
    229           return self.cache[name]  
      225     def getConfig(self, name=None):  
      226         if name is None: name = getCallersGlobals([__name__])['__name__']  
      227         if name in self.loaded: return self.loaded[name]  
      228         self.loaded[name] = self.loader.load(open(self.resources[name]))  
      229         return self.loaded[name]  
      230  
      231     def reloadConfig(self, name):  
      232         assert name in self.resources, 'Must have resource URL for reload'  
      233         del self.loaded[name]  
      234         return self.getConfig(name)  
    230 235  
    231       def addResource(self, name, url):  
      236     def addResource(self, name, url=None, config=None):  
    231 236         if name in self.resources:  
    232 237             raise ConfigLoaderException, 'Resource with name %s already registered' % name  
    233           self.resources[name] = url  
      238         if url: self.resources[name] = url  
      239         if config: self.loaded[name] = config  
    234 240  
    235 241  
  • trunk/xix/iserver.py

    r68 r120  
    25 25 # $Id$  
    26 26  
    27   from zope.interface import Interface  
      27 from xix.utils.comp.interface import Interface  
    27 27  
    28 28 __author__ = 'Drew Smathers'  
  • trunk/runtests.py

    r112 r120  
    12 12 # setup path for for tests  
    13 13 os.environ['XIXSITEPATH'] = os.path.abspath('xix/tests/data/xixpage'.replace('/',os.sep))  
    14   from xix import *  
    15   from tests import doctests as dt  
      14  
      15 import xix.interfaces  
      16 import xix.registry  
      17 import xix.rend.pagelets  
      18 import xix.rend.container  
      19 import xix.utils.console  
      20 import xix.utils.string  
      21 import xix.utils.rules  
      22 import xix.xixxml.xmlbuilder  
      23 from tests import doctests  
    16 24  
    17 25 __author__ = 'Drew Smathers'  
     
    21 29          
    22 30 if __name__ == '__main__':  
    23       suite = unittest.TestSuite()  
    24       suite.addTest(doctest.DocTestSuite(interfaces))  
    25       suite.addTest(doctest.DocTestSuite(registry))  
    26       suite.addTest(doctest.DocTestSuite(rend.pagelets))  
    27       suite.addTest(doctest.DocTestSuite(rend.container))  
    28       suite.addTest(doctest.DocTestSuite(utils.console))  
    29       suite.addTest(doctest.DocTestSuite(utils.string))  
    30       suite.addTest(doctest.DocTestSuite(utils.config))  
    31       suite.addTest(doctest.DocTestSuite(xixxml.xmlbuilder))  
    32       suite.addTest(doctest.DocTestSuite(dt.utils.config))  
      31     suite = unittest.TestSuite()  
      32     suite.addTest(doctest.DocTestSuite(xix.interfaces))  
      33     suite.addTest(doctest.DocTestSuite(xix.registry))  
      34     suite.addTest(doctest.DocTestSuite(xix.rend.pagelets))  
      35     suite.addTest(doctest.DocTestSuite(xix.rend.container))  
      36     suite.addTest(doctest.DocTestSuite(xix.utils.console))  
      37     suite.addTest(doctest.DocTestSuite(xix.utils.string))  
      38     suite.addTest(doctest.DocTestSuite(xix.utils.config))  
      39     suite.addTest(doctest.DocTestSuite(xix.utils.rules))  
      40     suite.addTest(doctest.DocTestSuite(xix.xixxml.xmlbuilder))  
      41     suite.addTest(doctest.DocTestSuite(doctests.utils.config))  
      42     suite.addTest(doctest.DocTestSuite(doctests.utils.python))  
    33 43     runner = unittest.TextTestRunner(verbosity=2)  
    34 44     runner.run(suite)