|
1 | 1 | # pylama:ignore=W0212 |
2 | 2 | import unittest |
3 | | -from datetime import datetime |
| 3 | +from datetime import datetime, timezone |
4 | 4 |
|
5 | 5 | import requests_mock |
6 | 6 |
|
|
175 | 175 | "date": "2015-11-01T00:00:00.000Z", |
176 | 176 | "due_date": "2015-11-15T00:00:00.000Z", |
177 | 177 | "currency": "USD", |
| 178 | + "disabled": False, |
| 179 | + "disabled_at": None, |
| 180 | + "disabled_by": None, |
| 181 | + "edit_history_summary": { |
| 182 | + "values_changed": { |
| 183 | + "amount_in_cents": { |
| 184 | + "original_value": 4500, |
| 185 | + "edited_value": 5000 |
| 186 | + } |
| 187 | + }, |
| 188 | + "latest_edit_author": "admin@example.com", |
| 189 | + "latest_edit_performed_at": "2024-01-10T12:00:00.000Z" |
| 190 | + }, |
| 191 | + "errors": None, |
178 | 192 | "line_items": [ |
179 | 193 | { |
180 | 194 | "uuid": "li_d72e6843-5793-41d0-bfdf-0269514c9c56", |
|
231 | 245 | "date": "2015-11-01T00:00:00.000Z", |
232 | 246 | "due_date": "2015-11-15T00:00:00.000Z", |
233 | 247 | "currency": "USD", |
| 248 | + "disabled": True, |
| 249 | + "disabled_at": "2024-01-15T10:30:00.000Z", |
| 250 | + "disabled_by": "user@example.com", |
| 251 | + "edit_history_summary": { |
| 252 | + "values_changed": { |
| 253 | + "currency": { |
| 254 | + "original_value": "EUR", |
| 255 | + "edited_value": "USD" |
| 256 | + }, |
| 257 | + "date": { |
| 258 | + "original_value": "2024-01-01T00:00:00.000Z", |
| 259 | + "edited_value": "2024-01-02T00:00:00.000Z" |
| 260 | + } |
| 261 | + }, |
| 262 | + "latest_edit_author": "editor@example.com", |
| 263 | + "latest_edit_performed_at": "2024-01-20T15:45:00.000Z" |
| 264 | + }, |
| 265 | + "errors": { |
| 266 | + "currency": ["Currency is invalid", "Currency must be supported"], |
| 267 | + "date": ["Date is in the future"] |
| 268 | + }, |
234 | 269 | "line_items": [ |
235 | 270 | { |
236 | 271 | "uuid": "li_d72e6843-5793-41d0-bfdf-0269514c9c56", |
@@ -447,3 +482,142 @@ def test_retrieve_invoice(self, mock_requests): |
447 | 482 | self.assertTrue(isinstance(result, Invoice)) |
448 | 483 |
|
449 | 484 | self.assertEqual(result.uuid, "inv_22910fc6-c931-48e7-ac12-90d2cb5f0059") |
| 485 | + |
| 486 | + @requests_mock.mock() |
| 487 | + def test_retrieve_invoice_with_validation_type(self, mock_requests): |
| 488 | + mock_requests.register_uri( |
| 489 | + "GET", |
| 490 | + ("https://api.chartmogul.com/v1/invoices/inv_22910fc6-c931-48e7-ac12-90d2cb5f0059" |
| 491 | + "?validation_type=all"), |
| 492 | + request_headers={"Authorization": "Basic dG9rZW46"}, |
| 493 | + headers={"Content-Type": "application/json"}, |
| 494 | + status_code=200, |
| 495 | + json=retrieveInvoiceExample, |
| 496 | + ) |
| 497 | + |
| 498 | + config = Config("token") |
| 499 | + result = Invoice.retrieve( |
| 500 | + config, |
| 501 | + uuid="inv_22910fc6-c931-48e7-ac12-90d2cb5f0059", |
| 502 | + validation_type="all" |
| 503 | + ).get() |
| 504 | + |
| 505 | + self.assertEqual(mock_requests.call_count, 1, "expected call") |
| 506 | + self.assertEqual( |
| 507 | + mock_requests.last_request.qs, |
| 508 | + {"validation_type": ["all"]}, |
| 509 | + ) |
| 510 | + self.assertTrue(isinstance(result, Invoice)) |
| 511 | + self.assertEqual(result.uuid, "inv_22910fc6-c931-48e7-ac12-90d2cb5f0059") |
| 512 | + |
| 513 | + @requests_mock.mock() |
| 514 | + def test_retrieve_invoice_with_all_params(self, mock_requests): |
| 515 | + mock_requests.register_uri( |
| 516 | + "GET", |
| 517 | + ("https://api.chartmogul.com/v1/invoices/inv_22910fc6-c931-48e7-ac12-90d2cb5f0059" |
| 518 | + "?validation_type=invalid&include_edit_histories=true&with_disabled=false"), |
| 519 | + request_headers={"Authorization": "Basic dG9rZW46"}, |
| 520 | + headers={"Content-Type": "application/json"}, |
| 521 | + status_code=200, |
| 522 | + json=retrieveInvoiceExample, |
| 523 | + ) |
| 524 | + |
| 525 | + config = Config("token") |
| 526 | + result = Invoice.retrieve( |
| 527 | + config, |
| 528 | + uuid="inv_22910fc6-c931-48e7-ac12-90d2cb5f0059", |
| 529 | + validation_type="invalid", |
| 530 | + include_edit_histories=True, |
| 531 | + with_disabled=False |
| 532 | + ).get() |
| 533 | + |
| 534 | + self.assertEqual(mock_requests.call_count, 1, "expected call") |
| 535 | + qs = mock_requests.last_request.qs |
| 536 | + self.assertEqual(qs["validation_type"], ["invalid"]) |
| 537 | + self.assertEqual(qs["include_edit_histories"], ["true"]) |
| 538 | + self.assertEqual(qs["with_disabled"], ["false"]) |
| 539 | + self.assertTrue(isinstance(result, Invoice)) |
| 540 | + self.assertEqual(result.uuid, "inv_22910fc6-c931-48e7-ac12-90d2cb5f0059") |
| 541 | + self.assertTrue(result.disabled) |
| 542 | + self.assertEqual(result.disabled_at, datetime(2024, 1, 15, 10, 30, tzinfo=timezone.utc)) |
| 543 | + self.assertEqual(result.disabled_by, "user@example.com") |
| 544 | + self.assertIsNotNone(result.edit_history_summary) |
| 545 | + self.assertIn("values_changed", result.edit_history_summary) |
| 546 | + self.assertIn("currency", result.edit_history_summary["values_changed"]) |
| 547 | + self.assertEqual( |
| 548 | + result.edit_history_summary["values_changed"]["currency"]["original_value"], |
| 549 | + "EUR" |
| 550 | + ) |
| 551 | + self.assertEqual( |
| 552 | + result.edit_history_summary["values_changed"]["currency"]["edited_value"], |
| 553 | + "USD" |
| 554 | + ) |
| 555 | + self.assertEqual( |
| 556 | + result.edit_history_summary["latest_edit_author"], |
| 557 | + "editor@example.com" |
| 558 | + ) |
| 559 | + self.assertEqual( |
| 560 | + result.edit_history_summary["latest_edit_performed_at"], |
| 561 | + "2024-01-20T15:45:00.000Z" |
| 562 | + ) |
| 563 | + self.assertIsNotNone(result.errors) |
| 564 | + self.assertIn("currency", result.errors) |
| 565 | + self.assertIsInstance(result.errors["currency"], list) |
| 566 | + self.assertEqual(len(result.errors["currency"]), 2) |
| 567 | + self.assertEqual(result.errors["currency"][0], "Currency is invalid") |
| 568 | + self.assertEqual(result.errors["currency"][1], "Currency must be supported") |
| 569 | + self.assertIn("date", result.errors) |
| 570 | + self.assertIsInstance(result.errors["date"], list) |
| 571 | + self.assertEqual(len(result.errors["date"]), 1) |
| 572 | + self.assertEqual(result.errors["date"][0], "Date is in the future") |
| 573 | + |
| 574 | + @requests_mock.mock() |
| 575 | + def test_all_invoices_with_validation_type(self, mock_requests): |
| 576 | + mock_requests.register_uri( |
| 577 | + "GET", |
| 578 | + "https://api.chartmogul.com/v1/invoices?validation_type=all", |
| 579 | + request_headers={"Authorization": "Basic dG9rZW46"}, |
| 580 | + headers={"Content-Type": "application/json"}, |
| 581 | + status_code=200, |
| 582 | + json=invoiceListExample, |
| 583 | + ) |
| 584 | + |
| 585 | + config = Config("token") |
| 586 | + result = Invoice.all(config, validation_type="all").get() |
| 587 | + |
| 588 | + self.assertEqual(mock_requests.call_count, 1, "expected call") |
| 589 | + self.assertEqual( |
| 590 | + mock_requests.last_request.qs, |
| 591 | + {"validation_type": ["all"]}, |
| 592 | + ) |
| 593 | + |
| 594 | + self.assertTrue(isinstance(result, Invoice._many)) |
| 595 | + self.assertEqual(len(result.invoices), 1) |
| 596 | + |
| 597 | + @requests_mock.mock() |
| 598 | + def test_all_invoices_with_all_params(self, mock_requests): |
| 599 | + mock_requests.register_uri( |
| 600 | + "GET", |
| 601 | + ("https://api.chartmogul.com/v1/invoices" |
| 602 | + "?validation_type=valid&include_edit_histories=true&with_disabled=true"), |
| 603 | + request_headers={"Authorization": "Basic dG9rZW46"}, |
| 604 | + headers={"Content-Type": "application/json"}, |
| 605 | + status_code=200, |
| 606 | + json=invoiceListExample, |
| 607 | + ) |
| 608 | + |
| 609 | + config = Config("token") |
| 610 | + result = Invoice.all( |
| 611 | + config, |
| 612 | + validation_type="valid", |
| 613 | + include_edit_histories=True, |
| 614 | + with_disabled=True |
| 615 | + ).get() |
| 616 | + |
| 617 | + self.assertEqual(mock_requests.call_count, 1, "expected call") |
| 618 | + qs = mock_requests.last_request.qs |
| 619 | + self.assertEqual(qs["validation_type"], ["valid"]) |
| 620 | + self.assertEqual(qs["include_edit_histories"], ["true"]) |
| 621 | + self.assertEqual(qs["with_disabled"], ["true"]) |
| 622 | + self.assertTrue(isinstance(result, Invoice._many)) |
| 623 | + self.assertEqual(len(result.invoices), 1) |
0 commit comments