Skip to content

Commit

Permalink
Fix unique_within_resource rule used in resources without datasource …
Browse files Browse the repository at this point in the history
…filter
  • Loading branch information
Arnau Orriols committed Mar 14, 2020
1 parent a486c21 commit c09cbfa
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
4 changes: 3 additions & 1 deletion docs/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1315,7 +1315,9 @@ defining the field validation rules. Allowed validation rules are:
Use this when the resource shares the database
collection with other resources but their documents
should not be taken into account when evaluating
the uniqueness of the field.
the uniqueness of the field. When used in a resource
without datasource filter, this rule behaves like
``unique``.

``data_relation`` Allows to specify a referential integrity rule
that the value must satisfy in order to
Expand Down
2 changes: 2 additions & 0 deletions eve/io/mongo/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ def _validate_unique_to_user(self, unique, field, value):
def _validate_unique_within_resource(self, unique, field, value):
""" {'type': 'boolean'} """
_, filter_, _, _ = app.data.datasource(self.resource)
if filter_ is None:
filter_ = {}
self._is_value_unique(unique, field, value, filter_)

def _validate_unique(self, unique, field, value):
Expand Down
16 changes: 15 additions & 1 deletion eve/tests/methods/post.py
Original file line number Diff line number Diff line change
Expand Up @@ -975,12 +975,26 @@ def test_post_projection_is_honored(self):
self.assertTrue("ref" in r)
self.assertTrue("aninteger" not in r)

def test_unique_value_different_resources(self):
def test_unique_within_resource_value_different_resources(self):
r, status = self.post("tenant_a", data={"name": "John"})
self.assert201(status)
r, status = self.post("tenant_b", data={"name": "John"})
self.assert201(status)

def test_unique_within_resource_in_resource_without_filter(self):
r, status = self.post(
"test_unique", data={"unique_within_resource_attribute": "unique_value"}
)
self.assert201(status)
r, status = self.post(
"test_unique", data={"unique_within_resource_attribute": "unique_value"}
)
self.assert422(status)
r, status = self.post(
"test_unique", data={"unique_within_resource_attribute": "unique_value 2"}
)
self.assert201(status)

def perform_post(self, data, valid_items=[0]):
r, status = self.post(self.known_resource_url, data=data)
self.assert201(status)
Expand Down
12 changes: 12 additions & 0 deletions eve/tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,17 @@
},
}

test_unique = {
"datasource": {"source": "test_unique"},
"schema": {
"unique_attribute": {"type": "string", "unique": True},
"unique_within_resource_attribute": {
"type": "string",
"unique_within_resource": True,
},
},
}

child_products = copy.deepcopy(products)
child_products["url"] = 'products/<regex("[A-Z]+"):parent_product>/children'
child_products["datasource"] = {"source": "products"}
Expand Down Expand Up @@ -334,4 +345,5 @@
"test_patch": test_patch,
"tenant_a": tenant_a,
"tenant_b": tenant_b,
"test_unique": test_unique,
}

0 comments on commit c09cbfa

Please sign in to comment.