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/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: