-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathtest_odata_internal.py
More file actions
636 lines (532 loc) · 27 KB
/
test_odata_internal.py
File metadata and controls
636 lines (532 loc) · 27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.
import json
import unittest
from unittest.mock import MagicMock
from PowerPlatform.Dataverse.core.errors import ValidationError
from PowerPlatform.Dataverse.data._odata import _ODataClient
def _make_odata_client() -> _ODataClient:
"""Return an _ODataClient with HTTP calls mocked out."""
mock_auth = MagicMock()
mock_auth._acquire_token.return_value = MagicMock(access_token="token")
client = _ODataClient(mock_auth, "https://example.crm.dynamics.com")
client._request = MagicMock()
return client
class TestUpsertMultipleValidation(unittest.TestCase):
"""Unit tests for _ODataClient._upsert_multiple internal validation."""
def setUp(self):
self.od = _make_odata_client()
def test_mismatched_lengths_raises_value_error(self):
"""_upsert_multiple raises ValueError when alternate_keys and records differ in length."""
with self.assertRaises(ValueError):
self.od._upsert_multiple(
"accounts",
"account",
[{"name": "acc1"}],
[{"description": "d1"}, {"description": "d2"}],
)
def test_mismatched_lengths_error_message(self):
"""ValueError message reports both lengths."""
with self.assertRaises(ValueError) as ctx:
self.od._upsert_multiple(
"accounts",
"account",
[{"name": "acc1"}, {"name": "acc2"}],
[{"description": "d1"}],
)
self.assertIn("2", str(ctx.exception))
self.assertIn("1", str(ctx.exception))
def test_equal_lengths_does_not_raise(self):
"""_upsert_multiple does not raise when both lists have the same length."""
self.od._upsert_multiple(
"accounts",
"account",
[{"name": "acc1"}, {"name": "acc2"}],
[{"description": "d1"}, {"description": "d2"}],
)
# Verify the UpsertMultiple POST was issued (other calls are picklist probes).
post_calls = [c for c in self.od._request.call_args_list if c.args[0] == "post"]
self.assertEqual(len(post_calls), 1)
self.assertIn("UpsertMultiple", post_calls[0].args[1])
def test_payload_excludes_alternate_key_fields(self):
"""Alternate key fields must NOT appear in the request body (only in @odata.id)."""
self.od._upsert_multiple(
"accounts",
"account",
[{"accountnumber": "ACC-001"}],
[{"name": "Contoso", "telephone1": "555-0100"}],
)
post_calls = [c for c in self.od._request.call_args_list if c.args[0] == "post"]
self.assertEqual(len(post_calls), 1)
payload = post_calls[0].kwargs.get("json", {})
target = payload["Targets"][0]
# accountnumber should only be in @odata.id, NOT as a body field
self.assertNotIn("accountnumber", target)
self.assertIn("name", target)
self.assertIn("telephone1", target)
self.assertIn("@odata.id", target)
self.assertIn("accountnumber", target["@odata.id"])
def test_payload_excludes_alternate_key_even_when_in_record(self):
"""If user passes matching key field in record, it should still be excluded from body."""
self.od._upsert_multiple(
"accounts",
"account",
[{"accountnumber": "ACC-001"}],
[{"accountnumber": "ACC-001", "name": "Contoso"}],
)
post_calls = [c for c in self.od._request.call_args_list if c.args[0] == "post"]
payload = post_calls[0].kwargs.get("json", {})
target = payload["Targets"][0]
# Even though user passed accountnumber in record with same value,
# it should still appear in the body because it came from record_processed
# (the conflict check allows matching values through)
self.assertIn("@odata.id", target)
self.assertIn("accountnumber", target["@odata.id"])
def test_record_conflicts_with_alternate_key_raises_value_error(self):
"""_upsert_multiple raises ValueError when a record field contradicts its alternate key."""
with self.assertRaises(ValueError) as ctx:
self.od._upsert_multiple(
"accounts",
"account",
[{"accountnumber": "ACC-001"}],
[{"accountnumber": "ACC-WRONG", "name": "Contoso"}],
)
self.assertIn("accountnumber", str(ctx.exception))
def test_record_matching_alternate_key_field_does_not_raise(self):
"""_upsert_multiple does not raise when a record field matches its alternate key value."""
self.od._upsert_multiple(
"accounts",
"account",
[{"accountnumber": "ACC-001"}],
[{"accountnumber": "ACC-001", "name": "Contoso"}],
)
class TestBuildAlternateKeyStr(unittest.TestCase):
"""Unit tests for _ODataClient._build_alternate_key_str."""
def setUp(self):
self.od = _make_odata_client()
def test_single_string_value(self):
"""Single string key is single-quoted."""
result = self.od._build_alternate_key_str({"accountnumber": "ACC-001"})
self.assertEqual(result, "accountnumber='ACC-001'")
def test_single_int_value(self):
"""Non-string value is rendered without quotes."""
result = self.od._build_alternate_key_str({"numberofemployees": 250})
self.assertEqual(result, "numberofemployees=250")
def test_composite_key_string_and_string(self):
"""Composite key with two string values produces comma-separated pairs."""
result = self.od._build_alternate_key_str({"accountnumber": "ACC-001", "address1_postalcode": "98052"})
self.assertEqual(result, "accountnumber='ACC-001',address1_postalcode='98052'")
def test_composite_key_string_and_int(self):
"""Composite key with mixed string and int values."""
result = self.od._build_alternate_key_str({"accountnumber": "ACC-001", "numberofemployees": 250})
self.assertEqual(result, "accountnumber='ACC-001',numberofemployees=250")
def test_key_name_lowercased(self):
"""Key names are lowercased in the output."""
result = self.od._build_alternate_key_str({"AccountNumber": "ACC-001"})
self.assertEqual(result, "accountnumber='ACC-001'")
def test_single_quote_in_value_is_escaped(self):
"""Single quotes in string values are doubled (OData escaping)."""
result = self.od._build_alternate_key_str({"name": "O'Brien"})
self.assertEqual(result, "name='O''Brien'")
def test_empty_dict_raises_value_error(self):
"""Empty alternate_key raises ValueError."""
with self.assertRaises(ValueError):
self.od._build_alternate_key_str({})
def test_non_string_key_raises_type_error(self):
"""Non-string key raises TypeError."""
with self.assertRaises(TypeError):
self.od._build_alternate_key_str({1: "ACC-001"})
class TestListTables(unittest.TestCase):
"""Unit tests for _ODataClient._list_tables filter and select parameters."""
def setUp(self):
self.od = _make_odata_client()
def _setup_response(self, value):
"""Configure _request to return a response with the given value list."""
mock_response = MagicMock()
mock_response.json.return_value = {"value": value}
self.od._request.return_value = mock_response
def test_no_filter_uses_default(self):
"""_list_tables() without filter sends only IsPrivate eq false."""
self._setup_response([])
self.od._list_tables()
self.od._request.assert_called_once()
url = self.od._request.call_args[0][1]
self.assertIn("$filter=IsPrivate eq false", url)
def test_filter_combined_with_default(self):
"""_list_tables(filter=...) combines user filter with IsPrivate eq false."""
self._setup_response([{"LogicalName": "account"}])
self.od._list_tables(filter="SchemaName eq 'Account'")
self.od._request.assert_called_once()
url = self.od._request.call_args[0][1]
self.assertIn("IsPrivate eq false and (SchemaName eq 'Account')", url)
def test_filter_none_same_as_no_filter(self):
"""_list_tables(filter=None) is equivalent to _list_tables()."""
self._setup_response([])
self.od._list_tables(filter=None)
self.od._request.assert_called_once()
url = self.od._request.call_args[0][1]
self.assertIn("$filter=IsPrivate eq false", url)
self.assertNotIn("and", url)
def test_returns_value_list(self):
"""_list_tables returns the 'value' array from the response."""
expected = [
{"LogicalName": "account"},
{"LogicalName": "contact"},
]
self._setup_response(expected)
result = self.od._list_tables()
self.assertEqual(result, expected)
def test_select_adds_query_param(self):
"""_list_tables(select=...) adds $select as comma-joined string."""
self._setup_response([])
self.od._list_tables(select=["LogicalName", "SchemaName", "DisplayName"])
self.od._request.assert_called_once()
url = self.od._request.call_args[0][1]
self.assertIn("$select=LogicalName,SchemaName,DisplayName", url)
def test_select_none_omits_query_param(self):
"""_list_tables(select=None) does not add $select to params."""
self._setup_response([])
self.od._list_tables(select=None)
self.od._request.assert_called_once()
url = self.od._request.call_args[0][1]
self.assertNotIn("$select", url)
def test_select_empty_list_omits_query_param(self):
"""_list_tables(select=[]) does not add $select (empty list is falsy)."""
self._setup_response([])
self.od._list_tables(select=[])
self.od._request.assert_called_once()
url = self.od._request.call_args[0][1]
self.assertNotIn("$select", url)
def test_select_preserves_case(self):
"""_list_tables does not lowercase select values (PascalCase preserved)."""
self._setup_response([])
self.od._list_tables(select=["EntitySetName", "LogicalName"])
self.od._request.assert_called_once()
url = self.od._request.call_args[0][1]
self.assertIn("$select=EntitySetName,LogicalName", url)
def test_select_with_filter(self):
"""_list_tables with both select and filter sends both params."""
self._setup_response([{"LogicalName": "account"}])
self.od._list_tables(
filter="SchemaName eq 'Account'",
select=["LogicalName", "SchemaName"],
)
self.od._request.assert_called_once()
url = self.od._request.call_args[0][1]
self.assertIn("IsPrivate eq false and (SchemaName eq 'Account')", url)
self.assertIn("$select=LogicalName,SchemaName", url)
def test_select_single_property(self):
"""_list_tables(select=[...]) with a single property works correctly."""
self._setup_response([])
self.od._list_tables(select=["LogicalName"])
self.od._request.assert_called_once()
url = self.od._request.call_args[0][1]
self.assertIn("$select=LogicalName", url)
def test_select_bare_string_raises_type_error(self):
"""_list_tables(select='LogicalName') raises TypeError for bare str."""
self._setup_response([])
with self.assertRaises(TypeError) as ctx:
self.od._list_tables(select="LogicalName")
self.assertIn("list of property names", str(ctx.exception))
class TestCreate(unittest.TestCase):
"""Unit tests for _ODataClient._create."""
def setUp(self):
self.od = _make_odata_client()
# Mock response with OData-EntityId header containing a GUID
mock_resp = MagicMock()
mock_resp.headers = {
"OData-EntityId": "https://example.crm.dynamics.com/api/data/v9.2/accounts(00000000-0000-0000-0000-000000000001)"
}
self.od._request.return_value = mock_resp
def _post_call(self):
"""Return the single POST call args from _request."""
post_calls = [c for c in self.od._request.call_args_list if c.args[0] == "post"]
self.assertEqual(len(post_calls), 1, "expected exactly one POST call")
return post_calls[0]
def test_record_keys_lowercased(self):
"""Regular record field names are lowercased before sending."""
self.od._create("accounts", "account", {"Name": "Contoso", "AccountNumber": "ACC-001"})
call = self._post_call()
payload = json.loads(call.kwargs["data"]) if "data" in call.kwargs else call.kwargs["json"]
self.assertIn("name", payload)
self.assertIn("accountnumber", payload)
self.assertNotIn("Name", payload)
self.assertNotIn("AccountNumber", payload)
def test_odata_bind_keys_preserve_case(self):
"""@odata.bind keys preserve navigation property casing in _create."""
self.od._create(
"new_tickets",
"new_ticket",
{
"new_name": "Ticket 1",
"new_CustomerId@odata.bind": "/contacts(00000000-0000-0000-0000-000000000001)",
"new_AgentId@odata.bind": "/systemusers(00000000-0000-0000-0000-000000000002)",
},
)
call = self._post_call()
payload = json.loads(call.kwargs["data"]) if "data" in call.kwargs else call.kwargs["json"]
self.assertIn("new_name", payload)
self.assertIn("new_CustomerId@odata.bind", payload)
self.assertIn("new_AgentId@odata.bind", payload)
self.assertNotIn("new_customerid@odata.bind", payload)
self.assertNotIn("new_agentid@odata.bind", payload)
def test_returns_guid_from_odata_entity_id(self):
"""_create returns the GUID from the OData-EntityId header."""
result = self.od._create("accounts", "account", {"name": "Contoso"})
self.assertEqual(result, "00000000-0000-0000-0000-000000000001")
def test_returns_guid_from_odata_entity_id_uppercase(self):
"""_create returns the GUID from the OData-EntityID header (uppercase D)."""
mock_resp = MagicMock()
mock_resp.headers = {
"OData-EntityID": "https://example.crm.dynamics.com/api/data/v9.2/accounts(00000000-0000-0000-0000-000000000002)"
}
self.od._request.return_value = mock_resp
result = self.od._create("accounts", "account", {"name": "Contoso"})
self.assertEqual(result, "00000000-0000-0000-0000-000000000002")
def test_returns_guid_from_location_header_fallback(self):
"""_create falls back to Location header when OData-EntityId is absent."""
mock_resp = MagicMock()
mock_resp.headers = {
"Location": "https://example.crm.dynamics.com/api/data/v9.2/accounts(00000000-0000-0000-0000-000000000003)"
}
self.od._request.return_value = mock_resp
result = self.od._create("accounts", "account", {"name": "Contoso"})
self.assertEqual(result, "00000000-0000-0000-0000-000000000003")
def test_raises_runtime_error_when_no_guid_in_headers(self):
"""_create raises RuntimeError when neither header contains a GUID."""
mock_resp = MagicMock()
mock_resp.headers = {}
mock_resp.status_code = 204
self.od._request.return_value = mock_resp
with self.assertRaises(RuntimeError):
self.od._create("accounts", "account", {"name": "Contoso"})
def test_issues_post_to_entity_set_url(self):
"""_create issues a POST request to the entity set URL."""
self.od._create("accounts", "account", {"name": "Contoso"})
call = self._post_call()
self.assertIn("/accounts", call.args[1])
class TestUpdate(unittest.TestCase):
"""Unit tests for _ODataClient._update."""
def setUp(self):
self.od = _make_odata_client()
# _update needs _entity_set_from_schema_name to resolve entity set
self.od._entity_set_from_schema_name = MagicMock(return_value="new_tickets")
def _patch_call(self):
"""Return the single PATCH call args from _request."""
patch_calls = [c for c in self.od._request.call_args_list if c.args[0] == "patch"]
self.assertEqual(len(patch_calls), 1, "expected exactly one PATCH call")
return patch_calls[0]
def test_record_keys_lowercased(self):
"""Regular field names are lowercased in _update."""
self.od._update("new_ticket", "00000000-0000-0000-0000-000000000001", {"New_Status": 100000001})
call = self._patch_call()
payload = json.loads(call.kwargs["data"]) if "data" in call.kwargs else call.kwargs["json"]
self.assertIn("new_status", payload)
self.assertNotIn("New_Status", payload)
def test_odata_bind_keys_preserve_case(self):
"""@odata.bind keys preserve navigation property casing in _update."""
self.od._update(
"new_ticket",
"00000000-0000-0000-0000-000000000001",
{
"new_status": 100000001,
"new_CustomerId@odata.bind": "/contacts(00000000-0000-0000-0000-000000000002)",
},
)
call = self._patch_call()
payload = json.loads(call.kwargs["data"]) if "data" in call.kwargs else call.kwargs["json"]
self.assertIn("new_status", payload)
self.assertIn("new_CustomerId@odata.bind", payload)
self.assertNotIn("new_customerid@odata.bind", payload)
def test_sends_if_match_star_header(self):
"""PATCH request includes If-Match: * header."""
self.od._update("new_ticket", "00000000-0000-0000-0000-000000000001", {"new_status": 1})
call = self._patch_call()
headers = call.kwargs.get("headers", {})
self.assertEqual(headers.get("If-Match"), "*")
def test_url_formats_bare_guid(self):
"""PATCH URL wraps a bare GUID in parentheses."""
self.od._update("new_ticket", "00000000-0000-0000-0000-000000000001", {"new_status": 1})
call = self._patch_call()
self.assertIn("(00000000-0000-0000-0000-000000000001)", call.args[1])
def test_returns_none(self):
"""_update always returns None."""
result = self.od._update("new_ticket", "00000000-0000-0000-0000-000000000001", {"new_status": 1})
self.assertIsNone(result)
def test_resolves_entity_set_from_schema_name(self):
"""_update delegates entity set resolution to _entity_set_from_schema_name."""
self.od._update("new_ticket", "00000000-0000-0000-0000-000000000001", {"new_status": 1})
self.od._entity_set_from_schema_name.assert_called_once_with("new_ticket")
class TestUpsert(unittest.TestCase):
"""Unit tests for _ODataClient._upsert."""
def setUp(self):
self.od = _make_odata_client()
def _patch_call(self):
"""Return the single PATCH call args from _request."""
patch_calls = [c for c in self.od._request.call_args_list if c.args[0] == "patch"]
self.assertEqual(len(patch_calls), 1, "expected exactly one PATCH call")
return patch_calls[0]
def test_issues_patch_request(self):
"""_upsert issues a PATCH request to the entity set URL."""
self.od._upsert("accounts", "account", {"accountnumber": "ACC-001"}, {"name": "Contoso"})
call = self._patch_call()
self.assertIn("accounts", call.args[1])
def test_url_contains_alternate_key(self):
"""PATCH URL encodes the alternate key in the entity path."""
self.od._upsert("accounts", "account", {"accountnumber": "ACC-001"}, {"name": "Contoso"})
call = self._patch_call()
self.assertIn("accounts(accountnumber='ACC-001')", call.args[1])
def test_url_contains_composite_alternate_key(self):
"""PATCH URL encodes a composite alternate key correctly."""
self.od._upsert(
"accounts",
"account",
{"accountnumber": "ACC-001", "address1_postalcode": "98052"},
{"name": "Contoso"},
)
call = self._patch_call()
expected_key = "accountnumber='ACC-001',address1_postalcode='98052'"
self.assertIn(expected_key, call.args[1])
def test_record_keys_lowercased(self):
"""Record field names are lowercased before sending."""
self.od._upsert("accounts", "account", {"accountnumber": "ACC-001"}, {"Name": "Contoso"})
call = self._patch_call()
payload = json.loads(call.kwargs["data"]) if "data" in call.kwargs else call.kwargs["json"]
self.assertIn("name", payload)
self.assertNotIn("Name", payload)
def test_odata_bind_keys_preserve_case(self):
"""@odata.bind keys must preserve PascalCase for navigation property."""
self.od._upsert(
"accounts",
"account",
{"accountnumber": "ACC-001"},
{
"Name": "Contoso",
"new_CustomerId@odata.bind": "/contacts(00000000-0000-0000-0000-000000000001)",
},
)
call = self._patch_call()
payload = json.loads(call.kwargs["data"]) if "data" in call.kwargs else call.kwargs["json"]
# Regular field is lowercased
self.assertIn("name", payload)
# @odata.bind key preserves original casing
self.assertIn("new_CustomerId@odata.bind", payload)
self.assertNotIn("new_customerid@odata.bind", payload)
def test_convert_labels_skips_odata_keys(self):
"""_convert_labels_to_ints should skip @odata.bind keys (no metadata lookup)."""
# Patch _optionset_map to track calls
calls = []
original = self.od._optionset_map
def tracking_optionset_map(table, attr):
calls.append(attr)
return original(table, attr)
self.od._optionset_map = tracking_optionset_map
record = {
"name": "Contoso",
"new_CustomerId@odata.bind": "/contacts(00000000-0000-0000-0000-000000000001)",
"@odata.type": "Microsoft.Dynamics.CRM.account",
}
self.od._convert_labels_to_ints("account", record)
# Only "name" should be checked, not the @odata keys
self.assertEqual(calls, ["name"])
def test_returns_none(self):
"""_upsert always returns None."""
result = self.od._upsert("accounts", "account", {"accountnumber": "ACC-001"}, {"name": "Contoso"})
self.assertIsNone(result)
class TestAttributePayload(unittest.TestCase):
"""Unit tests for _ODataClient._attribute_payload."""
def setUp(self):
self.od = _make_odata_client()
def test_memo_type(self):
"""'memo' should produce MemoAttributeMetadata with MaxLength 4000."""
result = self.od._attribute_payload("new_Notes", "memo")
self.assertEqual(result["@odata.type"], "Microsoft.Dynamics.CRM.MemoAttributeMetadata")
self.assertEqual(result["SchemaName"], "new_Notes")
self.assertEqual(result["MaxLength"], 4000)
self.assertEqual(result["FormatName"], {"Value": "Text"})
self.assertNotIn("IsPrimaryName", result)
def test_multiline_alias(self):
"""'multiline' should produce identical payload to 'memo'."""
memo_result = self.od._attribute_payload("new_Description", "memo")
multiline_result = self.od._attribute_payload("new_Description", "multiline")
self.assertEqual(multiline_result, memo_result)
def test_string_type(self):
"""'string' should produce StringAttributeMetadata with MaxLength 200."""
result = self.od._attribute_payload("new_Title", "string")
self.assertEqual(result["@odata.type"], "Microsoft.Dynamics.CRM.StringAttributeMetadata")
self.assertEqual(result["MaxLength"], 200)
self.assertEqual(result["FormatName"], {"Value": "Text"})
def test_unsupported_type_returns_none(self):
"""An unknown type string should return None."""
result = self.od._attribute_payload("new_Col", "unknown_type")
self.assertIsNone(result)
class TestBuildUpsertMultiple(unittest.TestCase):
"""Unit tests for _ODataClient._build_upsert_multiple (batch deferred build)."""
def setUp(self):
self.od = _make_odata_client()
def _targets(self, alt_keys, records):
import json
req = self.od._build_upsert_multiple("accounts", "account", alt_keys, records)
return json.loads(req.body)["Targets"]
def test_payload_excludes_alternate_key_fields(self):
"""Alternate key fields must NOT appear in the request body (only in @odata.id)."""
targets = self._targets(
[{"accountnumber": "ACC-001"}],
[{"name": "Contoso"}],
)
self.assertEqual(len(targets), 1)
target = targets[0]
self.assertNotIn("accountnumber", target)
self.assertIn("name", target)
self.assertIn("@odata.id", target)
self.assertIn("accountnumber", target["@odata.id"])
def test_payload_allows_matching_key_field_in_record(self):
"""If user passes matching key field in record with same value, it passes through to body."""
targets = self._targets(
[{"accountnumber": "ACC-001"}],
[{"accountnumber": "ACC-001", "name": "Contoso"}],
)
target = targets[0]
self.assertIn("name", target)
self.assertIn("@odata.id", target)
self.assertIn("accountnumber", target["@odata.id"])
def test_odata_type_added_when_absent(self):
"""@odata.type is injected when not provided by caller."""
targets = self._targets(
[{"accountnumber": "ACC-001"}],
[{"name": "Contoso"}],
)
self.assertIn("@odata.type", targets[0])
self.assertEqual(targets[0]["@odata.type"], "Microsoft.Dynamics.CRM.account")
def test_multiple_targets_all_have_odata_id(self):
"""Each target in a multi-item call gets its own @odata.id."""
targets = self._targets(
[{"accountnumber": "ACC-001"}, {"accountnumber": "ACC-002"}],
[{"name": "Contoso"}, {"name": "Fabrikam"}],
)
self.assertEqual(len(targets), 2)
self.assertIn("ACC-001", targets[0]["@odata.id"])
self.assertIn("ACC-002", targets[1]["@odata.id"])
def test_conflicting_key_field_raises(self):
"""Raises when a record field contradicts its alternate key value."""
with self.assertRaises(ValidationError) as ctx:
self.od._build_upsert_multiple(
"accounts",
"account",
[{"accountnumber": "ACC-001"}],
[{"accountnumber": "ACC-WRONG", "name": "Contoso"}],
)
self.assertIn("accountnumber", str(ctx.exception))
def test_mismatched_lengths_raises(self):
"""Raises when alternate_keys and records lengths differ."""
with self.assertRaises(ValidationError):
self.od._build_upsert_multiple("accounts", "account", [{"accountnumber": "ACC-001"}], [])
def test_url_contains_upsert_multiple_action(self):
"""POST URL targets the UpsertMultiple bound action."""
req = self.od._build_upsert_multiple(
"accounts", "account", [{"accountnumber": "ACC-001"}], [{"name": "Contoso"}]
)
self.assertIn("UpsertMultiple", req.url)
self.assertEqual(req.method, "POST")
if __name__ == "__main__":
unittest.main()