Ticket #14300: customsql_model_package.patch

File customsql_model_package.patch, 5.2 KB (added by Fede Heinz, 13 years ago)

Fixes the bug, adds tests

  • django/core/management/sql.py

    # HG changeset patch
    # User Federico Heinz <fheinz@vialibre.org.ar>
    # Date 1289675887 10800
    # Node ID 322d8f403154422a4f6b3e3dccf2da011ad70fa0
    # Parent  c8000aad4c103d7c18a36f6f7c190212227c46b2
    Fixed custom SQL when models resides in a directory, not a file
    
    diff -r c8000aad4c10 -r 322d8f403154 django/core/management/sql.py
    a b  
    141141
    142142def custom_sql_for_model(model, style, connection):
    143143    opts = model._meta
    144     app_dir = os.path.normpath(os.path.join(os.path.dirname(models.get_app(model._meta.app_label).__file__), 'sql'))
     144    app = models.get_app(model._meta.app_label)
     145    if hasattr(app, '__path__'):
     146        app_dir = os.path.dirname(app.__path__[0])
     147    else:
     148        app_dir = os.path.dirname(app.__file__)
     149    app_dir = os.path.normpath(os.path.join(app_dir, 'sql'))
    145150    output = []
    146151
    147152    # Post-creation SQL should come before any initial SQL data is loaded.
  • new file tests/modeltests/customsql_model/models.py

    diff -r c8000aad4c10 -r 322d8f403154 tests/modeltests/customsql_model/models.py
    - +  
     1from django.db import models
     2from django.conf import settings
     3
     4class Article(models.Model):
     5    headline = models.CharField(max_length=100, default='Default headline')
     6    pub_date = models.DateTimeField()
     7
     8    def __unicode__(self):
     9        return self.headline
     10
     11    class Meta:
     12        ordering = ('-pub_date', 'headline')
     13
     14__test__ = {'API_TESTS': """
     15>>> from django.core import management
     16>>> management.call_command('sqlall', 'customsql_model')
     17BEGIN;
     18CREATE TABLE "customsql_model_article" (
     19    "id" integer NOT NULL PRIMARY KEY,
     20    "headline" varchar(100) NOT NULL,
     21    "pub_date" datetime NOT NULL
     22)
     23;
     24INSERT INTO customsql_model_article (headline, pub_date) VALUES ("Python program becomes self aware", "2006-06-16 11:00:00");
     25COMMIT;
     26
     27# Syncdb introduces 1 initial data object from article.sql
     28>>> Article.objects.all()
     29[<Article: Python program becomes self aware>]
     30"""}
  • new file tests/modeltests/customsql_model/sql/article.sql

    diff -r c8000aad4c10 -r 322d8f403154 tests/modeltests/customsql_model/sql/article.sql
    - +  
     1INSERT INTO customsql_model_article (headline, pub_date) VALUES ("Python program becomes self aware", "2006-06-16 11:00:00");
  • new file tests/modeltests/customsql_model/tests.py

    diff -r c8000aad4c10 -r 322d8f403154 tests/modeltests/customsql_model/tests.py
    - +  
     1from django.test import TestCase
     2from models import Article
     3
     4class SampleTestCase(TestCase):
     5    def testClassCustomSQL(self):
     6        "Check that test case has installed 1 object from SQL"
     7        self.assertEqual(Article.objects.count(), 1)
     8        self.assertEquals(str(Article.objects.all()), "[<Article: Python program becomes self aware>]")
  • new file tests/modeltests/customsql_model_package/models/__init__.py

    diff -r c8000aad4c10 -r 322d8f403154 tests/modeltests/customsql_model_package/models/__init__.py
    - +  
     1from django.db import models
     2from django.conf import settings
     3
     4class Article(models.Model):
     5    headline = models.CharField(max_length=100, default='Default headline')
     6    pub_date = models.DateTimeField()
     7
     8    def __unicode__(self):
     9        return self.headline
     10
     11    class Meta:
     12        app_label = 'customsql_model_package'
     13        ordering = ('-pub_date', 'headline')
     14
     15__test__ = {'API_TESTS': """
     16>>> from django.core import management
     17>>> management.call_command('sqlall', 'customsql_model_package')
     18BEGIN;
     19CREATE TABLE "customsql_model_package_article" (
     20    "id" integer NOT NULL PRIMARY KEY,
     21    "headline" varchar(100) NOT NULL,
     22    "pub_date" datetime NOT NULL
     23)
     24;
     25INSERT INTO customsql_model_package_article (headline, pub_date) VALUES ("Python program becomes self aware", "2006-06-16 11:00:00");
     26COMMIT;
     27
     28# Syncdb introduces 1 initial data object from article.sql
     29>>> Article.objects.all()
     30[<Article: Python program becomes self aware>]
     31"""}
  • new file tests/modeltests/customsql_model_package/sql/article.sql

    diff -r c8000aad4c10 -r 322d8f403154 tests/modeltests/customsql_model_package/sql/article.sql
    - +  
     1INSERT INTO customsql_model_package_article (headline, pub_date) VALUES ("Python program becomes self aware", "2006-06-16 11:00:00");
  • new file tests/modeltests/customsql_model_package/tests.py

    diff -r c8000aad4c10 -r 322d8f403154 tests/modeltests/customsql_model_package/tests.py
    - +  
     1from django.test import TestCase
     2from models import Article
     3
     4class SampleTestCase(TestCase):
     5    def testClassCustomSQL(self):
     6        "Check that test case has installed 1 object from SQL"
     7        self.assertEqual(Article.objects.count(), 1)
     8        self.assertEquals(str(Article.objects.all()), "[<Article: Python program becomes self aware>]")
Back to Top