| | 1 | """ |
| | 2 | xx. unmanaged_models |
| | 3 | |
| | 4 | Models can have a ``managed`` attribute, which specifies whether the |
| | 5 | SQL code is generated for the table on various manage.py operations. |
| | 6 | The default is True. |
| | 7 | """ |
| | 8 | |
| | 9 | from django.db import models |
| | 10 | |
| | 11 | """ |
| | 12 | General test strategy: |
| | 13 | * All tests are numbered (01, 02, 03... etc). |
| | 14 | * Each test contains three models (A, B, C, followed with the number of test), |
| | 15 | containing both indexed and non-indexed fields (to verify sql_index), usual |
| | 16 | fields (model A), foreign keys (model B) and many-to-many fields (model C). |
| | 17 | D table is generated automatically as intermediate M2M one. |
| | 18 | * The normal (default; managed = True) behaviour during the manage.py |
| | 19 | operations is not thoroughly checked; it is the duty of the appropriate tests |
| | 20 | for the primary functionality of these operations. |
| | 21 | The most attention is paid to whether the managed = False disables the SQL |
| | 22 | generation properly. |
| | 23 | * The intermediate table for M2M relations is not ever verified explicitly, |
| | 24 | because it is not ever marked with managed explicitly. |
| | 25 | """ |
| | 26 | |
| | 27 | # This dictionary maps the name of the model/SQL table (like 'A01') |
| | 28 | # to the boolean specifying whether this name should appear in the final SQL |
| | 29 | checks = {} |
| | 30 | |
| | 31 | |
| | 32 | """ |
| | 33 | 01: managed is not set. |
| | 34 | In such case, it should be equal (by default) to True, |
| | 35 | and SQL is generated for all three models. |
| | 36 | """ |
| | 37 | checks['A01'] = True |
| | 38 | checks['B01'] = True |
| | 39 | checks['C01'] = True |
| | 40 | |
| | 41 | class A01(models.Model): |
| | 42 | class Meta: db_table = 'A01' |
| | 43 | |
| | 44 | f_a = models.TextField(db_index = True) |
| | 45 | f_b = models.IntegerField() |
| | 46 | |
| | 47 | class B01(models.Model): |
| | 48 | class Meta: db_table = 'B01' |
| | 49 | |
| | 50 | fk_a = models.ForeignKey(A01) |
| | 51 | f_a = models.TextField(db_index = True) |
| | 52 | f_b = models.IntegerField() |
| | 53 | |
| | 54 | class C01(models.Model): |
| | 55 | class Meta: db_table = 'C01' |
| | 56 | |
| | 57 | mm_a = models.ManyToManyField(A01, db_table = 'D01') |
| | 58 | f_a = models.TextField(db_index = True) |
| | 59 | f_b = models.IntegerField() |
| | 60 | |
| | 61 | """ |
| | 62 | 02: managed is set to True. |
| | 63 | SQL is generated for all three models. |
| | 64 | """ |
| | 65 | checks['A02'] = True |
| | 66 | checks['B02'] = True |
| | 67 | checks['C02'] = True |
| | 68 | |
| | 69 | class A02(models.Model): |
| | 70 | class Meta: |
| | 71 | db_table = 'A02' |
| | 72 | managed = True |
| | 73 | |
| | 74 | f_a = models.TextField(db_index = True) |
| | 75 | f_b = models.IntegerField() |
| | 76 | |
| | 77 | class B02(models.Model): |
| | 78 | class Meta: |
| | 79 | db_table = 'B02' |
| | 80 | managed = True |
| | 81 | |
| | 82 | fk_a = models.ForeignKey(A02) |
| | 83 | f_a = models.TextField(db_index = True) |
| | 84 | f_b = models.IntegerField() |
| | 85 | |
| | 86 | class C02(models.Model): |
| | 87 | class Meta: |
| | 88 | db_table = 'C02' |
| | 89 | managed = True |
| | 90 | |
| | 91 | mm_a = models.ManyToManyField(A02, db_table = 'D02') |
| | 92 | f_a = models.TextField(db_index = True) |
| | 93 | f_b = models.IntegerField() |
| | 94 | |
| | 95 | |
| | 96 | """ |
| | 97 | 03: managed is set to False. |
| | 98 | SQL is NOT generated for any of the three models. |
| | 99 | """ |
| | 100 | checks['A03'] = False |
| | 101 | checks['B03'] = False |
| | 102 | checks['C03'] = False |
| | 103 | |
| | 104 | class A03(models.Model): |
| | 105 | class Meta: |
| | 106 | db_table = 'A03' |
| | 107 | managed = False |
| | 108 | |
| | 109 | f_a = models.TextField(db_index = True) |
| | 110 | f_b = models.IntegerField() |
| | 111 | |
| | 112 | class B03(models.Model): |
| | 113 | class Meta: |
| | 114 | db_table = 'B03' |
| | 115 | managed = False |
| | 116 | |
| | 117 | fk_a = models.ForeignKey(A03) |
| | 118 | f_a = models.TextField(db_index = True) |
| | 119 | f_b = models.IntegerField() |
| | 120 | |
| | 121 | class C03(models.Model): |
| | 122 | class Meta: |
| | 123 | db_table = 'C03' |
| | 124 | managed = False |
| | 125 | |
| | 126 | mm_a = models.ManyToManyField(A03, db_table = 'D03') |
| | 127 | f_a = models.TextField(db_index = True) |
| | 128 | f_b = models.IntegerField() |
| | 129 | |
| | 130 | |
| | 131 | # We will use short names for these templates |
| | 132 | sql_templates = { |
| | 133 | 'create table': 'CREATE TABLE "%s"', |
| | 134 | 'create index': 'CREATE INDEX "%s_f_a"', |
| | 135 | 'drop table': 'DROP TABLE "%s"', |
| | 136 | 'delete from': 'DELETE FROM "%s"' |
| | 137 | } |
| | 138 | |
| | 139 | def get_failed_models(arr_sql, sql_template_names): |
| | 140 | """ |
| | 141 | Find the models which should not be in the SQL but they are present, |
| | 142 | or they should be in the SQL but they are missing. |
| | 143 | """ |
| | 144 | txt_sql = ' '.join(arr_sql) |
| | 145 | for (model, should_be_present) in checks.iteritems(): |
| | 146 | # Do we expect to see the model name in the SQL text? |
| | 147 | for sql_template_name in sql_template_names: |
| | 148 | # We are interested not in just the model name like "A01", |
| | 149 | # but in the whole string like 'CREATE TABLE "A01"' |
| | 150 | # so we apply the model name to the template |
| | 151 | # to find out the expected string |
| | 152 | expected = (sql_templates[sql_template_name])%model |
| | 153 | if ((expected in txt_sql) != should_be_present): |
| | 154 | # Our expectations failed! |
| | 155 | yield 'The string %s %s present in SQL but it %s.'%( |
| | 156 | expected, |
| | 157 | {False: 'is not', True: 'is'}[expected in txt_sql], |
| | 158 | {False: 'should not be', True: 'should be'}[should_be_present] |
| | 159 | ) |
| | 160 | |
| | 161 | |
| | 162 | __test__ = {'API_TESTS':""" |
| | 163 | >>> from django.db.models import get_app |
| | 164 | >>> from django.core.management.sql import * |
| | 165 | >>> from django.core.management.color import no_style |
| | 166 | >>> import sys |
| | 167 | |
| | 168 | >>> myapp = get_app('unmanaged_models') |
| | 169 | >>> mystyle = no_style() |
| | 170 | |
| | 171 | # a. Verify sql_create |
| | 172 | >>> list(get_failed_models( sql_create(myapp, mystyle), ['create table'] )) |
| | 173 | [] |
| | 174 | |
| | 175 | # b. Verify sql_delete |
| | 176 | >>> list(get_failed_models( sql_delete(myapp, mystyle), ['drop table'] )) |
| | 177 | [] |
| | 178 | |
| | 179 | # c. Verify sql_reset |
| | 180 | >>> list(get_failed_models( sql_reset(myapp, mystyle), ['drop table', 'create table', 'create index'] )) |
| | 181 | [] |
| | 182 | |
| | 183 | # d. Verify sql_flush |
| | 184 | >>> # sql_flush(mystyle) |
| | 185 | >>> list(get_failed_models( sql_flush(mystyle), ['delete from'] )) |
| | 186 | [] |
| | 187 | |
| | 188 | # e. Verify sql_custom |
| | 189 | # No custom data provided, should not be no output. |
| | 190 | >>> sql_custom(myapp,mystyle) |
| | 191 | [] |
| | 192 | |
| | 193 | # f. Verify sql_indexes |
| | 194 | >>> list(get_failed_models( sql_indexes(myapp, mystyle), ['create index'] )) |
| | 195 | [] |
| | 196 | |
| | 197 | # g. Verify sql_all |
| | 198 | >>> list(get_failed_models( sql_all(myapp, mystyle), ['create table', 'create index'] )) |
| | 199 | [] |
| | 200 | """} |