From 3de90952d1708026db584608b3e1c1267fea3a65 Mon Sep 17 00:00:00 2001 From: SomberNight Date: Mon, 10 Feb 2025 16:36:19 +0000 Subject: [PATCH] tests: add a test for util.ShortID class --- tests/test_util.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/tests/test_util.py b/tests/test_util.py index 9edf14d23..b354e75e8 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -4,7 +4,8 @@ from decimal import Decimal from electrum import util from electrum.util import (format_satoshis, format_fee_satoshis, is_hash256_str, chunks, is_ip_address, list_enabled_bits, format_satoshis_plain, is_private_netaddress, is_hex_str, - is_integer, is_non_negative_integer, is_int_or_float, is_non_negative_int_or_float) + is_integer, is_non_negative_integer, is_int_or_float, is_non_negative_int_or_float, + ShortID) from electrum.bip21 import parse_bip21_URI, InvalidBitcoinURI from . import ElectrumTestCase, as_testnet @@ -449,4 +450,25 @@ class TestUtil(ElectrumTestCase): self.assertEqual("in over 3 years", util.age(from_date=now.timestamp()+103012200, since_date=now)) + def test_shortchannelid(self): + scid1 = ShortID.from_components(2, 45, 789) + self.assertEqual("2x45x789", str(scid1)) + self.assertEqual(2, scid1.block_height) + self.assertEqual(45, scid1.txpos) + self.assertEqual(789, scid1.output_index) + + scid2_raw = bytes([0, 0, 2, 0, 0, 45, 789 // 256, 789 % 256]) + scid2 = ShortID(scid2_raw) + self.assertEqual(scid1, scid2_raw) + self.assertEqual(scid1, scid2) + self.assertEqual(scid1, ShortID.from_str(str(scid1))) + self.assertEqual(scid1, ShortID.normalize(scid1.hex())) + self.assertEqual(scid1, ShortID.normalize(bytes(scid1))) + + self.assertTrue(ShortID.from_components(3, 30, 300) == ShortID.from_components(3, 30, 300)) + self.assertTrue(ShortID.from_components(3, 30, 300) > ShortID.from_components(2, 999, 999)) + self.assertTrue(ShortID.from_components(3, 30, 300) < ShortID.from_components(3, 999, 999)) + self.assertTrue(ShortID.from_components(3, 30, 300) > ShortID.from_components(3, 1, 1)) + self.assertTrue(ShortID.from_components(3, 30, 300) > ShortID.from_components(3, 1, 999)) + self.assertTrue(ShortID.from_components(3, 30, 300) < ShortID.from_components(3, 999, 1))