Skip to content

Commit

Permalink
Added tests to reproduce issue #1416
Browse files Browse the repository at this point in the history
  • Loading branch information
jjimenez-teldat authored and nicolaiarocci committed Oct 22, 2020
1 parent 870e60e commit 8bc4df4
Showing 1 changed file with 90 additions and 0 deletions.
90 changes: 90 additions & 0 deletions eve/tests/methods/get.py
Original file line number Diff line number Diff line change
Expand Up @@ -1098,6 +1098,96 @@ def test_get_reference_embedded_in_subdocuments(self):
"location" in content["_items"][0]["departments"][0]["members"][0]
)

def test_get_reference_embedded_in_subdocuments_with_nested_dicts(self):
_db = self.connection[MONGO_DBNAME]
cpu_brand_name = self.random_string(10)
cpu_brand = {
"name": cpu_brand_name,
"address": self.random_string(30),
}
motherboard_brand_name = self.random_string(15)
motherboard_brand = {
"name": motherboard_brand_name,
"address": self.random_string(30),
}
cpu_brand_id, motherboard_brand_id = _db.brands.insert_many(
[cpu_brand, motherboard_brand]
).inserted_ids
cpu_component = {
"name": self.random_string(12),
"price": 499,
"brand": cpu_brand_id,
}
motherboard_component = {
"name": self.random_string(18),
"price": 199,
"brand": motherboard_brand_id,
}
cpu_component_id, motherboard_component_id = _db.components.insert_many(
[cpu_component, motherboard_component]
).inserted_ids
computer = {
"name": self.random_string(25),
"components": {
"cpu": cpu_component_id,
"motherboard": motherboard_component_id,
},
}
computer_id = _db.computers.insert_one(computer).inserted_id
computers = self.domain["computers"]
components = self.domain["components"]
# Test that doesn't come embedded if asking for a field that
# isn't embedded ('embeddable' is False by default)
embedded = (
'{"components.cpu": 1, "components.motherboard": 1,'
+ ' "components.cpu.brand": 1, "components.motherboard.brand": 1}'
)
result = self.test_client.get(
"%s/%s/%s" % (computers["url"], computer_id, "?embedded=%s" % embedded)
)
self.assert200(result.status_code)
content = json.loads(result.get_data())
self.assertEqual(content["components"]["cpu"], str(cpu_component_id))
self.assertEqual(
content["components"]["motherboard"], str(motherboard_component_id)
)
# Set field to be embedded
computers["schema"]["components"]["schema"]["cpu"]["data_relation"][
"embeddable"
] = True
computers["schema"]["components"]["schema"]["motherboard"]["data_relation"][
"embeddable"
] = True
components["schema"]["brand"]["data_relation"]["embeddable"] = True
# Test that global setting applies even if field is set to embedded
computers["embedding"] = False
components["embedding"] = False
result = self.test_client.get(
"%s/%s/%s" % (computers["url"], computer_id, "?embedded=%s" % embedded)
)
self.assert200(result.status_code)
content = json.loads(result.get_data())
self.assertEqual(content["components"]["cpu"], str(cpu_component_id))
self.assertEqual(
content["components"]["motherboard"], str(motherboard_component_id)
)
# Test that it works
computers["embedding"] = True
components["embedding"] = True
result = self.test_client.get(
"%s/%s/%s" % (computers["url"], computer_id, "?embedded=%s" % embedded)
)
self.assert200(result.status_code)
content = json.loads(result.get_data())
self.assertEqual(
content["components"]["cpu"]["brand"]["name"],
cpu_brand_name,
)
self.assertEqual(
content["components"]["motherboard"]["brand"]["name"],
motherboard_brand_name,
)

def test_get_nested_resource(self):
response, status = self.get("users/overseas")
self.assertGet(response, status, "users_overseas")
Expand Down

0 comments on commit 8bc4df4

Please sign in to comment.