|
1 | 1 | import pytest |
2 | 2 |
|
3 | 3 | from cratedb_django.models import CrateModel |
4 | | -from cratedb_django.models.model import CRATE_META_OPTIONS |
| 4 | +from cratedb_django.models.model import CRATE_META_OPTIONS, OMITTED |
5 | 5 |
|
6 | 6 | from django.forms.models import model_to_dict |
7 | 7 | from django.db import connection, models |
@@ -138,7 +138,7 @@ class Meta: |
138 | 138 |
|
139 | 139 |
|
140 | 140 | def test_model_meta_partition_by(): |
141 | | - """Test partition_by option in Meta class.""" |
| 141 | + """Test `partition_by` option in Meta class.""" |
142 | 142 |
|
143 | 143 | class MetaOptions(CrateModel): |
144 | 144 | one = models.TextField() |
@@ -174,3 +174,74 @@ class Meta: |
174 | 174 | ): |
175 | 175 | with connection.schema_editor() as schema_editor: |
176 | 176 | schema_editor.table_sql(MetaOptions) |
| 177 | + |
| 178 | + |
| 179 | +def test_clustered_by(): |
| 180 | + """ |
| 181 | + `clustered_by` and `number_of_shards` meta class attributes. |
| 182 | + """ |
| 183 | + |
| 184 | + class MetaOptions(CrateModel): |
| 185 | + id = models.IntegerField() |
| 186 | + one = models.TextField() |
| 187 | + two = models.TextField() |
| 188 | + three = models.TextField() |
| 189 | + |
| 190 | + class Meta: |
| 191 | + app_label = "ignore" |
| 192 | + clustered_by = "one" |
| 193 | + number_of_shards = 3 |
| 194 | + |
| 195 | + with connection.schema_editor() as schema_editor: |
| 196 | + sql, params = schema_editor.table_sql(MetaOptions) |
| 197 | + assert "CLUSTERED BY (one) INTO 3 shards" in sql |
| 198 | + |
| 199 | + MetaOptions._meta.clustered_by = "one" |
| 200 | + MetaOptions._meta.number_of_shards = OMITTED |
| 201 | + with connection.schema_editor() as schema_editor: |
| 202 | + sql, params = schema_editor.table_sql(MetaOptions) |
| 203 | + assert "CLUSTERED BY (one)" in sql |
| 204 | + assert "INTO 3 shards" not in sql |
| 205 | + |
| 206 | + MetaOptions._meta.clustered_by = OMITTED |
| 207 | + MetaOptions._meta.number_of_shards = 3 |
| 208 | + with connection.schema_editor() as schema_editor: |
| 209 | + sql, params = schema_editor.table_sql(MetaOptions) |
| 210 | + assert "CLUSTERED INTO 3 shards" not in sql |
| 211 | + |
| 212 | + MetaOptions._meta.clustered_by = OMITTED |
| 213 | + MetaOptions._meta.number_of_shards = OMITTED |
| 214 | + with connection.schema_editor() as schema_editor: |
| 215 | + sql, params = schema_editor.table_sql(MetaOptions) |
| 216 | + assert "INTO 3 shards" not in sql |
| 217 | + assert "CLUSTERED" not in sql |
| 218 | + |
| 219 | + with pytest.raises(ValueError, match="Column 'nocolumn' does not exist in model"): |
| 220 | + MetaOptions._meta.clustered_by = "nocolumn" |
| 221 | + MetaOptions._meta.number_of_shards = OMITTED |
| 222 | + with connection.schema_editor() as schema_editor: |
| 223 | + schema_editor.table_sql(MetaOptions) |
| 224 | + |
| 225 | + with pytest.raises( |
| 226 | + ValueError, match="clustered_by has to be a non-empty string, not 1" |
| 227 | + ): |
| 228 | + MetaOptions._meta.clustered_by = 1 |
| 229 | + with connection.schema_editor() as schema_editor: |
| 230 | + schema_editor.table_sql(MetaOptions) |
| 231 | + |
| 232 | + with pytest.raises( |
| 233 | + ValueError, match="number_of_shards has to be an integer bigger than 0" |
| 234 | + ): |
| 235 | + MetaOptions._meta.clustered_by = OMITTED |
| 236 | + MetaOptions._meta.number_of_shards = 0 |
| 237 | + with connection.schema_editor() as schema_editor: |
| 238 | + schema_editor.table_sql(MetaOptions) |
| 239 | + |
| 240 | + with pytest.raises( |
| 241 | + ValueError, |
| 242 | + match="number_of_shards has to be an integer bigger than 0, " "not 'abcdef'", |
| 243 | + ): |
| 244 | + MetaOptions._meta.clustered_by = OMITTED |
| 245 | + MetaOptions._meta.number_of_shards = "abcdef" |
| 246 | + with connection.schema_editor() as schema_editor: |
| 247 | + schema_editor.table_sql(MetaOptions) |
0 commit comments