|
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 |
@@ -212,3 +212,74 @@ class Meta: |
212 | 212 | SomeModel, SomeModel._meta.get_field("id") |
213 | 213 | ) |
214 | 214 | assert sql == "text NOT NULL PRIMARY KEY" |
| 215 | + |
| 216 | + |
| 217 | +def test_clustered_by(): |
| 218 | + """ |
| 219 | + `clustered_by` and `number_of_shards` meta class attributes. |
| 220 | + """ |
| 221 | + |
| 222 | + class MetaOptions(CrateModel): |
| 223 | + id = models.IntegerField() |
| 224 | + one = models.TextField() |
| 225 | + two = models.TextField() |
| 226 | + three = models.TextField() |
| 227 | + |
| 228 | + class Meta: |
| 229 | + app_label = "ignore" |
| 230 | + clustered_by = "one" |
| 231 | + number_of_shards = 3 |
| 232 | + |
| 233 | + with connection.schema_editor() as schema_editor: |
| 234 | + sql, params = schema_editor.table_sql(MetaOptions) |
| 235 | + assert "CLUSTERED BY (one) INTO 3 shards" in sql |
| 236 | + |
| 237 | + MetaOptions._meta.clustered_by = "one" |
| 238 | + MetaOptions._meta.number_of_shards = OMITTED |
| 239 | + with connection.schema_editor() as schema_editor: |
| 240 | + sql, params = schema_editor.table_sql(MetaOptions) |
| 241 | + assert "CLUSTERED BY (one)" in sql |
| 242 | + assert "INTO 3 shards" not in sql |
| 243 | + |
| 244 | + MetaOptions._meta.clustered_by = OMITTED |
| 245 | + MetaOptions._meta.number_of_shards = 3 |
| 246 | + with connection.schema_editor() as schema_editor: |
| 247 | + sql, params = schema_editor.table_sql(MetaOptions) |
| 248 | + assert "CLUSTERED INTO 3 shards" not in sql |
| 249 | + |
| 250 | + MetaOptions._meta.clustered_by = OMITTED |
| 251 | + MetaOptions._meta.number_of_shards = OMITTED |
| 252 | + with connection.schema_editor() as schema_editor: |
| 253 | + sql, params = schema_editor.table_sql(MetaOptions) |
| 254 | + assert "INTO 3 shards" not in sql |
| 255 | + assert "CLUSTERED" not in sql |
| 256 | + |
| 257 | + with pytest.raises(ValueError, match="Column 'nocolumn' does not exist in model"): |
| 258 | + MetaOptions._meta.clustered_by = "nocolumn" |
| 259 | + MetaOptions._meta.number_of_shards = OMITTED |
| 260 | + with connection.schema_editor() as schema_editor: |
| 261 | + schema_editor.table_sql(MetaOptions) |
| 262 | + |
| 263 | + with pytest.raises( |
| 264 | + ValueError, match="clustered_by has to be a non-empty string, not 1" |
| 265 | + ): |
| 266 | + MetaOptions._meta.clustered_by = 1 |
| 267 | + with connection.schema_editor() as schema_editor: |
| 268 | + schema_editor.table_sql(MetaOptions) |
| 269 | + |
| 270 | + with pytest.raises( |
| 271 | + ValueError, match="number_of_shards has to be an integer bigger than 0" |
| 272 | + ): |
| 273 | + MetaOptions._meta.clustered_by = OMITTED |
| 274 | + MetaOptions._meta.number_of_shards = 0 |
| 275 | + with connection.schema_editor() as schema_editor: |
| 276 | + schema_editor.table_sql(MetaOptions) |
| 277 | + |
| 278 | + with pytest.raises( |
| 279 | + ValueError, |
| 280 | + match="number_of_shards has to be an integer bigger than 0, " "not 'abcdef'", |
| 281 | + ): |
| 282 | + MetaOptions._meta.clustered_by = OMITTED |
| 283 | + MetaOptions._meta.number_of_shards = "abcdef" |
| 284 | + with connection.schema_editor() as schema_editor: |
| 285 | + schema_editor.table_sql(MetaOptions) |
0 commit comments