Skip to content

Commit 4bc4c3c

Browse files
authored
Merge pull request #166 from jimasuen/fix-pytest
convert tests to pytest
2 parents 8e56938 + 572f220 commit 4bc4c3c

File tree

8 files changed

+414
-283
lines changed

8 files changed

+414
-283
lines changed

tests/pytest.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# pytest.ini
2+
[pytest]
3+
asyncio_mode=auto

tests/test_invoices.py

Lines changed: 70 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,37 @@
1-
import asyncio
1+
import pytest
22

33
from aiohttp.client import ClientSession
4+
45
from pylnbits.config import Config
56
from pylnbits.invoices import Invoices
67
from pylnbits.DTOs.invoice_dto import InvoiceDTO
78

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+
"""
820

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:
1822

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()
2132

22-
# create invoice
33+
# create invoice
34+
async def test_create_invoice(self):
2335
invoice_dto = InvoiceDTO("wallet", "EUR", "open")
2436
invoice_dto.company_name = 'my_company_name10'
2537
invoice_dto.first_name = 'my_first_name'
@@ -28,31 +40,49 @@ async def main():
2840
invoice_dto.address = 'my_address'
2941
invoice_dto.phone = 'my_phone_number'
3042
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"\nCreated invoice \n{res}\n")
3448

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"\nInvoices:\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"\nInvoice:\n{res}\n")
3960

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"\nInvoice payment::\n{res}\n")
4374

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}")
5180
print(f"Invoice payment status:\n{res}\n")
5281

53-
# update invoice
82+
# update invoice
83+
async def test_update_invoice(self):
5484
invoice_dto = InvoiceDTO("wallet", "EUR", "draft")
55-
invoice_dto.id = invoice_id
85+
invoice_dto.id = self.invoice_id
5686
invoice_dto.company_name = 'my_company_name10_1'
5787
invoice_dto.first_name = 'my_first_name1'
5888
invoice_dto.last_name = 'my_last_name1'
@@ -61,13 +91,12 @@ async def main():
6191
invoice_dto.phone = 'my_phone_number1'
6292
invoice_dto.items = [{"description": "item33", "amount": 5},
6393
{"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"\nUpdated invoice: \n{res}\n")
7197

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")

tests/test_lndhub.py

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,35 @@
1-
# test lndhub link creation
2-
import asyncio
1+
import pytest
2+
33
from aiohttp.client import ClientSession
4+
45
from pylnbits.config import Config
56
from pylnbits.lndhub import LndHub
67

7-
# Example code
8+
"""
9+
Tests:
10+
11+
Get admin hub link
12+
Get invoice hub link
13+
"""
814

9-
async def main():
10-
c = Config(config_file="config.yml")
11-
url = c.lnbits_url
12-
print(f"url: {url}")
13-
print(f"headers: {c.headers()}")
14-
print(f"admin_headers: {c.admin_headers()}")
15+
class TestLndHub:
1516

16-
async with ClientSession() as session:
17-
lh = LndHub(c)
18-
adminhub = lh.admin()
19-
print(f'admin lndhub: {adminhub} ')
20-
invoicehub = lh.invoice()
21-
print(f'invoice lndhub: {invoicehub} ')
17+
@pytest.fixture(autouse=True)
18+
async def setup_vars(self):
19+
c = Config(config_file="config.yml")
20+
session = ClientSession()
21+
self.lh = LndHub(c)
22+
yield
23+
await session.close()
2224

25+
# admin
26+
async def test_admin(self):
27+
adminhub = self.lh.admin()
28+
assert adminhub, f"Failed to get adminhub link {adminhub}"
29+
print(f'\nadmin lndhub: {adminhub} ')
2330

24-
loop = asyncio.get_event_loop()
25-
loop.run_until_complete(main())
31+
# invoice
32+
async def test_invoice(self):
33+
invoicehub = self.lh.invoice()
34+
assert invoicehub, f"Failed to get adminhub link {invoicehub}"
35+
print(f'\ninvoice lndhub: {invoicehub} ')

tests/test_lnurlp.py

Lines changed: 48 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,71 @@
1-
# test lnurlp pay link
2-
import asyncio
1+
import pytest
32

43
from aiohttp.client import ClientSession
54

65
from pylnbits.config import Config
76
from pylnbits.lnurl_p import LnurlPay
87

9-
# Example code for testing LNURLp
8+
"""
9+
Tests:
10+
11+
Get list of paylinks
12+
Get paylink
13+
Create paylink
14+
Update paylink
15+
Delete paylink
16+
"""
1017

11-
async def main():
12-
c = Config(config_file="config.yml")
13-
url = c.lnbits_url
14-
print(f"url: {url}")
15-
print(f"headers: {c.headers()}")
16-
print(f"admin_headers: {c.admin_headers()}")
18+
class TestLnurlPay:
1719

18-
async with ClientSession() as session:
19-
lw = LnurlPay(c, session)
20+
@pytest.fixture(autouse=True)
21+
async def setup_vars(self):
22+
c = Config(config_file="config.yml")
23+
session = ClientSession()
24+
self.lp = LnurlPay(c, session)
25+
links = await self.lp.list_paylinks()
26+
self.pay_id = links[0]["id"] if len(links) else None
27+
yield
28+
await session.close()
2029

21-
# list links
22-
links = await lw.list_paylinks()
23-
print("list all links: " , str(links), "\n\n")
30+
# list links
31+
async def test_list_paylinks(self):
32+
links = await self.lp.list_paylinks()
33+
assert len(links) > 0, f"Failed to list links. Response: {links}"
34+
print("\nlist all links: " , str(links), "\n\n")
2435

25-
# get pay link
26-
pay_id = links[0]['id']
27-
print(f'pay_id for get_link: {pay_id}')
28-
getlink = await lw.get_paylink(pay_id=str(pay_id))
29-
print("get pay link: ", str(getlink), "\n")
36+
# get pay link
37+
async def test_get_paylink(self):
38+
print(f'\npay_id for get_link: {self.pay_id}')
39+
40+
getlink = await self.lp.get_paylink(pay_id=str(self.pay_id))
41+
assert getlink, f"Failed to get link: {getlink}"
42+
print("\nget pay link: ", getlink, "\n")
3043

31-
# create pay link
44+
# create pay links
45+
async def test_create_paylink(self):
3246
body = {"description": "auto pay link",
3347
"amount": 100,
3448
"max": 10000,
3549
"min": 100,
3650
"comment_chars": 100}
51+
newlink = await self.lp.create_paylink(body=body)
52+
assert "id" in newlink, f"Failed to create link: {newlink}"
53+
print(f"\ncreate pay link with body: {body}, result link: {newlink} \n")
3754

38-
newlink = await lw.create_paylink(body=body)
39-
print(f"create pay link with body: {body}, result link: {newlink} \n")
40-
pay_id = newlink['id']
41-
42-
# update newly created link above
43-
# all body fields are required
55+
# update pay link
56+
async def test_update_paylink(self):
4457
body = {"description": "update auto paylink",
4558
"amount": 150,
4659
"max": 10000,
4760
"min": 100,
4861
"comment_chars": 100}
49-
update_result = await lw.update_paylink(pay_id=str(pay_id), body=body)
50-
print(f'update pay link with intial id: {pay_id}, body: {body} \n result: {update_result}\n\n')
51-
52-
# delete above created link
53-
delete_result = await lw.delete_paylink(pay_id=str(pay_id))
54-
print(f'delete pay link id: {pay_id}, result: {delete_result}\n\n')
55-
56-
62+
63+
update_result = await self.lp.update_paylink(pay_id=str(self.pay_id), body=body)
64+
assert "id" in update_result, f"Failed to update pay link: {update_result}"
65+
print(f'\nupdate pay link with initial id: {self.pay_id}\n\nbody: {body} \n\nresult: {update_result}\n\n')
5766

58-
loop = asyncio.get_event_loop()
59-
loop.run_until_complete(main())
67+
# delete paylink
68+
async def test_delete_paylink(self):
69+
delete_result = await self.lp.delete_paylink(pay_id=str(self.pay_id))
70+
assert delete_result, f"Failed to delete paylink {delete_result}"
71+
print(f'delete pay link id: {self.pay_id}, result: {delete_result}\n\n')

0 commit comments

Comments
 (0)