| 1 | import shutil |
| 2 | import tempfile |
| 3 | |
| 4 | from electrum import SimpleConfig |
| 5 | from electrum.gui.qml.qetypes import QEAmount |
| 6 | from electrum.invoices import Invoice, LN_EXPIRY_NEVER |
| 7 | from electrum.transaction import PartialTxOutput |
| 8 | |
| 9 | from .qt_util import QETestCase, QEventReceiver, qt_test |
| 10 | |
| 11 | |
| 12 | class WalletMock: |
| 13 | def __init__(self, electrum_path): |
| 14 | self.config = SimpleConfig({ |
| 15 | 'electrum_path': electrum_path, |
| 16 | 'decimal_point': 5 |
| 17 | }) |
| 18 | self.contacts = None |
| 19 | |
| 20 | |
| 21 | class TestTypes(QETestCase): |
| 22 | |
| 23 | def setUp(self): |
| 24 | super().setUp() |
| 25 | self.electrum_path = tempfile.mkdtemp() |
| 26 | self.wallet = WalletMock(self.electrum_path) |
| 27 | |
| 28 | def tearDown(self): |
| 29 | super().tearDown() |
| 30 | shutil.rmtree(self.electrum_path) |
| 31 | |
| 32 | @qt_test |
| 33 | def test_qeamount(self): |
| 34 | a = QEAmount() |
| 35 | self.assertTrue(a.isEmpty) |
| 36 | a_er = QEventReceiver(a.valueChanged) |
| 37 | a.satsInt = 1 |
| 38 | self.assertTrue(bool(a_er.received)) |
| 39 | self.assertFalse(a.isEmpty) |
| 40 | self.assertEqual('1', a.satsStr) |
| 41 | |
| 42 | a_er.clear() |
| 43 | a.clear() |
| 44 | self.assertTrue(a.isEmpty) |
| 45 | self.assertTrue(bool(a_er.received)) |
| 46 | self.assertEqual('0', a.satsStr) |
| 47 | |
| 48 | a.clear() |
| 49 | a_er.clear() |
| 50 | a.isMax = True |
| 51 | self.assertTrue(a.isMax) |
| 52 | self.assertFalse(a.isEmpty) |
| 53 | self.assertTrue(bool(a_er.received)) |
| 54 | self.assertEqual('0', a.satsStr) |
| 55 | |
| 56 | a.clear() |
| 57 | a_er.clear() |
| 58 | a.msatsInt = 1 |
| 59 | self.assertTrue(bool(a_er.received)) |
| 60 | self.assertFalse(a.isEmpty) |
| 61 | self.assertEqual('1', a.msatsStr) |
| 62 | |
| 63 | @qt_test |
| 64 | def test_qeamount_copy(self): |
| 65 | a = QEAmount() |
| 66 | b = QEAmount() |
| 67 | b.satsInt = 1 |
| 68 | c = QEAmount() |
| 69 | c.msatsInt = 1 |
| 70 | d = QEAmount() |
| 71 | d.isMax = True |
| 72 | |
| 73 | t = QEAmount() |
| 74 | t_er = QEventReceiver(t.valueChanged) |
| 75 | |
| 76 | t.copyFrom(a) |
| 77 | self.assertTrue(t.isEmpty) |
| 78 | self.assertEqual(0, len(t_er.received)) |
| 79 | |
| 80 | t.clear() |
| 81 | t_er.clear() |
| 82 | t.copyFrom(b) |
| 83 | self.assertFalse(t.isEmpty) |
| 84 | self.assertEqual(t.satsInt, 1) |
| 85 | self.assertEqual(1, len(t_er.received)) |
| 86 | |
| 87 | t.clear() |
| 88 | t_er.clear() |
| 89 | t.copyFrom(c) |
| 90 | self.assertFalse(t.isEmpty) |
| 91 | self.assertEqual(t.msatsInt, 1) |
| 92 | self.assertEqual(1, len(t_er.received)) |
| 93 | |
| 94 | t.clear() |
| 95 | t_er.clear() |
| 96 | t.copyFrom(d) |
| 97 | self.assertFalse(t.isEmpty) |
| 98 | self.assertTrue(t.isMax) |
| 99 | self.assertEqual(1, len(t_er.received)) |
| 100 | |
| 101 | @qt_test |
| 102 | def test_qeamount_frominvoice(self): |
| 103 | amount_sat = 10_000 |
| 104 | outputs = [PartialTxOutput.from_address_and_value('bc1qj3zx2zc4rpv3npzmznxhdxzn0wm7pzqp8p2293', amount_sat)] |
| 105 | invoice = Invoice( |
| 106 | amount_msat=amount_sat * 1000, |
| 107 | message="mymsg", |
| 108 | time=1692716965, |
| 109 | exp=LN_EXPIRY_NEVER, |
| 110 | outputs=outputs, |
| 111 | height=0, |
| 112 | lightning_invoice=None, |
| 113 | ) |
| 114 | a = QEAmount(from_invoice=invoice) |
| 115 | self.assertEqual(10_000, a.satsInt) |
| 116 | self.assertEqual(10_000_000, a.msatsInt) |
| 117 | self.assertFalse(a.isMax) |
| 118 | |
| 119 | outputs = [PartialTxOutput.from_address_and_value('bc1qj3zx2zc4rpv3npzmznxhdxzn0wm7pzqp8p2293', '!')] |
| 120 | invoice = Invoice( |
| 121 | amount_msat='!', |
| 122 | message="mymsg", |
| 123 | time=1692716965, |
| 124 | exp=LN_EXPIRY_NEVER, |
| 125 | outputs=outputs, |
| 126 | height=0, |
| 127 | lightning_invoice=None, |
| 128 | ) |
| 129 | a = QEAmount(from_invoice=invoice) |
| 130 | self.assertTrue(a.isMax) |
| 131 | self.assertEqual(0, a.satsInt) |
| 132 | self.assertEqual(0, a.msatsInt) |
| 133 | |
| 134 | bolt11 = 'lnbc20m1pvjluezpp5qqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqypqhp58yjmdan79s6qqdhdzgynm4zwqd5d7xmw5fk98klysy043l2ahrqsfpp3qjmp7lwpagxun9pygexvgpjdc4jdj85fr9yq20q82gphp2nflc7jtzrcazrra7wwgzxqc8u7754cdlpfrmccae92qgzqvzq2ps8pqqqqqqpqqqqq9qqqvpeuqafqxu92d8lr6fvg0r5gv0heeeqgcrqlnm6jhphu9y00rrhy4grqszsvpcgpy9qqqqqqgqqqqq7qqzqj9n4evl6mr5aj9f58zp6fyjzup6ywn3x6sk8akg5v4tgn2q8g4fhx05wf6juaxu9760yp46454gpg5mtzgerlzezqcqvjnhjh8z3g2qqdhhwkj' |
| 135 | invoice = Invoice.from_bech32(bolt11) |
| 136 | a = QEAmount(from_invoice=invoice) |
| 137 | self.assertEqual(2_000_000, a.satsInt) |
| 138 | self.assertEqual(2_000_000_000, a.msatsInt) |
| 139 | self.assertFalse(a.isMax) |