Skip to content

Commit 839f37b

Browse files
tpellissierclaude
andcommitted
Address review: raise on unknown type, reuse factories, list all fields in docstrings
- from_api_response raises ValueError for unrecognized @odata.type (Dataverse only supports OneToMany and ManyToMany) - from_api_response delegates to from_one_to_many/from_many_to_many instead of constructing cls() directly - Docstrings on create methods now list all returned fields explicitly Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent b8d8dcb commit 839f37b

3 files changed

Lines changed: 23 additions & 23 deletions

File tree

src/PowerPlatform/Dataverse/models/relationship_info.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -130,36 +130,34 @@ def from_api_response(cls, response_data: Dict[str, Any]) -> RelationshipInfo:
130130
131131
Detects one-to-many vs many-to-many from the ``@odata.type`` field
132132
in the response and maps PascalCase keys to snake_case attributes.
133+
Dataverse only supports these two relationship types; an unrecognized
134+
``@odata.type`` raises :class:`ValueError`.
133135
134136
:param response_data: Raw relationship metadata from the Web API.
135137
:type response_data: :class:`dict`
136138
:rtype: :class:`RelationshipInfo`
139+
:raises ValueError: If the ``@odata.type`` is not a recognized
140+
relationship type.
137141
"""
138142
odata_type = response_data.get("@odata.type", "")
139143
rel_id = response_data.get("MetadataId")
140144
schema_name = response_data.get("SchemaName", "")
141145

142146
if ODATA_TYPE_ONE_TO_MANY_RELATIONSHIP in odata_type:
143-
return cls(
147+
return cls.from_one_to_many(
144148
relationship_id=rel_id,
145149
relationship_schema_name=schema_name,
146-
relationship_type="one_to_many",
147-
referenced_entity=response_data.get("ReferencedEntity"),
148-
referencing_entity=response_data.get("ReferencingEntity"),
149-
lookup_schema_name=response_data.get("ReferencingEntityNavigationPropertyName"),
150+
referenced_entity=response_data.get("ReferencedEntity", ""),
151+
referencing_entity=response_data.get("ReferencingEntity", ""),
152+
lookup_schema_name=response_data.get("ReferencingEntityNavigationPropertyName", ""),
150153
)
151154

152155
if ODATA_TYPE_MANY_TO_MANY_RELATIONSHIP in odata_type:
153-
return cls(
156+
return cls.from_many_to_many(
154157
relationship_id=rel_id,
155158
relationship_schema_name=schema_name,
156-
relationship_type="many_to_many",
157-
entity1_logical_name=response_data.get("Entity1LogicalName"),
158-
entity2_logical_name=response_data.get("Entity2LogicalName"),
159+
entity1_logical_name=response_data.get("Entity1LogicalName", ""),
160+
entity2_logical_name=response_data.get("Entity2LogicalName", ""),
159161
)
160162

161-
# Fallback: unknown type, populate what we can
162-
return cls(
163-
relationship_id=rel_id,
164-
relationship_schema_name=schema_name,
165-
)
163+
raise ValueError(f"Unrecognized relationship @odata.type: {odata_type!r}")

src/PowerPlatform/Dataverse/operations/tables.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,9 @@ def create_one_to_many_relationship(
283283
:type solution: :class:`str` or None
284284
285285
:return: Relationship metadata with ``relationship_id``,
286-
``lookup_schema_name``, and entity names.
286+
``relationship_schema_name``, ``relationship_type``,
287+
``lookup_schema_name``, ``referenced_entity``, and
288+
``referencing_entity``.
287289
:rtype: :class:`~PowerPlatform.Dataverse.models.relationship_info.RelationshipInfo`
288290
289291
:raises ~PowerPlatform.Dataverse.core.errors.HttpError:
@@ -358,7 +360,8 @@ def create_many_to_many_relationship(
358360
:type solution: :class:`str` or None
359361
360362
:return: Relationship metadata with ``relationship_id``,
361-
``relationship_schema_name``, and entity names.
363+
``relationship_schema_name``, ``relationship_type``,
364+
``entity1_logical_name``, and ``entity2_logical_name``.
362365
:rtype: :class:`~PowerPlatform.Dataverse.models.relationship_info.RelationshipInfo`
363366
364367
:raises ~PowerPlatform.Dataverse.core.errors.HttpError:

tests/unit/models/test_relationship_info.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,12 @@ def test_many_to_many_detection(self):
124124
self.assertEqual(info.entity1_logical_name, "new_employee")
125125
self.assertEqual(info.entity2_logical_name, "new_project")
126126

127-
def test_unknown_type_fallback(self):
128-
"""Should handle unknown @odata.type gracefully."""
127+
def test_unknown_type_raises(self):
128+
"""Should raise ValueError for unrecognized @odata.type."""
129129
raw = {"MetadataId": "guid", "SchemaName": "unknown_rel"}
130-
info = RelationshipInfo.from_api_response(raw)
131-
self.assertEqual(info.relationship_id, "guid")
132-
self.assertEqual(info.relationship_schema_name, "unknown_rel")
133-
self.assertEqual(info.relationship_type, "")
130+
with self.assertRaises(ValueError) as ctx:
131+
RelationshipInfo.from_api_response(raw)
132+
self.assertIn("Unrecognized relationship", str(ctx.exception))
134133

135134
def test_missing_fields(self):
136135
"""Should handle missing optional fields without error."""
@@ -141,7 +140,7 @@ def test_missing_fields(self):
141140
info = RelationshipInfo.from_api_response(raw)
142141
self.assertEqual(info.relationship_type, "one_to_many")
143142
self.assertIsNone(info.relationship_id)
144-
self.assertIsNone(info.referenced_entity)
143+
self.assertEqual(info.referenced_entity, "")
145144

146145

147146
if __name__ == "__main__":

0 commit comments

Comments
 (0)