Show
Ignore:
Timestamp:
05/31/08 22:01:20 (22 months ago)
Author:
JensDiemer
Message:
  • add a few test for the new plugin models
  • unittest: test plugin would be copied to the normal plugins. So this plugin would be auto installed with all other plugins.
  • Bugfix in lucidTag.py: 1 should not be converted to True ;)
Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/pylucid/tests/test_plugin_api.py

    r1609 r1612  
    1313""" 
    1414 
     15import os, re 
     16 
    1517import tests 
    1618 
     
    1820 
    1921from PyLucid.models import Page, Plugin 
     22from PyLucid.system.plugin_manager import install_plugin 
    2023 
    2124 
    2225TEST_PLUGIN_NAME = "unittest_plugin" 
    23 CONTENT_START = ( 
    24     '<div class="PyLucidPlugins unittest_plugin"' 
    25 ) 
    26 CONTENT_END = "</div>" 
    27  
    28  
    29 class PluginAPI_TestCase(tests.TestCase): 
     26CONTENT_START = "<pre>" 
     27CONTENT_END = "</pre>" 
     28CONTENT_RE = re.compile("<pre>(.*?)<\/pre>(?usm)") 
     29 
     30MODEL_TEST = """<pre> 
     31Test the plugin models 
     32Create TestArtist 
     33entry with ID '%(no)s' created 
     34Create TestAlbum 
     35entry with ID '%(no)s' created: 
     36TestAlbum 'A test Album', ID %(no)s, createby: superuser 
     37</pre>""" 
     38 
     39 
     40 
     41 
     42class PluginAPI_Base(tests.TestCase): 
    3043    """ 
    3144    Unit tests for detect_page function. 
    3245    """ 
    33 #    def tearDown(self): 
    34  
    35  
    36     def _init(self): 
    37         print "init" 
    38         try: 
    39             self.plugin = Plugin.objects.get(plugin_name=TEST_PLUGIN_NAME) 
    40         except Plugin.DoesNotExist, err: 
    41             from PyLucid.system.plugin_manager import install_plugin 
    42             self.plugin = install_plugin( 
    43                 package_name = "PyLucid.plugins_external", 
    44                 plugin_name = "unittest_plugin", 
    45                 debug = True, 
    46                 active=True, 
    47                 extra_verbose=True, 
    48             ) 
    49         else: 
    50             print "Plugin exists ID:", self.plugin.id 
    51              
    52     def _delete(self): 
    53         print "delete" 
    54         try: 
    55             self.plugin.delete() 
    56         except Exception, err: 
    57             print "tearDown Error:", err 
    58  
    59     def setUp(self):        
     46    def setUp(self): 
    6047        Page.objects.all().delete() # Delete all existins pages 
    6148 
     
    8067        self.test_url = self.test_page.get_absolute_url() 
    8168 
     69    #___________________________________________________________________________ 
     70    # SHARED UTILS 
     71 
    8272    def _get_plugin_content(self, url, debug=False): 
    8373        """ 
    8474        request the url and returns the plugin content output 
    8575        """ 
    86         print "22222222" 
    8776        response = self.client.get(url) 
    8877        # Check that the respose is 200 Ok. 
     
    9584            print "-"*79 
    9685 
    97         lines = raw_content.splitlines() 
    98         in_content = False 
    99         result = "" 
    100         for line in lines: 
    101             if line.startswith(CONTENT_START): 
    102                 in_content = True 
    103                 continue 
    104             elif in_content: 
    105                 if line == CONTENT_END: 
    106                     return result 
    107                 else: 
    108                     result += line 
     86        content = CONTENT_RE.findall(raw_content) 
     87        if len(content) == 1: 
     88            return content[0].strip() 
    10989 
    11090        msg = ( 
     
    11696        self.fail(msg) 
    11797 
    118     def test_init_plugin(self): 
    119         """ 
    120         First we must install the plugin 
    121         """ 
    122         print "XXXXXXXXX" 
     98    def _get_plugin(self): 
     99        return Plugin.objects.get(plugin_name=TEST_PLUGIN_NAME) 
     100 
     101    #___________________________________________________________________________ 
     102    # PRETESTS 
     103 
     104    def test_plugin_exist(self): 
     105        """ 
     106        Test if the unittest plugin is normal installed and active 
     107        """ 
     108        try: 
     109            self.plugin = self._get_plugin() 
     110        except Plugin.DoesNotExist, err: 
     111            self.fail("Plugin doesn't exist: %s" % err) 
     112 
     113        self.failUnless(self.plugin.active, True) 
     114        #print "Plugin exists ID:", self.plugin.id 
     115 
     116    def test_hello_world(self): 
     117        """ 
     118        Checks via _command url the hello world response 
     119        """ 
     120        url = self.command % "hello_world" 
     121        response = self.client.get(url) 
     122        # Check that the respose is 200 Ok. 
     123        self.failUnlessEqual(response.status_code, 200) 
     124        self.assertEqual2( 
     125            response.content.strip(), 
     126            ( 
     127                '<div class="PyLucidPlugins unittest_plugin"' 
     128                ' id="unittest_plugin_hello_world">\n' 
     129                'Hello world!\n' 
     130                '</div>' 
     131            ) 
     132        ) 
     133 
     134 
     135class PluginModel(PluginAPI_Base): 
     136    """ 
     137    Tests around the plugin models. 
     138    """ 
     139    def test_plugin_models(self): 
     140        """ 
     141        Test the plugin models. 
     142        Request three times the plugin_models view. This view creates on 
     143        every request a new model entry in both test models and display 
     144        some informations around this. 
     145        After this, we request a view with a list of all existing model entries. 
     146        """ 
     147        self.login("superuser") # login client as superuser 
     148 
    123149        url = self.command % "plugin_models" 
    124         content = self._get_plugin_content(url) 
    125  
    126  
    127     def test_first_check(self): 
    128         """ 
    129         Check if the test plugin exist and is active 
    130         """ 
    131 #        self._init() 
    132         try: 
    133             plugin = Plugin.objects.get(plugin_name=TEST_PLUGIN_NAME) 
    134         except Plugin.DoesNotExist, err: 
    135             self.fail("test plugin doesn't exist in the database: %s" % err) 
    136  
    137         self.failUnless(plugin.active, True) 
     150 
     151        content = self._get_plugin_content(url)#, debug=True) 
     152        self.assertEqual2( 
     153            content, 
     154            MODEL_TEST % {"no": 1} 
     155        ) 
     156 
     157        content = self._get_plugin_content(url)#, debug=True) 
     158        self.assertEqual2( 
     159            content, 
     160            MODEL_TEST % {"no": 2} 
     161        ) 
     162 
     163        content = self._get_plugin_content(url)#, debug=True) 
     164        self.assertEqual2( 
     165            content, 
     166            MODEL_TEST % {"no": 3} 
     167        ) 
     168 
     169        # Test all models view: A list of all existing models. 
     170        url = self.command % "all_models" 
     171        content = self._get_plugin_content(url)#, debug=True) 
     172        self.assertEqual2( 
     173            content, 
     174            ( 
     175                "<pre>\n" 
     176                "All Albums:\n" 
     177                "1: TestAlbum 'A test Album', ID 1, createby: superuser\n" 
     178                "2: TestAlbum 'A test Album', ID 2, createby: superuser\n" 
     179                "3: TestAlbum 'A test Album', ID 3, createby: superuser\n" 
     180                "</pre>" 
     181            ) 
     182        ) 
     183 
     184    def test_reinit(self): 
     185        """ 
     186        reinit the plugin and check if the plugin model tabels would be 
     187        droped and re-created. 
     188        """ 
     189        plugin = self._get_plugin() 
    138190         
    139 #        self._delete() 
    140  
    141     def test_hello_world(self): 
    142         """ 
    143         Checks via _command url the hello world response 
    144         """ 
    145         url = self.command % "hello_world" 
    146         content = self._get_plugin_content(url) 
    147         self.assertEqual(content, "Hello world!") 
    148  
     191        package_name = plugin.package_name 
     192        plugin_name = plugin.plugin_name 
     193         
     194        # remove the plugin completely from the database 
     195        # plugin model tables should be droped 
     196        plugin.delete() 
     197         
     198        # install the plugin 
     199        # plugin model tables should be re-created, too.  
     200        install_plugin(package_name, plugin_name, debug=True, active=True, 
     201                                                        extra_verbose=True) 
     202 
     203        # Check 1: 
     204        content = self._get_plugin_content(url)#, debug=True) 
     205        self.assertEqual2( 
     206            content, 
     207            MODEL_TEST % {"no": 1} 
     208        ) 
     209 
     210        # Check 2: 
     211        url = self.command % "all_models" 
     212        content = self._get_plugin_content(url)#, debug=True) 
     213        self.assertEqual2( 
     214            content, 
     215            ( 
     216                "<pre>\n" 
     217                "All Albums:\n" 
     218                "1: TestAlbum 'A test Album', ID 1, createby: superuser\n" 
     219                "</pre>" 
     220            ) 
     221        ) 
     222 
     223 
     224class PluginModel(PluginAPI_Base): 
     225    """ 
     226    pass parameters to the plugin method. 
     227    """ 
    149228    def test_lucidTag(self): 
    150229        content = self._get_plugin_content(self.test_url) 
    151         self.assertEqual(content, "args: (), kwargs: {}") 
     230        self.assertEqual2(content, "args:\n()\npformarted kwargs:\n{}") 
    152231 
    153232    def test_plugin_args1(self): 
    154233        """ 
    155         Test arguments in a _command url 
    156         """ 
    157         content = self._get_plugin_content(self.test_url, debug=False) 
    158         self.assertEqual( 
    159             content, "args: (), kwargs: {}" 
    160         ) 
    161  
    162         self.test_page.content = '{% lucidTag unittest_plugin arg1="test1" %}' 
    163         self.test_page.save() 
    164         content = self._get_plugin_content(self.test_url, debug=False) 
    165         self.assertEqual( 
    166             content, "args: (), kwargs: {'arg1': u'test1'}" 
    167         ) 
    168  
    169         self.test_page.content = '{% lucidTag unittest_plugin arg1=True %}' 
    170         self.test_page.save() 
    171         content = self._get_plugin_content(self.test_url, debug=True) 
    172         self.assertEqual( 
    173             content, "args: (), kwargs: {'arg1': True}" 
     234        Test arguments in the lucidTag 
     235        Handled in PyLucid.template_addons.lucidTag 
     236        """ 
     237        def get_args_info(page_content): 
     238            self.test_page.content = page_content 
     239            self.test_page.save() 
     240            content = self._get_plugin_content(self.test_url, debug=False) 
     241            args_info = content.split("\n") 
     242            return (args_info[1], args_info[3]) 
     243 
     244 
     245        args, kwargs = get_args_info('{% lucidTag unittest_plugin %}') 
     246        self.assertEqual2(args, "()") 
     247        self.assertEqual2(kwargs, "{}") 
     248 
     249 
     250        args, kwargs = get_args_info( 
     251            '{% lucidTag unittest_plugin arg1="test1" %}' 
     252        ) 
     253        self.assertEqual2(args, "()") 
     254        self.assertEqual2(kwargs, "{'arg1': u'test1'}") 
     255 
     256 
     257        args, kwargs = get_args_info( 
     258            '{% lucidTag unittest_plugin a="0" b="1" c="2" %}' 
     259        ) 
     260        self.assertEqual2(args, "()") 
     261        self.assertEqual2(kwargs, "{'a': u'0', 'b': u'1', 'c': u'2'}") 
     262 
     263 
     264        args, kwargs = get_args_info( 
     265            '{% lucidTag unittest_plugin' 
     266            ' t1="True" f1="False" t2="true" f2="false" %}' 
     267        ) 
     268        self.assertEqual2(args, "()") 
     269        self.assertEqual2(kwargs,  
     270            "{'f1': False, 'f2': False, 't1': True, 't2': True}" 
    174271        ) 
    175272 
     
    181278        url += "some/stuff/here/1/2/3/" 
    182279        content = self._get_plugin_content(url, debug=False) 
    183         self.assertEqual( 
    184             content, "args: (u'some/stuff/here/1/2/3/',), kwargs: {}" 
     280        self.assertEqual2( 
     281            content, 
     282            "args:\n" 
     283            "(u'some/stuff/here/1/2/3/',)\n" 
     284            "pformarted kwargs:\n" 
     285            "{}" 
    185286        ) 
    186287