1
- import asyncio
1
+ import pytest
2
2
3
3
from aiohttp .client import ClientSession
4
+
4
5
from pylnbits .config import Config
5
6
from pylnbits .invoices import Invoices
6
7
from pylnbits .DTOs .invoice_dto import InvoiceDTO
7
8
9
+ """
10
+ Tests:
11
+
12
+ Create invoice
13
+ Get invoices
14
+ Get invoice
15
+ Create invoice payment
16
+ Get payment status
17
+ Update invoice
18
+ Delete invoice
19
+ """
8
20
9
- async def main ():
10
-
11
- c = Config (config_file = "config.yml" )
12
- url = c .lnbits_url
13
- print (f"url: { url } " )
14
- print (f"headers: { c .headers ()} " )
15
- print (f"admin_headers: { c .admin_headers ()} " )
16
-
17
- async with ClientSession () as session :
21
+ class TestInvoices :
18
22
19
- inv = Invoices (c , session )
20
- print ("\n " )
23
+ @pytest .fixture (autouse = True )
24
+ async def setup_vars (self ):
25
+ c = Config (config_file = "config.yml" )
26
+ session = ClientSession ()
27
+ self .inv = Invoices (c , session )
28
+ invoices = await self .inv .get_invoices ()
29
+ self .invoice_id = invoices [0 ]["id" ] if invoices else None
30
+ yield
31
+ await session .close ()
21
32
22
- # create invoice
33
+ # create invoice
34
+ async def test_create_invoice (self ):
23
35
invoice_dto = InvoiceDTO ("wallet" , "EUR" , "open" )
24
36
invoice_dto .company_name = 'my_company_name10'
25
37
invoice_dto .first_name = 'my_first_name'
@@ -28,31 +40,49 @@ async def main():
28
40
invoice_dto .address = 'my_address'
29
41
invoice_dto .phone = 'my_phone_number'
30
42
invoice_dto .items = [{"description" : "item1" , "amount" : 5 },
31
- {"description" : "item2" , "amount" : 3 }]
32
- res = await inv .create_invoice (invoice_dto )
33
- print (f"Created invoice \n { res } \n " )
43
+ {"description" : "item2" , "amount" : 3 }]
44
+
45
+ res = await self .inv .create_invoice (invoice_dto )
46
+ assert "id" in res , f"Failed to create invoice { res } "
47
+ print (f"\n Created invoice \n { res } \n " )
34
48
35
- invoice_id = res ['id' ]
36
- # get invoices
37
- invoices = await inv .get_invoices ()
38
- print (f"Invoices:\n { invoices } \n " )
49
+ # get invoices
50
+ async def test_get_invoices (self ):
51
+ invoices = await self .inv .get_invoices ()
52
+ assert len (invoices ) > 0 , f"Failed to get invoices: { invoices } "
53
+ print (f"\n Invoices:\n { invoices } \n " )
54
+
55
+ # get invoice
56
+ async def test_get_invoice (self ):
57
+ res = await self .inv .get_invoice (self .invoice_id )
58
+ assert res , res .get ("detail" , f"Failed to get invoice: { res } " )
59
+ print (f"\n Invoice:\n { res } \n " )
39
60
40
- # get invoice
41
- res = await inv .get_invoice (invoice_id )
42
- print (f"Invoice:\n { res } \n " )
61
+ # create invoice payment
62
+ async def test_create_invoice_payment (self ):
63
+ result = await self .inv .get_invoice (self .invoice_id )
64
+
65
+ # calculate total value of invoice items
66
+ amount = 0
67
+ for item in result ["items" ]:
68
+ amount += item ["amount" ]
69
+
70
+ res = await self .inv .create_invoice_payment (self .invoice_id , amount )
71
+ assert "payment_request" in res , res .get ("detail" , f"Failed to get invoice payment details { res } " )
72
+ self .payment_hash = res ["payment_hash" ]
73
+ print (f"\n Invoice payment::\n { res } \n " )
43
74
44
- # create invoice payment
45
- res = await inv .create_invoice_payment (invoice_id )
46
- print (f"Invoice payment::\n { res } \n " )
47
-
48
- payment_hash = res ['payment_hash' ]
49
- # check invoice payment status
50
- res = await inv .get_invoice_payment_status (invoice_id , payment_hash )
75
+ # payment status
76
+ async def test_invoice_payment_status (self ):
77
+ await self .test_create_invoice_payment ()
78
+ res = await self .inv .get_invoice_payment_status (self .invoice_id , self .payment_hash )
79
+ assert res , res .get ("detail" , f"Failed to get status: { res } " )
51
80
print (f"Invoice payment status:\n { res } \n " )
52
81
53
- # update invoice
82
+ # update invoice
83
+ async def test_update_invoice (self ):
54
84
invoice_dto = InvoiceDTO ("wallet" , "EUR" , "draft" )
55
- invoice_dto .id = invoice_id
85
+ invoice_dto .id = self . invoice_id
56
86
invoice_dto .company_name = 'my_company_name10_1'
57
87
invoice_dto .first_name = 'my_first_name1'
58
88
invoice_dto .last_name = 'my_last_name1'
@@ -61,13 +91,12 @@ async def main():
61
91
invoice_dto .phone = 'my_phone_number1'
62
92
invoice_dto .items = [{"description" : "item33" , "amount" : 5 },
63
93
{"description" : "item24" , "amount" : 3 }]
64
- res = await inv .update_invoice (invoice_dto )
65
- print (f"Updated invoice: \n { res } \n " )
66
-
67
- # delete invoice
68
- res = await inv .delete_invoice (invoice_id )
69
- print (f"Delete Invoice result::\n { res } \n " )
70
-
94
+ res = await self .inv .update_invoice (invoice_dto )
95
+ assert "id" in res , f"Failed to create invoice { res } "
96
+ print (f"\n Updated invoice: \n { res } \n " )
71
97
72
- loop = asyncio .get_event_loop ()
73
- loop .run_until_complete (main ())
98
+ # delete invoice
99
+ async def test_delete_invoice (self ):
100
+ res = await self .inv .delete_invoice (self .invoice_id )
101
+ assert res , f"Failed to delete: { res } "
102
+ print (f"Delete Invoice result::\n { res } \n " )
0 commit comments