Changeset 1612

Show
Ignore:
Timestamp:
05/31/08 22:01:20 (3 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 ;)
Location:
trunk
Files:
8 modified

Legend:

Unmodified
Added
Removed
  • trunk/dev_scripts/local_tests/models_test.py

    r1607 r1612  
    1919from django.contrib.auth.models import User 
    2020 
     21# Use the models from the test plugin 
     22from tests.unittest_plugin.unittest_plugin import TestArtist, TestAlbum 
    2123 
    2224 
    23 class Musician(models.Model): 
    24     first_name = models.CharField(max_length=50) 
    25     last_name = models.CharField(max_length=50) 
    26     instrument = models.CharField(max_length=100) 
    27     class Meta: 
    28         # db_table is optional 
    29 #        db_table = 'PyLucid_test' 
    30         app_label = 'PyLucid' # must be set to "PyLucid" 
     25print "-----------------------------------------------------------------------" 
    3126 
    32 class Album(models.Model): 
    33     artist = models.ForeignKey("Musician") 
    34     name = models.CharField(max_length=100) 
    35     num_stars = models.IntegerField() 
    36  
    37     createtime = models.DateTimeField(auto_now_add=True) 
    38     lastupdatetime = models.DateTimeField(auto_now=True) 
    39  
    40     createby = models.ForeignKey(User, related_name="test_createby", 
    41         null=True, blank=True 
    42     ) 
    43     lastupdateby = models.ForeignKey(User, related_name="test_lastupdateby", 
    44         null=True, blank=True 
    45     ) 
    46     class Meta: 
    47         # db_table is optional 
    48 #        db_table = 'PyLucid_test' 
    49         app_label = 'PyLucid' # must be set to "PyLucid" 
     27print "XXX" 
     28print TestAlbum._meta.app_label 
     29#t = TestAlbum._meta 
     30#for i in dir(t): 
     31#    print i, getattr(t, i) 
    5032 
    5133 
    52 print "------------------------------------------------------------------------------------------------------" 
     34print "-----------------------------------------------------------------------" 
    5335try: 
    5436    # Here the table doesn't exist 
    55     m = Musician(first_name = "foo", last_name = "bar", instrument = "foobar") 
     37    m = TestArtist(name = "foobar") 
    5638    m.save() 
    5739except Exception, err: 
    5840    print err 
    5941 
    60 print "------------------------------------------------------------------------------------------------------" 
     42print "-----------------------------------------------------------------------" 
    6143 
    6244def get_create_table(models): 
     
    7254    return statements 
    7355 
    74 statements = get_create_table(models = (Musician, Album)) 
     56def get_create_table2(*plugin_models): 
     57    from django.conf import settings 
     58    from django.core.management.color import no_style 
     59    style = no_style() 
     60    from django.core.management import sql 
     61    from django.db import models 
     62 
     63    models.loading.register_models("PyLucidPlugins", *plugin_models) 
     64 
     65    # get all delete statements for the given App 
     66    app = models.get_app("PyLucidPlugins") 
     67    statements = sql.sql_create(app, style) 
     68 
     69    #cleanup 
     70    app_models = models.loading.cache.app_models 
     71    del(app_models["PyLucidPlugins"]) 
     72#    settings.INSTALLED_APPS = old_inst_apps 
     73 
     74    return statements 
     75 
     76statements = get_create_table(models = (TestArtist, TestAlbum)) 
     77#statements = get_create_table2(TestArtist, TestAlbum) 
    7578 
    7679from django.db import connection 
     
    8083    cursor.execute(statement) 
    8184 
    82 print "------------------------------------------------------------------------------------------------------" 
     85print "-----------------------------------------------------------------------" 
    8386 
    8487# Now we can use the created tables 
    85 m = Musician(first_name = "foo", last_name = "bar", instrument = "foobar") 
     88m = TestArtist(name = "foobar") 
    8689m.save() 
    87 a = Album( 
     90a = TestAlbum( 
    8891    artist = m, 
    8992    name = "jup", 
     
    9194) 
    9295a.save() 
    93 print Album.objects.all().values() 
     96print TestAlbum.objects.all().values() 
    9497 
    95 print "------------------------------------------------------------------------------------------------------" 
     98print "-----------------------------------------------------------------------" 
    9699 
    97100# Not needed to "insert" the models: 
    98101#from PyLucid.system.PyLucidPlugins import models as PluginModels 
    99 #PluginModels.Musician = Musician 
    100 #PluginModels.Album = Album 
     102#PluginModels.TestArtist = TestArtist 
     103#PluginModels.TestAlbum = TestAlbum 
    101104 
    102105def get_delete_sql(*plugin_models): 
     
    107110    from django.db import models 
    108111 
    109     # Insert app in installed apps 
    110     old_inst_apps = settings.INSTALLED_APPS 
    111     inst_apps = list(old_inst_apps) 
    112     inst_apps.append("PyLucid.system.PyLucidPlugins") 
    113     settings.INSTALLED_APPS = inst_apps 
     112#    # Insert app in installed apps 
     113#    old_inst_apps = settings.INSTALLED_APPS 
     114#    inst_apps = list(old_inst_apps) 
     115#    inst_apps.append("PyLucid.system.PyLucidPlugins") 
     116#    settings.INSTALLED_APPS = inst_apps 
    114117 
    115118    models.loading.register_models("PyLucidPlugins", *plugin_models) 
     
    122125    app_models = models.loading.cache.app_models 
    123126    del(app_models["PyLucidPlugins"]) 
    124     settings.INSTALLED_APPS = old_inst_apps 
     127#    settings.INSTALLED_APPS = old_inst_apps 
    125128 
    126129    return statements 
    127130 
    128 statements = get_delete_sql(Musician) 
     131statements = get_delete_sql(TestArtist) 
    129132print "1:", statements 
    130133 
    131 statements = get_delete_sql(Musician, Album) 
     134statements = get_delete_sql(TestArtist, TestAlbum) 
    132135print "2:", statements 
    133136 
     
    139142 
    140143# After this no tables left: 
    141 statements = get_delete_sql(Musician, Album) 
     144statements = get_delete_sql(TestArtist, TestAlbum) 
    142145print statements 
    143146 
    144 statements = get_delete_sql(Album) 
     147statements = get_delete_sql(TestAlbum) 
    145148print statements 
    146149 
    147 print "------------------------------------------------------------------------------------------------------" 
     150print "-----------------------------------------------------------------------" 
    148151 
    149152# No tables error: 
    150153try: 
    151     m = Musician(first_name = "foo", last_name = "bar", instrument = "foobar") 
     154    m = TestArtist(name = "foobar") 
    152155    m.save() 
    153156except Exception, err: 
  • trunk/pylucid/PyLucid/models/Plugin.py

    r1609 r1612  
    1919from pprint import pformat 
    2020 
     21from django.conf import settings 
     22 
    2123from django.db import models, transaction, connection 
    2224from django.core.cache import cache 
     
    3234preference_cache = {} 
    3335 
     36PLUGIN_MODEL_LABEL = "PyLucidPlugins" 
     37PLUGIN_MODEL_APP = "PyLucid.system.PyLucidPlugins" 
     38 
     39 
     40def get_plugin_models(package_name, plugin_name, debug=False): 
     41    """ 
     42    returns a list of all existing plugin models. 
     43    If no models exist, returns None! 
     44 
     45    Seperated function, because must be accessible from manager- and plugin 
     46    class, too. 
     47    """ 
     48    plugin_module = get_plugin_module(package_name, plugin_name, debug) 
     49    if not hasattr(plugin_module, "PLUGIN_MODELS"): 
     50        # Plugin has no models 
     51        return None 
     52 
     53    if not PLUGIN_MODEL_APP in settings.INSTALLED_APPS: 
     54        from django.core.exceptions import ImproperlyConfigured 
     55        raise ImproperlyConfigured( 
     56            "Error '%s' not in settings.INSTALLED_APPS!" % PLUGIN_MODEL_APP 
     57        ) 
     58 
     59    plugin_models = plugin_module.PLUGIN_MODELS 
     60 
     61    # Check app_label for every plugin model 
     62    for model in plugin_models: 
     63        app_label = model._meta.app_label 
     64        assert app_label == PLUGIN_MODEL_LABEL, ( 
     65            "Plugin models must defined in class Meta: app_label = '%s'" 
     66        ) % PLUGIN_MODEL_LABEL 
     67 
     68    return plugin_models 
     69 
     70 
     71 
     72 
     73 
    3474class PluginManager(models.Manager): 
    3575    """ 
     
    5191        plugin = self.get(plugin_name = plugin_name) 
    5292        return plugin.get_preferences() 
     93 
     94    def get_plugin_models(self, package_name, plugin_name, debug=False): 
     95        """ returns a list of all existing plugin models or None """ 
     96        return get_plugin_models(package_name, plugin_name, debug) 
     97 
    5398 
    5499 
     
    152197        return get_plugin_version(self.package_name, self.plugin_name, debug) 
    153198 
     199    def get_plugin_models(self, debug=False): 
     200        """ returns a list of all existing plugin models or None """ 
     201        return get_plugin_models(self.package_name, self.plugin_name, debug) 
     202 
    154203    #___________________________________________________________________________ 
    155204    # SAVE 
     
    173222 
    174223    def get_delete_sql(self, plugin_models): 
    175 #        from django.conf import settings 
     224        """ 
     225        Returns a list of sql statements for deleting the plugin model tabels. 
     226        For this, we used the fake django app "PyLucidPlugins" and attach 
     227        all models to this add temporarly. 
     228        """ 
     229        from django.core.management import sql 
    176230        from django.core.management.color import no_style 
    177231        style = no_style() 
    178         from django.core.management import sql 
    179232 
    180233        models.loading.register_models("PyLucidPlugins", *plugin_models) 
    181234 
    182         # get all delete statements for the given App 
    183235        app = models.get_app("PyLucidPlugins") 
     236 
    184237        statements = sql.sql_delete(app, style) 
    185238 
    186         print sql.table_list() 
    187  
    188         #cleanup 
     239        # cleanup 
    189240        app_models = models.loading.cache.app_models 
    190241        del(app_models["PyLucidPlugins"]) 
    191     #    settings.INSTALLED_APPS = old_inst_apps 
    192242 
    193243        return statements 
    194244 
    195     def get_delete_tables(self): 
    196         plugin_module = get_plugin_module( 
    197             self.package_name, 
    198             self.plugin_name, 
    199             debug=True, 
    200         ) 
    201         if not hasattr(plugin_module, "PLUGIN_MODELS"): 
     245    def _delete_tables(self): 
     246        """ 
     247        Delete all plugin model tabels. 
     248        """ 
     249        plugin_models = self.get_plugin_models() 
     250        if not plugin_models: 
    202251            # Plugin has no models 
    203252            return 
    204253 
    205         plugin_models = plugin_module.PLUGIN_MODELS 
    206  
    207254        statements = self.get_delete_sql(plugin_models) 
    208255 
    209256        cursor = connection.cursor() 
    210257        for statement in statements: 
    211             print repr(statement) 
     258            #print repr(statement) 
    212259            cursor.execute(statement) 
    213260 
     
    215262    @transaction.commit_on_success 
    216263    def delete(self): 
    217         self.get_delete_tables() 
     264        self._delete_tables() 
    218265 
    219266        super(Plugin, self).delete() 
  • trunk/pylucid/PyLucid/system/plugin_manager.py

    r1609 r1612  
    3030from django.db.models import Model 
    3131from django.core.management.sql import sql_model_create, \ 
    32                                                         sql_indexes_for_model, custom_sql_for_model 
     32                                    sql_indexes_for_model, custom_sql_for_model 
    3333from django.http import HttpResponse, Http404 
    3434 
     
    3636from PyLucid.system.exceptions import * 
    3737from PyLucid.system.plugin_import import get_plugin_module, \ 
    38                                                                         get_plugin_config, get_plugin_version 
    39  
    40  
     38                                        get_plugin_config, get_plugin_version 
    4139 
    4240 
     
    5351        ) 
    5452        request.page_msg(msg) 
    55         msg2 = '<i title="(Error details in page messages.)">["%s.%s" error.]</i>' % ( 
    56             plugin_name, method_name 
    57         ) 
     53        msg2 = ( 
     54            '<i title="(Error details in page messages.)">["%s.%s" error.]</i>' 
     55        ) % (plugin_name, method_name) 
    5856        local_response.write(msg2) 
    5957 
     
    198196 
    199197 
    200 def get_create_table(models): 
     198def get_create_table(plugin_models): 
    201199    from django.core.management.color import no_style 
    202200    style = no_style() 
    203201 
    204202    statements = [] 
    205     for model in models: 
     203    for model in plugin_models: 
    206204        statements += sql_model_create(model, style)[0] 
    207205        statements += sql_indexes_for_model(model, style) 
     
    210208 
    211209 
    212  
    213210def create_plugin_tables(plugin, extra_verbose): 
    214     plugin_module = get_plugin_module( 
     211    plugin_models = Plugin.objects.get_plugin_models( 
    215212        plugin.package_name, 
    216213        plugin.plugin_name, 
    217214        debug=extra_verbose, 
    218215    ) 
    219     if not hasattr(plugin_module, "PLUGIN_MODELS"): 
     216    if not plugin_models: 
    220217        # Plugin has no models 
    221         if extra_verbose: 
    222             print "Info: No 'plugin_models' list defined, ok." 
    223218        return 
    224  
    225     plugin_models = plugin_module.PLUGIN_MODELS 
    226219 
    227220    statements = get_create_table(plugin_models) 
    228221    cursor = connection.cursor() 
    229222    for statement in statements: 
    230         print repr(statement) 
     223        #print repr(statement) 
    231224        cursor.execute(statement) 
    232225 
  • trunk/pylucid/PyLucid/template_addons/lucidTag.py

    r1365 r1612  
    116116            # method Keywords must be Strings 
    117117            key = key.encode(settings.DEFAULT_CHARSET) 
    118             if value in ("True", "true", "on", "ON", "1"): 
     118            value_lower = value.lower() 
     119            if value_lower in ("true", "on"): 
    119120                value = True 
    120             elif value in ("False", "false", "off", "OFF", "0"): 
     121            elif value_lower in ("false", "off"): 
    121122                value = False 
    122123            method_kwargs[key] = value 
  • 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     &n