Skip to content

Commit 26c7437

Browse files
author
Saurabh Badenkal
committed
Address new Copilot review comments (round 3)
- Use VALIDATION_SQL_EMPTY constant instead of string literal in batch sql() - Use VALIDATION_UNSUPPORTED_COLUMN_TYPE constant in all 3 _odata.py locations - Import the constant from _error_codes.py - Add input validation to _build_create_multiple (all items must be dicts) - Narrow test assertions from Exception to ValidationError in test_odata_internal
1 parent 7d714be commit 26c7437

3 files changed

Lines changed: 14 additions & 6 deletions

File tree

src/PowerPlatform/Dataverse/data/_odata.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
_is_transient_status,
3131
VALIDATION_SQL_NOT_STRING,
3232
VALIDATION_SQL_EMPTY,
33+
VALIDATION_UNSUPPORTED_COLUMN_TYPE,
3334
METADATA_ENTITYSET_NOT_FOUND,
3435
METADATA_ENTITYSET_NAME_MISSING,
3536
METADATA_TABLE_NOT_FOUND,
@@ -1643,7 +1644,7 @@ def _create_columns(
16431644
if not attr:
16441645
raise ValidationError(
16451646
f"Unsupported column type '{column_type}' for column '{column_name}'.",
1646-
subcode="unsupported_column_type",
1647+
subcode=VALIDATION_UNSUPPORTED_COLUMN_TYPE,
16471648
)
16481649
if "OptionSet" in attr:
16491650
needs_picklist_flush = True
@@ -1759,6 +1760,8 @@ def _build_create_multiple(
17591760
records: List[Dict[str, Any]],
17601761
) -> _RawRequest:
17611762
"""Build a CreateMultiple POST request without sending it."""
1763+
if not all(isinstance(r, dict) for r in records):
1764+
raise TypeError("All items for multi-create must be dicts")
17621765
logical_name = table.lower()
17631766
enriched = []
17641767
for r in records:
@@ -2007,7 +2010,7 @@ def _build_create_entity(
20072010
if not attr:
20082011
raise ValidationError(
20092012
f"Unsupported column type '{dtype}' for column '{col_name}'.",
2010-
subcode="unsupported_column_type",
2013+
subcode=VALIDATION_UNSUPPORTED_COLUMN_TYPE,
20112014
)
20122015
attributes.append(attr)
20132016
body = {
@@ -2078,7 +2081,7 @@ def _build_create_column(
20782081
if not attr:
20792082
raise ValidationError(
20802083
f"Unsupported column type '{dtype}' for column '{col_name}'.",
2081-
subcode="unsupported_column_type",
2084+
subcode=VALIDATION_UNSUPPORTED_COLUMN_TYPE,
20822085
)
20832086
return _RawRequest(
20842087
method="POST",

src/PowerPlatform/Dataverse/operations/batch.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import pandas as pd
1111

1212
from ..core.errors import ValidationError
13+
from ..core._error_codes import VALIDATION_SQL_EMPTY
1314
from ..data._batch import (
1415
_BatchClient,
1516
_ChangeSet,
@@ -538,7 +539,10 @@ def sql(self, sql: str) -> None:
538539
batch.query.sql("SELECT accountid, name FROM account WHERE name = 'Contoso'")
539540
"""
540541
if not isinstance(sql, str) or not sql.strip():
541-
raise ValidationError("sql must be a non-empty string", subcode="VALIDATION_SQL_EMPTY")
542+
raise ValidationError(
543+
"sql must be a non-empty string",
544+
subcode=VALIDATION_SQL_EMPTY,
545+
)
542546
self._batch._items.append(_QuerySql(sql=sql.strip()))
543547

544548

tests/unit/data/test_odata_internal.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import unittest
66
from unittest.mock import MagicMock
77

8+
from PowerPlatform.Dataverse.core.errors import ValidationError
89
from PowerPlatform.Dataverse.data._odata import _ODataClient
910

1011

@@ -588,7 +589,7 @@ def test_multiple_targets_all_have_odata_id(self):
588589

589590
def test_conflicting_key_field_raises(self):
590591
"""Raises when a record field contradicts its alternate key value."""
591-
with self.assertRaises(Exception) as ctx:
592+
with self.assertRaises(ValidationError) as ctx:
592593
self.od._build_upsert_multiple(
593594
"accounts",
594595
"account",
@@ -599,7 +600,7 @@ def test_conflicting_key_field_raises(self):
599600

600601
def test_mismatched_lengths_raises(self):
601602
"""Raises when alternate_keys and records lengths differ."""
602-
with self.assertRaises(Exception):
603+
with self.assertRaises(ValidationError):
603604
self.od._build_upsert_multiple("accounts", "account", [{"accountnumber": "ACC-001"}], [])
604605

605606
def test_url_contains_upsert_multiple_action(self):

0 commit comments

Comments
 (0)