Skip to content

Commit 4d44ac9

Browse files
authored
feat: add option to list invoice items (#31)
Add option to list invoice items on an invoice. Signed-off-by: Roald Nefs <info@roaldnefs.com>
1 parent 32604ea commit 4d44ac9

2 files changed

Lines changed: 37 additions & 4 deletions

File tree

tests/services/test_invoices.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import unittest
2323

2424
from transip import TransIP
25-
from transip.v6.objects import Invoice
25+
from transip.v6.objects import Invoice, InvoiceItem
2626
from tests.utils import load_responses_fixtures
2727

2828
class InvoicesTest(unittest.TestCase):
@@ -44,12 +44,24 @@ def test_list(self) -> None:
4444
invoices: List[Invoice] = self.client.invoices.list() # type: ignore
4545
invoice: Invoice = invoices[0]
4646

47-
assert len(invoices) == 1
48-
assert invoice.get_id() == "F0000.1911.0000.0004" # type: ignore
47+
self.assertEqual(len(invoices), 1)
48+
self.assertEqual(invoice.get_id(), "F0000.1911.0000.0004") # type: ignore
4949

5050
@responses.activate
5151
def test_get(self) -> None:
5252
invoice_id: str = "F0000.1911.0000.0004"
5353
invoice: Invoice = self.client.invoices.get(invoice_id) # type: ignore
5454

55-
assert invoice.get_id() == "F0000.1911.0000.0004" # type: ignore
55+
self.assertEqual(invoice.get_id(), "F0000.1911.0000.0004") # type: ignore
56+
57+
@responses.activate
58+
def test_items_list(self) -> None:
59+
invoice: Invoice = self.client.invoices.get("F0000.1911.0000.0004") # type: ignore
60+
items: List[InvoiceItem] = invoice.items.list() # type: ignore
61+
62+
self.assertEqual(len(items), 1)
63+
self.assertEqual(items[0].product, "Big Storage Disk 2000 GB") # type: ignore
64+
65+
# Expect the get_id() method to return None as the invoice items don't
66+
# have a specific ID attribute.
67+
self.assertIsNone(items[0].get_id()) # type: ignore

transip/v6/objects.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,31 @@ class DomainService(CreateMixin, GetMixin, DeleteMixin, ListMixin, ApiService):
172172
)
173173

174174

175+
class InvoiceItem(ApiObject):
176+
pass
177+
178+
179+
class InvoiceItemService(ListMixin, ApiService):
180+
"""Service to items of an invoice."""
181+
182+
_path: str = "/invoices/{parent_id}/invoice-items"
183+
_obj_cls: Optional[Type[ApiObject]] = InvoiceItem
184+
185+
_resp_list_attr: str = "invoiceItems"
186+
187+
175188
class Invoice(ApiObject):
176189

177190
_id_attr: str = "invoiceNumber"
178191

192+
@property
193+
def items(self) -> InvoiceItemService:
194+
"""Return the service to manage the items of an invoice"""
195+
return InvoiceItemService(
196+
self.service.client,
197+
parent=self # type: ignore
198+
)
199+
179200

180201
class InvoiceService(GetMixin, ListMixin, ApiService):
181202

0 commit comments

Comments
 (0)