Skip to content

Commit b098aae

Browse files
committed
chore: refactor CRATE_META_OPTIONS from a dict to an Enum
1 parent a5c116f commit b098aae

File tree

2 files changed

+41
-19
lines changed

2 files changed

+41
-19
lines changed

cratedb_django/models/model.py

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from enum import Enum, auto
2+
13
from django.db import models, connection
24
from django.db.models.base import ModelBase
35

@@ -6,28 +8,47 @@
68
_OMITTED = type("OMITTED", (), {"__bool__": lambda _: False})
79
OMITTED = _OMITTED()
810

9-
# dict of all the extra options a CrateDBModel Meta class has.
10-
# (name, default_value)
11-
CRATE_META_OPTIONS = {
12-
"auto_refresh": False, # Automatically refresh a table on inserts.
13-
"partition_by": OMITTED,
14-
"clustered_by": OMITTED,
15-
"number_of_shards": OMITTED,
16-
}
11+
12+
class CrateMetaOptions(Enum):
13+
"""
14+
Represents the specific options a CrateDB table can have.
15+
"""
16+
17+
auto_refresh = auto(), False, False
18+
partition_by = auto(), OMITTED, False
19+
clustered_by = auto(), OMITTED, False
20+
number_of_shards = auto(), OMITTED, False
21+
22+
def __init__(self, _, current_value, used_in_parameters_table):
23+
self.current_value = current_value
24+
self.used_in_parameters_table = used_in_parameters_table
25+
26+
@staticmethod
27+
def options():
28+
return list(CrateMetaOptions.__members__)
29+
30+
@classmethod
31+
def by_name(cls, name):
32+
for m in cls:
33+
if m.value[1] == name:
34+
return m
35+
raise KeyError(name)
1736

1837

1938
class MetaCrate(ModelBase):
2039
def __new__(cls, name, bases, attrs, **kwargs):
21-
crate_attrs = {}
40+
_temp_crate_attrs = {}
2241

2342
# todo document
2443

2544
try:
2645
meta = attrs["Meta"]
27-
for key, default_value in CRATE_META_OPTIONS.items():
28-
crate_attrs[key] = getattr(meta, key, default_value)
29-
if hasattr(meta, key):
30-
delattr(meta, key)
46+
for option in CrateMetaOptions:
47+
_temp_crate_attrs[option.name] = getattr(
48+
meta, option.name, option.current_value
49+
)
50+
if hasattr(meta, option.name):
51+
delattr(meta, option.name)
3152
except KeyError:
3253
# Has no meta class
3354
pass
@@ -36,7 +57,7 @@ def __new__(cls, name, bases, attrs, **kwargs):
3657

3758
# Return back the crate_attrs we took from meta to the already
3859
# created object.
39-
for k, v in crate_attrs.items():
60+
for k, v in _temp_crate_attrs.items():
4061
setattr(o._meta, k, v)
4162
return o
4263

tests/test_model.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pytest
22

33
from cratedb_django.models import CrateDBModel
4-
from cratedb_django.models.model import CRATE_META_OPTIONS, OMITTED
4+
from cratedb_django.models.model import CrateMetaOptions, OMITTED
55
from cratedb_django import fields
66

77
from django.forms.models import model_to_dict
@@ -57,6 +57,7 @@ def test_insert_model_field():
5757

5858

5959
def test_update_model():
60+
assert SimpleModel.objects.count() == 0
6061
obj = SimpleModel.objects.create(field="text")
6162
SimpleModel.refresh()
6263
pk = obj.pk
@@ -131,15 +132,15 @@ class Meta:
131132
auto_refresh = True
132133

133134
# Check all defaults are set.
134-
for key, default_value in CRATE_META_OPTIONS.items():
135-
assert key in NoMetaOptions._meta.__dict__
136-
assert getattr(NoMetaOptions._meta, key) is default_value
135+
for option in CrateMetaOptions:
136+
assert option.name in NoMetaOptions._meta.__dict__
137+
assert getattr(NoMetaOptions._meta, option.name) is option.current_value
137138

138139
# Check the combination of user-defined + default.
139140
assert RefreshMetaOptions._meta.auto_refresh is True
140141
assert (
141142
RefreshMetaOptions._meta.partition_by
142-
is CRATE_META_OPTIONS["partition_by"]
143+
is CrateMetaOptions.partition_by.current_value
143144
)
144145

145146

0 commit comments

Comments
 (0)