|
14 | 14 | _RecordCreate, |
15 | 15 | _RecordDelete, |
16 | 16 | _RecordGet, |
| 17 | + _RecordUpsert, |
17 | 18 | _TableGet, |
18 | 19 | _TableList, |
19 | 20 | _QuerySql, |
|
23 | 24 | _parse_http_response_part, |
24 | 25 | _CRLF, |
25 | 26 | ) |
| 27 | +from PowerPlatform.Dataverse.models.upsert import UpsertItem |
26 | 28 | from PowerPlatform.Dataverse.data._raw_request import _RawRequest |
27 | 29 | from PowerPlatform.Dataverse.models.batch import BatchItemResponse, BatchResult |
28 | 30 |
|
@@ -337,5 +339,124 @@ def test_operations_in_order(self): |
337 | 339 | self.assertIsInstance(cs.operations[1], _RecordDelete) |
338 | 340 |
|
339 | 341 |
|
| 342 | +class TestResolveBatchUpsert(unittest.TestCase): |
| 343 | + """Tests that _BatchClient._resolve_record_upsert calls the correct _build_* methods.""" |
| 344 | + |
| 345 | + def _client_and_od(self): |
| 346 | + od = _make_od() |
| 347 | + od._entity_set_from_schema_name.return_value = "accounts" |
| 348 | + client = _BatchClient(od) |
| 349 | + return client, od |
| 350 | + |
| 351 | + def test_resolve_single_item_calls_build_upsert(self): |
| 352 | + client, od = self._client_and_od() |
| 353 | + mock_req = MagicMock() |
| 354 | + od._build_upsert.return_value = mock_req |
| 355 | + |
| 356 | + item = UpsertItem(alternate_key={"accountnumber": "ACC-001"}, record={"name": "Contoso"}) |
| 357 | + op = _RecordUpsert(table="account", items=[item]) |
| 358 | + result = client._resolve_record_upsert(op) |
| 359 | + |
| 360 | + od._build_upsert.assert_called_once_with( |
| 361 | + "accounts", "account", {"accountnumber": "ACC-001"}, {"name": "Contoso"} |
| 362 | + ) |
| 363 | + self.assertEqual(result, [mock_req]) |
| 364 | + |
| 365 | + def test_resolve_multiple_items_calls_build_upsert_multiple(self): |
| 366 | + client, od = self._client_and_od() |
| 367 | + mock_req = MagicMock() |
| 368 | + od._build_upsert_multiple.return_value = mock_req |
| 369 | + |
| 370 | + items = [ |
| 371 | + UpsertItem(alternate_key={"accountnumber": "ACC-001"}, record={"name": "Contoso"}), |
| 372 | + UpsertItem(alternate_key={"accountnumber": "ACC-002"}, record={"name": "Fabrikam"}), |
| 373 | + ] |
| 374 | + op = _RecordUpsert(table="account", items=items) |
| 375 | + result = client._resolve_record_upsert(op) |
| 376 | + |
| 377 | + od._build_upsert_multiple.assert_called_once_with( |
| 378 | + "accounts", |
| 379 | + "account", |
| 380 | + [{"accountnumber": "ACC-001"}, {"accountnumber": "ACC-002"}], |
| 381 | + [{"name": "Contoso"}, {"name": "Fabrikam"}], |
| 382 | + ) |
| 383 | + self.assertEqual(result, [mock_req]) |
| 384 | + |
| 385 | + def test_resolve_item_dispatch_routes_to_upsert(self): |
| 386 | + client, od = self._client_and_od() |
| 387 | + mock_req = MagicMock() |
| 388 | + od._build_upsert.return_value = mock_req |
| 389 | + |
| 390 | + item = UpsertItem(alternate_key={"accountnumber": "ACC-001"}, record={"name": "Contoso"}) |
| 391 | + op = _RecordUpsert(table="account", items=[item]) |
| 392 | + result = client._resolve_item(op) |
| 393 | + |
| 394 | + self.assertEqual(result, [mock_req]) |
| 395 | + |
| 396 | + |
| 397 | +class TestBatchRecordOperationsUpsert(unittest.TestCase): |
| 398 | + """Tests for BatchRecordOperations.upsert (operations/batch.py).""" |
| 399 | + |
| 400 | + def _make_batch(self): |
| 401 | + from PowerPlatform.Dataverse.operations.batch import BatchRecordOperations |
| 402 | + |
| 403 | + batch = MagicMock() |
| 404 | + batch._items = [] |
| 405 | + return BatchRecordOperations(batch), batch |
| 406 | + |
| 407 | + def test_upsert_single_upsert_item_appended(self): |
| 408 | + from PowerPlatform.Dataverse.operations.batch import BatchRecordOperations |
| 409 | + |
| 410 | + rec_ops, batch = self._make_batch() |
| 411 | + item = UpsertItem(alternate_key={"accountnumber": "ACC-001"}, record={"name": "Contoso"}) |
| 412 | + rec_ops.upsert("account", [item]) |
| 413 | + |
| 414 | + self.assertEqual(len(batch._items), 1) |
| 415 | + intent = batch._items[0] |
| 416 | + self.assertIsInstance(intent, _RecordUpsert) |
| 417 | + self.assertEqual(intent.table, "account") |
| 418 | + self.assertEqual(len(intent.items), 1) |
| 419 | + self.assertEqual(intent.items[0].alternate_key, {"accountnumber": "ACC-001"}) |
| 420 | + |
| 421 | + def test_upsert_plain_dict_normalised_to_upsert_item(self): |
| 422 | + from PowerPlatform.Dataverse.operations.batch import BatchRecordOperations |
| 423 | + |
| 424 | + rec_ops, batch = self._make_batch() |
| 425 | + rec_ops.upsert("account", [{"alternate_key": {"accountnumber": "X"}, "record": {"name": "Y"}}]) |
| 426 | + |
| 427 | + intent = batch._items[0] |
| 428 | + self.assertIsInstance(intent.items[0], UpsertItem) |
| 429 | + self.assertEqual(intent.items[0].record, {"name": "Y"}) |
| 430 | + |
| 431 | + def test_upsert_empty_list_raises(self): |
| 432 | + from PowerPlatform.Dataverse.operations.batch import BatchRecordOperations |
| 433 | + from PowerPlatform.Dataverse.core.errors import ValidationError |
| 434 | + |
| 435 | + rec_ops, _ = self._make_batch() |
| 436 | + with self.assertRaises(ValidationError): |
| 437 | + rec_ops.upsert("account", []) |
| 438 | + |
| 439 | + def test_upsert_invalid_item_raises(self): |
| 440 | + from PowerPlatform.Dataverse.operations.batch import BatchRecordOperations |
| 441 | + from PowerPlatform.Dataverse.core.errors import ValidationError |
| 442 | + |
| 443 | + rec_ops, _ = self._make_batch() |
| 444 | + with self.assertRaises(ValidationError): |
| 445 | + rec_ops.upsert("account", ["not_a_valid_item"]) |
| 446 | + |
| 447 | + def test_upsert_multiple_items_all_normalised(self): |
| 448 | + from PowerPlatform.Dataverse.operations.batch import BatchRecordOperations |
| 449 | + |
| 450 | + rec_ops, batch = self._make_batch() |
| 451 | + rec_ops.upsert("account", [ |
| 452 | + UpsertItem(alternate_key={"accountnumber": "A"}, record={"name": "Alpha"}), |
| 453 | + UpsertItem(alternate_key={"accountnumber": "B"}, record={"name": "Beta"}), |
| 454 | + ]) |
| 455 | + |
| 456 | + intent = batch._items[0] |
| 457 | + self.assertEqual(len(intent.items), 2) |
| 458 | + self.assertEqual(intent.items[1].alternate_key, {"accountnumber": "B"}) |
| 459 | + |
| 460 | + |
340 | 461 | if __name__ == "__main__": |
341 | 462 | unittest.main() |
0 commit comments