diff --git a/arches/app/datatypes/datatypes.py b/arches/app/datatypes/datatypes.py index de1b6f4a97..87dc366c03 100644 --- a/arches/app/datatypes/datatypes.py +++ b/arches/app/datatypes/datatypes.py @@ -2375,7 +2375,6 @@ def to_json(self, tile, node): Resource, ) # import here rather than top to avoid circular import - resourceid = None data = self.get_tile_data(tile) if data: nodevalue = self.get_nodevalues(data[str(node.nodeid)]) @@ -2388,13 +2387,16 @@ def to_json(self, tile, node): except (TypeError, ValueError, KeyError): pass other_resources = Resource.objects.filter(pk__in=other_resource_ids) - for resourceid in other_resource_ids: + for resourceXresource in nodevalue: + tileResourceId = uuid.UUID(resourceXresource["resourceId"]) for candidate in other_resources: - if candidate.pk == resourceid: + if candidate.pk == tileResourceId: related_resource = candidate break else: - logger.info(f'Resource with id "{resourceid}" not in the system.') + logger.info( + f'Resource with id "{tileResourceId}" not in the system.' + ) continue displayname = related_resource.displayname() resourceXresource["display_value"] = displayname diff --git a/tests/utils/datatypes/datatype_tests.py b/tests/utils/datatypes/datatype_tests.py index a26a68572f..d1f9458bb0 100644 --- a/tests/utils/datatypes/datatype_tests.py +++ b/tests/utils/datatypes/datatype_tests.py @@ -17,11 +17,19 @@ """ import uuid -from unittest import mock +from unittest.mock import patch + +from django.conf import settings from arches.app.datatypes.base import BaseDataType from arches.app.datatypes.datatypes import DataTypeFactory -from arches.app.models.models import Language, Node +from arches.app.models.models import ( + GraphModel, + Language, + Node, + ResourceInstance, + ResourceInstanceLifecycle, +) from arches.app.models.tile import Tile from tests.base_test import ArchesTestCase @@ -222,31 +230,39 @@ def test_pre_structure_tile_data(self): class ResourceInstanceListDataTypeTests(ArchesTestCase): - mock_display_value = {"@display_value": "mock display value"} + def displayname(self, context=None): + return str(self.name) - @mock.patch( - "arches.app.datatypes.base.BaseDataType.compile_json", - return_value=mock_display_value, - ) - def test_to_json(self, _mock): + @patch("arches.app.models.resource.Resource.displayname", displayname) + def test_to_json(self): ri_list = DataTypeFactory().get_instance("resource-instance-list") - node = Node(pk=uuid.uuid4()) - resource_1_id = uuid.uuid4() - resource_2_id = uuid.uuid4() + + dummy_node = Node(pk=uuid.uuid4()) + graph = GraphModel.objects.create(isresource=True) + lifecycle = ResourceInstanceLifecycle.objects.get( + pk=settings.DEFAULT_RESOURCE_INSTANCE_LIFECYCLE_ID + ) + state = lifecycle.resource_instance_lifecycle_states.first() + resource1 = ResourceInstance.objects.create( + graph=graph, resource_instance_lifecycle_state=state, name="ONE" + ) + resource2 = ResourceInstance.objects.create( + graph=graph, resource_instance_lifecycle_state=state, name="TWO" + ) tile = Tile( { "resourceinstance_id": uuid.uuid4(), - "nodegroup_id": str(node.pk), + "nodegroup_id": str(dummy_node.pk), "tileid": "", "data": { - str(node.pk): [ + str(dummy_node.pk): [ { - "resourceId": str(resource_1_id), + "resourceId": str(resource1.pk), "ontologyProperty": "", "inverseOntologyProperty": "", }, { - "resourceId": str(resource_2_id), + "resourceId": str(resource2.pk), "ontologyProperty": "", "inverseOntologyProperty": "", }, @@ -255,8 +271,12 @@ def test_to_json(self, _mock): } ) - # TODO: remove mock, fix underlying functionality to not - # requery for Resource objects yet again in get_display_value(). - with self.assertNumQueries(1): - json = ri_list.to_json(tile, node) - self.assertEqual(json, self.mock_display_value) + # Was 4, is now 3, should be 1-2 after fixing: + # https://github.com/archesproject/arches/issues/11632 + with self.assertNumQueries(3): + json = ri_list.to_json(tile, dummy_node) + self.assertEqual(json["@display_value"], "ONE, TWO") + self.assertEqual( + [inner["display_value"] for inner in json["instance_details"]], + ["ONE", "TWO"], + )