Exemplo n.º 1
0
Arquivo: db.py Projeto: KMhook/istweb
    def test_sql_defaults(self):
        """
        Test that sql default value is correct for non-string field types.
        """
        from datetime import datetime

        class CustomField(models.CharField):
            __metaclass__ = models.SubfieldBase
            description = 'CustomField'
            def get_default(self):
                if self.has_default():
                    if callable(self.default):
                        return self.default()
                    return self.default
                return super(CustomField, self).get_default()
            def get_prep_value(self, value):
                if not value:
                    return value
                return ','.join(map(str, value))
            def to_python(self, value):
                if not value or isinstance(value, list):
                    return value
                return map(int, value.split(','))

        defaults = (
            (models.DateTimeField(default=datetime(2012, 12, 21, 0, 0, 1)), 'DEFAULT \'2012-12-21 00:00:01'),
            (models.CharField(default='sukasuka'), 'DEFAULT \'sukasuka'),
            (models.BooleanField(default=False), 'DEFAULT False'),
            (models.IntegerField(default=42), 'DEFAULT 42'),
            (CustomField(default=[2012,2018,2021,2036]), 'DEFAULT \'2012,2018,2021,2036')
        )
        for field, sql_test_str in defaults:
            sql = db.column_sql('fish', 'YAAAAAAZ', field)
            if sql_test_str not in sql:
                self.fail("default sql value was not properly generated for field %r." % field)
Exemplo n.º 2
0
Arquivo: db.py Projeto: 10sr/hue
    def test_sql_defaults(self):
        """
        Test that sql default value is correct for non-string field types.
        Datetimes are handled in test_datetime_default.
        """

        class CustomField(with_metaclass(models.SubfieldBase, models.CharField)):
            description = 'CustomField'
            def get_default(self):
                if self.has_default():
                    if callable(self.default):
                        return self.default()
                    return self.default
                return super(CustomField, self).get_default()
            def get_prep_value(self, value):
                if not value:
                    return value
                return ','.join(map(str, value))
            def to_python(self, value):
                if not value or isinstance(value, list):
                    return value
                return list(map(int, value.split(',')))

        false_value = db.has_booleans and 'False' or '0'
        defaults = (
            (models.CharField(default='sukasuka'), 'DEFAULT \'sukasuka'),
            (models.BooleanField(default=False), 'DEFAULT %s' % false_value),
            (models.IntegerField(default=42), 'DEFAULT 42'),
            (CustomField(default=[2012, 2018, 2021, 2036]), 'DEFAULT \'2012,2018,2021,2036')
        )
        for field, sql_test_str in defaults:
            sql = db.column_sql('fish', 'YAAAAAAZ', field)
            if sql_test_str not in sql:
                self.fail("default sql value was not properly generated for field %r.\nSql was %s" % (field, sql))
Exemplo n.º 3
0
    def test_sql_defaults(self):
        """
        Test that sql default value is correct for non-string field types.
        Datetimes are handled in test_datetime_default.
        """

        class CustomField(models.CharField):
            __metaclass__ = models.SubfieldBase
            description = 'CustomField'
            def get_default(self):
                if self.has_default():
                    if callable(self.default):
                        return self.default()
                    return self.default
                return super(CustomField, self).get_default()
            def get_prep_value(self, value):
                if not value:
                    return value
                return ','.join(map(str, value))
            def to_python(self, value):
                if not value or isinstance(value, list):
                    return value
                return map(int, value.split(','))

        false_value = db.has_booleans and 'False' or '0'
        defaults = (
            (models.CharField(default='sukasuka'), 'DEFAULT \'sukasuka'),
            (models.BooleanField(default=False), 'DEFAULT %s' % false_value),
            (models.IntegerField(default=42), 'DEFAULT 42'),
            (CustomField(default=[2012, 2018, 2021, 2036]), 'DEFAULT \'2012,2018,2021,2036')
        )
        for field, sql_test_str in defaults:
            sql = db.column_sql('fish', 'YAAAAAAZ', field)
            if sql_test_str not in sql:
                self.fail("default sql value was not properly generated for field %r.\nSql was %s" % (field, sql))
Exemplo n.º 4
0
def get_sql_schema(model, already_defined):
    """ Returns a string with the database schema (SQL create table statement).
    """
    if model in already_defined:
        return ''

    lines = []

    table_name = model._meta.db_table

    for parent in model._meta.parents:
        defn = get_sql_schema(parent, already_defined)
        if defn:
            lines.append(defn)

    columns = [
        db.column_sql(table_name, field.db_column, field)
        for field in model._meta.local_fields
    ]

    lines.append("--")
    lines.append("-- %s" % model._meta.object_name)
    lines.append("--")
    lines.append("")
    lines.append('CREATE TABLE %s (\n    %s\n);' % (
            db.quote_name(table_name),
            ',\n    '.join([col for col in columns if col]),
        ))
    lines.append("\n".join(db.deferred_sql))
    lines.append("")

    already_defined.add(model)

    return '\n'.join(lines)
Exemplo n.º 5
0
Arquivo: db.py Projeto: KMhook/istweb
    def test_sql_defaults(self):
        """
        Test that sql default value is correct for non-string field types.
        """
        from datetime import datetime

        class CustomField(models.CharField):
            __metaclass__ = models.SubfieldBase
            description = 'CustomField'

            def get_default(self):
                if self.has_default():
                    if callable(self.default):
                        return self.default()
                    return self.default
                return super(CustomField, self).get_default()

            def get_prep_value(self, value):
                if not value:
                    return value
                return ','.join(map(str, value))

            def to_python(self, value):
                if not value or isinstance(value, list):
                    return value
                return map(int, value.split(','))

        defaults = ((models.DateTimeField(
            default=datetime(2012, 12, 21, 0, 0, 1)),
                     'DEFAULT \'2012-12-21 00:00:01'),
                    (models.CharField(default='sukasuka'),
                     'DEFAULT \'sukasuka'),
                    (models.BooleanField(default=False), 'DEFAULT False'),
                    (models.IntegerField(default=42), 'DEFAULT 42'),
                    (CustomField(default=[2012, 2018, 2021, 2036]),
                     'DEFAULT \'2012,2018,2021,2036'))
        for field, sql_test_str in defaults:
            sql = db.column_sql('fish', 'YAAAAAAZ', field)
            if sql_test_str not in sql:
                self.fail(
                    "default sql value was not properly generated for field %r."
                    % field)