|
| 1 | +import django |
| 2 | +import pytest |
| 3 | + |
| 4 | +from django.core.exceptions import ImproperlyConfigured |
| 5 | +from django.db import models |
| 6 | + |
1 | 7 | from psqlextra.models import PostgresPartitionedModel |
2 | 8 | from psqlextra.types import PostgresPartitioningMethod |
3 | 9 |
|
4 | | -from .fake_model import define_fake_partitioned_model |
| 10 | +from .fake_model import define_fake_model, define_fake_partitioned_model |
5 | 11 |
|
6 | 12 |
|
7 | 13 | def test_partitioned_model_abstract(): |
@@ -70,3 +76,190 @@ def test_partitioned_model_key_option_none(): |
70 | 76 | model = define_fake_partitioned_model(partitioning_options=dict(key=None)) |
71 | 77 |
|
72 | 78 | assert model._partitioning_meta.key == [] |
| 79 | + |
| 80 | + |
| 81 | +@pytest.mark.skipif( |
| 82 | + django.VERSION < (5, 2), |
| 83 | + reason="Django < 5.2 doesn't implement composite primary keys", |
| 84 | +) |
| 85 | +def test_partitioned_model_custom_composite_primary_key_with_auto_field(): |
| 86 | + model = define_fake_partitioned_model( |
| 87 | + fields={ |
| 88 | + "auto_id": models.AutoField(), |
| 89 | + "my_custom_pk": models.CompositePrimaryKey("auto_id", "timestamp"), |
| 90 | + "timestamp": models.DateTimeField(), |
| 91 | + }, |
| 92 | + partitioning_options=dict(key=["timestamp"], special=True), |
| 93 | + ) |
| 94 | + |
| 95 | + assert isinstance(model._meta.pk, models.CompositePrimaryKey) |
| 96 | + assert model._meta.pk.name == "my_custom_pk" |
| 97 | + assert model._meta.pk.columns == ("auto_id", "timestamp") |
| 98 | + |
| 99 | + |
| 100 | +@pytest.mark.skipif( |
| 101 | + django.VERSION < (5, 2), |
| 102 | + reason="Django < 5.2 doesn't implement composite primary keys", |
| 103 | +) |
| 104 | +def test_partitioned_model_custom_composite_primary_key_with_id_field(): |
| 105 | + model = define_fake_partitioned_model( |
| 106 | + fields={ |
| 107 | + "id": models.IntegerField(), |
| 108 | + "my_custom_pk": models.CompositePrimaryKey("id", "timestamp"), |
| 109 | + "timestamp": models.DateTimeField(), |
| 110 | + }, |
| 111 | + partitioning_options=dict(key=["timestamp"], special=True), |
| 112 | + ) |
| 113 | + |
| 114 | + assert isinstance(model._meta.pk, models.CompositePrimaryKey) |
| 115 | + assert model._meta.pk.name == "my_custom_pk" |
| 116 | + assert model._meta.pk.columns == ("id", "timestamp") |
| 117 | + |
| 118 | + |
| 119 | +@pytest.mark.skipif( |
| 120 | + django.VERSION < (5, 2), |
| 121 | + reason="Django < 5.2 doesn't implement composite primary keys", |
| 122 | +) |
| 123 | +def test_partitioned_model_custom_composite_primary_key_named_id(): |
| 124 | + model = define_fake_partitioned_model( |
| 125 | + fields={ |
| 126 | + "other_field": models.TextField(), |
| 127 | + "id": models.CompositePrimaryKey("other_field", "timestamp"), |
| 128 | + "timestamp": models.DateTimeField(), |
| 129 | + }, |
| 130 | + partitioning_options=dict(key=["timestamp"], special=True), |
| 131 | + ) |
| 132 | + |
| 133 | + assert isinstance(model._meta.pk, models.CompositePrimaryKey) |
| 134 | + assert model._meta.pk.name == "id" |
| 135 | + assert model._meta.pk.columns == ("other_field", "timestamp") |
| 136 | + |
| 137 | + |
| 138 | +@pytest.mark.skipif( |
| 139 | + django.VERSION < (5, 2), |
| 140 | + reason="Django < 5.2 doesn't implement composite primary keys", |
| 141 | +) |
| 142 | +def test_partitioned_model_field_named_pk_not_composite_not_primary(): |
| 143 | + with pytest.raises(ImproperlyConfigured): |
| 144 | + define_fake_partitioned_model( |
| 145 | + fields={ |
| 146 | + "pk": models.TextField(), |
| 147 | + "id": models.CompositePrimaryKey("other_field", "timestamp"), |
| 148 | + "timestamp": models.DateTimeField(), |
| 149 | + }, |
| 150 | + partitioning_options=dict(key=["timestamp"], special=True), |
| 151 | + ) |
| 152 | + |
| 153 | + |
| 154 | +@pytest.mark.skipif( |
| 155 | + django.VERSION < (5, 2), |
| 156 | + reason="Django < 5.2 doesn't implement composite primary keys", |
| 157 | +) |
| 158 | +def test_partitioned_model_field_named_pk_not_composite(): |
| 159 | + with pytest.raises(ImproperlyConfigured): |
| 160 | + define_fake_partitioned_model( |
| 161 | + fields={ |
| 162 | + "pk": models.AutoField(primary_key=True), |
| 163 | + "timestamp": models.DateTimeField(), |
| 164 | + }, |
| 165 | + partitioning_options=dict(key=["timestamp"], special=True), |
| 166 | + ) |
| 167 | + |
| 168 | + |
| 169 | +@pytest.mark.skipif( |
| 170 | + django.VERSION < (5, 2), |
| 171 | + reason="Django < 5.2 doesn't implement composite primary keys", |
| 172 | +) |
| 173 | +def test_partitioned_model_field_multiple_pks(): |
| 174 | + with pytest.raises(ImproperlyConfigured): |
| 175 | + define_fake_partitioned_model( |
| 176 | + fields={ |
| 177 | + "id": models.AutoField(primary_key=True), |
| 178 | + "another_pk": models.TextField(primary_key=True), |
| 179 | + "timestamp": models.DateTimeField(), |
| 180 | + "real_pk": models.CompositePrimaryKey("id", "timestamp"), |
| 181 | + }, |
| 182 | + partitioning_options=dict(key=["timestamp"], special=True), |
| 183 | + ) |
| 184 | + |
| 185 | + |
| 186 | +@pytest.mark.skipif( |
| 187 | + django.VERSION < (5, 2), |
| 188 | + reason="Django < 5.2 doesn't implement composite primary keys", |
| 189 | +) |
| 190 | +def test_partitioned_model_no_pk_defined(): |
| 191 | + model = define_fake_partitioned_model( |
| 192 | + fields={ |
| 193 | + "timestamp": models.DateTimeField(), |
| 194 | + }, |
| 195 | + partitioning_options=dict(key=["timestamp"], special=True), |
| 196 | + ) |
| 197 | + |
| 198 | + assert isinstance(model._meta.pk, models.CompositePrimaryKey) |
| 199 | + assert model._meta.pk.name == "pk" |
| 200 | + assert model._meta.pk.columns == ("id", "timestamp") |
| 201 | + |
| 202 | + id_field = model._meta.get_field("id") |
| 203 | + assert id_field.name == "id" |
| 204 | + assert id_field.column == "id" |
| 205 | + assert isinstance(id_field, models.AutoField) |
| 206 | + assert id_field.primary_key is True |
| 207 | + |
| 208 | + |
| 209 | +@pytest.mark.skipif( |
| 210 | + django.VERSION < (5, 2), |
| 211 | + reason="Django < 5.2 doesn't implement composite primary keys", |
| 212 | +) |
| 213 | +def test_partitioned_model_composite_primary_key(): |
| 214 | + model = define_fake_partitioned_model( |
| 215 | + fields={ |
| 216 | + "id": models.AutoField(primary_key=True), |
| 217 | + "pk": models.CompositePrimaryKey("id", "timestamp"), |
| 218 | + "timestamp": models.DateTimeField(), |
| 219 | + }, |
| 220 | + partitioning_options=dict(key=["timestamp"], special=True), |
| 221 | + ) |
| 222 | + |
| 223 | + assert isinstance(model._meta.pk, models.CompositePrimaryKey) |
| 224 | + assert model._meta.pk.name == "pk" |
| 225 | + assert model._meta.pk.columns == ("id", "timestamp") |
| 226 | + |
| 227 | + |
| 228 | +@pytest.mark.skipif( |
| 229 | + django.VERSION < (5, 2), |
| 230 | + reason="Django < 5.2 doesn't implement composite primary keys", |
| 231 | +) |
| 232 | +def test_partitioned_model_composite_primary_key_foreign_key(): |
| 233 | + model = define_fake_partitioned_model( |
| 234 | + fields={ |
| 235 | + "timestamp": models.DateTimeField(), |
| 236 | + }, |
| 237 | + partitioning_options=dict(key=["timestamp"], special=True), |
| 238 | + ) |
| 239 | + |
| 240 | + define_fake_model( |
| 241 | + fields={ |
| 242 | + "model": models.ForeignKey(model, on_delete=models.CASCADE), |
| 243 | + }, |
| 244 | + ) |
| 245 | + |
| 246 | + |
| 247 | +@pytest.mark.skipif( |
| 248 | + django.VERSION < (5, 2), |
| 249 | + reason="Django < 5.2 doesn't implement composite primary keys", |
| 250 | +) |
| 251 | +def test_partitioned_model_custom_composite_primary_key_foreign_key(): |
| 252 | + model = define_fake_partitioned_model( |
| 253 | + fields={ |
| 254 | + "id": models.TextField(primary_key=True), |
| 255 | + "timestamp": models.DateTimeField(), |
| 256 | + "custom": models.CompositePrimaryKey("id", "timestamp"), |
| 257 | + }, |
| 258 | + partitioning_options=dict(key=["timestamp"], special=True), |
| 259 | + ) |
| 260 | + |
| 261 | + define_fake_model( |
| 262 | + fields={ |
| 263 | + "model": models.ForeignKey(model, on_delete=models.CASCADE), |
| 264 | + }, |
| 265 | + ) |
0 commit comments