2014-03-15 01:34:49 +08:00
|
|
|
import json
|
|
|
|
|
2018-04-03 23:12:56 +08:00
|
|
|
from django.core import checks, exceptions, serializers
|
2019-07-22 16:45:26 +08:00
|
|
|
from django.db import connection
|
2020-11-16 06:43:47 +08:00
|
|
|
from django.db.models import F, OuterRef, Subquery
|
2019-08-20 15:54:41 +08:00
|
|
|
from django.db.models.expressions import RawSQL
|
2015-05-24 23:52:35 +08:00
|
|
|
from django.forms import Form
|
2019-07-22 16:45:26 +08:00
|
|
|
from django.test.utils import CaptureQueriesContext, isolate_apps
|
2014-03-15 01:34:49 +08:00
|
|
|
|
2018-11-27 03:05:02 +08:00
|
|
|
from . import PostgreSQLSimpleTestCase, PostgreSQLTestCase
|
2018-04-03 23:12:56 +08:00
|
|
|
from .models import HStoreModel, PostgreSQLModel
|
2014-03-15 01:34:49 +08:00
|
|
|
|
2017-09-07 20:16:21 +08:00
|
|
|
try:
|
2015-04-05 00:10:26 +08:00
|
|
|
from django.contrib.postgres import forms
|
|
|
|
from django.contrib.postgres.fields import HStoreField
|
2019-08-14 21:25:35 +08:00
|
|
|
from django.contrib.postgres.fields.hstore import KeyTransform
|
2015-04-05 00:10:26 +08:00
|
|
|
from django.contrib.postgres.validators import KeysValidator
|
2017-09-07 20:16:21 +08:00
|
|
|
except ImportError:
|
|
|
|
pass
|
2014-03-15 01:34:49 +08:00
|
|
|
|
2015-04-05 00:10:26 +08:00
|
|
|
|
2018-11-27 03:05:02 +08:00
|
|
|
class SimpleTests(PostgreSQLTestCase):
|
2014-03-15 01:34:49 +08:00
|
|
|
def test_save_load_success(self):
|
|
|
|
value = {"a": "b"}
|
|
|
|
instance = HStoreModel(field=value)
|
|
|
|
instance.save()
|
|
|
|
reloaded = HStoreModel.objects.get()
|
|
|
|
self.assertEqual(reloaded.field, value)
|
|
|
|
|
|
|
|
def test_null(self):
|
|
|
|
instance = HStoreModel(field=None)
|
|
|
|
instance.save()
|
|
|
|
reloaded = HStoreModel.objects.get()
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIsNone(reloaded.field)
|
2014-03-15 01:34:49 +08:00
|
|
|
|
|
|
|
def test_value_null(self):
|
|
|
|
value = {"a": None}
|
|
|
|
instance = HStoreModel(field=value)
|
|
|
|
instance.save()
|
|
|
|
reloaded = HStoreModel.objects.get()
|
|
|
|
self.assertEqual(reloaded.field, value)
|
|
|
|
|
2016-01-25 20:30:40 +08:00
|
|
|
def test_key_val_cast_to_string(self):
|
2017-03-04 22:47:49 +08:00
|
|
|
value = {"a": 1, "b": "B", 2: "c", "ï": "ê"}
|
|
|
|
expected_value = {"a": "1", "b": "B", "2": "c", "ï": "ê"}
|
2016-01-25 20:30:40 +08:00
|
|
|
|
|
|
|
instance = HStoreModel.objects.create(field=value)
|
|
|
|
instance = HStoreModel.objects.get()
|
2017-03-17 19:51:48 +08:00
|
|
|
self.assertEqual(instance.field, expected_value)
|
2016-01-25 20:30:40 +08:00
|
|
|
|
|
|
|
instance = HStoreModel.objects.get(field__a=1)
|
2017-03-17 19:51:48 +08:00
|
|
|
self.assertEqual(instance.field, expected_value)
|
2016-01-25 20:30:40 +08:00
|
|
|
|
|
|
|
instance = HStoreModel.objects.get(field__has_keys=[2, "a", "ï"])
|
2017-03-17 19:51:48 +08:00
|
|
|
self.assertEqual(instance.field, expected_value)
|
2016-01-25 20:30:40 +08:00
|
|
|
|
2017-05-03 12:24:54 +08:00
|
|
|
def test_array_field(self):
|
|
|
|
value = [
|
|
|
|
{"a": 1, "b": "B", 2: "c", "ï": "ê"},
|
|
|
|
{"a": 1, "b": "B", 2: "c", "ï": "ê"},
|
|
|
|
]
|
|
|
|
expected_value = [
|
|
|
|
{"a": "1", "b": "B", "2": "c", "ï": "ê"},
|
|
|
|
{"a": "1", "b": "B", "2": "c", "ï": "ê"},
|
|
|
|
]
|
|
|
|
instance = HStoreModel.objects.create(array_field=value)
|
|
|
|
instance.refresh_from_db()
|
|
|
|
self.assertEqual(instance.array_field, expected_value)
|
|
|
|
|
2014-03-15 01:34:49 +08:00
|
|
|
|
2018-11-27 03:05:02 +08:00
|
|
|
class TestQuerying(PostgreSQLTestCase):
|
2018-11-24 09:59:38 +08:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
|
|
|
cls.objs = HStoreModel.objects.bulk_create(
|
|
|
|
[
|
|
|
|
HStoreModel(field={"a": "b"}),
|
|
|
|
HStoreModel(field={"a": "b", "c": "d"}),
|
|
|
|
HStoreModel(field={"c": "d"}),
|
|
|
|
HStoreModel(field={}),
|
|
|
|
HStoreModel(field=None),
|
2021-10-01 09:57:56 +08:00
|
|
|
HStoreModel(field={"cat": "TigrOu", "breed": "birman"}),
|
|
|
|
HStoreModel(field={"cat": "minou", "breed": "ragdoll"}),
|
|
|
|
HStoreModel(field={"cat": "kitty", "breed": "Persian"}),
|
|
|
|
HStoreModel(field={"cat": "Kit Kat", "breed": "persian"}),
|
2018-11-24 09:59:38 +08:00
|
|
|
]
|
|
|
|
)
|
2014-03-15 01:34:49 +08:00
|
|
|
|
|
|
|
def test_exact(self):
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
HStoreModel.objects.filter(field__exact={"a": "b"}), self.objs[:1]
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_contained_by(self):
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
HStoreModel.objects.filter(field__contained_by={"a": "b", "c": "d"}),
|
|
|
|
self.objs[:4],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_contains(self):
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
HStoreModel.objects.filter(field__contains={"a": "b"}), self.objs[:2]
|
|
|
|
)
|
|
|
|
|
2015-11-07 23:06:06 +08:00
|
|
|
def test_in_generator(self):
|
|
|
|
def search():
|
|
|
|
yield {"a": "b"}
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2015-11-07 23:06:06 +08:00
|
|
|
self.assertSequenceEqual(
|
|
|
|
HStoreModel.objects.filter(field__in=search()), self.objs[:1]
|
|
|
|
)
|
|
|
|
|
2014-03-15 01:34:49 +08:00
|
|
|
def test_has_key(self):
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
HStoreModel.objects.filter(field__has_key="c"), self.objs[1:3]
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_has_keys(self):
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
HStoreModel.objects.filter(field__has_keys=["a", "c"]), self.objs[1:2]
|
|
|
|
)
|
|
|
|
|
2015-05-31 04:22:36 +08:00
|
|
|
def test_has_any_keys(self):
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
HStoreModel.objects.filter(field__has_any_keys=["a", "c"]), self.objs[:3]
|
|
|
|
)
|
|
|
|
|
2014-03-15 01:34:49 +08:00
|
|
|
def test_key_transform(self):
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
HStoreModel.objects.filter(field__a="b"), self.objs[:2]
|
|
|
|
)
|
|
|
|
|
2019-08-14 21:25:35 +08:00
|
|
|
def test_key_transform_raw_expression(self):
|
|
|
|
expr = RawSQL("%s::hstore", ["x => b, y => c"])
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
HStoreModel.objects.filter(field__a=KeyTransform("x", expr)), self.objs[:2]
|
|
|
|
)
|
|
|
|
|
2020-11-16 06:43:47 +08:00
|
|
|
def test_key_transform_annotation(self):
|
|
|
|
qs = HStoreModel.objects.annotate(a=F("field__a"))
|
|
|
|
self.assertCountEqual(
|
|
|
|
qs.values_list("a", flat=True),
|
2021-10-01 09:57:56 +08:00
|
|
|
["b", "b", None, None, None, None, None, None, None],
|
2020-11-16 06:43:47 +08:00
|
|
|
)
|
|
|
|
|
2014-03-15 01:34:49 +08:00
|
|
|
def test_keys(self):
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
HStoreModel.objects.filter(field__keys=["a"]), self.objs[:1]
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_values(self):
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
HStoreModel.objects.filter(field__values=["b"]), self.objs[:1]
|
|
|
|
)
|
|
|
|
|
2021-10-01 09:57:56 +08:00
|
|
|
def test_field_chaining_contains(self):
|
2014-03-15 01:34:49 +08:00
|
|
|
self.assertSequenceEqual(
|
|
|
|
HStoreModel.objects.filter(field__a__contains="b"), self.objs[:2]
|
|
|
|
)
|
|
|
|
|
2021-10-01 09:57:56 +08:00
|
|
|
def test_field_chaining_icontains(self):
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
HStoreModel.objects.filter(field__cat__icontains="INo"),
|
|
|
|
[self.objs[6]],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_field_chaining_startswith(self):
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
HStoreModel.objects.filter(field__cat__startswith="kit"),
|
|
|
|
[self.objs[7]],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_field_chaining_istartswith(self):
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
HStoreModel.objects.filter(field__cat__istartswith="kit"),
|
|
|
|
self.objs[7:],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_field_chaining_endswith(self):
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
HStoreModel.objects.filter(field__cat__endswith="ou"),
|
|
|
|
[self.objs[6]],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_field_chaining_iendswith(self):
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
HStoreModel.objects.filter(field__cat__iendswith="ou"),
|
|
|
|
self.objs[5:7],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_field_chaining_iexact(self):
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
HStoreModel.objects.filter(field__breed__iexact="persian"),
|
|
|
|
self.objs[7:],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_field_chaining_regex(self):
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
HStoreModel.objects.filter(field__cat__regex=r"ou$"),
|
|
|
|
[self.objs[6]],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_field_chaining_iregex(self):
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
HStoreModel.objects.filter(field__cat__iregex=r"oU$"),
|
|
|
|
self.objs[5:7],
|
|
|
|
)
|
|
|
|
|
2017-06-18 23:53:40 +08:00
|
|
|
def test_order_by_field(self):
|
|
|
|
more_objs = (
|
|
|
|
HStoreModel.objects.create(field={"g": "637"}),
|
|
|
|
HStoreModel.objects.create(field={"g": "002"}),
|
|
|
|
HStoreModel.objects.create(field={"g": "042"}),
|
|
|
|
HStoreModel.objects.create(field={"g": "981"}),
|
|
|
|
)
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
HStoreModel.objects.filter(field__has_key="g").order_by("field__g"),
|
|
|
|
[more_objs[1], more_objs[2], more_objs[0], more_objs[3]],
|
|
|
|
)
|
|
|
|
|
2014-03-15 01:34:49 +08:00
|
|
|
def test_keys_contains(self):
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
HStoreModel.objects.filter(field__keys__contains=["a"]), self.objs[:2]
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_values_overlap(self):
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
HStoreModel.objects.filter(field__values__overlap=["b", "d"]), self.objs[:3]
|
|
|
|
)
|
2015-05-13 21:52:19 +08:00
|
|
|
|
|
|
|
def test_key_isnull(self):
|
|
|
|
obj = HStoreModel.objects.create(field={"a": None})
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
HStoreModel.objects.filter(field__a__isnull=True),
|
2021-10-01 09:57:56 +08:00
|
|
|
self.objs[2:9] + [obj],
|
2015-05-13 21:52:19 +08:00
|
|
|
)
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
HStoreModel.objects.filter(field__a__isnull=False), self.objs[:2]
|
|
|
|
)
|
2014-03-15 01:34:49 +08:00
|
|
|
|
2015-06-04 23:15:25 +08:00
|
|
|
def test_usage_in_subquery(self):
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
HStoreModel.objects.filter(id__in=HStoreModel.objects.filter(field__a="b")),
|
|
|
|
self.objs[:2],
|
|
|
|
)
|
|
|
|
|
2019-07-22 16:45:26 +08:00
|
|
|
def test_key_sql_injection(self):
|
|
|
|
with CaptureQueriesContext(connection) as queries:
|
|
|
|
self.assertFalse(
|
|
|
|
HStoreModel.objects.filter(
|
|
|
|
**{
|
|
|
|
"field__test' = 'a') OR 1 = 1 OR ('d": "x",
|
|
|
|
}
|
|
|
|
).exists()
|
|
|
|
)
|
|
|
|
self.assertIn(
|
|
|
|
"""."field" -> 'test'' = ''a'') OR 1 = 1 OR (''d') = 'x' """,
|
|
|
|
queries[0]["sql"],
|
|
|
|
)
|
|
|
|
|
2019-09-16 11:25:50 +08:00
|
|
|
def test_obj_subquery_lookup(self):
|
|
|
|
qs = HStoreModel.objects.annotate(
|
|
|
|
value=Subquery(
|
|
|
|
HStoreModel.objects.filter(pk=OuterRef("pk")).values("field")
|
|
|
|
),
|
|
|
|
).filter(value__a="b")
|
|
|
|
self.assertSequenceEqual(qs, self.objs[:2])
|
|
|
|
|
2014-03-15 01:34:49 +08:00
|
|
|
|
2018-04-03 23:12:56 +08:00
|
|
|
@isolate_apps("postgres_tests")
|
2018-11-27 03:05:02 +08:00
|
|
|
class TestChecks(PostgreSQLSimpleTestCase):
|
2018-04-03 23:12:56 +08:00
|
|
|
def test_invalid_default(self):
|
|
|
|
class MyModel(PostgreSQLModel):
|
|
|
|
field = HStoreField(default={})
|
|
|
|
|
|
|
|
model = MyModel()
|
|
|
|
self.assertEqual(
|
|
|
|
model.check(),
|
|
|
|
[
|
|
|
|
checks.Warning(
|
|
|
|
msg=(
|
|
|
|
"HStoreField default should be a callable instead of an "
|
|
|
|
"instance so that it's not shared between all field "
|
|
|
|
"instances."
|
|
|
|
),
|
|
|
|
hint="Use a callable instead, e.g., use `dict` instead of `{}`.",
|
|
|
|
obj=MyModel._meta.get_field("field"),
|
2019-10-17 17:36:39 +08:00
|
|
|
id="fields.E010",
|
2018-04-03 23:12:56 +08:00
|
|
|
)
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_valid_default(self):
|
|
|
|
class MyModel(PostgreSQLModel):
|
|
|
|
field = HStoreField(default=dict)
|
|
|
|
|
|
|
|
self.assertEqual(MyModel().check(), [])
|
|
|
|
|
|
|
|
|
2018-11-27 03:05:02 +08:00
|
|
|
class TestSerialization(PostgreSQLSimpleTestCase):
|
2017-05-03 12:24:54 +08:00
|
|
|
test_data = json.dumps(
|
2022-02-04 03:24:19 +08:00
|
|
|
[
|
2017-05-03 12:24:54 +08:00
|
|
|
{
|
|
|
|
"model": "postgres_tests.hstoremodel",
|
|
|
|
"pk": None,
|
|
|
|
"fields": {
|
|
|
|
"field": json.dumps({"a": "b"}),
|
|
|
|
"array_field": json.dumps(
|
|
|
|
[
|
|
|
|
json.dumps({"a": "b"}),
|
|
|
|
json.dumps({"b": "a"}),
|
|
|
|
]
|
|
|
|
),
|
|
|
|
},
|
|
|
|
}
|
2022-02-04 03:24:19 +08:00
|
|
|
]
|
2017-05-03 12:24:54 +08:00
|
|
|
)
|
2014-03-15 01:34:49 +08:00
|
|
|
|
|
|
|
def test_dumping(self):
|
2017-05-03 12:24:54 +08:00
|
|
|
instance = HStoreModel(field={"a": "b"}, array_field=[{"a": "b"}, {"b": "a"}])
|
2014-03-15 01:34:49 +08:00
|
|
|
data = serializers.serialize("json", [instance])
|
|
|
|
self.assertEqual(json.loads(data), json.loads(self.test_data))
|
|
|
|
|
|
|
|
def test_loading(self):
|
|
|
|
instance = list(serializers.deserialize("json", self.test_data))[0].object
|
|
|
|
self.assertEqual(instance.field, {"a": "b"})
|
2017-05-03 12:24:54 +08:00
|
|
|
self.assertEqual(instance.array_field, [{"a": "b"}, {"b": "a"}])
|
2014-03-15 01:34:49 +08:00
|
|
|
|
2016-02-16 02:28:49 +08:00
|
|
|
def test_roundtrip_with_null(self):
|
|
|
|
instance = HStoreModel(field={"a": "b", "c": None})
|
|
|
|
data = serializers.serialize("json", [instance])
|
|
|
|
new_instance = list(serializers.deserialize("json", data))[0].object
|
|
|
|
self.assertEqual(instance.field, new_instance.field)
|
|
|
|
|
2014-03-15 01:34:49 +08:00
|
|
|
|
2018-11-27 03:05:02 +08:00
|
|
|
class TestValidation(PostgreSQLSimpleTestCase):
|
2014-03-15 01:34:49 +08:00
|
|
|
def test_not_a_string(self):
|
|
|
|
field = HStoreField()
|
|
|
|
with self.assertRaises(exceptions.ValidationError) as cm:
|
|
|
|
field.clean({"a": 1}, None)
|
|
|
|
self.assertEqual(cm.exception.code, "not_a_string")
|
2019-06-28 00:39:47 +08:00
|
|
|
self.assertEqual(
|
|
|
|
cm.exception.message % cm.exception.params,
|
|
|
|
"The value of “a” is not a string or null.",
|
|
|
|
)
|
2016-12-09 08:17:02 +08:00
|
|
|
|
|
|
|
def test_none_allowed_as_value(self):
|
|
|
|
field = HStoreField()
|
|
|
|
self.assertEqual(field.clean({"a": None}, None), {"a": None})
|
2014-03-15 01:34:49 +08:00
|
|
|
|
|
|
|
|
2018-11-27 03:05:02 +08:00
|
|
|
class TestFormField(PostgreSQLSimpleTestCase):
|
2014-03-15 01:34:49 +08:00
|
|
|
def test_valid(self):
|
|
|
|
field = forms.HStoreField()
|
|
|
|
value = field.clean('{"a": "b"}')
|
|
|
|
self.assertEqual(value, {"a": "b"})
|
|
|
|
|
|
|
|
def test_invalid_json(self):
|
|
|
|
field = forms.HStoreField()
|
|
|
|
with self.assertRaises(exceptions.ValidationError) as cm:
|
|
|
|
field.clean('{"a": "b"')
|
|
|
|
self.assertEqual(cm.exception.messages[0], "Could not load JSON data.")
|
|
|
|
self.assertEqual(cm.exception.code, "invalid_json")
|
|
|
|
|
2016-05-29 20:17:07 +08:00
|
|
|
def test_non_dict_json(self):
|
|
|
|
field = forms.HStoreField()
|
|
|
|
msg = "Input must be a JSON dictionary."
|
|
|
|
with self.assertRaisesMessage(exceptions.ValidationError, msg) as cm:
|
|
|
|
field.clean('["a", "b", 1]')
|
|
|
|
self.assertEqual(cm.exception.code, "invalid_format")
|
|
|
|
|
2014-03-15 01:34:49 +08:00
|
|
|
def test_not_string_values(self):
|
|
|
|
field = forms.HStoreField()
|
|
|
|
value = field.clean('{"a": 1}')
|
|
|
|
self.assertEqual(value, {"a": "1"})
|
|
|
|
|
2016-12-09 08:17:02 +08:00
|
|
|
def test_none_value(self):
|
|
|
|
field = forms.HStoreField()
|
|
|
|
value = field.clean('{"a": null}')
|
|
|
|
self.assertEqual(value, {"a": None})
|
|
|
|
|
2014-03-15 01:34:49 +08:00
|
|
|
def test_empty(self):
|
|
|
|
field = forms.HStoreField(required=False)
|
|
|
|
value = field.clean("")
|
|
|
|
self.assertEqual(value, {})
|
|
|
|
|
|
|
|
def test_model_field_formfield(self):
|
|
|
|
model_field = HStoreField()
|
|
|
|
form_field = model_field.formfield()
|
|
|
|
self.assertIsInstance(form_field, forms.HStoreField)
|
|
|
|
|
2015-08-04 08:47:58 +08:00
|
|
|
def test_field_has_changed(self):
|
2015-05-24 23:52:35 +08:00
|
|
|
class HStoreFormTest(Form):
|
2015-08-04 08:47:58 +08:00
|
|
|
f1 = forms.HStoreField()
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2015-05-24 22:56:48 +08:00
|
|
|
form_w_hstore = HStoreFormTest()
|
|
|
|
self.assertFalse(form_w_hstore.has_changed())
|
|
|
|
|
2015-08-04 08:47:58 +08:00
|
|
|
form_w_hstore = HStoreFormTest({"f1": '{"a": 1}'})
|
|
|
|
self.assertTrue(form_w_hstore.has_changed())
|
|
|
|
|
|
|
|
form_w_hstore = HStoreFormTest({"f1": '{"a": 1}'}, initial={"f1": '{"a": 1}'})
|
|
|
|
self.assertFalse(form_w_hstore.has_changed())
|
|
|
|
|
|
|
|
form_w_hstore = HStoreFormTest({"f1": '{"a": 2}'}, initial={"f1": '{"a": 1}'})
|
|
|
|
self.assertTrue(form_w_hstore.has_changed())
|
|
|
|
|
2015-08-08 01:06:56 +08:00
|
|
|
form_w_hstore = HStoreFormTest({"f1": '{"a": 1}'}, initial={"f1": {"a": 1}})
|
|
|
|
self.assertFalse(form_w_hstore.has_changed())
|
|
|
|
|
|
|
|
form_w_hstore = HStoreFormTest({"f1": '{"a": 2}'}, initial={"f1": {"a": 1}})
|
|
|
|
self.assertTrue(form_w_hstore.has_changed())
|
|
|
|
|
2014-03-15 01:34:49 +08:00
|
|
|
|
2018-11-27 03:05:02 +08:00
|
|
|
class TestValidator(PostgreSQLSimpleTestCase):
|
2014-03-15 01:34:49 +08:00
|
|
|
def test_simple_valid(self):
|
|
|
|
validator = KeysValidator(keys=["a", "b"])
|
|
|
|
validator({"a": "foo", "b": "bar", "c": "baz"})
|
|
|
|
|
|
|
|
def test_missing_keys(self):
|
|
|
|
validator = KeysValidator(keys=["a", "b"])
|
|
|
|
with self.assertRaises(exceptions.ValidationError) as cm:
|
|
|
|
validator({"a": "foo", "c": "baz"})
|
|
|
|
self.assertEqual(cm.exception.messages[0], "Some keys were missing: b")
|
|
|
|
self.assertEqual(cm.exception.code, "missing_keys")
|
|
|
|
|
|
|
|
def test_strict_valid(self):
|
|
|
|
validator = KeysValidator(keys=["a", "b"], strict=True)
|
|
|
|
validator({"a": "foo", "b": "bar"})
|
|
|
|
|
|
|
|
def test_extra_keys(self):
|
|
|
|
validator = KeysValidator(keys=["a", "b"], strict=True)
|
|
|
|
with self.assertRaises(exceptions.ValidationError) as cm:
|
|
|
|
validator({"a": "foo", "b": "bar", "c": "baz"})
|
|
|
|
self.assertEqual(cm.exception.messages[0], "Some unknown keys were provided: c")
|
|
|
|
self.assertEqual(cm.exception.code, "extra_keys")
|
|
|
|
|
|
|
|
def test_custom_messages(self):
|
|
|
|
messages = {
|
|
|
|
"missing_keys": "Foobar",
|
|
|
|
}
|
|
|
|
validator = KeysValidator(keys=["a", "b"], strict=True, messages=messages)
|
|
|
|
with self.assertRaises(exceptions.ValidationError) as cm:
|
|
|
|
validator({"a": "foo", "c": "baz"})
|
|
|
|
self.assertEqual(cm.exception.messages[0], "Foobar")
|
|
|
|
self.assertEqual(cm.exception.code, "missing_keys")
|
|
|
|
with self.assertRaises(exceptions.ValidationError) as cm:
|
|
|
|
validator({"a": "foo", "b": "bar", "c": "baz"})
|
|
|
|
self.assertEqual(cm.exception.messages[0], "Some unknown keys were provided: c")
|
|
|
|
self.assertEqual(cm.exception.code, "extra_keys")
|
|
|
|
|
|
|
|
def test_deconstruct(self):
|
|
|
|
messages = {
|
|
|
|
"missing_keys": "Foobar",
|
|
|
|
}
|
|
|
|
validator = KeysValidator(keys=["a", "b"], strict=True, messages=messages)
|
|
|
|
path, args, kwargs = validator.deconstruct()
|
|
|
|
self.assertEqual(path, "django.contrib.postgres.validators.KeysValidator")
|
|
|
|
self.assertEqual(args, ())
|
|
|
|
self.assertEqual(
|
|
|
|
kwargs, {"keys": ["a", "b"], "strict": True, "messages": messages}
|
|
|
|
)
|